Skip to content

Commit 55f464d

Browse files
committed
Merge branch 'editor_v05' into dev
2 parents b020ffc + ebc708e commit 55f464d

209 files changed

Lines changed: 3332 additions & 1932 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,5 @@ global.sz
2424
*.serialization.cpp
2525
src/**/temp.pgc
2626
test/pgcompiler/scripts/*compiled.pgc
27-
.vscode/.intellisense-cache
27+
.vscode/.intellisense-cache
28+
graph.dot

.vscode/tasks.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@
134134
"showOutput": "always",
135135
"args": [
136136
"-c",
137-
"cd ${workspaceFolder}/em; emcmake cmake .; cmake --build . --config Release --target InvadersBreaker -j 10 --"
137+
"cd ${workspaceFolder}/em; emcmake cmake .; cmake --build . --config Release --target BoxBouncer -j 10 --"
138138
]
139139
},
140140
"linux": {

CMakeLists.txt

Lines changed: 187 additions & 182 deletions
Large diffs are not rendered by default.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "application.h"
22

33
#include "ECS/entitysystem.h"
4+
#include "ECS/entitysystem_vm_modules.h"
45

56
#include "ECS/standardsystem.h"
67

@@ -14,6 +15,9 @@
1415

1516
#include "window.h"
1617

18+
#include "particle_system.h"
19+
#include "particlemodule.h"
20+
1721
using namespace pg;
1822

1923
namespace
@@ -190,6 +194,11 @@ GameApp::GameApp(const std::string &appName) : engine(appName)
190194
ttfSys->registerFont("res/font/Inter/static/Inter_28pt-Bold.ttf", "bold");
191195
ttfSys->registerFont("res/font/Inter/static/Inter_28pt-Italic.ttf", "italic");
192196

197+
ecs.succeed<MasterRenderer, TTFTextSystem>();
198+
199+
// Register custom VM modules for scripts
200+
ecs.registerCustomVmModule("particle", ParticleModule{&ecs});
201+
193202
VM vm;
194203
ecs.setupVm(vm);
195204

@@ -208,6 +217,9 @@ GameApp::GameApp(const std::string &appName) : engine(appName)
208217

209218
ecs.registerSystem(createFPSSystem());
210219

220+
// Create particle system
221+
ecs.createSystem<ParticleSystem>();
222+
211223
// Register collision handler for Bullet-Asteroid collisions
212224
makeCollisionHandleScript(&ecs, "res/asteroid/bullet_asteroid_collision.pg",
213225
[](Entity* ent) { return ent->has("Bullet"); },
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
#pragma once
2+
3+
#include "ECS/entitysystem.h"
4+
#include "ECS/standardsystem.h"
5+
#include "Systems/coresystems.h"
6+
#include "2D/simple2dobject.h"
7+
8+
using namespace pg;
9+
10+
// Particle component to track lifetime
11+
struct Particle : public Component
12+
{
13+
DEFAULT_COMPONENT_MEMBERS(Particle)
14+
15+
float lifetime = 500.0f; // milliseconds
16+
float elapsed = 0.0f;
17+
};
18+
19+
// Velocity component for particle movement
20+
struct ParticleVelocity : public Component
21+
{
22+
DEFAULT_COMPONENT_MEMBERS(ParticleVelocity)
23+
24+
float dx = 0.0f;
25+
float dy = 0.0f;
26+
};
27+
28+
// Particle system that updates and manages particles
29+
class ParticleSystem : public System<InitSys, DeltaTime>
30+
{
31+
public:
32+
void init() override
33+
{
34+
registerGroup<Particle, PositionComponent, ParticleVelocity>();
35+
}
36+
37+
void onExecute(float deltaTime) override
38+
{
39+
std::vector<EntityRef> toDestroy;
40+
41+
for (auto entity : viewGroup<Particle, PositionComponent, ParticleVelocity>())
42+
{
43+
auto particle = entity->get<Particle>();
44+
auto pos = entity->get<PositionComponent>();
45+
auto vel = entity->get<ParticleVelocity>();
46+
47+
particle->elapsed += deltaTime * 1000.0f; // deltaTime is in seconds
48+
49+
// Update position
50+
pos->setX(pos->x + vel->dx * deltaTime);
51+
pos->setY(pos->y + vel->dy * deltaTime);
52+
53+
// Fade out over lifetime
54+
if (auto shape = entity->get<Simple2DObject>())
55+
{
56+
float alpha = 1.0f - (particle->elapsed / particle->lifetime);
57+
alpha = std::max(0.0f, std::min(1.0f, alpha)); // Clamp to [0, 1]
58+
59+
auto colors = shape->colors;
60+
colors.w = (uint8_t)(255 * alpha);
61+
shape->setColors(colors);
62+
}
63+
64+
// Mark for destruction when lifetime expires
65+
if (particle->elapsed >= particle->lifetime)
66+
{
67+
toDestroy.push_back(entity->entity);
68+
}
69+
}
70+
71+
// Clean up dead particles
72+
for (auto& entity : toDestroy)
73+
{
74+
ecsRef->removeEntity(entity);
75+
}
76+
}
77+
};

examples/Asteroid/particlemodule.h

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
#pragma once
2+
3+
#include "Compiler/native_module.h"
4+
#include "ECS/entitysystem.h"
5+
#include "2D/simple2dobject.h"
6+
#include "particle_system.h"
7+
#include <cmath>
8+
#include <cstdlib>
9+
10+
namespace pg
11+
{
12+
/**
13+
* Particle Module for Asteroid game
14+
* Provides function to spawn particle effects
15+
*/
16+
class ParticleModule : public NativeModule
17+
{
18+
public:
19+
ParticleModule(EntitySystem* ecsRef)
20+
{
21+
// Capture ecsRef for use in native functions
22+
auto ecsRefCopy = ecsRef;
23+
24+
// Add spawnParticles function
25+
// Usage: spawnParticles(x, y, count, colorHex, size)
26+
// Example: spawnParticles(100, 200, 8, 0x333333, 3)
27+
addNativeFunction("spawnParticles", [ecsRefCopy](VM* vm, int argCount, Value* args) -> Value {
28+
if (argCount != 5)
29+
{
30+
throw std::runtime_error("spawnParticles expects 5 arguments (x, y, count, colorHex, size)");
31+
}
32+
33+
// Validate all arguments are numbers
34+
if (!IS_DOUBLE(args[0]) || !IS_DOUBLE(args[1]) || !IS_INT(args[2]) || !IS_INT(args[3]) || !IS_DOUBLE(args[4]))
35+
{
36+
throw std::runtime_error("spawnParticles expects (float x, float y, int count, int colorHex, float size)");
37+
}
38+
39+
float posX = AS_DOUBLE(args[0]);
40+
float posY = AS_DOUBLE(args[1]);
41+
int particleCount = static_cast<int>(AS_INT(args[2]));
42+
int64_t color = AS_INT(args[3]);
43+
float particleSize = AS_DOUBLE(args[4]);
44+
45+
// Extract RGB from packed integer (0xRRGGBB format)
46+
uint8_t r = (color >> 16) & 0xFF;
47+
uint8_t g = (color >> 8) & 0xFF;
48+
uint8_t b = color & 0xFF;
49+
50+
// Spawn particles in random directions
51+
for (int i = 0; i < particleCount; i++)
52+
{
53+
auto particle = makeSimple2DShape(ecsRefCopy, Shape2D::Square, particleSize, particleSize, {r, g, b, 255});
54+
55+
auto pos = particle.get<PositionComponent>();
56+
pos->setX(posX);
57+
pos->setY(posY);
58+
59+
auto vel = particle.attach<ParticleVelocity>();
60+
61+
// Random angle (0 to 2π)
62+
float angle = ((float)rand() / RAND_MAX) * 6.28318f;
63+
// Random speed between 50 and 150 pixels/second
64+
float speed = 50.0f + ((float)rand() / RAND_MAX) * 100.0f;
65+
66+
vel->dx = cos(angle) * speed;
67+
vel->dy = sin(angle) * speed;
68+
69+
auto particleComp = particle.attach<Particle>();
70+
// Random lifetime between 300ms and 700ms
71+
particleComp->lifetime = 300.0f + ((float)rand() / RAND_MAX) * 400.0f;
72+
}
73+
74+
return makeIntValue(particleCount);
75+
});
76+
}
77+
};
78+
}
Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -537,7 +537,7 @@ void initGame() {
537537

538538
printf("Engine initialized ...\n");
539539

540-
auto ttfSys = mainWindow->ecs.createSystem<TTFTextSystem>(mainWindow->masterRenderer);
540+
auto ttfSys = mainWindow->ecs->createSystem<TTFTextSystem>(mainWindow->masterRenderer);
541541

542542
// Need to fix this
543543
ttfSys->registerFont("res/font/Inter/static/Inter_28pt-Light.ttf", "light");
@@ -546,19 +546,17 @@ void initGame() {
546546

547547
// mainWindow->masterRenderer->processTextureRegister();
548548

549-
mainWindow->ecs.createSystem<FpsSystem>();
549+
mainWindow->ecs->succeed<MasterRenderer, TTFTextSystem>();
550550

551-
mainWindow->ecs.succeed<MasterRenderer, TTFTextSystem>();
551+
mainWindow->ecs->createSystem<TextHandlingSys>();
552552

553-
mainWindow->ecs.createSystem<TextHandlingSys>();
554-
555-
mainWindow->ecs.dumbTaskflow();
553+
mainWindow->ecs->dumbTaskflow();
556554

557555
mainWindow->render();
558556

559557
mainWindow->resize(820, 640);
560558

561-
mainWindow->ecs.start();
559+
mainWindow->ecs->start();
562560

563561
printf("Engine initialized\n");
564562
}

0 commit comments

Comments
 (0)