feat: implemented player movement

main
Sara 2025-01-09 21:57:21 +01:00
parent 4fb9a645d8
commit 16fb1d26a4
2 changed files with 69 additions and 5 deletions

View File

@ -1,16 +1,71 @@
#include "player.hpp"
#include "core/callable.hpp"
#include "core/canvas_engine.hpp"
#include "core/collidable_node.hpp"
#include "core/collision_shape.hpp"
#include "core/input/input_effects.hpp"
#include "core/input/input_map.hpp"
#include "core/math/transform.hpp"
#include "core/sprite.hpp"
#include "truck.hpp"
#include <SDL2/SDL_keyboard.h>
#include <SDL2/SDL_keycode.h>
Player::Player()
: ce::CollidableNode("player", 0x1u, 0x1u) {
this->sprite = this->create_child<ce::Sprite>("bike", "bike");
this->shape = this->create_child<ce::CollisionShape>("PlayerShape", this, ce::Shape::make_box(5.f, 10.f));
ce::Transform trans{this->get_global_transform()};
this->set_global_transform(trans);
: ce::CollidableNode("player", 0x1u, 0x1u)
, sprite{this->create_child<ce::Sprite>("bike", "bike")}
, shape{this->create_child<ce::CollisionShape>("player_col_shape", this, ce::Shape::make_box(1.f, 2.f))} {
this->sprite->set_global_transform(this->sprite->get_global_transform()
.scaled({1.5f, 1.5f})
);
ce::InputMap &map{ce::CanvasEngine::get_singleton()->get_input_map()};
map.bind_input("horizontal", {
new ce::ButtonAxis(
new ce::KeyboardScancode(SDL_SCANCODE_A),
new ce::KeyboardScancode(SDL_SCANCODE_D)
)
})
.changed
.connect(ce::Callable<void, ce::InputValue>::make(
this, &Player::_input_horizontal_movement)
);
map.bind_input("vertical", {
new ce::ButtonAxis(
new ce::KeyboardScancode(SDL_SCANCODE_W),
new ce::KeyboardScancode(SDL_SCANCODE_S)
)
})
.changed
.connect(ce::Callable<void, ce::InputValue>::make(
this, &Player::_input_vertical_movement)
);
this->overlap_enter.connect(ce::Callable<void, ce::CollisionShape *, ce::CollidableNode *, ce::CollisionShape *>::make(this, &Player::_on_overlap_enter));
}
void Player::_tick(double const &delta) {
this->velocity = ce::Vecf::move_towards(
this->velocity,
this->input.scaled(SPEED),
ACCELERATION * delta
);
ce::Transform trans{this->get_global_transform().translated(this->velocity * delta)};
trans.position.x = std::clamp(trans.position.x, -3.f, 3.f);
trans.position.y = std::clamp(trans.position.y, -2.f, 2.f);
this->set_global_transform(trans);
}
void Player::_input_horizontal_movement(ce::InputValue value) {
this->input.x = value.get<float>().value_or(0.f);
}
void Player::_input_vertical_movement(ce::InputValue value) {
this->input.y = value.get<float>().value_or(0.f);
}
void Player::_on_overlap_enter(ce::CollisionShape *, ce::CollidableNode *other, ce::CollisionShape *) {
if(Truck *truck{dynamic_cast<Truck*>(other)}) {
// TODO: Implement damage
this->flag_for_deletion();
}
SDL_Log("overlap");
}

View File

@ -2,6 +2,7 @@
#define PLAYER_HPP
#include "core/collidable_node.hpp"
#include "core/input/input_value.hpp"
namespace ce {
class CollisionShape;
@ -9,11 +10,19 @@ class Sprite;
};
class Player : public ce::CollidableNode {
ce::Vecf const SPEED{3.f, 2.5f};
float const ACCELERATION{20.f};
ce::Vecf velocity{0.f, 0.f};
ce::Vecf input{0.f, 0.f};
ce::CollisionShape *shape{nullptr};
ce::Sprite *sprite{nullptr};
public:
Player();
virtual void _tick(double const &delta) override;
void _input_horizontal_movement(ce::InputValue value);
void _input_vertical_movement(ce::InputValue value);
void _on_overlap_enter(ce::CollisionShape *, ce::CollidableNode *node, ce::CollisionShape*);
};
#endif // !PLAYER_HPP