Update: Reimplement Systems in particle example
This commit is contained in:
parent
57c7231dc1
commit
c88484a794
|
|
@ -7,7 +7,7 @@ fi
|
|||
if [[ $1 == "--setup" ]]; then
|
||||
git submodule update --init
|
||||
pushd "./external/raylib/"
|
||||
cmake -S . -B build
|
||||
cmake -S . -B build -DBUILD_EXAMPLES=Off
|
||||
cmake --build build
|
||||
popd
|
||||
fi
|
||||
|
|
|
|||
|
|
@ -46,6 +46,41 @@ void SpawnParticle(BECS_ECS *ecs, BECS_ComponentID posID, BECS_ComponentID velID
|
|||
data->lifetime = 2.0f;
|
||||
}
|
||||
|
||||
void LifetimeSystem(BECS_ECS *ecs, float dt) {
|
||||
ParticleData *data = (ParticleData *)ecs->componentPools[3].denseArray;
|
||||
BECS_Entity *entity = (BECS_Entity *)ecs->componentPools[3].denseEntityMapping;
|
||||
for (uint32_t i = 0; i < ecs->entityCount; i++) {
|
||||
data[i].lifetime -= dt;
|
||||
if (data[i].lifetime <= 0.0f) {
|
||||
BECS_EntityDestroy(ecs, entity[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void PhysicsSystem(BECS_ECS *ecs, float dt) {
|
||||
Position *pos = ((Position *)ecs->componentPools[0].denseArray);
|
||||
Velocity *vel = ((Velocity *)ecs->componentPools[1].denseArray);
|
||||
Acceleration *acc = ((Acceleration *)ecs->componentPools[2].denseArray);
|
||||
for (uint32_t i = 0; i < ecs->entityCount; i++)
|
||||
{
|
||||
vel[i].vy += acc[i].ay * dt;
|
||||
vel[i].vx += acc[i].ax * dt;
|
||||
pos[i].y += vel[i].vy * dt;
|
||||
pos[i].x += vel[i].vx * dt;
|
||||
}
|
||||
}
|
||||
|
||||
void RenderSystem(BECS_ECS *ecs, float dt) {
|
||||
Position *pos = ((Position *)ecs->componentPools[0].denseArray);
|
||||
ParticleData *data = ((ParticleData *)ecs->componentPools[3].denseArray);
|
||||
for (uint32_t i = 0; i < ecs->entityCount; i++)
|
||||
{
|
||||
float alpha = data[i].lifetime / 2.0;
|
||||
Color color = {255, 255, 255, (unsigned char)(alpha * 255)};
|
||||
DrawCircle(pos[i].x, pos[i].y, data[i].radius, color);
|
||||
}
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
// Initialize ECS
|
||||
size_t componentSizes[4] = {sizeof(Position), sizeof(Velocity), sizeof(Acceleration), sizeof(ParticleData)};
|
||||
|
|
@ -66,57 +101,61 @@ int main(void) {
|
|||
float dt = GetFrameTime();
|
||||
|
||||
// Update lifetimes and despawn
|
||||
for (BECS_Entity entity = 0; entity < BECS_MAX_ENTITIES; entity++) {
|
||||
if (!BECS_IsEntityAlive(&ecs, entity)) continue;
|
||||
if (BECS_ComponentHas(&ecs, entity, dataID)) {
|
||||
ParticleData *data = (ParticleData *)BECS_ComponentGet(&ecs, entity, dataID);
|
||||
data->lifetime -= dt;
|
||||
if (data->lifetime <= 0.0f) {
|
||||
BECS_EntityDestroy(&ecs, entity);
|
||||
}
|
||||
}
|
||||
}
|
||||
// for (BECS_Entity entity = 0; entity < BECS_MAX_ENTITIES; entity++) {
|
||||
// if (!BECS_IsEntityAlive(&ecs, entity)) continue;
|
||||
// if (BECS_ComponentHas(&ecs, entity, dataID)) {
|
||||
// ParticleData *data = (ParticleData *)BECS_ComponentGet(&ecs, entity, dataID);
|
||||
// data->lifetime -= dt;
|
||||
// if (data->lifetime <= 0.0f) {
|
||||
// BECS_EntityDestroy(&ecs, entity);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
LifetimeSystem(&ecs, dt);
|
||||
|
||||
// Spawn particle on mouse click
|
||||
if (IsMouseButtonDown(MOUSE_LEFT_BUTTON)) {
|
||||
Vector2 mousePos = GetMousePosition();
|
||||
for (uint32_t i = 0; i < 10; i++)
|
||||
{
|
||||
for (uint32_t i = 0; i < 10000; i++) {
|
||||
SpawnParticle(&ecs, posID, velID, accID, dataID, mousePos.x, mousePos.y);
|
||||
}
|
||||
}
|
||||
|
||||
// Physics system: update position and velocity
|
||||
for (BECS_Entity entity = 0; entity < BECS_MAX_ENTITIES; entity++) {
|
||||
if (!BECS_IsEntityAlive(&ecs, entity)) continue;
|
||||
if (BECS_ComponentHas(&ecs, entity, posID) &&
|
||||
BECS_ComponentHas(&ecs, entity, velID) &&
|
||||
BECS_ComponentHas(&ecs, entity, accID)) {
|
||||
Position *pos = (Position *)BECS_ComponentGet(&ecs, entity, posID);
|
||||
Velocity *vel = (Velocity *)BECS_ComponentGet(&ecs, entity, velID);
|
||||
Acceleration *acc = (Acceleration *)BECS_ComponentGet(&ecs, entity, accID);
|
||||
// for (BECS_Entity entity = 0; entity < BECS_MAX_ENTITIES; entity++) {
|
||||
// if (!BECS_IsEntityAlive(&ecs, entity)) continue;
|
||||
// if (BECS_ComponentHas(&ecs, entity, posID) &&
|
||||
// BECS_ComponentHas(&ecs, entity, velID) &&
|
||||
// BECS_ComponentHas(&ecs, entity, accID)) {
|
||||
// Position *pos = (Position *)BECS_ComponentGet(&ecs, entity, posID);
|
||||
// Velocity *vel = (Velocity *)BECS_ComponentGet(&ecs, entity, velID);
|
||||
// Acceleration *acc = (Acceleration *)BECS_ComponentGet(&ecs, entity, accID);
|
||||
//
|
||||
// vel->vx += acc->ax * dt;
|
||||
// vel->vy += acc->ay * dt;
|
||||
// pos->x += vel->vx * dt;
|
||||
// pos->y += vel->vy * dt;
|
||||
// }
|
||||
// }
|
||||
|
||||
vel->vx += acc->ax * dt;
|
||||
vel->vy += acc->ay * dt;
|
||||
pos->x += vel->vx * dt;
|
||||
pos->y += vel->vy * dt;
|
||||
}
|
||||
}
|
||||
PhysicsSystem(&ecs, dt);
|
||||
|
||||
BeginDrawing();
|
||||
ClearBackground(BLACK);
|
||||
|
||||
// Render system: draw particles
|
||||
for (BECS_Entity entity = 0; entity < BECS_MAX_ENTITIES; entity++) {
|
||||
if (!BECS_IsEntityAlive(&ecs, entity)) continue;
|
||||
if (BECS_ComponentHas(&ecs, entity, posID) && BECS_ComponentHas(&ecs, entity, dataID)) {
|
||||
Position *pos = (Position *)BECS_ComponentGet(&ecs, entity, posID);
|
||||
ParticleData *data = (ParticleData *)BECS_ComponentGet(&ecs, entity, dataID);
|
||||
float alpha = data->lifetime / 2.0f;
|
||||
Color color = {255, 255, 255, (unsigned char)(alpha * 255)};
|
||||
DrawCircle((int)pos->x, (int)pos->y, data->radius, color);
|
||||
}
|
||||
}
|
||||
// for (BECS_Entity entity = 0; entity < BECS_MAX_ENTITIES; entity++) {
|
||||
// if (!BECS_IsEntityAlive(&ecs, entity)) continue;
|
||||
// if (BECS_ComponentHas(&ecs, entity, posID) && BECS_ComponentHas(&ecs, entity, dataID)) {
|
||||
// Position *pos = (Position *)BECS_ComponentGet(&ecs, entity, posID);
|
||||
// ParticleData *data = (ParticleData *)BECS_ComponentGet(&ecs, entity, dataID);
|
||||
// float alpha = data->lifetime / 2.0f;
|
||||
// Color color = {255, 255, 255, (unsigned char)(alpha * 255)};
|
||||
// DrawCircle((int)pos->x, (int)pos->y, data->radius, color);
|
||||
// }
|
||||
// }
|
||||
|
||||
RenderSystem(&ecs, dt);
|
||||
|
||||
DrawText(TextFormat("Particles: %u", ecs.entityCount), 10, 10, 20, WHITE);
|
||||
EndDrawing();
|
||||
|
|
|
|||
Loading…
Reference in a new issue