diff options
Diffstat (limited to 'game/preview/PrevPlayerScript.cpp')
-rw-r--r-- | game/preview/PrevPlayerScript.cpp | 71 |
1 files changed, 48 insertions, 23 deletions
diff --git a/game/preview/PrevPlayerScript.cpp b/game/preview/PrevPlayerScript.cpp index 2657b8d..ae25dad 100644 --- a/game/preview/PrevPlayerScript.cpp +++ b/game/preview/PrevPlayerScript.cpp @@ -1,12 +1,11 @@ #include "PrevPlayerScript.h" #include "../missile/SpawnEvent.h" -#include "api/Transform.h" + #include <crepe/api/AudioSource.h> #include <crepe/api/Camera.h> +#include <crepe/api/Transform.h> #include <crepe/manager/SaveManager.h> -#include <iostream> -#include <ostream> using namespace crepe; @@ -22,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); @@ -59,25 +54,20 @@ bool PrevPlayerScript::key_pressed(const KeyPressEvent & ev) { this->head_anim->set_anim(7); break; case Keycode::LEFT: - this->head->data.angle_offset -= 1; + this->get_component<Transform>().rotation += 10; break; case Keycode::RIGHT: - this->head->data.angle_offset += 1; + this->get_component<Transform>().rotation -= 10; break; case Keycode::UP: - this->head->data.scale_offset += 0.1; + this->head->data.position_offset += 10; break; case Keycode::DOWN: - this->head->data.scale_offset -= 0.1; + this->head->data.position_offset -= 10; break; case Keycode::P: - this->get_component<AudioSource>().play(); - break; - case Keycode::Q: - this->get_components_by_name<Camera>("camera").front().get().data.zoom -= 0.01; - break; - case Keycode::E: - this->get_components_by_name<Camera>("camera").front().get().data.zoom += 0.01; + this->get_components_by_name<AudioSource>("background_music").front().get().active + = true; break; case Keycode::J: this->get_components_by_name<Transform>("camera").front().get().position.x @@ -98,11 +88,6 @@ bool PrevPlayerScript::key_pressed(const KeyPressEvent & ev) { case Keycode::M: trigger_event<MissileSpawnEvent>(MissileSpawnEvent {}); break; - //todo - case Keycode::PAGE_UP: - case Keycode::PAGE_DOWN: - case Keycode::HOME: - break; default: break; } @@ -110,6 +95,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]; @@ -121,12 +108,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; +} |