From 7bcee4f662b20b8e2e7183bb6bd4cb061fa14e12 Mon Sep 17 00:00:00 2001 From: JAROWMR Date: Sun, 22 Dec 2024 00:11:56 +0100 Subject: added menu buttons + scene switching --- game/CMakeLists.txt | 3 ++ game/main.cpp | 1 + game/mainmenu/ButtonStartScript.cpp | 48 +++++++++++++++++++++++++++++++ game/mainmenu/ButtonStartScript.h | 19 +++++++++++++ game/mainmenu/ButtonSubScene.cpp | 57 +++++++++++++++++++++++++------------ game/mainmenu/ButtonSubScene.h | 24 +++++++++++++++- game/mainmenu/MainMenuConfig.h | 22 ++++++++++++++ game/mainmenu/MainMenuScene.cpp | 42 +++++++++++++++++++++++++-- src/crepe/api/Config.h | 2 +- src/crepe/api/Engine.cpp | 1 + 10 files changed, 197 insertions(+), 22 deletions(-) create mode 100644 game/mainmenu/ButtonStartScript.cpp create mode 100644 game/mainmenu/ButtonStartScript.h create mode 100644 game/mainmenu/MainMenuConfig.h diff --git a/game/CMakeLists.txt b/game/CMakeLists.txt index 8fae19f..5365fb4 100644 --- a/game/CMakeLists.txt +++ b/game/CMakeLists.txt @@ -25,6 +25,7 @@ set(SOURCES mainmenu/ButtonScript.cpp mainmenu/ButtonSubScene.cpp mainmenu/MainMenuScene.cpp + mainmenu/ButtonStartScript.cpp ) set(HEADERS @@ -42,6 +43,8 @@ set(HEADERS mainmenu/ButtonScript.h mainmenu/ButtonSubScene.h mainmenu/MainMenuScene.h + mainmenu/MainMenuConfig.h + mainmenu/ButtonStartScript.h ) add_executable(main ${SOURCES} ${HEADERS}) diff --git a/game/main.cpp b/game/main.cpp index 9c9f9ee..5d3dbaf 100644 --- a/game/main.cpp +++ b/game/main.cpp @@ -9,6 +9,7 @@ using namespace crepe; int main() { Engine gameloop; gameloop.add_scene(); + gameloop.add_scene(); return gameloop.main(); } diff --git a/game/mainmenu/ButtonStartScript.cpp b/game/mainmenu/ButtonStartScript.cpp new file mode 100644 index 0000000..8f76802 --- /dev/null +++ b/game/mainmenu/ButtonStartScript.cpp @@ -0,0 +1,48 @@ +#include "ButtonStartScript.h" +#include "api/Rigidbody.h" +#include "api/Transform.h" +#include "iostream" +#include +#include "MainMenuConfig.h" + +using namespace crepe; +using namespace std; + +void ButtonStartScript::init(){ + cout << "script init" << endl; + this->subscribe([this](const ButtonPressEvent& e) { return this->on_button_press(e); }); + this->subscribe([this](const ButtonEnterEvent& e) { return this->on_button_enter(e); }); + this->subscribe([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(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 new file mode 100644 index 0000000..3d6f920 --- /dev/null +++ b/game/mainmenu/ButtonStartScript.h @@ -0,0 +1,19 @@ +#pragma once + +#include + +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 ac51c1b..e61613b 100644 --- a/game/mainmenu/ButtonSubScene.cpp +++ b/game/mainmenu/ButtonSubScene.cpp @@ -1,7 +1,7 @@ #include "ButtonSubScene.h" #include "ButtonScript.h" - -#include "system/RenderSystem.h" +#include "ButtonStartScript.h" +#include "MainMenuConfig.h" #include #include @@ -12,31 +12,52 @@ using namespace crepe; using namespace std; -void ButtonSubScene::create(Scene & scn){ - GameObject button_object = scn.new_object("button"); - button_object.add_component