diff options
Diffstat (limited to 'game/preview')
-rw-r--r-- | game/preview/NpcScript.cpp | 8 | ||||
-rw-r--r-- | game/preview/NpcSubScene.cpp | 2 | ||||
-rw-r--r-- | game/preview/PrevPlayerScript.cpp | 50 | ||||
-rw-r--r-- | game/preview/PrevPlayerScript.h | 17 | ||||
-rw-r--r-- | game/preview/PrevPlayerSubScene.cpp | 1 |
5 files changed, 60 insertions, 18 deletions
diff --git a/game/preview/NpcScript.cpp b/game/preview/NpcScript.cpp index 5a93c2b..86117d4 100644 --- a/game/preview/NpcScript.cpp +++ b/game/preview/NpcScript.cpp @@ -9,7 +9,7 @@ using namespace crepe; void NpcScript::fixed_update(duration_t dt) { auto & rb = this->get_component<Rigidbody>(); - auto & npc = this->get_component<Sprite>(); + auto npc = this->get_components<Sprite>(); auto & transform = this->get_component<Transform>(); if (transform.position.x < 200) { @@ -20,8 +20,10 @@ void NpcScript::fixed_update(duration_t dt) { } if (rb.data.linear_velocity.x < 0) { - npc.data.flip = {true, false}; + npc.front().get().data.flip = {true, false}; + npc.back().get().data.flip = {true, false}; } else { - npc.data.flip = {false, false}; + npc.front().get().data.flip = {false, false}; + npc.back().get().data.flip = {false, false}; } } diff --git a/game/preview/NpcSubScene.cpp b/game/preview/NpcSubScene.cpp index c9ab5b6..9fddb78 100644 --- a/game/preview/NpcSubScene.cpp +++ b/game/preview/NpcSubScene.cpp @@ -57,7 +57,7 @@ NpcSubScene::NpcSubScene(Scene & scn) { .body_type = Rigidbody::BodyType::DYNAMIC, .linear_velocity = {-50, 0}, //.max_linear_velocity = 40, - .collision_layers = {COLL_LAY_BOT_TOP, COLL_LAY_PLAYER, 100}, + .collision_layers = {COLL_LAY_BOT_TOP, 100}, .collision_layer = COLL_LAY_PLAYER, }); diff --git a/game/preview/PrevPlayerScript.cpp b/game/preview/PrevPlayerScript.cpp index fce5c5a..c63c1c8 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); @@ -72,12 +68,6 @@ bool PrevPlayerScript::key_pressed(const KeyPressEvent & ev) { 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; - break; case Keycode::J: this->get_components_by_name<Transform>("camera").front().get().position.x -= move_speed; @@ -104,6 +94,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 +107,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; }; diff --git a/game/preview/PrevPlayerSubScene.cpp b/game/preview/PrevPlayerSubScene.cpp index 9ff111c..56a434a 100644 --- a/game/preview/PrevPlayerSubScene.cpp +++ b/game/preview/PrevPlayerSubScene.cpp @@ -78,4 +78,5 @@ PrevPlayerSubScene::PrevPlayerSubScene(Scene & scn) { }); player.add_component<BoxCollider>(vec2(50, 50)); player.add_component<BehaviorScript>().set_script<PrevPlayerScript>(); + player.add_component<AudioSource>(Asset {"asset/music/level.ogg"}).stop(); } |