aboutsummaryrefslogtreecommitdiff
path: root/game/player/PlayerScript.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'game/player/PlayerScript.cpp')
-rw-r--r--game/player/PlayerScript.cpp47
1 files changed, 43 insertions, 4 deletions
diff --git a/game/player/PlayerScript.cpp b/game/player/PlayerScript.cpp
index 57819c0..8cbe8dc 100644
--- a/game/player/PlayerScript.cpp
+++ b/game/player/PlayerScript.cpp
@@ -2,6 +2,7 @@
#include "../Config.h"
#include "../enemy/BattleScript.h"
+
#include <crepe/api/Animator.h>
#include <crepe/api/AudioSource.h>
#include <crepe/api/BoxCollider.h>
@@ -17,8 +18,42 @@ void PlayerScript::init() {
subscribe<CollisionEvent>([this](const CollisionEvent & ev) -> bool {
return this->on_collision(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);
+ });
this->last_fired = std::chrono::steady_clock::now();
+ this->body = get_component<Rigidbody>();
+}
+
+bool PlayerScript::on_key_down(const KeyPressEvent & ev) {
+ if (ev.key == Keycode::SPACE) {
+ const vec2 UP = {0, -1};
+ this->help_kick(UP);
+ }
+ return false;
+}
+
+bool PlayerScript::on_key_up(const KeyReleaseEvent & ev) {
+ if (ev.key == Keycode::SPACE) {
+ const vec2 DOWN = {0, 1};
+ this->help_kick(DOWN);
+ }
+ return false;
+}
+
+void PlayerScript::help_kick(const vec2 & direction) {
+ // softly "kick" the player (at start/end of flight)
+ vec2 & velocity = this->body->data.linear_velocity;
+ float kick_amount = std::min(
+ velocity.length() * PLAYER_HELP_KICK_SCALE, engine_gravity * PLAYER_HELP_KICK_MAX
+ );
+ velocity += direction * kick_amount;
}
+
bool PlayerScript::on_collision(const CollisionEvent & ev) {
BehaviorScript & play_scr = this->get_components_by_name<BehaviorScript>("player").front();
BehaviorScript & end_scr = this->get_components_by_name<BehaviorScript>("player").back();
@@ -93,7 +128,7 @@ void PlayerScript::fixed_update(crepe::duration_t dt) {
emitter.data.boundary.offset = vec2(0, -transform.position.y);
}
- Rigidbody & rb = this->get_components_by_name<Rigidbody>("player").front();
+ Rigidbody & rb = this->body;
if (this->get_key_state(Keycode::ENTER)) {
auto now = std::chrono::steady_clock::now();
@@ -104,7 +139,10 @@ void PlayerScript::fixed_update(crepe::duration_t dt) {
}
}
if (this->get_key_state(Keycode::SPACE)) {
- rb.add_force_linear(vec2(0, -PLAYER_GRAVITY_SCALE / 2.5) * dt.count() / 0.02);
+ rb.add_force_linear(
+ vec2(0, -1) * (engine_gravity * PLAYER_GRAVITY_SCALE * dt.count())
+ );
+
if (prev_anim != 1) {
for (Animator & anim : animators) {
anim.active = true;
@@ -125,8 +163,9 @@ void PlayerScript::fixed_update(crepe::duration_t dt) {
if (current_jetpack_sound > 7) {
current_jetpack_sound = 0;
}
- } else if (transform.position.y == 195) {
- if (prev_anim != 0) {
+ } else if (transform.position.y == 200) {
+ Rigidbody & rb = this->body;
+ if (prev_anim != 0 && rb.data.linear_velocity.x != 0) {
for (Animator & anim : animators) {
anim.active = true;
anim.set_anim(0);