diff options
author | Max-001 <maxsmits21@kpnmail.nl> | 2025-01-11 11:35:33 +0100 |
---|---|---|
committer | Max-001 <maxsmits21@kpnmail.nl> | 2025-01-11 11:35:33 +0100 |
commit | 46aec26cae3d2d405aac64e69bbba74944cd7a7b (patch) | |
tree | feb80d722c670de3e18922c57137febfc96e9d3e | |
parent | d557cfd311a6239e579fad35e3684f8c0e170ce7 (diff) |
Fix: Made player much more responsive
-rw-r--r-- | game/preview/PrevPlayerScript.cpp | 44 | ||||
-rw-r--r-- | game/preview/PrevPlayerScript.h | 17 |
2 files changed, 53 insertions, 8 deletions
diff --git a/game/preview/PrevPlayerScript.cpp b/game/preview/PrevPlayerScript.cpp index fce5c5a..0126bb2 100644 --- a/game/preview/PrevPlayerScript.cpp +++ b/game/preview/PrevPlayerScript.cpp @@ -21,10 +21,6 @@ bool PrevPlayerScript::key_pressed(const KeyPressEvent & ev) { this->body->data.flip = {false, false}; this->head->data.flip = {false, false}; break; - - case Keycode::SPACE: - this->get_component<Rigidbody>().data.linear_velocity.y = -move_speed; - break; case Keycode::D0: this->body_anim->set_anim(0); this->head_anim->set_anim(0); @@ -104,6 +100,8 @@ bool PrevPlayerScript::key_pressed(const KeyPressEvent & ev) { } void PrevPlayerScript::init() { + this->rb = get_component<Rigidbody>(); + auto animations = this->get_components<Animator>(); body_anim = animations[0]; head_anim = animations[1]; @@ -115,12 +113,50 @@ void PrevPlayerScript::init() { subscribe<KeyPressEvent>([this](const KeyPressEvent & ev) -> bool { return this->key_pressed(ev); }); + subscribe<KeyPressEvent>([this](const KeyPressEvent & ev) -> bool { + if (ev.repeat) return false; + return this->on_key_down(ev); + }); + subscribe<KeyReleaseEvent>([this](const KeyReleaseEvent & ev) -> bool { + return this->on_key_up(ev); + }); }; void PrevPlayerScript::fixed_update(crepe::duration_t dt) { + if (this->get_key_state(Keycode::SPACE)) { + this->rb->add_force_linear( + vec2(0, -1) * (engine_gravity * PLAYER_GRAVITY_SCALE * dt.count()) + ); + } + auto & savemgr = this->get_save_manager(); const auto & pos = this->get_component<Transform>().position; savemgr.set("player_x", pos.x); savemgr.set("player_y", pos.y); }; + +bool PrevPlayerScript::on_key_down(const KeyPressEvent & ev) { + if (ev.key == Keycode::SPACE) { + const vec2 UP = {0, -1}; + this->help_kick(UP); + } + return false; +} + +bool PrevPlayerScript::on_key_up(const KeyReleaseEvent & ev) { + if (ev.key == Keycode::SPACE) { + const vec2 DOWN = {0, 1}; + this->help_kick(DOWN); + } + return false; +} + +void PrevPlayerScript::help_kick(const vec2 & direction) { + // softly "kick" the player (at start/end of flight) + vec2 & velocity = this->rb->data.linear_velocity; + float kick_amount = std::min( + velocity.length() * PLAYER_HELP_KICK_SCALE, engine_gravity * PLAYER_HELP_KICK_MAX + ); + velocity += direction * kick_amount; +} diff --git a/game/preview/PrevPlayerScript.h b/game/preview/PrevPlayerScript.h index cc3184e..ae66449 100644 --- a/game/preview/PrevPlayerScript.h +++ b/game/preview/PrevPlayerScript.h @@ -1,10 +1,10 @@ - +#include <crepe/api/Animator.h> +#include <crepe/api/Config.h> #include <crepe/api/Event.h> +#include <crepe/api/Rigidbody.h> #include <crepe/api/Script.h> -#include <crepe/util/OptionalRef.h> - -#include <crepe/api/Animator.h> #include <crepe/api/Sprite.h> +#include <crepe/util/OptionalRef.h> class PrevPlayerScript : public crepe::Script { private: @@ -20,4 +20,13 @@ private: void init(); void fixed_update(crepe::duration_t dt); bool key_pressed(const crepe::KeyPressEvent & ev); + +private: + bool on_key_down(const crepe::KeyPressEvent & ev); + bool on_key_up(const crepe::KeyReleaseEvent & ev); + void help_kick(const crepe::vec2 & direction); + +private: + float & engine_gravity = crepe::Config::get_instance().physics.gravity; + crepe::OptionalRef<crepe::Rigidbody> rb; }; |