From 16fb1d26a4e64a61ee593ce3556ab39651604f6c Mon Sep 17 00:00:00 2001 From: Sara Date: Thu, 9 Jan 2025 21:57:21 +0100 Subject: [PATCH] feat: implemented player movement --- src/player.cpp | 65 ++++++++++++++++++++++++++++++++++++++++++++++---- src/player.hpp | 9 +++++++ 2 files changed, 69 insertions(+), 5 deletions(-) diff --git a/src/player.cpp b/src/player.cpp index 76f1532..9dd6bb5 100644 --- a/src/player.cpp +++ b/src/player.cpp @@ -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 +#include Player::Player() -: ce::CollidableNode("player", 0x1u, 0x1u) { - this->sprite = this->create_child("bike", "bike"); - this->shape = this->create_child("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("bike", "bike")} +, shape{this->create_child("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::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::make( + this, &Player::_input_vertical_movement) + ); + this->overlap_enter.connect(ce::Callable::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().value_or(0.f); +} + +void Player::_input_vertical_movement(ce::InputValue value) { + this->input.y = value.get().value_or(0.f); +} + +void Player::_on_overlap_enter(ce::CollisionShape *, ce::CollidableNode *other, ce::CollisionShape *) { + if(Truck *truck{dynamic_cast(other)}) { + // TODO: Implement damage + this->flag_for_deletion(); + } + SDL_Log("overlap"); } diff --git a/src/player.hpp b/src/player.hpp index 9a2982a..2c0321f 100644 --- a/src/player.hpp +++ b/src/player.hpp @@ -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