Skip to content

Commit

Permalink
feat(platformer): add moving platform
Browse files Browse the repository at this point in the history
  • Loading branch information
RiscadoA committed Nov 14, 2023
1 parent d5e91cc commit da2a96a
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 3 deletions.
2 changes: 2 additions & 0 deletions platformer/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
14 changes: 12 additions & 2 deletions platformer/assets/scenes/level1.cubos
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
3 changes: 2 additions & 1 deletion platformer/assets/scenes/platform.cubos
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
"y": 0.5,
"z": 8
},
"cubos::engine::Collider": {}
"cubos::engine::Collider": {},
"cubos::engine::PhysicsVelocity": {}
}
}
}
2 changes: 2 additions & 0 deletions platformer/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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");
Expand Down
17 changes: 17 additions & 0 deletions platformer/src/mover/mover.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include "mover.hpp"

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

#include <cubos/core/reflection/external/glm.hpp>
#include <cubos/core/reflection/external/primitives.hpp>

CUBOS_REFLECT_IMPL(demo::Mover)
{
return cubos::core::ecs::ComponentTypeBuilder<Mover>("demo::Mover")
.withField("from", &Mover::from)
.withField("to", &Mover::to)
.withField("duration", &Mover::duration)
.withField("time", &Mover::time)
.withField("direction", &Mover::direction)
.build();
}
19 changes: 19 additions & 0 deletions platformer/src/mover/mover.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#pragma once

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

#include <glm/vec3.hpp>

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
38 changes: 38 additions & 0 deletions platformer/src/mover/plugin.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#include "plugin.hpp"
#include "mover.hpp"

#include <cubos/engine/transform/position.hpp>
#include <cubos/engine/physics/components/physics_velocity.hpp>

#include <glm/gtc/matrix_transform.hpp>

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<Mover>, Write<Position>, Write<PhysicsVelocity>> movers, Read<DeltaTime> 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<Mover>();
cubos.system(moverSystem).before("cubos.transform.update");
}
8 changes: 8 additions & 0 deletions platformer/src/mover/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 moverPlugin(cubos::engine::Cubos& cubos);
}

0 comments on commit da2a96a

Please sign in to comment.