From da2a96ac10ba865181a0760d1beb45dbeca5cfde Mon Sep 17 00:00:00 2001 From: Ricardo Antunes Date: Tue, 14 Nov 2023 10:52:15 +0000 Subject: [PATCH] feat(platformer): add moving platform --- platformer/CMakeLists.txt | 2 ++ platformer/assets/scenes/level1.cubos | 14 +++++++-- platformer/assets/scenes/platform.cubos | 3 +- platformer/src/main.cpp | 2 ++ platformer/src/mover/mover.cpp | 17 +++++++++++ platformer/src/mover/mover.hpp | 19 +++++++++++++ platformer/src/mover/plugin.cpp | 38 +++++++++++++++++++++++++ platformer/src/mover/plugin.hpp | 8 ++++++ 8 files changed, 100 insertions(+), 3 deletions(-) create mode 100644 platformer/src/mover/mover.cpp create mode 100644 platformer/src/mover/mover.hpp create mode 100644 platformer/src/mover/plugin.cpp create mode 100644 platformer/src/mover/plugin.hpp diff --git a/platformer/CMakeLists.txt b/platformer/CMakeLists.txt index 3cf6320..99ca9e8 100644 --- a/platformer/CMakeLists.txt +++ b/platformer/CMakeLists.txt @@ -18,6 +18,8 @@ add_executable(platformer src/offset/offset.cpp src/death/plugin.cpp src/death/dead.cpp + src/mover/plugin.cpp + src/mover/mover.cpp ) target_link_libraries(platformer tesseratos) diff --git a/platformer/assets/scenes/level1.cubos b/platformer/assets/scenes/level1.cubos index 9fd8dc1..efd916f 100644 --- a/platformer/assets/scenes/level1.cubos +++ b/platformer/assets/scenes/level1.cubos @@ -25,8 +25,18 @@ } }, "platform-1.base": { - "cubos::engine::Position": { - "x": -10 + "demo::Mover": { + "from": { + "x": -10, + "y": -10, + "z": -5 + }, + "to": { + "x": -10, + "y": 10, + "z": 5 + }, + "duration": 5.0 } }, "platform-2.base": { diff --git a/platformer/assets/scenes/platform.cubos b/platformer/assets/scenes/platform.cubos index ac87a0a..c620262 100644 --- a/platformer/assets/scenes/platform.cubos +++ b/platformer/assets/scenes/platform.cubos @@ -16,7 +16,8 @@ "y": 0.5, "z": 8 }, - "cubos::engine::Collider": {} + "cubos::engine::Collider": {}, + "cubos::engine::PhysicsVelocity": {} } } } \ No newline at end of file diff --git a/platformer/src/main.cpp b/platformer/src/main.cpp index 947a077..a63a209 100644 --- a/platformer/src/main.cpp +++ b/platformer/src/main.cpp @@ -18,6 +18,7 @@ #include "collider_gizmos/plugin.hpp" #include "offset/plugin.hpp" #include "death/plugin.hpp" +#include "mover/plugin.hpp" using cubos::core::ecs::Commands; using cubos::core::ecs::Query; @@ -74,6 +75,7 @@ int main(int argc, char** argv) cubos.addPlugin(demo::colliderGizmosPlugin); cubos.addPlugin(demo::offsetPlugin); cubos.addPlugin(demo::deathPlugin); + cubos.addPlugin(demo::moverPlugin); cubos.startupSystem(settings).tagged("cubos.settings"); cubos.startupSystem(setup).tagged("cubos.assets").after("cubos.renderer.init"); diff --git a/platformer/src/mover/mover.cpp b/platformer/src/mover/mover.cpp new file mode 100644 index 0000000..bd58b96 --- /dev/null +++ b/platformer/src/mover/mover.cpp @@ -0,0 +1,17 @@ +#include "mover.hpp" + +#include + +#include +#include + +CUBOS_REFLECT_IMPL(demo::Mover) +{ + return cubos::core::ecs::ComponentTypeBuilder("demo::Mover") + .withField("from", &Mover::from) + .withField("to", &Mover::to) + .withField("duration", &Mover::duration) + .withField("time", &Mover::time) + .withField("direction", &Mover::direction) + .build(); +} diff --git a/platformer/src/mover/mover.hpp b/platformer/src/mover/mover.hpp new file mode 100644 index 0000000..b03eac5 --- /dev/null +++ b/platformer/src/mover/mover.hpp @@ -0,0 +1,19 @@ +#pragma once + +#include + +#include + +namespace demo +{ + struct [[cubos::component("")]] Mover + { + CUBOS_REFLECT; + + glm::vec3 from; + glm::vec3 to; + float duration = 2.0F; + float time = 0.0F; + bool direction = false; + }; +} // namespace demo diff --git a/platformer/src/mover/plugin.cpp b/platformer/src/mover/plugin.cpp new file mode 100644 index 0000000..168f0d1 --- /dev/null +++ b/platformer/src/mover/plugin.cpp @@ -0,0 +1,38 @@ +#include "plugin.hpp" +#include "mover.hpp" + +#include +#include + +#include + +using cubos::core::ecs::Query; +using cubos::core::ecs::Read; +using cubos::core::ecs::Write; + +using namespace cubos::engine; + +using namespace demo; + +static void moverSystem(Query, Write, Write> movers, Read deltaTime) +{ + for (auto [entity, mover, position, velocity] : movers) + { + mover->time += deltaTime->value * (mover->direction ? 1.0F : -1.0F); + if (mover->time > mover->duration || mover->time < 0.0F) + { + mover->time = glm::clamp(mover->time, 0.0F, mover->duration); + mover->direction = !mover->direction; + } + + auto prev = position->vec; + position->vec = glm::mix(mover->from, mover->to, mover->time / mover->duration); + velocity->velocity = (position->vec - prev) / deltaTime->value; + } +} + +void demo::moverPlugin(Cubos& cubos) +{ + cubos.addComponent(); + cubos.system(moverSystem).before("cubos.transform.update"); +} diff --git a/platformer/src/mover/plugin.hpp b/platformer/src/mover/plugin.hpp new file mode 100644 index 0000000..6a76d5c --- /dev/null +++ b/platformer/src/mover/plugin.hpp @@ -0,0 +1,8 @@ +#pragma once + +#include + +namespace demo +{ + void moverPlugin(cubos::engine::Cubos& cubos); +}