Skip to content

Commit

Permalink
bork(platformer): bork
Browse files Browse the repository at this point in the history
  • Loading branch information
RiscadoA committed Nov 14, 2023
1 parent da2a96a commit fbcbb48
Show file tree
Hide file tree
Showing 9 changed files with 93 additions and 46 deletions.
14 changes: 8 additions & 6 deletions platformer/assets/scenes/level1.cubos
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,22 @@
"demo::Mover": {
"from": {
"x": -10,
"y": -10,
"y": 5,
"z": -5
},
"to": {
"x": -10,
"y": 10,
"x": -20,
"y": 0,
"z": 5
},
"duration": 5.0
"speed": 10.0
}
},
"platform-2.base": {
"cubos::engine::Position": {
"x": 10
"demo::Mover": {
"from": {
"x": 10
}
}
},
"sun": {
Expand Down
11 changes: 10 additions & 1 deletion platformer/assets/scenes/platform.cubos
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,17 @@
"y": 0.5,
"z": 8
},
"demo::Mover": {
"speed": 0.0
},
"cubos::engine::Collider": {},
"cubos::engine::PhysicsVelocity": {}
"cubos::engine::PhysicsVelocity": {},
"cubos::engine::PhysicsMass": {
"mass": 1.0,
"inverseMass": 1.0
},
"cubos::engine::PreviousPosition": {},
"cubos::engine::AccumulatedCorrection": {}
}
}
}
3 changes: 1 addition & 2 deletions platformer/src/mover/mover.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ 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("speed", &Mover::speed)
.withField("direction", &Mover::direction)
.build();
}
4 changes: 2 additions & 2 deletions platformer/src/mover/mover.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ namespace demo

glm::vec3 from;
glm::vec3 to;
float duration = 2.0F;
float time = 0.0F;
float speed = 2.0F;
bool direction = false;
bool initialized = false;
};
} // namespace demo
28 changes: 21 additions & 7 deletions platformer/src/mover/plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@

#include <cubos/engine/transform/position.hpp>
#include <cubos/engine/physics/components/physics_velocity.hpp>
#include <cubos/core/data/old/debug_serializer.hpp>

#include <glm/gtc/matrix_transform.hpp>

using cubos::core::data::old::Debug;
using cubos::core::ecs::Query;
using cubos::core::ecs::Read;
using cubos::core::ecs::Write;
Expand All @@ -18,21 +20,33 @@ static void moverSystem(Query<Write<Mover>, Write<Position>, Write<PhysicsVeloci
{
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)
if (!mover->initialized)
{
mover->initialized = true;
mover->direction = true;
position->vec = mover->from;
}

glm::vec3 target = mover->direction ? mover->to : mover->from;
if (glm::distance(position->vec, target) < 1.0F)
{
mover->time = glm::clamp(mover->time, 0.0F, mover->duration);
mover->direction = !mover->direction;
target = mover->direction ? mover->to : mover->from;
}

auto prev = position->vec;
position->vec = glm::mix(mover->from, mover->to, mover->time / mover->duration);
velocity->velocity = (position->vec - prev) / deltaTime->value;
if (target == position->vec)
{
velocity->velocity = {0.0F, 0.0F, 0.0F};
}
else
{
velocity->velocity = glm::normalize(target - position->vec) * mover->speed;
}
}
}

void demo::moverPlugin(Cubos& cubos)
{
cubos.addComponent<Mover>();
cubos.system(moverSystem).before("cubos.transform.update");
cubos.system(moverSystem).before("player.move").tagged("cubos.physics.apply_forces");
}
2 changes: 1 addition & 1 deletion platformer/src/orbit_camera/plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ static void inputSystem(Read<Input> input, Query<Write<OrbitCameraController>> c
{
camera->theta += input->axis("look-horizontal", camera->playerId) * deltaTime->value * camera->rotationSpeed;
camera->phi += input->axis("look-vertical", camera->playerId) * deltaTime->value * camera->rotationSpeed;
camera->phi = glm::clamp(camera->phi, 0.1F, 179.9F);
camera->phi = glm::clamp(camera->phi, 1.0F, 89.0F);
camera->distance += input->axis("look-zoom", camera->playerId) * deltaTime->value * camera->zoomSpeed;
camera->distance = glm::clamp(camera->distance, 1.0F, 100.0F);
}
Expand Down
2 changes: 1 addition & 1 deletion platformer/src/player/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ CUBOS_REFLECT_IMPL(demo::Player)
{
return cubos::core::ecs::ComponentTypeBuilder<Player>("demo::Player")
.withField("id", &Player::id)
.withField("isOnGround", &Player::isOnGround)
.withField("ground", &Player::ground)
.withField("animationTime", &Player::animationTime)
.withField("animationSpeed", &Player::animationSpeed)
.withField("speed", &Player::speed)
Expand Down
4 changes: 3 additions & 1 deletion platformer/src/player/player.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ namespace demo
CUBOS_REFLECT;

int id = 0;
bool isOnGround = false;
float animationTime = 0.0F;
float animationSpeed = 1.0F;
float jumpForce = 1000.0F;
Expand All @@ -24,5 +23,8 @@ namespace demo
cubos::core::ecs::Entity rightFoot;
glm::vec3 forward = {0.0F, 0.0F, 1.0F};
glm::vec3 right = {1.0F, 0.0F, 0.0F};

cubos::core::ecs::Entity ground{};
bool wasOnGround = false;
};
} // namespace demo
71 changes: 46 additions & 25 deletions platformer/src/player/plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,30 +26,44 @@ using namespace cubos::engine;

