Skip to content

Commit

Permalink
feat(platformer): move some settings to Player component
Browse files Browse the repository at this point in the history
  • Loading branch information
RiscadoA committed Nov 14, 2023
1 parent ae10157 commit 450637e
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 14 deletions.
4 changes: 2 additions & 2 deletions platformer/assets/bindings/player0.bind
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@
},
"look-horizontal": {
"pos": [
"Left"
"Right"
],
"neg": [
"Right"
"Left"
],
"gamepad": []
},
Expand Down
4 changes: 2 additions & 2 deletions platformer/assets/bindings/player1.bind
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
},
"turn": {
"pos": [
"f"
"h"
],
"neg": [
"h"
"f"
],
"gamepad": []
},
Expand Down
7 changes: 5 additions & 2 deletions platformer/assets/scenes/player.cubos
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
"leftHand": "left-hand",
"rightHand": "right-hand",
"leftFoot": "left-foot",
"rightFoot": "right-foot"
"rightFoot": "right-foot",
"speed": 20.0,
"animationSpeed": 1.5,
"jumpForce": 1500.0
},
"cubos::engine::LocalToWorld": {},
"cubos::engine::Position": {},
Expand All @@ -31,7 +34,7 @@
"target": "player",
"theta": 90.0,
"phi": 15.0,
"distance": 10.0,
"distance": 15.0,
"speed": 10.0,
"rotationSpeed": 150.0,
"zoomSpeed": 30.0
Expand Down
10 changes: 9 additions & 1 deletion platformer/src/orbit_camera/plugin.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "plugin.hpp"
#include "controller.hpp"
#include "../player/player.hpp"

#include <cubos/engine/input/input.hpp>
#include <cubos/engine/transform/position.hpp>
Expand Down Expand Up @@ -29,7 +30,7 @@ static void inputSystem(Read<Input> input, Query<Write<OrbitCameraController>> c
}

static void followSystem(Query<Write<OrbitCameraController>, Write<Rotation>> cameras, Query<Write<Position>> positions,
Read<DeltaTime> deltaTime)
Read<DeltaTime> deltaTime, Query<Write<Player>> players)
{
for (auto [orbitEntity, orbitCamera, orbitRotation] : cameras)
{
Expand All @@ -46,6 +47,13 @@ static void followSystem(Query<Write<OrbitCameraController>, Write<Rotation>> ca

orbitPosition->vec = orbitCamera->center + offset * orbitCamera->distance;
orbitRotation->quat = glm::quatLookAt(-offset, {0.0F, 1.0F, 0.0F});

if (auto playerComponents = players[orbitCamera->target])
{
auto [player] = *playerComponents;
player->forward = glm::normalize(glm::vec3(offset.x, 0.0F, offset.z));
player->right = glm::normalize(glm::cross(player->forward, {0.0F, 1.0F, 0.0F}));
}
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions platformer/src/player/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ CUBOS_REFLECT_IMPL(demo::Player)
.withField("id", &Player::id)
.withField("isOnGround", &Player::isOnGround)
.withField("animationTime", &Player::animationTime)
.withField("animationSpeed", &Player::animationSpeed)
.withField("speed", &Player::speed)
.withField("jumpForce", &Player::jumpForce)
.withField("torso", &Player::torso)
.withField("leftHand", &Player::leftHand)
.withField("rightHand", &Player::rightHand)
Expand Down
3 changes: 3 additions & 0 deletions platformer/src/player/player.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ namespace demo
int id = 0;
bool isOnGround = false;
float animationTime = 0.0F;
float animationSpeed = 1.0F;
float jumpForce = 1000.0F;
float speed = 10.0F;
cubos::core::ecs::Entity torso;
cubos::core::ecs::Entity leftHand;
cubos::core::ecs::Entity rightHand;
Expand Down
13 changes: 6 additions & 7 deletions platformer/src/player/plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,24 +51,22 @@ static void move(Query<Write<Player>, Write<Position>, Write<PhysicsVelocity>, W
for (auto [entity, player, position, velocity, rotation] : query)
{
const float force = settings->getDouble("force", 5000.0F);
const float jumpForce = settings->getDouble("jumpForce", 2000.0F);
const float dragForce = settings->getDouble("dragForce", -2000.0F);
const float rotationSpeed = settings->getDouble("rotationSpeed", 0.02F);
const float speed = settings->getDouble("speed", 25.0F);

auto moveVertical = -input->axis("move", player->id);
//auto moveHorizontal = input->axis("horizontal", player->id);
// auto moveHorizontal = input->axis("horizontal", player->id);
auto jump = input->pressed("jump", player->id);

if (player->isOnGround)
{
glm::vec3 newVelocity = moveVertical * player->forward * speed;
glm::vec3 newVelocity = moveVertical * player->forward * player->speed;
velocity->velocity.x = newVelocity.x;
velocity->velocity.z = newVelocity.z;

if (jump)
{
velocity->impulse += glm::vec3{0.0F, 1.0F, 0.0F} * jumpForce;
velocity->impulse += glm::vec3{0.0F, 1.0F, 0.0F} * player->jumpForce;
player->isOnGround = false;
}
}
Expand All @@ -82,7 +80,7 @@ static void move(Query<Write<Player>, Write<Position>, Write<PhysicsVelocity>, W
if (player->isOnGround)
{
player->animationTime +=
glm::sign(moveVertical) * deltaTime->value * settings->getDouble("animationSpeed", 20.0F);
glm::sign(moveVertical) * deltaTime->value * player->speed * player->animationSpeed;

if (moveVertical != 0.0F)
{
Expand All @@ -109,7 +107,8 @@ static void move(Query<Write<Player>, Write<Position>, Write<PhysicsVelocity>, W

if (velocity->velocity.x != 0.0F || velocity->velocity.z != 0.0F)
{
rotation->quat = glm::quatLookAt(glm::normalize(-velocity->velocity * glm::vec3(1.0F, 0.0F, 1.0F)), glm::vec3{0.0F, 1.0F, 0.0F});
rotation->quat = glm::quatLookAt(glm::normalize(-velocity->velocity * glm::vec3(1.0F, 0.0F, 1.0F)),
glm::vec3{0.0F, 1.0F, 0.0F});
}

auto [torso] = *offsets[player->torso];
Expand Down

0 comments on commit 450637e

Please sign in to comment.