aboutsummaryrefslogtreecommitdiff
path: root/game
diff options
context:
space:
mode:
authorJAROWMR <jarorutjes07@gmail.com>2024-12-22 00:11:56 +0100
committerJAROWMR <jarorutjes07@gmail.com>2024-12-22 00:11:56 +0100
commit7bcee4f662b20b8e2e7183bb6bd4cb061fa14e12 (patch)
treecb7976922b3cc6070dc9c14d0ffcaea48b3efee6 /game
parent5a2f6a14803885b7c53bab7d43208476d2e1bd5b (diff)
added menu buttons + scene switching
Diffstat (limited to 'game')
-rw-r--r--game/CMakeLists.txt3
-rw-r--r--game/main.cpp1
-rw-r--r--game/mainmenu/ButtonStartScript.cpp48
-rw-r--r--game/mainmenu/ButtonStartScript.h19
-rw-r--r--game/mainmenu/ButtonSubScene.cpp57
-rw-r--r--game/mainmenu/ButtonSubScene.h24
-rw-r--r--game/mainmenu/MainMenuConfig.h22
-rw-r--r--game/mainmenu/MainMenuScene.cpp42
8 files changed, 195 insertions, 21 deletions
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<MainMenuScene>();
+ gameloop.add_scene<GameScene>();
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 <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
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 <crepe/api/BehaviorScript.h>
#include <crepe/api/Sprite.h>
@@ -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<Button>(vec2{250,100},vec2{0,0});
- button_object.add_component<BehaviorScript>().set_script<ButtonScript>();
+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 = 0,
- .size = vec2{250,100},
+ .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 = 1,
- .size = vec2{50,150},
- .position_offset = {110,0},
+ .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 = 1,
- .size = vec2{75,150},
- .position_offset = {-110,0},
+ .sorting_in_layer = MainMenuConfig::STARTING_SORTING_IN_LAYER+2,
+ .size = MainMenuConfig::SIDE_PANEL_SIZE,
+ .position_offset = {-offset.x,offset.y},
});
- //fc-match arial
- button_object.add_component<Text>(vec2{220,100},vec2{0,0}, "dejavusans", Text::Data{
- .text_color = Color::MAGENTA,
- }, "Hallo");
+}
+
+//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
index fab076d..658e651 100644
--- a/game/mainmenu/ButtonSubScene.h
+++ b/game/mainmenu/ButtonSubScene.h
@@ -1,6 +1,7 @@
#pragma once
#include <crepe/api/GameObject.h>
+#include "MainMenuConfig.h"
namespace crepe {
class Scene;
@@ -8,7 +9,28 @@ class Scene;
class ButtonSubScene {
public:
- void create(crepe::Scene & scn);
+ //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
index bc8ea86..4067681 100644
--- a/game/mainmenu/MainMenuScene.cpp
+++ b/game/mainmenu/MainMenuScene.cpp
@@ -2,19 +2,57 @@
#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("camera");
+ 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;
- button.create(*this);
+ 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"; }