using namespace demo;

static void move(Query<Write<Player>, Write<Position>, Write<PhysicsVelocity>, Write<Rotation>> query,
Query<Write<Offset>> offsets, Read<Input> input, Read<DeltaTime> deltaTime, Write<Settings> settings,
EventReader<CollisionEvent> collisions)
static void collisionSystem(Query<Write<Player>, Write<Position>> players, Query<Write<PhysicsVelocity>> velocities,
EventReader<CollisionEvent> collisions)
{
for (auto collision : collisions)
{
// Only handle collisions between players and other entities which are not players.
if (!query[collision.entity] /* || query[collision.other]*/)
// Only handle player collisions
if (!players[collision.entity])
{
continue;
}

auto [player, position, velocity, rotation] = *query[collision.entity];
auto [player, position] = *players[collision.entity];
position->vec -= collision.normal * collision.penetration;
velocity->velocity -= collision.normal * glm::dot(collision.normal, velocity->velocity);

auto [playerVelocity] = *velocities[collision.entity];
playerVelocity->velocity -= collision.normal * glm::dot(collision.normal, playerVelocity->velocity);

if (collision.normal.y < -0.1F)
{
player->isOnGround = true;
player->ground = collision.other;
player->wasOnGround = true;
}
else
{
player->ground = Entity{};
player->wasOnGround = false;
}
}
}

static void move(Query<Write<Player>, Write<Position>, Write<Rotation>> query, Query<Write<PhysicsVelocity>> velocities,
Query<Write<Offset>> offsets, Read<Input> input, Read<DeltaTime> deltaTime, Write<Settings> settings)
{

for (auto [entity, player, position, velocity, rotation] : query)
for (auto [entity, player, position, rotation] : query)
{
auto [velocity] = *velocities[entity];

const float force = settings->getDouble("force", 5000.0F);
const float dragForce = settings->getDouble("dragForce", -2000.0F);
const float rotationSpeed = settings->getDouble("rotationSpeed", 0.02F);
Expand All @@ -58,27 +72,28 @@ static void move(Query<Write<Player>, Write<Position>, Write<PhysicsVelocity>, W
// auto moveHorizontal = input->axis("horizontal", player->id);
auto jump = input->pressed("jump", player->id);

if (player->isOnGround)
glm::vec3 targetTorso = {0.0F, 0.0F, 0.0F};
glm::vec3 targetLeftHand = {7.0F, -3.0F, 0.0F};
glm::vec3 targetRightHand = {-7.0F, -3.0F, 0.0F};
glm::vec3 targetLeftFoot = {3.5F, -10.0F, 1.0F};
glm::vec3 targetRightFoot = {-3.5F, -10.0F, 1.0F};

if (!player->ground.isNull())
{
glm::vec3 newVelocity = moveVertical * player->forward * player->speed;
velocity->velocity.x = newVelocity.x;
velocity->velocity.z = newVelocity.z;
if (velocities[player->ground])
{
auto [groundVelocity] = *velocities[player->ground];
velocity->velocity = glm::max(velocity->velocity, groundVelocity->velocity);
}
velocity->velocity.x += newVelocity.x;
velocity->velocity.z += newVelocity.z;

if (jump)
{
velocity->impulse += glm::vec3{0.0F, 1.0F, 0.0F} * player->jumpForce;
player->isOnGround = false;
}
}

glm::vec3 targetTorso = {0.0F, 0.0F, 0.0F};
glm::vec3 targetLeftHand = {7.0F, -3.0F, 0.0F};
glm::vec3 targetRightHand = {-7.0F, -3.0F, 0.0F};
glm::vec3 targetLeftFoot = {3.5F, -10.0F, 1.0F};
glm::vec3 targetRightFoot = {-3.5F, -10.0F, 1.0F};

if (player->isOnGround)
{
player->animationTime +=
glm::sign(moveVertical) * deltaTime->value * player->speed * player->animationSpeed;

Expand All @@ -97,7 +112,14 @@ static void move(Query<Write<Player>, Write<Position>, Write<PhysicsVelocity>, W
targetTorso.y += glm::sin(player->animationTime) * 0.5;
}

player->isOnGround = false;
if (!player->wasOnGround)
{
player->ground = Entity{};
}
else
{
player->wasOnGround = false;
}
}
else
{
Expand All @@ -124,10 +146,9 @@ static void move(Query<Write<Player>, Write<Position>, Write<PhysicsVelocity>, W
}
}

// handle dead

void demo::playersPlugin(Cubos& cubos)
{
cubos.addComponent<Player>();
cubos.system(move).tagged("player.move").tagged("cubos.physics.apply_forces").before("cubos.transform.update");
cubos.system(collisionSystem).after("player.move").tagged("cubos.physics.apply_forces");
}

0 comments on commit fbcbb48

Please sign in to comment.