From c7fd00a1b80769cf805aa338c7da587f1742ae4c Mon Sep 17 00:00:00 2001 From: Derek Christ Date: Sat, 31 May 2025 17:16:26 +0200 Subject: [PATCH] Fix propagate system --- src/definitions.hpp | 1 + src/level.hpp | 7 +++++++ src/physics.cpp | 26 +++++--------------------- src/physics.hpp | 4 ++++ 4 files changed, 17 insertions(+), 21 deletions(-) diff --git a/src/definitions.hpp b/src/definitions.hpp index 4a6b8c4..f505a89 100644 --- a/src/definitions.hpp +++ b/src/definitions.hpp @@ -14,4 +14,5 @@ struct SdlHandles struct Game { uint32_t ticks; + uint32_t score; }; diff --git a/src/level.hpp b/src/level.hpp index ac1d30e..b4ad5fe 100644 --- a/src/level.hpp +++ b/src/level.hpp @@ -6,6 +6,7 @@ #include "sprite.hpp" #include +#include struct Fruit { @@ -78,6 +79,7 @@ struct LevelModule }); // world.system("DespawnFruits") + // .kind(flecs::OnValidate) // .each( // [](flecs::entity e, WorldPosition const& pos, Fruit) // { @@ -85,6 +87,11 @@ struct LevelModule // e.destruct(); // }); + world.system("CollectItem") + .term_at(0) + .singleton() + .each([](flecs::entity e, Game& game, Fruit, Collided) { game.score += 10; }); + world.system("MoveBasket") .term_at(0) .singleton() diff --git a/src/physics.cpp b/src/physics.cpp index 795b100..c8df384 100644 --- a/src/physics.cpp +++ b/src/physics.cpp @@ -1,5 +1,4 @@ #include "physics.hpp" -#include "definitions.hpp" #include "level.hpp" #include @@ -47,23 +46,6 @@ PhysicsModule::PhysicsModule(flecs::world& world) propagate_to_children(e, world_pos); }); - world.system("DrawBoxes") - .term_at(0) - .singleton() - .each( - [](SdlHandles const& sdl_handles, - WorldPosition const& pos, - Size const& size, - CollisionBox) - { - SDL_FRect rect{static_cast(pos.x), - static_cast(pos.y), - static_cast(size.w), - static_cast(size.h)}; - SDL_SetRenderDrawColor(sdl_handles.renderer, 0, 0, 255, 255); - SDL_RenderRect(sdl_handles.renderer, &rect); - }); - auto basket_query = world.query(); world.system("CollisionCheck") @@ -75,8 +57,10 @@ PhysicsModule::PhysicsModule(flecs::world& world) if (e.parent().has()) return; - basket_query.first().children( - [world_pos, size](flecs::entity basket_child) + auto fruit = e.parent(); + auto basket = basket_query.first(); + basket.children( + [fruit, world_pos, size](flecs::entity basket_child) { if (!basket_child.has()) return; @@ -89,7 +73,7 @@ PhysicsModule::PhysicsModule(flecs::world& world) basket_child_pos->y + basket_child_size->h >= world_pos.y && basket_child_pos->y <= world_pos.y + size.h) { - spdlog::info("collision"); + fruit.add(); } }); }); diff --git a/src/physics.hpp b/src/physics.hpp index d81e969..6fcbcdd 100644 --- a/src/physics.hpp +++ b/src/physics.hpp @@ -30,6 +30,10 @@ struct CollisionBox { }; +struct Collided +{ +}; + struct PhysicsModule { PhysicsModule(flecs::world& world);