From f2a24bc4f5956d7d59ae42fade277a7826e174da Mon Sep 17 00:00:00 2001 From: Max-001 Date: Mon, 6 Jan 2025 13:21:23 +0100 Subject: First audio --- game/GameScene.cpp | 9 +++++++++ game/StartGameScript.cpp | 9 +++++++++ 2 files changed, 18 insertions(+) (limited to 'game') diff --git a/game/GameScene.cpp b/game/GameScene.cpp index a8fcb47..e193f44 100644 --- a/game/GameScene.cpp +++ b/game/GameScene.cpp @@ -9,6 +9,7 @@ #include #include #include +#include #include #include #include @@ -67,6 +68,14 @@ void GameScene::load_scene() { GameObject start_game_script = new_object("start_game_script", "script", vec2(0, 0)); start_game_script.add_component().set_script(); + GameObject background_music = new_object("background_music", "audio", vec2(0, 0)); + Asset background_music_asset {"asset/music/level.ogg"}; + background_music.add_component(background_music_asset).loop = true; + + GameObject boom_audio = new_object("boom_audio", "audio", vec2(0, 0)); + Asset boom_audio_asset {"asset/sfx/window_smash.ogg"}; + boom_audio.add_component(boom_audio_asset); + // zapper, laser and missile (below) for testing purpose only!!! GameObject zapper = new_object("zapper", "zapper", vec2(1000, 0)); Asset zapper_asset {"asset/obstacles/zapper/regular_zappers/zapEffect.png"}; diff --git a/game/StartGameScript.cpp b/game/StartGameScript.cpp index c786eb4..273666c 100644 --- a/game/StartGameScript.cpp +++ b/game/StartGameScript.cpp @@ -2,6 +2,7 @@ #include "Config.h" #include +#include #include #include @@ -34,6 +35,10 @@ void StartGameScript::fixed_update(crepe::duration_t dt) { emitter.active = true; } + AudioSource & boom_audio + = this->get_components_by_name("boom_audio").front(); + boom_audio.play(); + this->created_hole = true; } @@ -45,6 +50,10 @@ void StartGameScript::fixed_update(crepe::duration_t dt) { Sprite & jetpack_sprite = this->get_components_by_name("player").back(); jetpack_sprite.active = true; + AudioSource & background_music + = this->get_components_by_name("background_music").front(); + background_music.play(); + this->took_jetpack = true; } -- cgit v1.2.3 From a97068c12dbbe309a98fd00bd24fdfd5a9cb9012 Mon Sep 17 00:00:00 2001 From: Max-001 Date: Mon, 6 Jan 2025 13:43:15 +0100 Subject: Added looping bool --- game/StartGameScript.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'game') diff --git a/game/StartGameScript.cpp b/game/StartGameScript.cpp index 273666c..466dbce 100644 --- a/game/StartGameScript.cpp +++ b/game/StartGameScript.cpp @@ -52,7 +52,7 @@ void StartGameScript::fixed_update(crepe::duration_t dt) { AudioSource & background_music = this->get_components_by_name("background_music").front(); - background_music.play(); + background_music.play(true); this->took_jetpack = true; } -- cgit v1.2.3 From 1433845c6eb04c7b017dfad1b091f20e6ceaf823 Mon Sep 17 00:00:00 2001 From: Max-001 Date: Mon, 6 Jan 2025 13:50:13 +0100 Subject: Deleted loop --- game/GameScene.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'game') diff --git a/game/GameScene.cpp b/game/GameScene.cpp index e193f44..2b6b051 100644 --- a/game/GameScene.cpp +++ b/game/GameScene.cpp @@ -70,7 +70,7 @@ void GameScene::load_scene() { GameObject background_music = new_object("background_music", "audio", vec2(0, 0)); Asset background_music_asset {"asset/music/level.ogg"}; - background_music.add_component(background_music_asset).loop = true; + background_music.add_component(background_music_asset); GameObject boom_audio = new_object("boom_audio", "audio", vec2(0, 0)); Asset boom_audio_asset {"asset/sfx/window_smash.ogg"}; -- cgit v1.2.3 From 2f278d36afd79efafaa5ed08ef123789b5563680 Mon Sep 17 00:00:00 2001 From: Max-001 Date: Mon, 6 Jan 2025 15:52:40 +0100 Subject: Added footstep sound --- game/CMakeLists.txt | 1 + game/StartGameScript.cpp | 7 ++++- game/player/PlayerAudioScript.cpp | 62 +++++++++++++++++++++++++++++++++++++++ game/player/PlayerAudioScript.h | 13 ++++++++ game/player/PlayerSubScene.cpp | 15 ++++++++++ 5 files changed, 97 insertions(+), 1 deletion(-) create mode 100644 game/player/PlayerAudioScript.cpp create mode 100644 game/player/PlayerAudioScript.h (limited to 'game') diff --git a/game/CMakeLists.txt b/game/CMakeLists.txt index 8e3692b..0b85b96 100644 --- a/game/CMakeLists.txt +++ b/game/CMakeLists.txt @@ -20,6 +20,7 @@ add_executable(main player/PlayerSubScene.cpp StartGameScript.cpp player/PlayerEndScript.cpp + player/PlayerAudioScript.cpp background/StartSubScene.cpp main.cpp ) diff --git a/game/StartGameScript.cpp b/game/StartGameScript.cpp index 466dbce..1a7e3e7 100644 --- a/game/StartGameScript.cpp +++ b/game/StartGameScript.cpp @@ -1,5 +1,6 @@ #include "StartGameScript.h" #include "Config.h" +#include "api/BehaviorScript.h" #include #include @@ -39,6 +40,10 @@ void StartGameScript::fixed_update(crepe::duration_t dt) { = this->get_components_by_name("boom_audio").front(); boom_audio.play(); + BehaviorScript & player_audio_script + = this->get_components_by_name("player_audio").front(); + player_audio_script.active = true; + this->created_hole = true; } @@ -52,7 +57,7 @@ void StartGameScript::fixed_update(crepe::duration_t dt) { AudioSource & background_music = this->get_components_by_name("background_music").front(); - background_music.play(true); + //background_music.play(true); this->took_jetpack = true; } diff --git a/game/player/PlayerAudioScript.cpp b/game/player/PlayerAudioScript.cpp new file mode 100644 index 0000000..6b5f630 --- /dev/null +++ b/game/player/PlayerAudioScript.cpp @@ -0,0 +1,62 @@ +#include "PlayerAudioScript.h" + +#include +#include + +using namespace crepe; +using namespace std; + +void PlayerAudioScript::fixed_update(crepe::duration_t dt) { + Animator & animator = this->get_components_by_name("player").front(); + + if (animator.data.col == 0) { + if (animator.data.row != this->last_row) { + if (animator.data.row == 0) { + // right footstep + if (current_footstep == 0) { + AudioSource & audio + = this->get_components_by_name("player_audio").at(0); + audio.play(); + } else if (current_footstep == 1) { + AudioSource & audio + = this->get_components_by_name("player_audio").at(2); + audio.play(); + } else if (current_footstep == 2) { + AudioSource & audio + = this->get_components_by_name("player_audio").at(4); + audio.play(); + } else if (current_footstep == 3) { + AudioSource & audio + = this->get_components_by_name("player_audio").at(6); + audio.play(); + } + } else if (animator.data.row == 2) { + // left footstep + if (current_footstep == 0) { + AudioSource & audio + = this->get_components_by_name("player_audio").at(1); + audio.play(); + current_footstep = 1; + } else if (current_footstep == 1) { + AudioSource & audio + = this->get_components_by_name("player_audio").at(3); + audio.play(); + current_footstep = 2; + } else if (current_footstep == 2) { + AudioSource & audio + = this->get_components_by_name("player_audio").at(5); + audio.play(); + current_footstep = 3; + } else if (current_footstep == 3) { + AudioSource & audio + = this->get_components_by_name("player_audio").at(7); + audio.play(); + current_footstep = 0; + } + } + this->last_row = animator.data.row; + } + } else { + this->last_row = -1; + } +} diff --git a/game/player/PlayerAudioScript.h b/game/player/PlayerAudioScript.h new file mode 100644 index 0000000..764cb20 --- /dev/null +++ b/game/player/PlayerAudioScript.h @@ -0,0 +1,13 @@ +#pragma once + +#include +#include + +class PlayerAudioScript : public crepe::Script { +public: + void fixed_update(crepe::duration_t dt); + +private: + int last_row = -1; + int current_footstep = 0; +}; diff --git a/game/player/PlayerSubScene.cpp b/game/player/PlayerSubScene.cpp index c1e5e2f..11575c6 100644 --- a/game/player/PlayerSubScene.cpp +++ b/game/player/PlayerSubScene.cpp @@ -1,10 +1,12 @@ #include "PlayerSubScene.h" +#include "PlayerAudioScript.h" #include "PlayerEndScript.h" #include "PlayerScript.h" #include "../Config.h" #include +#include #include #include #include @@ -150,4 +152,17 @@ PlayerSubScene::PlayerSubScene(Scene & scn) { }); player.add_component().set_script().active = false; player.add_component().set_script().active = false; + + GameObject player_audio = scn.new_object("player_audio", "player_audio", vec2(0, 0)); + player_audio.add_component(Asset("asset/sfx/barefoot_step_left_1.ogg")); + player_audio.add_component(Asset("asset/sfx/barefoot_step_right_1.ogg")); + player_audio.add_component(Asset("asset/sfx/barefoot_step_left_2.ogg")); + player_audio.add_component(Asset("asset/sfx/barefoot_step_right_2.ogg")); + player_audio.add_component(Asset("asset/sfx/barefoot_step_left_3.ogg")); + player_audio.add_component(Asset("asset/sfx/barefoot_step_right_3.ogg")); + player_audio.add_component(Asset("asset/sfx/barefoot_step_left_4.ogg")); + player_audio.add_component(Asset("asset/sfx/barefoot_step_right_4.ogg")); + + player_audio.add_component().set_script().active + = false; } -- cgit v1.2.3 From c8b1eb57bb373f80be9055baa51fed24e531373e Mon Sep 17 00:00:00 2001 From: Max-001 Date: Mon, 6 Jan 2025 16:46:55 +0100 Subject: Added more audio --- game/player/PlayerScript.cpp | 22 ++++++++++++++++++++++ game/player/PlayerScript.h | 1 + game/player/PlayerSubScene.cpp | 13 +++++++++++++ 3 files changed, 36 insertions(+) (limited to 'game') diff --git a/game/player/PlayerScript.cpp b/game/player/PlayerScript.cpp index 472d7c8..4404bd8 100644 --- a/game/player/PlayerScript.cpp +++ b/game/player/PlayerScript.cpp @@ -3,6 +3,7 @@ #include "../Config.h" #include +#include #include #include #include @@ -36,6 +37,10 @@ bool PlayerScript::on_collision(const CollisionEvent & ev) { } play_scr.active = false; end_scr.active = true; + + AudioSource & audio = this->get_components_by_name("player").at(0); + audio.play(); + return true; } else if (ev.info.other.metadata.tag == "laser") { for (Animator & anim : animators) { @@ -49,6 +54,10 @@ bool PlayerScript::on_collision(const CollisionEvent & ev) { } play_scr.active = false; end_scr.active = true; + + AudioSource & audio = this->get_components_by_name("player").at(1); + audio.play(); + return true; } else if (ev.info.other.metadata.tag == "missile") { for (Animator & anim : animators) { @@ -62,6 +71,10 @@ bool PlayerScript::on_collision(const CollisionEvent & ev) { } play_scr.active = false; end_scr.active = true; + + AudioSource & audio = this->get_components_by_name("player").at(2); + audio.play(); + return true; } @@ -92,6 +105,15 @@ void PlayerScript::fixed_update(crepe::duration_t dt) { emitter.data.emission_rate = 30; } } + + AudioSource & audio = this->get_components_by_name("player").at( + 3 + current_jetpack_sound + ); + audio.play(); + current_jetpack_sound++; + if (current_jetpack_sound > 7) { + current_jetpack_sound = 0; + } } else if (transform.position.y == 195) { if (prev_anim != 0) { for (Animator & anim : animators) { diff --git a/game/player/PlayerScript.h b/game/player/PlayerScript.h index d8eb098..482b40d 100644 --- a/game/player/PlayerScript.h +++ b/game/player/PlayerScript.h @@ -13,4 +13,5 @@ private: private: int prev_anim = 0; + int current_jetpack_sound = 0; }; diff --git a/game/player/PlayerSubScene.cpp b/game/player/PlayerSubScene.cpp index 11575c6..41fcf51 100644 --- a/game/player/PlayerSubScene.cpp +++ b/game/player/PlayerSubScene.cpp @@ -4,6 +4,7 @@ #include "PlayerScript.h" #include "../Config.h" +#include "api/Asset.h" #include #include @@ -153,6 +154,18 @@ PlayerSubScene::PlayerSubScene(Scene & scn) { player.add_component().set_script().active = false; player.add_component().set_script().active = false; + player.add_component(Asset("asset/sfx/dud_zapper_lp.ogg")); + player.add_component(Asset("asset/sfx/dud_zapper_pop.ogg")); + player.add_component(Asset("asset/sfx/dud_fire.ogg")); + player.add_component(Asset("asset/sfx/jetpack_firecracker_lp_01.ogg")); + player.add_component(Asset("asset/sfx/jetpack_firecracker_lp_02.ogg")); + player.add_component(Asset("asset/sfx/jetpack_firecracker_lp_03.ogg")); + player.add_component(Asset("asset/sfx/jetpack_firecracker_lp_04.ogg")); + player.add_component(Asset("asset/sfx/jetpack_firecracker_lp_05.ogg")); + player.add_component(Asset("asset/sfx/jetpack_firecracker_lp_06.ogg")); + player.add_component(Asset("asset/sfx/jetpack_firecracker_lp_07.ogg")); + player.add_component(Asset("asset/sfx/jetpack_firecracker_lp_08.ogg")); + GameObject player_audio = scn.new_object("player_audio", "player_audio", vec2(0, 0)); player_audio.add_component(Asset("asset/sfx/barefoot_step_left_1.ogg")); player_audio.add_component(Asset("asset/sfx/barefoot_step_right_1.ogg")); -- cgit v1.2.3 From a2d9f875a219d6d1c53784a307b4915cc4e1ee14 Mon Sep 17 00:00:00 2001 From: Max-001 Date: Mon, 6 Jan 2025 16:54:45 +0100 Subject: Set volumes --- game/StartGameScript.cpp | 2 +- game/player/PlayerSubScene.cpp | 52 +++++++++++++++++++++++++++++------------- 2 files changed, 37 insertions(+), 17 deletions(-) (limited to 'game') diff --git a/game/StartGameScript.cpp b/game/StartGameScript.cpp index 1a7e3e7..e88b329 100644 --- a/game/StartGameScript.cpp +++ b/game/StartGameScript.cpp @@ -57,7 +57,7 @@ void StartGameScript::fixed_update(crepe::duration_t dt) { AudioSource & background_music = this->get_components_by_name("background_music").front(); - //background_music.play(true); + background_music.play(true); this->took_jetpack = true; } diff --git a/game/player/PlayerSubScene.cpp b/game/player/PlayerSubScene.cpp index 41fcf51..e9e2167 100644 --- a/game/player/PlayerSubScene.cpp +++ b/game/player/PlayerSubScene.cpp @@ -157,24 +157,44 @@ PlayerSubScene::PlayerSubScene(Scene & scn) { player.add_component(Asset("asset/sfx/dud_zapper_lp.ogg")); player.add_component(Asset("asset/sfx/dud_zapper_pop.ogg")); player.add_component(Asset("asset/sfx/dud_fire.ogg")); - player.add_component(Asset("asset/sfx/jetpack_firecracker_lp_01.ogg")); - player.add_component(Asset("asset/sfx/jetpack_firecracker_lp_02.ogg")); - player.add_component(Asset("asset/sfx/jetpack_firecracker_lp_03.ogg")); - player.add_component(Asset("asset/sfx/jetpack_firecracker_lp_04.ogg")); - player.add_component(Asset("asset/sfx/jetpack_firecracker_lp_05.ogg")); - player.add_component(Asset("asset/sfx/jetpack_firecracker_lp_06.ogg")); - player.add_component(Asset("asset/sfx/jetpack_firecracker_lp_07.ogg")); - player.add_component(Asset("asset/sfx/jetpack_firecracker_lp_08.ogg")); + player.add_component(Asset("asset/sfx/jetpack_firecracker_lp_01.ogg")).volume + = 0.1; + player.add_component(Asset("asset/sfx/jetpack_firecracker_lp_02.ogg")).volume + = 0.1; + player.add_component(Asset("asset/sfx/jetpack_firecracker_lp_03.ogg")).volume + = 0.1; + player.add_component(Asset("asset/sfx/jetpack_firecracker_lp_04.ogg")).volume + = 0.1; + player.add_component(Asset("asset/sfx/jetpack_firecracker_lp_05.ogg")).volume + = 0.1; + player.add_component(Asset("asset/sfx/jetpack_firecracker_lp_06.ogg")).volume + = 0.1; + player.add_component(Asset("asset/sfx/jetpack_firecracker_lp_07.ogg")).volume + = 0.1; + player.add_component(Asset("asset/sfx/jetpack_firecracker_lp_08.ogg")).volume + = 0.1; GameObject player_audio = scn.new_object("player_audio", "player_audio", vec2(0, 0)); - player_audio.add_component(Asset("asset/sfx/barefoot_step_left_1.ogg")); - player_audio.add_component(Asset("asset/sfx/barefoot_step_right_1.ogg")); - player_audio.add_component(Asset("asset/sfx/barefoot_step_left_2.ogg")); - player_audio.add_component(Asset("asset/sfx/barefoot_step_right_2.ogg")); - player_audio.add_component(Asset("asset/sfx/barefoot_step_left_3.ogg")); - player_audio.add_component(Asset("asset/sfx/barefoot_step_right_3.ogg")); - player_audio.add_component(Asset("asset/sfx/barefoot_step_left_4.ogg")); - player_audio.add_component(Asset("asset/sfx/barefoot_step_right_4.ogg")); + player_audio.add_component(Asset("asset/sfx/barefoot_step_left_1.ogg")).volume + = 3.0; + player_audio.add_component(Asset("asset/sfx/barefoot_step_right_1.ogg")) + .volume + = 3.0; + player_audio.add_component(Asset("asset/sfx/barefoot_step_left_2.ogg")).volume + = 3.0; + player_audio.add_component(Asset("asset/sfx/barefoot_step_right_2.ogg")) + .volume + = 3.0; + player_audio.add_component(Asset("asset/sfx/barefoot_step_left_3.ogg")).volume + = 3.0; + player_audio.add_component(Asset("asset/sfx/barefoot_step_right_3.ogg")) + .volume + = 3.0; + player_audio.add_component(Asset("asset/sfx/barefoot_step_left_4.ogg")).volume + = 3.0; + player_audio.add_component(Asset("asset/sfx/barefoot_step_right_4.ogg")) + .volume + = 3.0; player_audio.add_component().set_script().active = false; -- cgit v1.2.3 From 80fc064c77bb61d9b329b58532c9cacd99038e0a Mon Sep 17 00:00:00 2001 From: Max-001 Date: Tue, 7 Jan 2025 13:21:09 +0100 Subject: First setup for NPCs --- game/CMakeLists.txt | 2 + game/Config.h | 1 + game/GameScene.cpp | 3 + game/workers/WorkerScript.cpp | 106 ++++++++++ game/workers/WorkerScript.h | 8 + game/workers/WorkersSubScene.cpp | 405 +++++++++++++++++++++++++++++++++++++++ game/workers/WorkersSubScene.h | 20 ++ 7 files changed, 545 insertions(+) create mode 100644 game/workers/WorkerScript.cpp create mode 100644 game/workers/WorkerScript.h create mode 100644 game/workers/WorkersSubScene.cpp create mode 100644 game/workers/WorkersSubScene.h (limited to 'game') diff --git a/game/CMakeLists.txt b/game/CMakeLists.txt index 0b85b96..8a63bd0 100644 --- a/game/CMakeLists.txt +++ b/game/CMakeLists.txt @@ -22,6 +22,8 @@ add_executable(main player/PlayerEndScript.cpp player/PlayerAudioScript.cpp background/StartSubScene.cpp + workers/WorkersSubScene.cpp + workers/WorkerScript.cpp main.cpp ) diff --git a/game/Config.h b/game/Config.h index d6f8349..ea95a90 100644 --- a/game/Config.h +++ b/game/Config.h @@ -6,6 +6,7 @@ static constexpr int SORT_IN_LAY_FORE_BACKGROUND = 5; // For all scenes static constexpr int SORT_IN_LAY_PARTICLES_BACKGROUND = 6; // For all scenes static constexpr int SORT_IN_LAY_OBSTACLES = 8; // Only for GameScene static constexpr int SORT_IN_LAY_PLAYER = 10; // Only for GameScene +static constexpr int SORT_IN_LAY_WORKERS = 12; // Only for GameScene static constexpr int SORT_IN_LAY_PARTICLES_FOREGROUND = 15; // Only for GameScene static constexpr int COLL_LAY_BOT_TOP = 1; // Only for GameScene diff --git a/game/GameScene.cpp b/game/GameScene.cpp index 2b6b051..02af8db 100644 --- a/game/GameScene.cpp +++ b/game/GameScene.cpp @@ -5,6 +5,7 @@ #include "background/BackgroundSubScene.h" #include "player/PlayerSubScene.h" +#include "workers/WorkersSubScene.h" #include #include @@ -41,6 +42,8 @@ void GameScene::load_scene() { PlayerSubScene player(*this); + WorkersSubScene workers(*this); + GameObject floor = new_object("floor", "game_world", vec2(0, 325)); floor.add_component(Rigidbody::Data { .body_type = Rigidbody::BodyType::STATIC, diff --git a/game/workers/WorkerScript.cpp b/game/workers/WorkerScript.cpp new file mode 100644 index 0000000..d641c6a --- /dev/null +++ b/game/workers/WorkerScript.cpp @@ -0,0 +1,106 @@ +#include "WorkerScript.h" + +#include "../Config.h" + +#include +#include +#include +#include +#include +#include + +using namespace crepe; +using namespace std; + +void WorkerScript::fixed_update(duration_t dt) { + RefVector rb_workers = this->get_components_by_tag("worker"); + RefVector trans_workers = this->get_components_by_tag("worker"); + + Rigidbody & rb_cam = this->get_components_by_name("camera").back(); + Transform & trans_cam = this->get_components_by_name("camera").back(); + + int counter = 0; + for (Rigidbody & rb_worker : rb_workers) { + Transform & trans_worker = trans_workers.at(counter); + + float result_x = rb_cam.data.linear_velocity.x - rb_worker.data.linear_velocity.x; + + if (result_x > 0) { + float left_cam_pos_x = trans_cam.position.x - VIEWPORT_X / 2; + if (trans_worker.position.x < left_cam_pos_x - 1000) { + trans_worker.position.x = left_cam_pos_x + VIEWPORT_X + 1000; + + do { + float min_value = -2500 * dt.count(); + float max_value = 2500 * dt.count(); + rb_worker.data.linear_velocity.x + = min_value + + static_cast(rand()) + / (static_cast(RAND_MAX / (max_value - min_value))); + } while (rb_worker.data.linear_velocity.x < 500 * dt.count() + && rb_worker.data.linear_velocity.x > -500 * dt.count()); + + RefVector sprite_worker + = this->get_components_by_id(trans_worker.game_object_id); + RefVector animator_worker + = this->get_components_by_id(trans_worker.game_object_id); + + if (rb_worker.data.linear_velocity.x < 0) { + sprite_worker.front().get().data.flip.flip_x = true; + sprite_worker.back().get().data.flip.flip_x = true; + + animator_worker.front().get().data.fps + = -rb_worker.data.linear_velocity.x / 5; + animator_worker.back().get().data.fps + = -rb_worker.data.linear_velocity.x / 5; + } else { + sprite_worker.front().get().data.flip.flip_x = false; + sprite_worker.back().get().data.flip.flip_x = false; + + animator_worker.front().get().data.fps + = rb_worker.data.linear_velocity.x / 5; + animator_worker.back().get().data.fps + = rb_worker.data.linear_velocity.x / 5; + } + } + } else { + float right_cam_pos_x = trans_cam.position.x + VIEWPORT_X / 2; + if (trans_worker.position.x > right_cam_pos_x + 1000) { + do { + float min_value = -2500 * dt.count(); + float max_value = 2500 * dt.count(); + rb_worker.data.linear_velocity.x + = min_value + + static_cast(rand()) + / (static_cast(RAND_MAX / (max_value - min_value))); + } while (rb_worker.data.linear_velocity.x < 500 * dt.count() + && rb_worker.data.linear_velocity.x > -500 * dt.count()); + + RefVector sprite_worker + = this->get_components_by_id(trans_worker.game_object_id); + RefVector animator_worker + = this->get_components_by_id(trans_worker.game_object_id); + + if (rb_worker.data.linear_velocity.x < 0) { + sprite_worker.front().get().data.flip.flip_x = true; + sprite_worker.back().get().data.flip.flip_x = true; + + animator_worker.front().get().data.fps + = -rb_worker.data.linear_velocity.x / 5; + animator_worker.back().get().data.fps + = -rb_worker.data.linear_velocity.x / 5; + } else { + sprite_worker.front().get().data.flip.flip_x = false; + sprite_worker.back().get().data.flip.flip_x = false; + + animator_worker.front().get().data.fps + = rb_worker.data.linear_velocity.x / 5; + animator_worker.back().get().data.fps + = rb_worker.data.linear_velocity.x / 5; + } + } + } + + counter++; + } +} diff --git a/game/workers/WorkerScript.h b/game/workers/WorkerScript.h new file mode 100644 index 0000000..ae4a6c7 --- /dev/null +++ b/game/workers/WorkerScript.h @@ -0,0 +1,8 @@ +#pragma once + +#include + +class WorkerScript : public crepe::Script { +public: + void fixed_update(crepe::duration_t dt); +}; diff --git a/game/workers/WorkersSubScene.cpp b/game/workers/WorkersSubScene.cpp new file mode 100644 index 0000000..94d9848 --- /dev/null +++ b/game/workers/WorkersSubScene.cpp @@ -0,0 +1,405 @@ +#include "WorkersSubScene.h" +#include "WorkerScript.h" + +#include "../Config.h" +#include "api/GameObject.h" + +#include +#include +#include +#include +#include +#include + +using namespace crepe; +using namespace std; + +WorkersSubScene::WorkersSubScene(Scene & scn) { + this->worker1(scn, 1200, -50); + this->worker2(scn, 1300, 20); + this->worker3(scn, 1400, -40); + this->worker4(scn, 7250, 50); + this->worker5(scn, 3400, -20); + this->worker6(scn, 2000, 30); + this->worker7(scn, 3725, 35); + this->worker8(scn, 2200, -15); + + GameObject script = scn.new_object("workers_script"); + script.add_component().set_script(); +} + +void WorkersSubScene::worker1(crepe::Scene & scn, float start_x, float init_speed) { + GameObject worker_1 = scn.new_object("worker_1", "worker", vec2(start_x, 200)); + Sprite & worker_1_body_sprite = worker_1.add_component( + Asset {"asset/workers/worker1Body.png"}, + Sprite::Data { + .sorting_in_layer = SORT_IN_LAY_WORKERS, + .order_in_layer = 0, + .size = vec2(0, 50), + } + ); + worker_1.add_component( + worker_1_body_sprite, ivec2(32, 32), uvec2(4, 8), + Animator::Data { + .fps = static_cast(abs(init_speed) / 5), + .looping = true, + } + ); + Sprite & worker_1_head_sprite = worker_1.add_component( + Asset {"asset/workers/worker1Head.png"}, + Sprite::Data { + .sorting_in_layer = SORT_IN_LAY_WORKERS, + .order_in_layer = 1, + .size = vec2(0, 50), + .position_offset = vec2(0, -20), + } + ); + worker_1.add_component( + worker_1_head_sprite, ivec2(32, 32), uvec2(4, 8), + Animator::Data { + .fps = static_cast(abs(init_speed) / 5), + .looping = true, + } + ); + worker_1.add_component(vec2(50, 50)); + worker_1.add_component(Rigidbody::Data { + .gravity_scale = 20, + .body_type = Rigidbody::BodyType::DYNAMIC, + .linear_velocity = vec2(init_speed, 0), + .collision_layers = {COLL_LAY_BOT_TOP}, + }); + + if (init_speed < 0) { + worker_1_body_sprite.data.flip = Sprite::FlipSettings {true, false}; + worker_1_head_sprite.data.flip = Sprite::FlipSettings {true, false}; + } +} + +void WorkersSubScene::worker2(crepe::Scene & scn, float start_x, float init_speed) { + GameObject worker_2 = scn.new_object("worker_2", "worker", vec2(start_x, 200)); + Sprite & worker_2_body_sprite = worker_2.add_component( + Asset {"asset/workers/worker2Body.png"}, + Sprite::Data { + .sorting_in_layer = SORT_IN_LAY_WORKERS, + .order_in_layer = 2, + .size = vec2(0, 50), + } + ); + worker_2.add_component( + worker_2_body_sprite, ivec2(32, 32), uvec2(4, 8), + Animator::Data { + .fps = static_cast(abs(init_speed) / 5), + .looping = true, + } + ); + Sprite & worker_2_head_sprite = worker_2.add_component( + Asset {"asset/workers/worker1Head.png"}, + Sprite::Data { + .sorting_in_layer = SORT_IN_LAY_WORKERS, + .order_in_layer = 3, + .size = vec2(0, 50), + .position_offset = vec2(0, -20), + } + ); + worker_2.add_component( + worker_2_head_sprite, ivec2(32, 32), uvec2(4, 8), + Animator::Data { + .fps = static_cast(abs(init_speed) / 5), + .looping = true, + } + ); + worker_2.add_component(vec2(50, 50)); + worker_2.add_component(Rigidbody::Data { + .gravity_scale = 20, + .body_type = Rigidbody::BodyType::DYNAMIC, + .linear_velocity = vec2(init_speed, 0), + .collision_layers = {COLL_LAY_BOT_TOP}, + }); + + if (init_speed < 0) { + worker_2_body_sprite.data.flip = Sprite::FlipSettings {true, false}; + worker_2_head_sprite.data.flip = Sprite::FlipSettings {true, false}; + } +} + +void WorkersSubScene::worker3(crepe::Scene & scn, float start_x, float init_speed) { + GameObject worker_3 = scn.new_object("worker_3", "worker", vec2(start_x, 200)); + Sprite & worker_3_body_sprite = worker_3.add_component( + Asset {"asset/workers/worker1Body.png"}, + Sprite::Data { + .sorting_in_layer = SORT_IN_LAY_WORKERS, + .order_in_layer = 4, + .size = vec2(0, 50), + } + ); + worker_3.add_component( + worker_3_body_sprite, ivec2(32, 32), uvec2(4, 8), + Animator::Data { + .fps = static_cast(abs(init_speed) / 5), + .looping = true, + } + ); + Sprite & worker_3_head_sprite = worker_3.add_component( + Asset {"asset/workers/worker2Head.png"}, + Sprite::Data { + .sorting_in_layer = SORT_IN_LAY_WORKERS, + .order_in_layer = 5, + .size = vec2(0, 50), + .position_offset = vec2(0, -20), + } + ); + worker_3.add_component( + worker_3_head_sprite, ivec2(32, 32), uvec2(4, 8), + Animator::Data { + .fps = static_cast(abs(init_speed) / 5), + .looping = true, + } + ); + worker_3.add_component(vec2(50, 50)); + worker_3.add_component(Rigidbody::Data { + .gravity_scale = 20, + .body_type = Rigidbody::BodyType::DYNAMIC, + .linear_velocity = vec2(init_speed, 0), + .collision_layers = {COLL_LAY_BOT_TOP}, + }); + + if (init_speed < 0) { + worker_3_body_sprite.data.flip = Sprite::FlipSettings {true, false}; + worker_3_head_sprite.data.flip = Sprite::FlipSettings {true, false}; + } +} + +void WorkersSubScene::worker4(crepe::Scene & scn, float start_x, float init_speed) { + GameObject worker_4 = scn.new_object("worker_4", "worker", vec2(start_x, 200)); + Sprite & worker_4_body_sprite = worker_4.add_component( + Asset {"asset/workers/worker2Body.png"}, + Sprite::Data { + .sorting_in_layer = SORT_IN_LAY_WORKERS, + .order_in_layer = 6, + .size = vec2(0, 50), + } + ); + worker_4.add_component( + worker_4_body_sprite, ivec2(32, 32), uvec2(4, 8), + Animator::Data { + .fps = static_cast(abs(init_speed) / 5), + .looping = true, + } + ); + Sprite & worker_4_head_sprite = worker_4.add_component( + Asset {"asset/workers/worker2Head.png"}, + Sprite::Data { + .sorting_in_layer = SORT_IN_LAY_WORKERS, + .order_in_layer = 7, + .size = vec2(0, 50), + .position_offset = vec2(0, -20), + } + ); + worker_4.add_component( + worker_4_head_sprite, ivec2(32, 32), uvec2(4, 8), + Animator::Data { + .fps = static_cast(abs(init_speed) / 5), + .looping = true, + } + ); + worker_4.add_component(vec2(50, 50)); + worker_4.add_component(Rigidbody::Data { + .gravity_scale = 20, + .body_type = Rigidbody::BodyType::DYNAMIC, + .linear_velocity = vec2(init_speed, 0), + .collision_layers = {COLL_LAY_BOT_HIGH}, + }); + + if (init_speed < 0) { + worker_4_body_sprite.data.flip = Sprite::FlipSettings {true, false}; + worker_4_head_sprite.data.flip = Sprite::FlipSettings {true, false}; + } +} + +void WorkersSubScene::worker5(crepe::Scene & scn, float start_x, float init_speed) { + GameObject worker_5 = scn.new_object("worker_5", "worker", vec2(start_x, 200)); + Sprite & worker_5_body_sprite = worker_5.add_component( + Asset {"asset/workers/workerFatBody.png"}, + Sprite::Data { + .sorting_in_layer = SORT_IN_LAY_WORKERS, + .order_in_layer = 8, + .size = vec2(0, 50), + } + ); + worker_5.add_component( + worker_5_body_sprite, ivec2(32, 32), uvec2(4, 8), + Animator::Data { + .fps = static_cast(abs(init_speed) / 5), + .looping = true, + } + ); + Sprite & worker_5_head_sprite = worker_5.add_component( + Asset {"asset/workers/worker1Head.png"}, + Sprite::Data { + .sorting_in_layer = SORT_IN_LAY_WORKERS, + .order_in_layer = 9, + .size = vec2(0, 50), + .position_offset = vec2(0, -20), + } + ); + worker_5.add_component( + worker_5_head_sprite, ivec2(32, 32), uvec2(4, 8), + Animator::Data { + .fps = static_cast(abs(init_speed) / 5), + .looping = true, + } + ); + worker_5.add_component(vec2(50, 50)); + worker_5.add_component(Rigidbody::Data { + .gravity_scale = 20, + .body_type = Rigidbody::BodyType::DYNAMIC, + .linear_velocity = vec2(init_speed, 0), + .collision_layers = {COLL_LAY_BOT_HIGH}, + }); + + if (init_speed < 0) { + worker_5_body_sprite.data.flip = Sprite::FlipSettings {true, false}; + worker_5_head_sprite.data.flip = Sprite::FlipSettings {true, false}; + } +} + +void WorkersSubScene::worker6(crepe::Scene & scn, float start_x, float init_speed) { + GameObject worker_6 = scn.new_object("worker_6", "worker", vec2(start_x, 200)); + Sprite & worker_6_body_sprite = worker_6.add_component( + Asset {"asset/workers/workerFatBody.png"}, + Sprite::Data { + .sorting_in_layer = SORT_IN_LAY_WORKERS, + .order_in_layer = 10, + .size = vec2(0, 50), + } + ); + worker_6.add_component( + worker_6_body_sprite, ivec2(32, 32), uvec2(4, 8), + Animator::Data { + .fps = static_cast(abs(init_speed) / 5), + .looping = true, + } + ); + Sprite & worker_6_head_sprite = worker_6.add_component( + Asset {"asset/workers/worker2Head.png"}, + Sprite::Data { + .sorting_in_layer = SORT_IN_LAY_WORKERS, + .order_in_layer = 11, + .size = vec2(0, 50), + .position_offset = vec2(0, -20), + } + ); + worker_6.add_component( + worker_6_head_sprite, ivec2(32, 32), uvec2(4, 8), + Animator::Data { + .fps = static_cast(abs(init_speed) / 5), + .looping = true, + } + ); + worker_6.add_component(vec2(50, 50)); + worker_6.add_component(Rigidbody::Data { + .gravity_scale = 20, + .body_type = Rigidbody::BodyType::DYNAMIC, + .linear_velocity = vec2(init_speed, 0), + .collision_layers = {COLL_LAY_BOT_LOW}, + }); + + if (init_speed < 0) { + worker_6_body_sprite.data.flip = Sprite::FlipSettings {true, false}; + worker_6_head_sprite.data.flip = Sprite::FlipSettings {true, false}; + } +} + +void WorkersSubScene::worker7(crepe::Scene & scn, float start_x, float init_speed) { + GameObject worker_7 = scn.new_object("worker_7", "worker", vec2(start_x, 200)); + Sprite & worker_7_body_sprite = worker_7.add_component( + Asset {"asset/workers/workerTallBody.png"}, + Sprite::Data { + .sorting_in_layer = SORT_IN_LAY_WORKERS, + .order_in_layer = 12, + .size = vec2(0, 50), + } + ); + worker_7.add_component( + worker_7_body_sprite, ivec2(32, 32), uvec2(4, 8), + Animator::Data { + .fps = static_cast(abs(init_speed) / 5), + .looping = true, + } + ); + Sprite & worker_7_head_sprite = worker_7.add_component( + Asset {"asset/workers/worker1Head.png"}, + Sprite::Data { + .sorting_in_layer = SORT_IN_LAY_WORKERS, + .order_in_layer = 13, + .size = vec2(0, 50), + .position_offset = vec2(0, -20), + } + ); + worker_7.add_component( + worker_7_head_sprite, ivec2(32, 32), uvec2(4, 8), + Animator::Data { + .fps = static_cast(abs(init_speed) / 5), + .looping = true, + } + ); + worker_7.add_component(vec2(50, 50)); + worker_7.add_component(Rigidbody::Data { + .gravity_scale = 20, + .body_type = Rigidbody::BodyType::DYNAMIC, + .linear_velocity = vec2(init_speed, 0), + .collision_layers = {COLL_LAY_BOT_LOW}, + }); + + if (init_speed < 0) { + worker_7_body_sprite.data.flip = Sprite::FlipSettings {true, false}; + worker_7_head_sprite.data.flip = Sprite::FlipSettings {true, false}; + } +} + +void WorkersSubScene::worker8(crepe::Scene & scn, float start_x, float init_speed) { + GameObject worker_8 = scn.new_object("worker_8", "worker", vec2(start_x, 200)); + Sprite & worker_8_body_sprite = worker_8.add_component( + Asset {"asset/workers/workerTallBody.png"}, + Sprite::Data { + .sorting_in_layer = SORT_IN_LAY_WORKERS, + .order_in_layer = 14, + .size = vec2(0, 50), + } + ); + worker_8.add_component( + worker_8_body_sprite, ivec2(32, 32), uvec2(4, 8), + Animator::Data { + .fps = static_cast(abs(init_speed) / 5), + .looping = true, + } + ); + Sprite & worker_8_head_sprite = worker_8.add_component( + Asset {"asset/workers/worker2Head.png"}, + Sprite::Data { + .sorting_in_layer = SORT_IN_LAY_WORKERS, + .order_in_layer = 15, + .size = vec2(0, 50), + .position_offset = vec2(0, -20), + } + ); + worker_8.add_component( + worker_8_head_sprite, ivec2(32, 32), uvec2(4, 8), + Animator::Data { + .fps = static_cast(abs(init_speed) / 5), + .looping = true, + } + ); + worker_8.add_component(vec2(50, 50)); + worker_8.add_component(Rigidbody::Data { + .gravity_scale = 20, + .body_type = Rigidbody::BodyType::DYNAMIC, + .linear_velocity = vec2(init_speed, 0), + .collision_layers = {COLL_LAY_BOT_LOW}, + }); + + if (init_speed < 0) { + worker_8_body_sprite.data.flip = Sprite::FlipSettings {true, false}; + worker_8_head_sprite.data.flip = Sprite::FlipSettings {true, false}; + } +} diff --git a/game/workers/WorkersSubScene.h b/game/workers/WorkersSubScene.h new file mode 100644 index 0000000..6692d87 --- /dev/null +++ b/game/workers/WorkersSubScene.h @@ -0,0 +1,20 @@ +#pragma once + +namespace crepe { +class Scene; +} + +class WorkersSubScene { +public: + WorkersSubScene(crepe::Scene & scn); + +private: + void worker1(crepe::Scene & scn, float start_x, float init_speed); + void worker2(crepe::Scene & scn, float start_x, float init_speed); + void worker3(crepe::Scene & scn, float start_x, float init_speed); + void worker4(crepe::Scene & scn, float start_x, float init_speed); + void worker5(crepe::Scene & scn, float start_x, float init_speed); + void worker6(crepe::Scene & scn, float start_x, float init_speed); + void worker7(crepe::Scene & scn, float start_x, float init_speed); + void worker8(crepe::Scene & scn, float start_x, float init_speed); +}; -- cgit v1.2.3 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 (limited to 'game') 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 From f31bd86ae5d7df21b788a273d4f2e530136ec184 Mon Sep 17 00:00:00 2001 From: Max-001 Date: Tue, 7 Jan 2025 14:42:11 +0100 Subject: Added Script suffix --- game/CMakeLists.txt | 2 +- game/workers/PanicFromPlayer.cpp | 45 ---------------------------------- game/workers/PanicFromPlayer.h | 8 ------ game/workers/PanicFromPlayerScript.cpp | 45 ++++++++++++++++++++++++++++++++++ game/workers/PanicFromPlayerScript.h | 8 ++++++ game/workers/WorkersSubScene.cpp | 18 +++++++------- 6 files changed, 63 insertions(+), 63 deletions(-) delete mode 100644 game/workers/PanicFromPlayer.cpp delete mode 100644 game/workers/PanicFromPlayer.h create mode 100644 game/workers/PanicFromPlayerScript.cpp create mode 100644 game/workers/PanicFromPlayerScript.h (limited to 'game') diff --git a/game/CMakeLists.txt b/game/CMakeLists.txt index dc6a3fd..0fb2424 100644 --- a/game/CMakeLists.txt +++ b/game/CMakeLists.txt @@ -24,7 +24,7 @@ add_executable(main background/StartSubScene.cpp workers/WorkersSubScene.cpp workers/WorkerScript.cpp - workers/PanicFromPlayer.cpp + workers/PanicFromPlayerScript.cpp main.cpp ) diff --git a/game/workers/PanicFromPlayer.cpp b/game/workers/PanicFromPlayer.cpp deleted file mode 100644 index fbf4611..0000000 --- a/game/workers/PanicFromPlayer.cpp +++ /dev/null @@ -1,45 +0,0 @@ -#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 deleted file mode 100644 index 804df5c..0000000 --- a/game/workers/PanicFromPlayer.h +++ /dev/null @@ -1,8 +0,0 @@ -#pragma once - -#include - -class PanicFromPlayer : public crepe::Script { -public: - void fixed_update(crepe::duration_t dt); -}; diff --git a/game/workers/PanicFromPlayerScript.cpp b/game/workers/PanicFromPlayerScript.cpp new file mode 100644 index 0000000..1e49aaa --- /dev/null +++ b/game/workers/PanicFromPlayerScript.cpp @@ -0,0 +1,45 @@ +#include "PanicFromPlayerScript.h" + +#include +#include +#include +#include +#include + +using namespace crepe; +using namespace std; + +void PanicFromPlayerScript::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/PanicFromPlayerScript.h b/game/workers/PanicFromPlayerScript.h new file mode 100644 index 0000000..d173e89 --- /dev/null +++ b/game/workers/PanicFromPlayerScript.h @@ -0,0 +1,8 @@ +#pragma once + +#include + +class PanicFromPlayerScript : public crepe::Script { +public: + void fixed_update(crepe::duration_t dt); +}; diff --git a/game/workers/WorkersSubScene.cpp b/game/workers/WorkersSubScene.cpp index 5b070f8..e5b4678 100644 --- a/game/workers/WorkersSubScene.cpp +++ b/game/workers/WorkersSubScene.cpp @@ -1,5 +1,5 @@ #include "WorkersSubScene.h" -#include "PanicFromPlayer.h" +#include "PanicFromPlayerScript.h" #include "WorkerScript.h" #include "../Config.h" @@ -69,7 +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(); + worker_1.add_component().set_script(); if (init_speed < 0) { worker_1_body_sprite.data.flip = Sprite::FlipSettings {true, false}; @@ -117,7 +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(); + worker_2.add_component().set_script(); if (init_speed < 0) { worker_2_body_sprite.data.flip = Sprite::FlipSettings {true, false}; @@ -165,7 +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(); + worker_3.add_component().set_script(); if (init_speed < 0) { worker_3_body_sprite.data.flip = Sprite::FlipSettings {true, false}; @@ -213,7 +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(); + worker_4.add_component().set_script(); if (init_speed < 0) { worker_4_body_sprite.data.flip = Sprite::FlipSettings {true, false}; @@ -261,7 +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(); + worker_5.add_component().set_script(); if (init_speed < 0) { worker_5_body_sprite.data.flip = Sprite::FlipSettings {true, false}; @@ -309,7 +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(); + worker_6.add_component().set_script(); if (init_speed < 0) { worker_6_body_sprite.data.flip = Sprite::FlipSettings {true, false}; @@ -357,7 +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(); + worker_7.add_component().set_script(); if (init_speed < 0) { worker_7_body_sprite.data.flip = Sprite::FlipSettings {true, false}; @@ -405,7 +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(); + worker_8.add_component().set_script(); if (init_speed < 0) { worker_8_body_sprite.data.flip = Sprite::FlipSettings {true, false}; -- cgit v1.2.3