diff options
-rw-r--r-- | game/CMakeLists.txt | 1 | ||||
-rw-r--r-- | game/workers/PanicFromPlayer.cpp | 45 | ||||
-rw-r--r-- | game/workers/PanicFromPlayer.h | 8 | ||||
-rw-r--r-- | game/workers/WorkerScript.cpp | 10 | ||||
-rw-r--r-- | game/workers/WorkersSubScene.cpp | 9 |
5 files changed, 73 insertions, 0 deletions
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 <crepe/api/Animator.h> +#include <crepe/api/Rigidbody.h> +#include <crepe/api/Sprite.h> +#include <crepe/api/Transform.h> +#include <crepe/types.h> + +using namespace crepe; +using namespace std; + +void PanicFromPlayer::fixed_update(duration_t dt) { + Animator & anim_player = this->get_components_by_name<Animator>("player").front(); + + if (anim_player.data.col == 1) { + Transform & trans_player = this->get_components_by_name<Transform>("player").back(); + Transform & trans_worker = this->get_components<Transform>().back(); + + float result_x = trans_player.position.x - trans_worker.position.x; + + if (result_x < 100 && result_x > -20) { + RefVector<Animator> anim_worker = this->get_components<Animator>(); + RefVector<Sprite> sprite_worker = this->get_components<Sprite>(); + Rigidbody & rb_worker = this->get_components<Rigidbody>().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 <crepe/api/Script.h> + +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<BehaviorScript>().set_script<PanicFromPlayer>(); 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<BehaviorScript>().set_script<PanicFromPlayer>(); 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<BehaviorScript>().set_script<PanicFromPlayer>(); 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<BehaviorScript>().set_script<PanicFromPlayer>(); 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<BehaviorScript>().set_script<PanicFromPlayer>(); 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<BehaviorScript>().set_script<PanicFromPlayer>(); 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<BehaviorScript>().set_script<PanicFromPlayer>(); 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<BehaviorScript>().set_script<PanicFromPlayer>(); if (init_speed < 0) { worker_8_body_sprite.data.flip = Sprite::FlipSettings {true, false}; |