diff options
-rw-r--r-- | game/CMakeLists.txt | 28 | ||||
-rw-r--r-- | game/main.cpp | 2 | ||||
-rw-r--r-- | game/mainmenu/ButtonScript.cpp | 26 | ||||
-rw-r--r-- | game/mainmenu/ButtonScript.h | 11 | ||||
-rw-r--r-- | game/mainmenu/ButtonStartScript.cpp | 48 | ||||
-rw-r--r-- | game/mainmenu/ButtonStartScript.h | 19 | ||||
-rw-r--r-- | game/mainmenu/ButtonSubScene.cpp | 63 | ||||
-rw-r--r-- | game/mainmenu/ButtonSubScene.h | 36 | ||||
-rw-r--r-- | game/mainmenu/MainMenuConfig.h | 22 | ||||
-rw-r--r-- | game/mainmenu/MainMenuScene.cpp | 58 | ||||
-rw-r--r-- | game/mainmenu/MainMenuScene.h | 11 | ||||
-rw-r--r-- | src/crepe/api/Config.h | 2 | ||||
-rw-r--r-- | src/crepe/api/Engine.cpp | 1 |
13 files changed, 325 insertions, 2 deletions
diff --git a/game/CMakeLists.txt b/game/CMakeLists.txt index dc55523..5365fb4 100644 --- a/game/CMakeLists.txt +++ b/game/CMakeLists.txt @@ -8,7 +8,8 @@ set(CMAKE_BUILD_TYPE Debug) project(game C CXX) add_subdirectory(../src crepe) -add_executable(main + +set(SOURCES AquariumSubScene.cpp BackgroundSubScene.cpp ForestParallaxScript.cpp @@ -21,7 +22,32 @@ add_executable(main StartGameScript.cpp StartSubScene.cpp main.cpp + mainmenu/ButtonScript.cpp + mainmenu/ButtonSubScene.cpp + mainmenu/MainMenuScene.cpp + mainmenu/ButtonStartScript.cpp +) + +set(HEADERS + AquariumSubScene.h + BackgroundSubScene.h + ForestParallaxScript.h + ForestSubScene.h + GameScene.h + HallwaySubScene.h + MoveCameraManualyScript.h + PlayerScript.h + PlayerSubScene.h + StartGameScript.h + StartSubScene.h + mainmenu/ButtonScript.h + mainmenu/ButtonSubScene.h + mainmenu/MainMenuScene.h + mainmenu/MainMenuConfig.h + mainmenu/ButtonStartScript.h ) +add_executable(main ${SOURCES} ${HEADERS}) + target_link_libraries(main PUBLIC crepe) diff --git a/game/main.cpp b/game/main.cpp index 325b66d..5d3dbaf 100644 --- a/game/main.cpp +++ b/game/main.cpp @@ -2,11 +2,13 @@ #include <crepe/api/Script.h> #include "GameScene.h" +#include "mainmenu/MainMenuScene.h" using namespace crepe; int main() { Engine gameloop; + gameloop.add_scene<MainMenuScene>(); gameloop.add_scene<GameScene>(); return gameloop.main(); diff --git a/game/mainmenu/ButtonScript.cpp b/game/mainmenu/ButtonScript.cpp new file mode 100644 index 0000000..44ba381 --- /dev/null +++ b/game/mainmenu/ButtonScript.cpp @@ -0,0 +1,26 @@ +#include "ButtonScript.h" +#include "iostream" +using namespace crepe; +using namespace std; + +bool ButtonScript::on_button_press(const ButtonPressEvent& e){ + + cout << "button triggered:" << e.metadata.game_object_id << std::endl; + return false; +} +bool ButtonScript::on_button_enter(const ButtonEnterEvent& e){ + + cout << "button Enter:" << e.metadata.game_object_id << std::endl; + return false; +} +bool ButtonScript::on_button_exit(const ButtonExitEvent& e){ + + cout << "button Exit:" << e.metadata.game_object_id << std::endl; + return false; +} +void ButtonScript::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); }); +} diff --git a/game/mainmenu/ButtonScript.h b/game/mainmenu/ButtonScript.h new file mode 100644 index 0000000..96f52f4 --- /dev/null +++ b/game/mainmenu/ButtonScript.h @@ -0,0 +1,11 @@ +#pragma once + +#include <crepe/api/Script.h> + +class ButtonScript : public crepe::Script { +public: + bool on_button_press(const crepe::ButtonPressEvent& e); + bool on_button_enter(const crepe::ButtonEnterEvent& e); + bool on_button_exit(const crepe::ButtonExitEvent& e); + void init(); +}; 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 <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 new file mode 100644 index 0000000..3d6f920 --- /dev/null +++ b/game/mainmenu/ButtonStartScript.h @@ -0,0 +1,19 @@ +#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 new file mode 100644 index 0000000..e61613b --- /dev/null +++ b/game/mainmenu/ButtonSubScene.cpp @@ -0,0 +1,63 @@ +#include "ButtonSubScene.h" +#include "ButtonScript.h" +#include "ButtonStartScript.h" +#include "MainMenuConfig.h" + +#include <crepe/api/BehaviorScript.h> +#include <crepe/api/Sprite.h> +#include <crepe/api/Scene.h> +#include <crepe/api/Button.h> +#include <crepe/api/Text.h> + +using namespace crepe; +using namespace std; + +void ButtonSubScene::create(Scene & scn,const Data & data){ + GameObject button_object = scn.new_object("button","",data.position,0,1); + this->large_btn_overlay(button_object); + this->btn_text_middle(button_object,data.text,data.text_offset,data.text_size); + this->set_script(button_object,data.script); +} +void ButtonSubScene::large_btn_overlay(crepe::GameObject & button_object){ + button_object.add_component<Sprite>(Asset("asset/ui/buttonBacking.png"),Sprite::Data{ + .sorting_in_layer = MainMenuConfig::STARTING_SORTING_IN_LAYER+1, + .size = MainMenuConfig::LARGE_OVERLAY_SIZE, + }); + button_object.add_component<Button>(MainMenuConfig::LARGE_OVERLAY_SIZE,vec2{0,0}); + this->btn_color_side(button_object,SIDE_PANEL_OFFSET); +} + +void ButtonSubScene::btn_color_side(crepe::GameObject & button_object,const vec2 & offset){ + button_object.add_component<Sprite>(Asset("asset/ui/buttonSmallBlue.png"),Sprite::Data{ + .sorting_in_layer = MainMenuConfig::STARTING_SORTING_IN_LAYER+2, + .size = MainMenuConfig::SIDE_PANEL_SIZE, + .position_offset = offset, + }); + button_object.add_component<Sprite>(Asset("asset/ui/buttonSmallBlue.png"),Sprite::Data{ + .flip = {true,false}, + .sorting_in_layer = MainMenuConfig::STARTING_SORTING_IN_LAYER+2, + .size = MainMenuConfig::SIDE_PANEL_SIZE, + .position_offset = {-offset.x,offset.y}, + }); +} + +//fc-match arial +void ButtonSubScene::btn_text_middle(crepe::GameObject & button_object,const std::string & text,const crepe::vec2 & text_offset,const crepe::vec2 & text_size){ + button_object.add_component<Text>(text_size,text_offset+MainMenuConfig::FONTOFFSET, MainMenuConfig::FONT, Text::Data{ + .text_color = Color::WHITE, + }, text); +} + +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>(); + break; + case ScriptSelect::NONE: + break; + } +} diff --git a/game/mainmenu/ButtonSubScene.h b/game/mainmenu/ButtonSubScene.h new file mode 100644 index 0000000..658e651 --- /dev/null +++ b/game/mainmenu/ButtonSubScene.h @@ -0,0 +1,36 @@ +#pragma once + +#include <crepe/api/GameObject.h> +#include "MainMenuConfig.h" + +namespace crepe { +class Scene; +} + +class ButtonSubScene { +public: + //script enum + enum class ScriptSelect { + START, + PREVIEW, + SHOP, + NONE, + }; + //data struct + struct Data{ + const std::string & text = "NODATA"; + const crepe::vec2 & text_offset = {0,0}; + const crepe::vec2 & text_size = {200,200}; + const crepe::vec2 & position = {0,0}; + const ScriptSelect script = ScriptSelect::NONE; + }; +public: + void create(crepe::Scene & scn,const Data & data); +private: + void large_btn_overlay(crepe::GameObject & button_object); + void btn_color_side(crepe::GameObject & button_object,const crepe::vec2 & offset); + void btn_text_middle(crepe::GameObject & button_object,const std::string & text,const crepe::vec2 & text_offset,const crepe::vec2 & text_size); + void set_script(crepe::GameObject & button_object,ScriptSelect script); +private: + static constexpr crepe::vec2 SIDE_PANEL_OFFSET = {113,0}; +}; diff --git a/game/mainmenu/MainMenuConfig.h b/game/mainmenu/MainMenuConfig.h new file mode 100644 index 0000000..8992ce7 --- /dev/null +++ b/game/mainmenu/MainMenuConfig.h @@ -0,0 +1,22 @@ +#pragma once +#include "types.h" +#include <string> + +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}; + + //button config + static constexpr const char* FONT = "Garuda"; + static constexpr crepe::vec2 FONTOFFSET = {0,0}; + static constexpr crepe::vec2 LARGE_OVERLAY_SIZE = {250,100}; + static constexpr crepe::vec2 SIDE_PANEL_SIZE = {50,150}; +}; + diff --git a/game/mainmenu/MainMenuScene.cpp b/game/mainmenu/MainMenuScene.cpp new file mode 100644 index 0000000..4067681 --- /dev/null +++ b/game/mainmenu/MainMenuScene.cpp @@ -0,0 +1,58 @@ + +#include "MainMenuScene.h" +#include "ButtonSubScene.h" +#include "api/Camera.h" +#include "../StartSubScene.h" +#include "MainMenuConfig.h" +#include "api/GameObject.h" +#include "api/Rigidbody.h" +#include "api/Sprite.h" + +using namespace crepe; +using namespace std; + +void MainMenuScene::load_scene(){ + GameObject camera_object = this->new_object(MainMenuConfig::CAMERA_NAME); + camera_object.add_component<Camera>(ivec2(990, 720), vec2(1100, 800), + Camera::Data{ + .bg_color = Color::RED, + }); + + GameObject menu_background = this->new_object("menu_background","",MainMenuConfig::MENU_OFFSET + MainMenuConfig::MENU_OFFSET_BACKGROUND); + menu_background.add_component<Sprite>( + Asset("asset/ui/itemsButtonBlankDark.png"), + Sprite::Data{ + .sorting_in_layer = MainMenuConfig::STARTING_SORTING_IN_LAYER+0, + .size = {300,922}, + }); + + 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}, + .position = pos, + .script = ButtonSubScene::ScriptSelect::PREVIEW, + }); + + //Shop btn + // pos.y += MainMenuConfig::MENU_BUTTON_SPACING + MainMenuConfig::LARGE_OVERLAY_SIZE.y; + // button.create(*this,"SHOP",vec2{0,0},vec2{200,80},pos); + + //Start of map + StartSubScene start; + start.create(*this, MainMenuConfig::STARTMAP_OFFSET); +} + +string MainMenuScene::get_name() const { return "mainmenu"; } diff --git a/game/mainmenu/MainMenuScene.h b/game/mainmenu/MainMenuScene.h new file mode 100644 index 0000000..f7319cb --- /dev/null +++ b/game/mainmenu/MainMenuScene.h @@ -0,0 +1,11 @@ +#pragma once + +#include <crepe/api/Scene.h> +#include <string> + +class MainMenuScene : public crepe::Scene { +public: + void load_scene(); + + std::string get_name() const; +}; diff --git a/src/crepe/api/Config.h b/src/crepe/api/Config.h index 6b9e3ca..3e9c9ef 100644 --- a/src/crepe/api/Config.h +++ b/src/crepe/api/Config.h @@ -85,7 +85,7 @@ struct Config final { * This config option is the font size at which all fonts will be loaded initially. * */ - unsigned int size = 16; + unsigned int size = 40; } font; //! Configuration for click tolerance. struct { diff --git a/src/crepe/api/Engine.cpp b/src/crepe/api/Engine.cpp index 2e9d35a..4add4e1 100644 --- a/src/crepe/api/Engine.cpp +++ b/src/crepe/api/Engine.cpp @@ -54,6 +54,7 @@ void Engine::loop() { try { systems.frame_update(); + this->scene_manager.load_next_scene(); } catch (const exception & e) { Log::logf(Log::Level::WARNING, "Uncaught exception in frame update function: {}\n", e.what()); |