-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(platformer): add moving platform
- Loading branch information
Showing
8 changed files
with
100 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |