diff --git a/platformer/CMakeLists.txt b/platformer/CMakeLists.txt index 9e71e7a..3cf6320 100644 --- a/platformer/CMakeLists.txt +++ b/platformer/CMakeLists.txt @@ -16,6 +16,8 @@ add_executable(platformer src/collider_gizmos/plugin.cpp src/offset/plugin.cpp src/offset/offset.cpp + src/death/plugin.cpp + src/death/dead.cpp ) target_link_libraries(platformer tesseratos) diff --git a/platformer/src/death/dead.cpp b/platformer/src/death/dead.cpp new file mode 100644 index 0000000..c7ba894 --- /dev/null +++ b/platformer/src/death/dead.cpp @@ -0,0 +1,8 @@ +#include "dead.hpp" + +#include + +CUBOS_REFLECT_IMPL(demo::Dead) +{ + return cubos::core::ecs::ComponentTypeBuilder("demo::Dead").build(); +} diff --git a/platformer/src/death/dead.hpp b/platformer/src/death/dead.hpp new file mode 100644 index 0000000..1ba92d3 --- /dev/null +++ b/platformer/src/death/dead.hpp @@ -0,0 +1,11 @@ +#pragma once + +#include + +namespace demo +{ + struct [[cubos::component("")]] Dead + { + CUBOS_REFLECT; + }; +} // namespace demo \ No newline at end of file diff --git a/platformer/src/death/plugin.cpp b/platformer/src/death/plugin.cpp new file mode 100644 index 0000000..eee4712 --- /dev/null +++ b/platformer/src/death/plugin.cpp @@ -0,0 +1,44 @@ +#include "plugin.hpp" + +#include + +#include "../player/player.hpp" +#include "dead.hpp" + +using cubos::core::ecs::Commands; +using cubos::core::ecs::Query; +using cubos::core::ecs::Read; +using cubos::core::ecs::Write; + +using namespace cubos::engine; + +using namespace demo; + +#define DEATH_HEIGHT -10.0F + +static void fallDetectionSystem(Commands cmds, Query, Read> query) +{ + for (auto [entity, player, collider] : query) + { + if (collider->worldAABB.diag[1].y < DEATH_HEIGHT) + { + cmds.add(entity, Dead{}); + } + } +} + +static void killSystem(Commands cmds, Query, Read> query) +{ + for (auto [entity, player, dead] : query) + { + cmds.destroy(entity); + } +} + +void demo::deathPlugin(Cubos& cubos) +{ + cubos.addComponent(); + + cubos.system(fallDetectionSystem).before("kill"); + cubos.system(killSystem).tagged("kill"); +} diff --git a/platformer/src/death/plugin.hpp b/platformer/src/death/plugin.hpp new file mode 100644 index 0000000..82d67f4 --- /dev/null +++ b/platformer/src/death/plugin.hpp @@ -0,0 +1,8 @@ +#pragma once + +#include + +namespace demo +{ + void deathPlugin(cubos::engine::Cubos& cubos); +} diff --git a/platformer/src/main.cpp b/platformer/src/main.cpp index 367c292..947a077 100644 --- a/platformer/src/main.cpp +++ b/platformer/src/main.cpp @@ -17,6 +17,7 @@ #include "spawn/plugin.hpp" #include "collider_gizmos/plugin.hpp" #include "offset/plugin.hpp" +#include "death/plugin.hpp" using cubos::core::ecs::Commands; using cubos::core::ecs::Query; @@ -72,6 +73,7 @@ int main(int argc, char** argv) cubos.addPlugin(demo::spawnPlugin); cubos.addPlugin(demo::colliderGizmosPlugin); cubos.addPlugin(demo::offsetPlugin); + cubos.addPlugin(demo::deathPlugin); cubos.startupSystem(settings).tagged("cubos.settings"); cubos.startupSystem(setup).tagged("cubos.assets").after("cubos.renderer.init"); diff --git a/platformer/src/offset/plugin.cpp b/platformer/src/offset/plugin.cpp index fb5d9d9..e504381 100644 --- a/platformer/src/offset/plugin.cpp +++ b/platformer/src/offset/plugin.cpp @@ -5,6 +5,7 @@ #include +using cubos::core::ecs::Commands; using cubos::core::ecs::Query; using cubos::core::ecs::Read; using cubos::core::ecs::Write; @@ -13,10 +14,16 @@ using namespace cubos::engine; using namespace demo; -static void offsetSystem(Query> offsets, Query> localToWorlds) +static void offsetSystem(Commands cmds, Query> offsets, Query> localToWorlds) { for (auto [entity, offset] : offsets) { + if (!localToWorlds[offset->parent]) + { + cmds.destroy(entity); + continue; + } + auto [entityLocalToWorld] = *localToWorlds[entity]; auto [parentLocalToWorld] = *localToWorlds[offset->parent]; entityLocalToWorld->mat = parentLocalToWorld->mat * glm::translate(glm::mat4(1.0f), offset->vec);