From 1f0edff951e7580163d8c987d4a021b24d45e9f1 Mon Sep 17 00:00:00 2001 From: Max-001 Date: Tue, 7 Jan 2025 14:34:12 +0100 Subject: Added panic from player --- game/CMakeLists.txt | 1 + game/workers/PanicFromPlayer.cpp | 45 ++++++++++++++++++++++++++++++++++++++++ game/workers/PanicFromPlayer.h | 8 +++++++ game/workers/WorkerScript.cpp | 10 +++++++++ game/workers/WorkersSubScene.cpp | 9 ++++++++ 5 files changed, 73 insertions(+) create mode 100644 game/workers/PanicFromPlayer.cpp create mode 100644 game/workers/PanicFromPlayer.h diff --git a/game/CMakeLists.txt b/game/CMakeLists.txt index 8a63bd0..dc6a3fd 100644 --- a/game/CMakeLists.txt +++ b/game/CMakeLists.txt @@ -24,6 +24,7 @@ add_executable(main background/StartSubScene.cpp workers/WorkersSubScene.cpp workers/WorkerScript.cpp + workers/PanicFromPlayer.cpp main.cpp ) diff --git a/game/workers/PanicFromPlayer.cpp b/game/workers/PanicFromPlayer.cpp new file mode 100644 index 0000000..fbf4611 --- /dev/null +++ b/game/workers/PanicFromPlayer.cpp @@ -0,0 +1,45 @@ +#include "PanicFromPlayer.h" + +#include +#include +#include +#include +#include + +using namespace crepe; +using namespace std; + +void PanicFromPlayer::fixed_update(duration_t dt) { + Animator & anim_player = this->get_components_by_name("player").front(); + + if (anim_player.data.col == 1) { + Transform & trans_player = this->get_components_by_name("player").back(); + Transform & trans_worker = this->get_components().back(); + + float result_x = trans_player.position.x - trans_worker.position.x; + + if (result_x < 100 && result_x > -20) { + RefVector anim_worker = this->get_components(); + RefVector sprite_worker = this->get_components(); + Rigidbody & rb_worker = this->get_components().back(); + + if (anim_worker.front().get().data.col != 1) { + anim_worker.front().get().set_anim(1); + anim_worker.back().get().set_anim(1); + + anim_worker.front().get().data.fps = 10; + anim_worker.back().get().data.fps = 10; + } + + if (result_x < 0) { + rb_worker.data.linear_velocity.x = 10000 * dt.count(); + sprite_worker.front().get().data.flip.flip_x = false; + sprite_worker.back().get().data.flip.flip_x = false; + } else { + rb_worker.data.linear_velocity.x = -5000 * dt.count(); + sprite_worker.front().get().data.flip.flip_x = true; + sprite_worker.back().get().data.flip.flip_x = true; + } + } + } +}; diff --git a/game/workers/PanicFromPlayer.h b/game/workers/PanicFromPlayer.h new file mode 100644 index 0000000..804df5c --- /dev/null +++ b/game/workers/PanicFromPlayer.h @@ -0,0 +1,8 @@ +#pragma once + +#include + +class PanicFromPlayer : public crepe::Script { +public: + void fixed_update(crepe::duration_t dt); +}; diff --git a/game/workers/WorkerScript.cpp b/game/workers/WorkerScript.cpp index d641c6a..1bcf8d5 100644 --- a/game/workers/WorkerScript.cpp +++ b/game/workers/WorkerScript.cpp @@ -53,6 +53,8 @@ void WorkerScript::fixed_update(duration_t dt) { = -rb_worker.data.linear_velocity.x / 5; animator_worker.back().get().data.fps = -rb_worker.data.linear_velocity.x / 5; + animator_worker.front().get().set_anim(0); + animator_worker.back().get().set_anim(0); } else { sprite_worker.front().get().data.flip.flip_x = false; sprite_worker.back().get().data.flip.flip_x = false; @@ -61,6 +63,8 @@ void WorkerScript::fixed_update(duration_t dt) { = rb_worker.data.linear_velocity.x / 5; animator_worker.back().get().data.fps = rb_worker.data.linear_velocity.x / 5; + animator_worker.front().get().set_anim(0); + animator_worker.back().get().set_anim(0); } } } else { @@ -89,6 +93,9 @@ void WorkerScript::fixed_update(duration_t dt) { = -rb_worker.data.linear_velocity.x / 5; animator_worker.back().get().data.fps = -rb_worker.data.linear_velocity.x / 5; + + animator_worker.front().get().set_anim(0); + animator_worker.back().get().set_anim(0); } else { sprite_worker.front().get().data.flip.flip_x = false; sprite_worker.back().get().data.flip.flip_x = false; @@ -97,6 +104,9 @@ void WorkerScript::fixed_update(duration_t dt) { = rb_worker.data.linear_velocity.x / 5; animator_worker.back().get().data.fps = rb_worker.data.linear_velocity.x / 5; + + animator_worker.front().get().set_anim(0); + animator_worker.back().get().set_anim(0); } } } diff --git a/game/workers/WorkersSubScene.cpp b/game/workers/WorkersSubScene.cpp index 94d9848..5b070f8 100644 --- a/game/workers/WorkersSubScene.cpp +++ b/game/workers/WorkersSubScene.cpp @@ -1,4 +1,5 @@ #include "WorkersSubScene.h" +#include "PanicFromPlayer.h" #include "WorkerScript.h" #include "../Config.h" @@ -68,6 +69,7 @@ void WorkersSubScene::worker1(crepe::Scene & scn, float start_x, float init_spee .linear_velocity = vec2(init_speed, 0), .collision_layers = {COLL_LAY_BOT_TOP}, }); + worker_1.add_component().set_script(); if (init_speed < 0) { worker_1_body_sprite.data.flip = Sprite::FlipSettings {true, false}; @@ -115,6 +117,7 @@ void WorkersSubScene::worker2(crepe::Scene & scn, float start_x, float init_spee .linear_velocity = vec2(init_speed, 0), .collision_layers = {COLL_LAY_BOT_TOP}, }); + worker_2.add_component().set_script(); if (init_speed < 0) { worker_2_body_sprite.data.flip = Sprite::FlipSettings {true, false}; @@ -162,6 +165,7 @@ void WorkersSubScene::worker3(crepe::Scene & scn, float start_x, float init_spee .linear_velocity = vec2(init_speed, 0), .collision_layers = {COLL_LAY_BOT_TOP}, }); + worker_3.add_component().set_script(); if (init_speed < 0) { worker_3_body_sprite.data.flip = Sprite::FlipSettings {true, false}; @@ -209,6 +213,7 @@ void WorkersSubScene::worker4(crepe::Scene & scn, float start_x, float init_spee .linear_velocity = vec2(init_speed, 0), .collision_layers = {COLL_LAY_BOT_HIGH}, }); + worker_4.add_component().set_script(); if (init_speed < 0) { worker_4_body_sprite.data.flip = Sprite::FlipSettings {true, false}; @@ -256,6 +261,7 @@ void WorkersSubScene::worker5(crepe::Scene & scn, float start_x, float init_spee .linear_velocity = vec2(init_speed, 0), .collision_layers = {COLL_LAY_BOT_HIGH}, }); + worker_5.add_component().set_script(); if (init_speed < 0) { worker_5_body_sprite.data.flip = Sprite::FlipSettings {true, false}; @@ -303,6 +309,7 @@ void WorkersSubScene::worker6(crepe::Scene & scn, float start_x, float init_spee .linear_velocity = vec2(init_speed, 0), .collision_layers = {COLL_LAY_BOT_LOW}, }); + worker_6.add_component().set_script(); if (init_speed < 0) { worker_6_body_sprite.data.flip = Sprite::FlipSettings {true, false}; @@ -350,6 +357,7 @@ void WorkersSubScene::worker7(crepe::Scene & scn, float start_x, float init_spee .linear_velocity = vec2(init_speed, 0), .collision_layers = {COLL_LAY_BOT_LOW}, }); + worker_7.add_component().set_script(); if (init_speed < 0) { worker_7_body_sprite.data.flip = Sprite::FlipSettings {true, false}; @@ -397,6 +405,7 @@ void WorkersSubScene::worker8(crepe::Scene & scn, float start_x, float init_spee .linear_velocity = vec2(init_speed, 0), .collision_layers = {COLL_LAY_BOT_LOW}, }); + worker_8.add_component().set_script(); if (init_speed < 0) { worker_8_body_sprite.data.flip = Sprite::FlipSettings {true, false}; -- cgit v1.2.3