diff options
author | JAROWMR <jarorutjes07@gmail.com> | 2024-12-23 21:17:45 +0100 |
---|---|---|
committer | JAROWMR <jarorutjes07@gmail.com> | 2024-12-23 21:17:45 +0100 |
commit | 225e873c8ac9c4c08720a69b8f2c817d22cf0071 (patch) | |
tree | 20c1539a7ea7d133fca143e922165fd7533c87fe | |
parent | a3097a20daaf58e4e3852bfb36315dc5e14d3e52 (diff) |
made script reusable to switch scenes
-rw-r--r-- | game/CMakeLists.txt | 8 | ||||
-rw-r--r-- | game/mainmenu/ButtonStartScript.cpp | 48 | ||||
-rw-r--r-- | game/mainmenu/ButtonStartScript.h | 19 | ||||
-rw-r--r-- | game/mainmenu/ButtonSubScene.cpp | 10 | ||||
-rw-r--r-- | game/mainmenu/ButtonSubScene.h | 1 | ||||
-rw-r--r-- | game/mainmenu/MainMenuConfig.h | 9 | ||||
-rw-r--r-- | game/mainmenu/MainMenuScene.cpp | 16 | ||||
-rw-r--r-- | game/mainmenu/ShowPreviewScript.cpp | 35 | ||||
-rw-r--r-- | game/mainmenu/ShowPreviewScript.h | 12 | ||||
-rw-r--r-- | game/mainmenu/ShowScript.cpp | 26 | ||||
-rw-r--r-- | game/mainmenu/ShowScript.h | 13 | ||||
-rw-r--r-- | game/mainmenu/ShowStartScript.cpp | 21 | ||||
-rw-r--r-- | game/mainmenu/ShowStartScript.h | 10 |
13 files changed, 139 insertions, 89 deletions
diff --git a/game/CMakeLists.txt b/game/CMakeLists.txt index 5365fb4..4776d03 100644 --- a/game/CMakeLists.txt +++ b/game/CMakeLists.txt @@ -25,7 +25,9 @@ set(SOURCES mainmenu/ButtonScript.cpp mainmenu/ButtonSubScene.cpp mainmenu/MainMenuScene.cpp - mainmenu/ButtonStartScript.cpp + mainmenu/ShowScript.cpp + mainmenu/ShowStartScript.cpp + mainmenu/ShowPreviewScript.cpp ) set(HEADERS @@ -44,7 +46,9 @@ set(HEADERS mainmenu/ButtonSubScene.h mainmenu/MainMenuScene.h mainmenu/MainMenuConfig.h - mainmenu/ButtonStartScript.h + mainmenu/ShowScript.h + mainmenu/ShowStartScript.h + mainmenu/ShowPreviewScript.h ) add_executable(main ${SOURCES} ${HEADERS}) diff --git a/game/mainmenu/ButtonStartScript.cpp b/game/mainmenu/ButtonStartScript.cpp deleted file mode 100644 index 8f76802..0000000 --- a/game/mainmenu/ButtonStartScript.cpp +++ /dev/null @@ -1,48 +0,0 @@ -#include "ButtonStartScript.h" -#include "api/Rigidbody.h" -#include "api/Transform.h" -#include "iostream" -#include <crepe/api/Camera.h> -#include "MainMenuConfig.h" - -using namespace crepe; -using namespace std; - -void ButtonStartScript::init(){ - cout << "script init" << endl; - this->subscribe<ButtonPressEvent>([this](const ButtonPressEvent& e) { return this->on_button_press(e); }); - this->subscribe<ButtonEnterEvent>([this](const ButtonEnterEvent& e) { return this->on_button_enter(e); }); - this->subscribe<ButtonExitEvent>([this](const ButtonExitEvent& e) { return this->on_button_exit(e); }); -} - -void ButtonStartScript::frame_update(crepe::duration_t delta_time){ - if(this->transition) - { - cout << "transition:" << velocity << std::endl; - Transform & cam = this->get_components_by_name<Transform>(MainMenuConfig::CAMERA_NAME).front(); - if(velocity < velocity_max && cam.position.x < SLOW_DOWN) velocity += velocity_step * delta_time.count(); - else if(velocity > 20) velocity -= velocity_step * delta_time.count(); - if(cam.position.x < END) cam.position.x += (velocity * delta_time.count()); - if(cam.position.x >= END) - { - this->set_next_scene(MainMenuConfig::START_SCENE); - } - - } -} - -bool ButtonStartScript::on_button_press(const ButtonPressEvent& e){ - this->transition = true; - cout << "Start triggered:" << e.metadata.game_object_id << std::endl; - return false; -} -bool ButtonStartScript::on_button_enter(const ButtonEnterEvent& e){ - - cout << "Start Enter:" << e.metadata.game_object_id << std::endl; - return false; -} -bool ButtonStartScript::on_button_exit(const ButtonExitEvent& e){ - - cout << "Start Exit:" << e.metadata.game_object_id << std::endl; - return false; -} diff --git a/game/mainmenu/ButtonStartScript.h b/game/mainmenu/ButtonStartScript.h deleted file mode 100644 index 3d6f920..0000000 --- a/game/mainmenu/ButtonStartScript.h +++ /dev/null @@ -1,19 +0,0 @@ -#pragma once - -#include <crepe/api/Script.h> - -class ButtonStartScript : public crepe::Script { -public: - void init() override; - void frame_update(crepe::duration_t delta_time) override; - bool on_button_press(const crepe::ButtonPressEvent& e); - bool on_button_enter(const crepe::ButtonEnterEvent& e); - bool on_button_exit(const crepe::ButtonExitEvent& e); -private: - bool transition = false; - static constexpr float SLOW_DOWN = 200; - static constexpr float END = 300; - const float velocity_max = 200; - const float velocity_step = 200; - float velocity = 20; -}; diff --git a/game/mainmenu/ButtonSubScene.cpp b/game/mainmenu/ButtonSubScene.cpp index e61613b..b415f74 100644 --- a/game/mainmenu/ButtonSubScene.cpp +++ b/game/mainmenu/ButtonSubScene.cpp @@ -1,6 +1,7 @@ #include "ButtonSubScene.h" #include "ButtonScript.h" -#include "ButtonStartScript.h" +#include "ShowPreviewScript.h" +#include "ShowStartScript.h" #include "MainMenuConfig.h" #include <crepe/api/BehaviorScript.h> @@ -50,13 +51,10 @@ void ButtonSubScene::btn_text_middle(crepe::GameObject & button_object,const std void ButtonSubScene::set_script(crepe::GameObject & button_object,ScriptSelect script){ switch (script) { - case ScriptSelect::START: - button_object.add_component<BehaviorScript>().set_script<ButtonStartScript>(); - break; case ScriptSelect::PREVIEW: - case ScriptSelect::SHOP: - button_object.add_component<BehaviorScript>().set_script<ButtonScript>(); + button_object.add_component<BehaviorScript>().set_script<ShowPreviewScript>(); break; + case ScriptSelect::SHOP: case ScriptSelect::NONE: break; } diff --git a/game/mainmenu/ButtonSubScene.h b/game/mainmenu/ButtonSubScene.h index 658e651..9905a8b 100644 --- a/game/mainmenu/ButtonSubScene.h +++ b/game/mainmenu/ButtonSubScene.h @@ -11,7 +11,6 @@ class ButtonSubScene { public: //script enum enum class ScriptSelect { - START, PREVIEW, SHOP, NONE, diff --git a/game/mainmenu/MainMenuConfig.h b/game/mainmenu/MainMenuConfig.h index 8992ce7..75b81db 100644 --- a/game/mainmenu/MainMenuConfig.h +++ b/game/mainmenu/MainMenuConfig.h @@ -6,13 +6,18 @@ struct MainMenuConfig { //generic menu config static constexpr unsigned int STARTING_SORTING_IN_LAYER = 7; static constexpr const char* CAMERA_NAME = "camera"; - static constexpr const char* START_SCENE = "scene1"; //main menu config static constexpr float STARTMAP_OFFSET = 50; static constexpr crepe::vec2 MENU_OFFSET = {-400,-200}; static constexpr float MENU_BUTTON_SPACING = 10; static constexpr crepe::vec2 MENU_OFFSET_BACKGROUND = {0,+200}; - + // Moving to new scene (Start and Preview) + static constexpr const char* START_SCENE = "scene1"; + static constexpr const char* PREVIEW_SCENE = "scene1"; + static constexpr float SLOW_DOWN = 200; + static constexpr float END = 300; + static constexpr float VELOCITY_MAX = 200; + static constexpr float VELOCITY_STEP = 200; //button config static constexpr const char* FONT = "Garuda"; static constexpr crepe::vec2 FONTOFFSET = {0,0}; diff --git a/game/mainmenu/MainMenuScene.cpp b/game/mainmenu/MainMenuScene.cpp index 4067681..7a86ba5 100644 --- a/game/mainmenu/MainMenuScene.cpp +++ b/game/mainmenu/MainMenuScene.cpp @@ -1,6 +1,8 @@ #include "MainMenuScene.h" #include "ButtonSubScene.h" +#include "ShowStartScript.h" +#include "api/BehaviorScript.h" #include "api/Camera.h" #include "../StartSubScene.h" #include "MainMenuConfig.h" @@ -18,27 +20,19 @@ void MainMenuScene::load_scene(){ .bg_color = Color::RED, }); - GameObject menu_background = this->new_object("menu_background","",MainMenuConfig::MENU_OFFSET + MainMenuConfig::MENU_OFFSET_BACKGROUND); - menu_background.add_component<Sprite>( + GameObject menu = this->new_object("menu_background","",MainMenuConfig::MENU_OFFSET + MainMenuConfig::MENU_OFFSET_BACKGROUND); + menu.add_component<Sprite>( Asset("asset/ui/itemsButtonBlankDark.png"), Sprite::Data{ .sorting_in_layer = MainMenuConfig::STARTING_SORTING_IN_LAYER+0, .size = {300,922}, }); + menu.add_component<BehaviorScript>().set_script<ShowStartScript>(); ButtonSubScene button; vec2 pos = MainMenuConfig::MENU_OFFSET; - //Start btn - button.create(*this,ButtonSubScene::Data{ - .text = "START", - .text_size = vec2{200,80}, - .position = pos, - .script = ButtonSubScene::ScriptSelect::START, - }); - //Preview btn - pos.y += MainMenuConfig::MENU_BUTTON_SPACING + MainMenuConfig::LARGE_OVERLAY_SIZE.y; button.create(*this,ButtonSubScene::Data{ .text = "PREVIEW", .text_size = vec2{200,80}, diff --git a/game/mainmenu/ShowPreviewScript.cpp b/game/mainmenu/ShowPreviewScript.cpp new file mode 100644 index 0000000..d015827 --- /dev/null +++ b/game/mainmenu/ShowPreviewScript.cpp @@ -0,0 +1,35 @@ +#include "ShowPreviewScript.h" +#include "MainMenuConfig.h" +#include "iostream" + +using namespace crepe; +using namespace std; + + +void ShowPreviewScript::init(){ + cout << "script init" << endl; + this->subscribe<ButtonPressEvent>([this](const ButtonPressEvent& e) { return this->on_button_press(e); }); + this->subscribe<ButtonEnterEvent>([this](const ButtonEnterEvent& e) { return this->on_button_enter(e); }); + this->subscribe<ButtonExitEvent>([this](const ButtonExitEvent& e) { return this->on_button_exit(e); }); +} + +bool ShowPreviewScript::on_button_press(const ButtonPressEvent& e){ + if(!this->transition) this->transition = true; + cout << "Start triggered:" << e.metadata.game_object_id << std::endl; + return false; +} +bool ShowPreviewScript::on_button_enter(const ButtonEnterEvent& e){ + + cout << "Start Enter:" << e.metadata.game_object_id << std::endl; + return false; +} +bool ShowPreviewScript::on_button_exit(const ButtonExitEvent& e){ + + cout << "Start Exit:" << e.metadata.game_object_id << std::endl; + return false; +} + +const char* ShowPreviewScript::get_scene_name() const { + // Provide the next scene defined in MainMenuConfig + return MainMenuConfig::PREVIEW_SCENE; +} diff --git a/game/mainmenu/ShowPreviewScript.h b/game/mainmenu/ShowPreviewScript.h new file mode 100644 index 0000000..9612ca5 --- /dev/null +++ b/game/mainmenu/ShowPreviewScript.h @@ -0,0 +1,12 @@ +#pragma once + +#include "ShowScript.h" + +class ShowPreviewScript : public ShowScript { +public: + void init() override; + bool on_button_press(const crepe::ButtonPressEvent& e); + bool on_button_enter(const crepe::ButtonEnterEvent& e); + bool on_button_exit(const crepe::ButtonExitEvent& e); + const char* get_scene_name() const override; +}; diff --git a/game/mainmenu/ShowScript.cpp b/game/mainmenu/ShowScript.cpp new file mode 100644 index 0000000..dbd638e --- /dev/null +++ b/game/mainmenu/ShowScript.cpp @@ -0,0 +1,26 @@ +#include "ShowScript.h" +#include "api/Rigidbody.h" +#include "api/Transform.h" +#include "iostream" +#include <crepe/api/Camera.h> +#include "MainMenuConfig.h" + +using namespace crepe; +using namespace std; + +void ShowScript::frame_update(crepe::duration_t delta_time){ + if(this->transition) + { + cout << "transition:" << velocity << std::endl; + Transform & cam = this->get_components_by_name<Transform>(MainMenuConfig::CAMERA_NAME).front(); + if(velocity < MainMenuConfig::VELOCITY_MAX && cam.position.x < MainMenuConfig::SLOW_DOWN) velocity += MainMenuConfig::VELOCITY_STEP * delta_time.count(); + else if(velocity > 20) velocity -= MainMenuConfig::VELOCITY_STEP * delta_time.count(); + if(cam.position.x < MainMenuConfig::END) cam.position.x += (velocity * delta_time.count()); + if(cam.position.x >= MainMenuConfig::END) + { + this->set_next_scene(this->get_scene_name()); + } + + } +} + diff --git a/game/mainmenu/ShowScript.h b/game/mainmenu/ShowScript.h new file mode 100644 index 0000000..5bfabcc --- /dev/null +++ b/game/mainmenu/ShowScript.h @@ -0,0 +1,13 @@ +#pragma once + +#include <crepe/api/Script.h> + +class ShowScript : public crepe::Script { +public: + void frame_update(crepe::duration_t delta_time) override; + virtual const char* get_scene_name() const = 0; +private: + float velocity = 20; +protected: + bool transition = false; +}; diff --git a/game/mainmenu/ShowStartScript.cpp b/game/mainmenu/ShowStartScript.cpp new file mode 100644 index 0000000..7179038 --- /dev/null +++ b/game/mainmenu/ShowStartScript.cpp @@ -0,0 +1,21 @@ +#include "ShowStartScript.h" +#include "MainMenuConfig.h" +#include "api/Event.h" +#include "iostream" + +using namespace crepe; +using namespace std; + + +void ShowStartScript::init(){ + cout << "script init" << endl; +} + +void ShowStartScript::fixed_update(crepe::duration_t dt){ + if(this->get_key_state(Keycode::ENTER) && this->transition == false) this->transition = true; +} + +const char* ShowStartScript::get_scene_name() const { + // Provide the next scene defined in MainMenuConfig + return MainMenuConfig::START_SCENE; +} diff --git a/game/mainmenu/ShowStartScript.h b/game/mainmenu/ShowStartScript.h new file mode 100644 index 0000000..4d8d894 --- /dev/null +++ b/game/mainmenu/ShowStartScript.h @@ -0,0 +1,10 @@ +#pragma once + +#include "ShowScript.h" + +class ShowStartScript : public ShowScript { +public: + void init() override; + void fixed_update(crepe::duration_t dt) override; + const char* get_scene_name() const override; +}; |