Skip to content

Commit

Permalink
feat(platformer): add death and respawn
Browse files Browse the repository at this point in the history
  • Loading branch information
luishfonseca committed Nov 14, 2023
1 parent 558cb1a commit d5e91cc
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 1 deletion.
2 changes: 2 additions & 0 deletions platformer/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 8 additions & 0 deletions platformer/src/death/dead.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include "dead.hpp"

#include <cubos/core/ecs/component/reflection.hpp>

CUBOS_REFLECT_IMPL(demo::Dead)
{
return cubos::core::ecs::ComponentTypeBuilder<Dead>("demo::Dead").build();
}
11 changes: 11 additions & 0 deletions platformer/src/death/dead.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#pragma once

#include <cubos/core/reflection/reflect.hpp>

namespace demo
{
struct [[cubos::component("")]] Dead
{
CUBOS_REFLECT;
};
} // namespace demo
44 changes: 44 additions & 0 deletions platformer/src/death/plugin.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#include "plugin.hpp"

#include <cubos/engine/collisions/collider.hpp>

#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<Write<Player>, Read<Collider>> 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<Player>, Read<Dead>> query)
{
for (auto [entity, player, dead] : query)
{
cmds.destroy(entity);
}
}

void demo::deathPlugin(Cubos& cubos)
{
cubos.addComponent<Dead>();

cubos.system(fallDetectionSystem).before("kill");
cubos.system(killSystem).tagged("kill");
}
8 changes: 8 additions & 0 deletions platformer/src/death/plugin.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#pragma once

#include <cubos/engine/cubos.hpp>

namespace demo
{
void deathPlugin(cubos::engine::Cubos& cubos);
}
2 changes: 2 additions & 0 deletions platformer/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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");
Expand Down
9 changes: 8 additions & 1 deletion platformer/src/offset/plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include <glm/gtc/matrix_transform.hpp>

using cubos::core::ecs::Commands;
using cubos::core::ecs::Query;
using cubos::core::ecs::Read;
using cubos::core::ecs::Write;
Expand All @@ -13,10 +14,16 @@ using namespace cubos::engine;

using namespace demo;

static void offsetSystem(Query<Read<Offset>> offsets, Query<Write<LocalToWorld>> localToWorlds)
static void offsetSystem(Commands cmds, Query<Read<Offset>> offsets, Query<Write<LocalToWorld>> 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);
Expand Down

0 comments on commit d5e91cc

Please sign in to comment.