From d15de7503f2f7320e23863a11e97fcd401bc2eeb Mon Sep 17 00:00:00 2001 From: Max-001 Date: Tue, 24 Dec 2024 09:45:44 +0100 Subject: Moved Player files to seperate folder --- game/GameScene.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'game/GameScene.cpp') diff --git a/game/GameScene.cpp b/game/GameScene.cpp index 2511567..ee9b7dd 100644 --- a/game/GameScene.cpp +++ b/game/GameScene.cpp @@ -1,10 +1,10 @@ #include "GameScene.h" #include "Config.h" #include "MoveCameraManualyScript.h" -#include "PlayerSubScene.h" #include "StartGameScript.h" #include "background/BackgroundSubScene.h" +#include "player/PlayerSubScene.h" #include #include -- cgit v1.2.3 From 192aea3112af0b36f6f26670e66c7e24926c579a Mon Sep 17 00:00:00 2001 From: Max-001 Date: Tue, 24 Dec 2024 12:10:28 +0100 Subject: Added collision animations --- game/Config.h | 3 +++ game/GameScene.cpp | 50 +++++++++++++++++++++++++++++++++++++++ game/player/PlayerScript.cpp | 53 ++++++++++++++++++++++++++++++++++++++++++ game/player/PlayerScript.h | 5 ++++ game/player/PlayerSubScene.cpp | 3 ++- 5 files changed, 113 insertions(+), 1 deletion(-) (limited to 'game/GameScene.cpp') diff --git a/game/Config.h b/game/Config.h index ec753df..796ad6c 100644 --- a/game/Config.h +++ b/game/Config.h @@ -13,6 +13,9 @@ static constexpr int COLL_LAY_BOT_LOW = 2; // Only for GameScene static constexpr int COLL_LAY_BOT_HIGH = 3; // Only for GameScene static constexpr int COLL_LAY_PLAYER = 4; // Only for GameScene static constexpr int COLL_LAY_WALL_FRAGS = 5; // Only for GameScene +static constexpr int COLL_LAY_ZAPPER = 6; // Only for GameScene +static constexpr int COLL_LAY_LASER = 7; // Only for GameScene +static constexpr int COLL_LAY_MISSILE = 8; // Only for GameScene static constexpr int GAME_HEIGHT = 800; // In game units diff --git a/game/GameScene.cpp b/game/GameScene.cpp index ee9b7dd..821191d 100644 --- a/game/GameScene.cpp +++ b/game/GameScene.cpp @@ -66,6 +66,56 @@ void GameScene::load_scene() { GameObject start_game_script = new_object("start_game_script", "script", vec2(0, 0)); start_game_script.add_component().set_script(); + + // 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"}; + Sprite & zapper_sprite = zapper.add_component( + zapper_asset, + Sprite::Data { + .sorting_in_layer = SORT_IN_LAY_OBSTACLES, + .order_in_layer = 0, + .size = vec2(100, 100), + } + ); + zapper.add_component(Rigidbody::Data { + .body_type = Rigidbody::BodyType::KINEMATIC, + .kinematic_collision = false, + .collision_layer = COLL_LAY_ZAPPER, + }); + zapper.add_component(vec2(100, 100)); + GameObject laser = new_object("laser", "laser", vec2(2000, 0)); + Asset laser_asset {"asset/obstacles/laser/laserPower.png"}; + Sprite & laser_sprite = laser.add_component( + laser_asset, + Sprite::Data { + .sorting_in_layer = SORT_IN_LAY_OBSTACLES, + .order_in_layer = 0, + .size = vec2(100, 100), + } + ); + laser.add_component(Rigidbody::Data { + .body_type = Rigidbody::BodyType::KINEMATIC, + .kinematic_collision = false, + .collision_layer = COLL_LAY_LASER, + }); + laser.add_component(vec2(100, 100)); + GameObject missile = new_object("missile", "missile", vec2(3000, 0)); + Asset missile_asset {"asset/obstacles/missile/missile.png"}; + Sprite & missile_sprite = missile.add_component( + missile_asset, + Sprite::Data { + .sorting_in_layer = SORT_IN_LAY_OBSTACLES, + .order_in_layer = 0, + .size = vec2(100, 100), + } + ); + missile.add_component(Rigidbody::Data { + .body_type = Rigidbody::BodyType::KINEMATIC, + .kinematic_collision = false, + .collision_layer = COLL_LAY_MISSILE, + }); + missile.add_component(vec2(100, 100)); } string GameScene::get_name() const { return "scene1"; } diff --git a/game/player/PlayerScript.cpp b/game/player/PlayerScript.cpp index 04e7264..1a5d497 100644 --- a/game/player/PlayerScript.cpp +++ b/game/player/PlayerScript.cpp @@ -9,6 +9,59 @@ using namespace crepe; using namespace std; +void PlayerScript::init() { + subscribe([this](const CollisionEvent & ev) -> bool { + return this->on_collision(ev); + }); +} + +bool PlayerScript::on_collision(const CollisionEvent & ev) { + BehaviorScript & play_scr = this->get_components_by_name("player").front(); + RefVector animators = this->get_components_by_name("player"); + RefVector emitters + = this->get_components_by_name("player"); + + if (ev.info.other.metadata.tag == "zapper") { + for (Animator & anim : animators) { + anim.active = true; + anim.set_anim(4); + anim.data.looping = true; + prev_anim = 0; + } + for (ParticleEmitter & emitter : emitters) { + emitter.data.emission_rate = 0; + } + play_scr.active = false; + return true; + } else if (ev.info.other.metadata.tag == "laser") { + for (Animator & anim : animators) { + anim.active = true; + anim.set_anim(4); + anim.data.looping = true; + prev_anim = 0; + } + for (ParticleEmitter & emitter : emitters) { + emitter.data.emission_rate = 0; + } + play_scr.active = false; + return true; + } else if (ev.info.other.metadata.tag == "missile") { + for (Animator & anim : animators) { + anim.active = true; + anim.set_anim(5); + anim.data.looping = true; + prev_anim = 0; + } + for (ParticleEmitter & emitter : emitters) { + emitter.data.emission_rate = 0; + } + play_scr.active = false; + return true; + } + + return false; +} + void PlayerScript::fixed_update(crepe::duration_t dt) { RefVector animators = this->get_components_by_name("player"); RefVector emitters diff --git a/game/player/PlayerScript.h b/game/player/PlayerScript.h index dfd02eb..d8eb098 100644 --- a/game/player/PlayerScript.h +++ b/game/player/PlayerScript.h @@ -1,11 +1,16 @@ #pragma once +#include #include class PlayerScript : public crepe::Script { public: + void init(); void fixed_update(crepe::duration_t dt); +private: + bool on_collision(const crepe::CollisionEvent & ev); + private: int prev_anim = 0; }; diff --git a/game/player/PlayerSubScene.cpp b/game/player/PlayerSubScene.cpp index ccf3d42..812d99a 100644 --- a/game/player/PlayerSubScene.cpp +++ b/game/player/PlayerSubScene.cpp @@ -143,7 +143,8 @@ PlayerSubScene::PlayerSubScene(Scene & scn) { .gravity_scale = 20, .body_type = Rigidbody::BodyType::DYNAMIC, .linear_velocity = vec2(100, 0), - .collision_layers = {COLL_LAY_BOT_TOP}, + .collision_layers + = {COLL_LAY_BOT_TOP, COLL_LAY_ZAPPER, COLL_LAY_LASER, COLL_LAY_MISSILE}, .collision_layer = COLL_LAY_PLAYER, }); player.add_component().set_script().active = false; -- cgit v1.2.3 From a0a3e7e2d12d3acb7bf4073cb5a10c08047cd08e Mon Sep 17 00:00:00 2001 From: Max-001 Date: Tue, 24 Dec 2024 12:14:51 +0100 Subject: Added EndGameScript --- game/CMakeLists.txt | 1 + game/EndGameScript.cpp | 6 ++++++ game/EndGameScript.h | 8 ++++++++ game/GameScene.cpp | 3 +++ game/player/PlayerScript.cpp | 6 ++++++ 5 files changed, 24 insertions(+) create mode 100644 game/EndGameScript.cpp create mode 100644 game/EndGameScript.h (limited to 'game/GameScene.cpp') diff --git a/game/CMakeLists.txt b/game/CMakeLists.txt index c3a2d52..33ceaf4 100644 --- a/game/CMakeLists.txt +++ b/game/CMakeLists.txt @@ -19,6 +19,7 @@ add_executable(main player/PlayerScript.cpp player/PlayerSubScene.cpp StartGameScript.cpp + EndGameScript.cpp background/StartSubScene.cpp main.cpp ) diff --git a/game/EndGameScript.cpp b/game/EndGameScript.cpp new file mode 100644 index 0000000..4d7b5fd --- /dev/null +++ b/game/EndGameScript.cpp @@ -0,0 +1,6 @@ +#include "EndGameScript.h" + +void EndGameScript::fixed_update(crepe::duration_t dt) { + logf("EndGameScript::fixed_update"); + // ... +} diff --git a/game/EndGameScript.h b/game/EndGameScript.h new file mode 100644 index 0000000..b63ac9d --- /dev/null +++ b/game/EndGameScript.h @@ -0,0 +1,8 @@ +#pragma once + +#include + +class EndGameScript : public crepe::Script { +public: + void fixed_update(crepe::duration_t dt); +}; diff --git a/game/GameScene.cpp b/game/GameScene.cpp index 821191d..b471484 100644 --- a/game/GameScene.cpp +++ b/game/GameScene.cpp @@ -1,5 +1,6 @@ #include "GameScene.h" #include "Config.h" +#include "EndGameScript.h" #include "MoveCameraManualyScript.h" #include "StartGameScript.h" @@ -66,6 +67,8 @@ 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 end_game_script = new_object("end_game_script", "script", vec2(0, 0)); + end_game_script.add_component().set_script().active = false; // zapper, laser and missile (below) for testing purpose only!!! GameObject zapper = new_object("zapper", "zapper", vec2(1000, 0)); diff --git a/game/player/PlayerScript.cpp b/game/player/PlayerScript.cpp index 1a5d497..de53fc7 100644 --- a/game/player/PlayerScript.cpp +++ b/game/player/PlayerScript.cpp @@ -1,4 +1,5 @@ #include "PlayerScript.h" +#include "api/BehaviorScript.h" #include #include @@ -17,6 +18,8 @@ void PlayerScript::init() { bool PlayerScript::on_collision(const CollisionEvent & ev) { BehaviorScript & play_scr = this->get_components_by_name("player").front(); + BehaviorScript & end_scr + = this->get_components_by_name("end_game_script").front(); RefVector animators = this->get_components_by_name("player"); RefVector emitters = this->get_components_by_name("player"); @@ -32,6 +35,7 @@ bool PlayerScript::on_collision(const CollisionEvent & ev) { emitter.data.emission_rate = 0; } play_scr.active = false; + end_scr.active = true; return true; } else if (ev.info.other.metadata.tag == "laser") { for (Animator & anim : animators) { @@ -44,6 +48,7 @@ bool PlayerScript::on_collision(const CollisionEvent & ev) { emitter.data.emission_rate = 0; } play_scr.active = false; + end_scr.active = true; return true; } else if (ev.info.other.metadata.tag == "missile") { for (Animator & anim : animators) { @@ -56,6 +61,7 @@ bool PlayerScript::on_collision(const CollisionEvent & ev) { emitter.data.emission_rate = 0; } play_scr.active = false; + end_scr.active = true; return true; } -- cgit v1.2.3 From abbfe5d06697b93de485dcfe975812befc018a00 Mon Sep 17 00:00:00 2001 From: Max-001 Date: Tue, 24 Dec 2024 16:31:58 +0100 Subject: Moved EndGameScript to Player --- game/CMakeLists.txt | 2 +- game/EndGameScript.cpp | 60 ----------------------------------------- game/EndGameScript.h | 12 --------- game/GameScene.cpp | 3 --- game/player/PlayerEndScript.cpp | 60 +++++++++++++++++++++++++++++++++++++++++ game/player/PlayerEndScript.h | 12 +++++++++ game/player/PlayerScript.cpp | 3 +-- game/player/PlayerSubScene.cpp | 2 ++ 8 files changed, 76 insertions(+), 78 deletions(-) delete mode 100644 game/EndGameScript.cpp delete mode 100644 game/EndGameScript.h create mode 100644 game/player/PlayerEndScript.cpp create mode 100644 game/player/PlayerEndScript.h (limited to 'game/GameScene.cpp') diff --git a/game/CMakeLists.txt b/game/CMakeLists.txt index 33ceaf4..8e3692b 100644 --- a/game/CMakeLists.txt +++ b/game/CMakeLists.txt @@ -19,7 +19,7 @@ add_executable(main player/PlayerScript.cpp player/PlayerSubScene.cpp StartGameScript.cpp - EndGameScript.cpp + player/PlayerEndScript.cpp background/StartSubScene.cpp main.cpp ) diff --git a/game/EndGameScript.cpp b/game/EndGameScript.cpp deleted file mode 100644 index a18f18c..0000000 --- a/game/EndGameScript.cpp +++ /dev/null @@ -1,60 +0,0 @@ -#include "EndGameScript.h" - -#include -#include -#include -#include -#include - -using namespace crepe; -using namespace std; - -void EndGameScript::init() { - BoxCollider jetpack_coll = this->get_components_by_name("player").back(); - CircleCollider head_coll = this->get_components_by_name("player").back(); - jetpack_coll.active = false; - head_coll.active = false; -} - -void EndGameScript::fixed_update(crepe::duration_t dt) { - Transform & transform_player = this->get_components_by_name("player").front(); - RefVector anim_player = this->get_components_by_name("player"); - Rigidbody & rb_player = this->get_components_by_name("player").front(); - Rigidbody & rb_camera = this->get_components_by_name("camera").front(); - - if (transform_player.position.y >= 194) { - if (jump == 0 || jump == 1) { - int random_number = rand() % 4; - for (Animator & anim : anim_player) { - anim.active = false; - anim.set_anim(6); - for (int i = 0; i < random_number; i++) { - anim.next_anim(); - } - } - } else if (jump == 2) { - for (Animator & anim : anim_player) { - anim.active = false; - anim.set_anim(7); - } - rb_player.data.angular_velocity = 0; - transform_player.rotation = 90; - } - if (jump == 0) { - rb_player.data.linear_velocity = vec2(100, -150); - rb_player.data.angular_velocity = 320; - rb_player.data.angular_velocity_coefficient = 0.8; - jump++; - } else if (jump == 1) { - rb_player.data.linear_velocity = vec2(100, -125); - rb_player.data.angular_velocity = 300; - rb_player.data.angular_velocity_coefficient = 0.8; - jump++; - } else if (jump == 2) { - rb_player.data.linear_velocity = vec2(100, 0); - rb_player.data.linear_velocity_coefficient = vec2(0.5, 0.5); - rb_camera.data.linear_velocity_coefficient = vec2(0.5, 0.5); - jump++; - } - } -} diff --git a/game/EndGameScript.h b/game/EndGameScript.h deleted file mode 100644 index 980dff5..0000000 --- a/game/EndGameScript.h +++ /dev/null @@ -1,12 +0,0 @@ -#pragma once - -#include - -class EndGameScript : public crepe::Script { -public: - void init(); - void fixed_update(crepe::duration_t dt); - -private: - int jump = 0; -}; diff --git a/game/GameScene.cpp b/game/GameScene.cpp index b471484..821191d 100644 --- a/game/GameScene.cpp +++ b/game/GameScene.cpp @@ -1,6 +1,5 @@ #include "GameScene.h" #include "Config.h" -#include "EndGameScript.h" #include "MoveCameraManualyScript.h" #include "StartGameScript.h" @@ -67,8 +66,6 @@ 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 end_game_script = new_object("end_game_script", "script", vec2(0, 0)); - end_game_script.add_component().set_script().active = false; // zapper, laser and missile (below) for testing purpose only!!! GameObject zapper = new_object("zapper", "zapper", vec2(1000, 0)); diff --git a/game/player/PlayerEndScript.cpp b/game/player/PlayerEndScript.cpp new file mode 100644 index 0000000..c0b4e74 --- /dev/null +++ b/game/player/PlayerEndScript.cpp @@ -0,0 +1,60 @@ +#include "PlayerEndScript.h" + +#include +#include +#include +#include +#include + +using namespace crepe; +using namespace std; + +void PlayerEndScript::init() { + BoxCollider jetpack_coll = this->get_components_by_name("player").back(); + CircleCollider head_coll = this->get_components_by_name("player").back(); + jetpack_coll.active = false; + head_coll.active = false; +} + +void PlayerEndScript::fixed_update(crepe::duration_t dt) { + Transform & transform_player = this->get_components_by_name("player").front(); + RefVector anim_player = this->get_components_by_name("player"); + Rigidbody & rb_player = this->get_components_by_name("player").front(); + Rigidbody & rb_camera = this->get_components_by_name("camera").front(); + + if (transform_player.position.y >= 194) { + if (jump == 0 || jump == 1) { + int random_number = rand() % 4; + for (Animator & anim : anim_player) { + anim.active = false; + anim.set_anim(6); + for (int i = 0; i < random_number; i++) { + anim.next_anim(); + } + } + } else if (jump == 2) { + for (Animator & anim : anim_player) { + anim.active = false; + anim.set_anim(7); + } + rb_player.data.angular_velocity = 0; + transform_player.rotation = 90; + } + if (jump == 0) { + rb_player.data.linear_velocity = vec2(100, -150); + rb_player.data.angular_velocity = 320; + rb_player.data.angular_velocity_coefficient = 0.8; + jump++; + } else if (jump == 1) { + rb_player.data.linear_velocity = vec2(100, -125); + rb_player.data.angular_velocity = 300; + rb_player.data.angular_velocity_coefficient = 0.8; + jump++; + } else if (jump == 2) { + rb_player.data.linear_velocity = vec2(100, 0); + rb_player.data.linear_velocity_coefficient = vec2(0.5, 0.5); + rb_camera.data.linear_velocity_coefficient = vec2(0.5, 0.5); + jump++; + } + } +} diff --git a/game/player/PlayerEndScript.h b/game/player/PlayerEndScript.h new file mode 100644 index 0000000..240ab7c --- /dev/null +++ b/game/player/PlayerEndScript.h @@ -0,0 +1,12 @@ +#pragma once + +#include + +class PlayerEndScript : public crepe::Script { +public: + void init(); + void fixed_update(crepe::duration_t dt); + +private: + int jump = 0; +}; diff --git a/game/player/PlayerScript.cpp b/game/player/PlayerScript.cpp index de53fc7..7f5d0c4 100644 --- a/game/player/PlayerScript.cpp +++ b/game/player/PlayerScript.cpp @@ -18,8 +18,7 @@ void PlayerScript::init() { bool PlayerScript::on_collision(const CollisionEvent & ev) { BehaviorScript & play_scr = this->get_components_by_name("player").front(); - BehaviorScript & end_scr - = this->get_components_by_name("end_game_script").front(); + BehaviorScript & end_scr = this->get_components_by_name("player").back(); RefVector animators = this->get_components_by_name("player"); RefVector emitters = this->get_components_by_name("player"); diff --git a/game/player/PlayerSubScene.cpp b/game/player/PlayerSubScene.cpp index 812d99a..91ae882 100644 --- a/game/player/PlayerSubScene.cpp +++ b/game/player/PlayerSubScene.cpp @@ -1,4 +1,5 @@ #include "PlayerSubScene.h" +#include "PlayerEndScript.h" #include "PlayerScript.h" #include "../Config.h" @@ -148,4 +149,5 @@ PlayerSubScene::PlayerSubScene(Scene & scn) { .collision_layer = COLL_LAY_PLAYER, }); player.add_component().set_script().active = false; + player.add_component().set_script().active = false; } -- cgit v1.2.3 From b31934cc6867be10e986a4bd4d0ab81dc91d56ec Mon Sep 17 00:00:00 2001 From: Max-001 Date: Mon, 6 Jan 2025 10:14:58 +0100 Subject: Paralax stops when player stops --- game/GameScene.cpp | 2 +- game/player/PlayerEndScript.cpp | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) (limited to 'game/GameScene.cpp') diff --git a/game/GameScene.cpp b/game/GameScene.cpp index 821191d..a8fcb47 100644 --- a/game/GameScene.cpp +++ b/game/GameScene.cpp @@ -100,7 +100,7 @@ void GameScene::load_scene() { .collision_layer = COLL_LAY_LASER, }); laser.add_component(vec2(100, 100)); - GameObject missile = new_object("missile", "missile", vec2(3000, 0)); + GameObject missile = new_object("missile", "missile", vec2(4000, 0)); Asset missile_asset {"asset/obstacles/missile/missile.png"}; Sprite & missile_sprite = missile.add_component( missile_asset, diff --git a/game/player/PlayerEndScript.cpp b/game/player/PlayerEndScript.cpp index 8833c28..4cd2cd0 100644 --- a/game/player/PlayerEndScript.cpp +++ b/game/player/PlayerEndScript.cpp @@ -50,6 +50,12 @@ bool PlayerEndScript::on_collision(const crepe::CollisionEvent & ev) { } else if (jump == 1) { jump++; } else if (jump == 2) { + RefVector rb_back_forest + = this->get_components_by_tag("forest_background"); + for (Rigidbody & rb : rb_back_forest) { + rb.data.linear_velocity_coefficient = vec2(0.5, 0.5); + } + rb_player.data.angular_velocity = 0; rb_player.data.elasticity_coefficient = 0; rb_player.data.linear_velocity = vec2(PLAYER_SPEED, 0); -- cgit v1.2.3