From ea970a901d169824a99a336eacd06ebc032e5f66 Mon Sep 17 00:00:00 2001 From: max-001 Date: Thu, 19 Dec 2024 11:21:37 +0100 Subject: Moved scripts to seperate files --- src/example/CMakeLists.txt | 4 ++ src/example/GameScene.cpp | 86 +-------------------------------- src/example/MoveCameraManualyScript.cpp | 22 +++++++++ src/example/MoveCameraManualyScript.h | 11 +++++ src/example/StartGameScript.cpp | 61 +++++++++++++++++++++++ src/example/StartGameScript.h | 12 +++++ 6 files changed, 112 insertions(+), 84 deletions(-) create mode 100644 src/example/MoveCameraManualyScript.cpp create mode 100644 src/example/MoveCameraManualyScript.h create mode 100644 src/example/StartGameScript.cpp create mode 100644 src/example/StartGameScript.h (limited to 'src') diff --git a/src/example/CMakeLists.txt b/src/example/CMakeLists.txt index cd7407f..4b9f4f1 100644 --- a/src/example/CMakeLists.txt +++ b/src/example/CMakeLists.txt @@ -6,6 +6,8 @@ set(GAME_HEADERS HallwaySubScene.h StartSubScene.h GameScene.h + StartGameScript.h + MoveCameraManualyScript.h PlayerSubScene.h PlayerScript.h ) @@ -19,6 +21,8 @@ set(GAME_SOURCES HallwaySubScene.cpp StartSubScene.cpp GameScene.cpp + StartGameScript.cpp + MoveCameraManualyScript.cpp PlayerSubScene.cpp PlayerScript.cpp ) diff --git a/src/example/GameScene.cpp b/src/example/GameScene.cpp index 50b61d8..c280474 100644 --- a/src/example/GameScene.cpp +++ b/src/example/GameScene.cpp @@ -1,6 +1,8 @@ #include "GameScene.h" #include "BackgroundSubScene.h" +#include "MoveCameraManualyScript.h" #include "PlayerSubScene.h" +#include "StartGameScript.h" #include #include @@ -22,90 +24,6 @@ using namespace crepe; using namespace std; -class MoveCameraManualyScript : public Script { -public: - void init() { - subscribe( - [this](const KeyPressEvent & ev) -> bool { return this->keypressed(ev); }); - } - -private: - bool keypressed(const KeyPressEvent & event) { - if (event.key == Keycode::RIGHT) { - Transform & cam = this->get_components_by_name("camera").front(); - cam.position.x += 100; - return true; - } else if (event.key == Keycode::LEFT) { - Transform & cam = this->get_components_by_name("camera").front(); - cam.position.x -= 100; - return true; - } - return false; - } -}; - -class StartGameScript : public Script { -public: - void update() { - Transform & player_transform - = this->get_components_by_name("player").front(); - - // Create hole in wall and activate panic lamp - if (player_transform.position.x > 75 && !this->created_hole) { - Sprite & lamp_sprite = this->get_components_by_name("start_end").back(); - lamp_sprite.active = true; - Sprite & hole_sprite = this->get_components_by_name("start_hole").front(); - hole_sprite.active = true; - - RefVector frags_rg - = this->get_components_by_tag("wall_fragment"); - RefVector frags_sprite - = this->get_components_by_tag("wall_fragment"); - for (Rigidbody & frag_rg : frags_rg) { - frag_rg.active = true; - } - for (Sprite & frag_sprite : frags_sprite) { - frag_sprite.active = true; - } - - RefVector smoke_emitters - = this->get_components_by_name("smoke_particles"); - for (ParticleEmitter & emitter : smoke_emitters) { - emitter.active = true; - } - - this->created_hole = true; - } - - // Take jetpack from jetpack stand - if (player_transform.position.x > 275 && !this->took_jetpack) { - Animator & jetpack_stand_anim - = this->get_components_by_name("start_begin").back(); - jetpack_stand_anim.next_anim(); - Sprite & jetpack_sprite = this->get_components_by_name("player").back(); - jetpack_sprite.active = true; - - this->took_jetpack = true; - } - - // Start camera movement, enable player jumping and disable this script - if (player_transform.position.x > 500) { - Rigidbody & rb = this->get_components_by_name("camera").front(); - rb.data.linear_velocity = vec2(100, 0); - BehaviorScript & player_script - = this->get_components_by_name("player").front(); - player_script.active = true; - BehaviorScript & this_script - = this->get_components_by_name("start_game_script").front(); - this_script.active = false; - } - } - -private: - bool created_hole = false; - bool took_jetpack = false; -}; - void GameScene::load_scene() { BackgroundSubScene background(*this); diff --git a/src/example/MoveCameraManualyScript.cpp b/src/example/MoveCameraManualyScript.cpp new file mode 100644 index 0000000..0181333 --- /dev/null +++ b/src/example/MoveCameraManualyScript.cpp @@ -0,0 +1,22 @@ +#include "MoveCameraManualyScript.h" + +using namespace crepe; +using namespace std; + +void MoveCameraManualyScript::init() { + subscribe( + [this](const KeyPressEvent & ev) -> bool { return this->keypressed(ev); }); +} + +bool MoveCameraManualyScript::keypressed(const KeyPressEvent & event) { + if (event.key == Keycode::RIGHT) { + Transform & cam = this->get_components_by_name("camera").front(); + cam.position.x += 100; + return true; + } else if (event.key == Keycode::LEFT) { + Transform & cam = this->get_components_by_name("camera").front(); + cam.position.x -= 100; + return true; + } + return false; +} diff --git a/src/example/MoveCameraManualyScript.h b/src/example/MoveCameraManualyScript.h new file mode 100644 index 0000000..5a09055 --- /dev/null +++ b/src/example/MoveCameraManualyScript.h @@ -0,0 +1,11 @@ +#pragma once + +#include + +class MoveCameraManualyScript : public crepe::Script { +public: + void init(); + +private: + bool keypressed(const crepe::KeyPressEvent & event); +}; diff --git a/src/example/StartGameScript.cpp b/src/example/StartGameScript.cpp new file mode 100644 index 0000000..00f419d --- /dev/null +++ b/src/example/StartGameScript.cpp @@ -0,0 +1,61 @@ +#include "StartGameScript.h" + +#include +#include +#include + +using namespace crepe; +using namespace std; + +void StartGameScript::update() { + Transform & player_transform = this->get_components_by_name("player").front(); + + // Create hole in wall and activate panic lamp + if (player_transform.position.x > 75 && !this->created_hole) { + Sprite & lamp_sprite = this->get_components_by_name("start_end").back(); + lamp_sprite.active = true; + Sprite & hole_sprite = this->get_components_by_name("start_hole").front(); + hole_sprite.active = true; + + RefVector frags_rg + = this->get_components_by_tag("wall_fragment"); + RefVector frags_sprite = this->get_components_by_tag("wall_fragment"); + for (Rigidbody & frag_rg : frags_rg) { + frag_rg.active = true; + } + for (Sprite & frag_sprite : frags_sprite) { + frag_sprite.active = true; + } + + RefVector smoke_emitters + = this->get_components_by_name("smoke_particles"); + for (ParticleEmitter & emitter : smoke_emitters) { + emitter.active = true; + } + + this->created_hole = true; + } + + // Take jetpack from jetpack stand + if (player_transform.position.x > 275 && !this->took_jetpack) { + Animator & jetpack_stand_anim + = this->get_components_by_name("start_begin").back(); + jetpack_stand_anim.next_anim(); + Sprite & jetpack_sprite = this->get_components_by_name("player").back(); + jetpack_sprite.active = true; + + this->took_jetpack = true; + } + + // Start camera movement, enable player jumping and disable this script + if (player_transform.position.x > 500) { + Rigidbody & rb = this->get_components_by_name("camera").front(); + rb.data.linear_velocity = vec2(100, 0); + BehaviorScript & player_script + = this->get_components_by_name("player").front(); + player_script.active = true; + BehaviorScript & this_script + = this->get_components_by_name("start_game_script").front(); + this_script.active = false; + } +} diff --git a/src/example/StartGameScript.h b/src/example/StartGameScript.h new file mode 100644 index 0000000..f3534d0 --- /dev/null +++ b/src/example/StartGameScript.h @@ -0,0 +1,12 @@ +#pragma once + +#include + +class StartGameScript : public crepe::Script { +public: + void update(); + +private: + bool created_hole = false; + bool took_jetpack = false; +}; -- cgit v1.2.3