From 01f56eca5e9421d9d7bc28b528aa48200df3f601 Mon Sep 17 00:00:00 2001 From: devZoGok Date: Sun, 2 Jul 2023 10:12:50 +0300 Subject: [PATCH] starting the game using the new GUI subsystem --- Assets/Scripts/Gui/inGame.lua | 2 + Assets/Scripts/Gui/main.lua | 7 +++- Assets/Scripts/Gui/singlePlayerMenu.lua | 52 +++++++++++++++++++++++++ CMakeLists.txt | 2 +- concreteGuiManager.cpp | 36 +++++++++++++++++ concreteGuiManager.h | 7 +++- playButton.cpp | 38 ++++++++++++++++++ playButton.h | 22 +++++++++++ singlePlayerButton.cpp | 45 ++------------------- 9 files changed, 165 insertions(+), 46 deletions(-) create mode 100644 Assets/Scripts/Gui/inGame.lua create mode 100644 Assets/Scripts/Gui/singlePlayerMenu.lua create mode 100644 playButton.cpp create mode 100644 playButton.h diff --git a/Assets/Scripts/Gui/inGame.lua b/Assets/Scripts/Gui/inGame.lua new file mode 100644 index 0000000..78d900a --- /dev/null +++ b/Assets/Scripts/Gui/inGame.lua @@ -0,0 +1,2 @@ +numGui = 0 +gui = {} diff --git a/Assets/Scripts/Gui/main.lua b/Assets/Scripts/Gui/main.lua index c665673..0fd106e 100644 --- a/Assets/Scripts/Gui/main.lua +++ b/Assets/Scripts/Gui/main.lua @@ -17,7 +17,8 @@ ButtonType = { NEW_MAP_OK = 13, LOAD_MAP = 14, LOAD_MAP_OK = 15, - EXPORT = 16 + EXPORT = 16, + PLAY = 17 } ListboxType = { @@ -26,5 +27,7 @@ ListboxType = { MAPS = 2, UNITS = 3, SKYBOX_TEXTURES = 4, - LAND_TEXTURES = 5 + LAND_TEXTURES = 5, + CPU_DIFFICULTIES = 6, + FACTIONS = 7 } diff --git a/Assets/Scripts/Gui/singlePlayerMenu.lua b/Assets/Scripts/Gui/singlePlayerMenu.lua new file mode 100644 index 0000000..196c270 --- /dev/null +++ b/Assets/Scripts/Gui/singlePlayerMenu.lua @@ -0,0 +1,52 @@ +Size = {x = 100, y = 20} + +numGui = 6 +gui = { + { + pos = {x = 100, y = 100}, + size = Size, + guiType = GuiType.LISTBOX, + listboxType = ListboxType.CPU_DIFFICULTIES + }, + { + pos = {x = 210, y = 100}, + size = Size, + guiType = GuiType.LISTBOX, + listboxType = ListboxType.FACTIONS + }, + { + pos = {x = 100, y = 230}, + size = Size, + guiType = GuiType.LISTBOX, + listboxType = ListboxType.FACTIONS + }, + { + pos = {x = 110, y = 310}, + size = Size, + guiType = GuiType.LISTBOX, + listboxType = ListboxType.MAPS, + numMaxDisplay = 5 + }, + { + pos = {x = 110, y = 460}, + size = Size, + guiType = GuiType.BUTTON, + buttonType = ButtonType.PLAY, + name = 'Play', + numDependencies = 4, + dependencies = { + {id = 0}, + {id = 1}, + {id = 2}, + {id = 3} + } + }, + { + pos = {x = 110 + Size.x + 10, y = 460}, + size = Size, + guiType = GuiType.BUTTON, + buttonType = ButtonType.BACK, + name = 'Back', + screen = 'mainMenu.lua' + } +} diff --git a/CMakeLists.txt b/CMakeLists.txt index a2d2e32..153f2ff 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -8,7 +8,7 @@ set(CMAKE_BUILD_TYPE Debug) set(BUILD_TESTS ON) cmake_policy(SET CMP0015 NEW) -set(BUTTONS singlePlayerButton.cpp mapEditorButton.cpp newMapButton.cpp loadMapButton.cpp optionsButton.cpp exitButton.cpp tabButton.cpp okButton.cpp defaultsButton.cpp backButton.cpp consoleCommand.cpp exportButton.cpp) +set(BUTTONS singlePlayerButton.cpp mapEditorButton.cpp newMapButton.cpp loadMapButton.cpp optionsButton.cpp exitButton.cpp tabButton.cpp okButton.cpp defaultsButton.cpp backButton.cpp consoleCommand.cpp exportButton.cpp playButton.cpp) set(LISTBOXES skyboxTextureListbox.cpp landTextureListbox.cpp unitListbox.cpp) set(GUI tooltip.cpp concreteGuiManager.cpp ${BUTTONS} ${LISTBOXES}) diff --git a/concreteGuiManager.cpp b/concreteGuiManager.cpp index 086e635..c0b0305 100644 --- a/concreteGuiManager.cpp +++ b/concreteGuiManager.cpp @@ -16,6 +16,7 @@ #include "skyboxTextureListbox.h" #include "landTextureListbox.h" #include "unitListbox.h" +#include "playButton.h" namespace battleship{ using namespace std; @@ -32,6 +33,7 @@ namespace battleship{ return concreteGuiManager; } + //TODO refactor player difficulty and faction listbox selection Button* ConcreteGuiManager::parseButton(int guiId){ LuaManager *lm = LuaManager::getSingleton(); @@ -115,6 +117,24 @@ namespace battleship{ case EXPORT: button = new ExportButton(pos, size); break; + case PLAY:{ + int did = lm->getIntFromTable(guiTable, vector{Index(guiId + 1), Index("dependencies"), Index(1), Index("id")}); + vector difficultyListboxes = vector{(Listbox*)guiElements[did].second}; + + vector factionsListboxes; + int numPlayers = 2; + + for(int i = 0; i < numPlayers; i++){ + int fid = lm->getIntFromTable(guiTable, vector{Index(guiId + 1), Index("dependencies"), Index(i + 2), Index("id")}); + factionsListboxes.push_back((Listbox*)guiElements[fid].second); + } + + int mid = lm->getIntFromTable(guiTable, vector{Index(guiId + 1), Index("dependencies"), Index(4), Index("id")}); + Listbox *mapListbox = (Listbox*)guiElements[mid].second; + + button = new PlayButton(difficultyListboxes, factionsListboxes, mapListbox, pos, size, name, true); + break; + } } int typeArr[2]{(int)GuiElementType::BUTTON, (int)type}; @@ -216,6 +236,22 @@ namespace battleship{ listbox = new LandTextureListbox(pos, size, lines, maxDisplay, fontPath); break; + case CPU_DIFFICULTIES: + lines = vector{"Easy", "Medium", "Hard"}; + numLines = lines.size(); + closable = true; + maxDisplay = (numLines > numMaxDisplay ? numMaxDisplay : numLines); + + listbox = new Listbox(pos, size, lines, maxDisplay, fontPath); + break; + case FACTIONS: + lines = vector{"Empire", "Mutants", "Shapeshifters"}; + numLines = lines.size(); + closable = true; + maxDisplay = (numLines > numMaxDisplay ? numMaxDisplay : numLines); + + listbox = new Listbox(pos, size, lines, maxDisplay, fontPath); + break; } int typeArr[2]{(int)GuiElementType::LISTBOX, (int)listboxType}; diff --git a/concreteGuiManager.h b/concreteGuiManager.h index 8d7aa6f..c4e8177 100644 --- a/concreteGuiManager.h +++ b/concreteGuiManager.h @@ -26,7 +26,8 @@ namespace battleship{ NEW_MAP_OK, LOAD_MAP, LOAD_MAP_OK, - EXPORT + EXPORT, + PLAY }; enum ListboxType { CONTROLS, @@ -34,7 +35,9 @@ namespace battleship{ MAPS, UNITS, SKYBOX_TEXTURES, - LAND_TEXTURES + LAND_TEXTURES, + CPU_DIFFICULTIES, + FACTIONS }; class ConcreteGuiManager : public vb01Gui::AbstractGuiManager{ diff --git a/playButton.cpp b/playButton.cpp new file mode 100644 index 0000000..0a9d147 --- /dev/null +++ b/playButton.cpp @@ -0,0 +1,38 @@ +#include "playButton.h" +#include "gameManager.h" +#include "inGameAppState.h" +#include "concreteGuiManager.h" + +#include + +#include + +namespace battleship{ + using namespace std; + using namespace vb01; + using namespace vb01Gui; + using namespace gameBase; + + PlayButton::PlayButton(vector difficulties, vector factions, Listbox *mapListbox, Vector2 pos, Vector2 size, string name, bool separate) : Button(pos, size, name, GameManager::getSingleton()->getPath() + "Fonts/batang.ttf", GLFW_KEY_P, separate) { + difficultiesListboxes = difficulties; + factionsListboxes = factions; + this->mapListbox = mapListbox; + } + + void PlayButton::onClick() { + std::vector difficulties, factions; + + for(int i = 0; i < difficultiesListboxes.size(); i++) + difficulties.push_back(wstringToString(difficultiesListboxes[i]->getContents()[difficultiesListboxes[i]->getSelectedOption()])); + + for(int i = 0; i < factionsListboxes.size(); i++) + factions.push_back(to_string(factionsListboxes[i]->getSelectedOption())); + + StateManager *stateManager = GameManager::getSingleton()->getStateManager(); + int selectedMap = mapListbox->getSelectedOption(); + string mapName = wstringToString(mapListbox->getContents()[selectedMap]); + stateManager->attachAppState(new InGameAppState(difficulties, factions, mapName)); + + ConcreteGuiManager::getSingleton()->readLuaScreenScript("inGame.lua"); + } +} diff --git a/playButton.h b/playButton.h new file mode 100644 index 0000000..f2da5ae --- /dev/null +++ b/playButton.h @@ -0,0 +1,22 @@ +#ifndef PLAY_BUTTON_H +#define PLAY_BUTTON_H + +#include + +namespace vb01Gui{ + class Listbox; +} + +namespace battleship{ + class PlayButton : public vb01Gui::Button { + public: + PlayButton(std::vector, std::vector, vb01Gui::Listbox*, vb01::Vector2, vb01::Vector2, std::string, bool); + void onClick(); + private: + std::vector difficultiesListboxes, factionsListboxes; + vb01Gui::Listbox *mapListbox = nullptr; + int lengths[2]; + }; +} + +#endif diff --git a/singlePlayerButton.cpp b/singlePlayerButton.cpp index a1c5a47..f5acb85 100644 --- a/singlePlayerButton.cpp +++ b/singlePlayerButton.cpp @@ -1,6 +1,7 @@ #include "singlePlayerButton.h" #include "gameManager.h" #include "inGameAppState.h" +#include "concreteGuiManager.h" #include @@ -28,45 +29,8 @@ namespace battleship{ void SinglePlayerButton::onClick() { vector difficulties, factions; - class PlayButton : public Button { - public: - PlayButton(Listbox **difficulties, Listbox **factions, Listbox *mapListbox, int lengths[2], Vector2 pos, Vector2 size, string name, bool separate) : Button(pos, size, name, GameManager::getSingleton()->getPath() + "Fonts/batang.ttf", GLFW_KEY_P, separate) { - this->state = ((GuiAppState*)GameManager::getSingleton()->getStateManager()->getAppStateByType(AppStateType::GUI_STATE)); - this->lengths[0] = lengths[0]; - this->lengths[1] = lengths[1]; - difficultiesListboxes = difficulties; - factionsListboxes = factions; - this->mapListbox = mapListbox; - } - - void onClick() { - GameManager *gm = GameManager::getSingleton(); - std::vector difficulties, factions; - - for(int i = 0; i < lengths[0]; i++) - difficulties.push_back(wstringToString(difficultiesListboxes[i]->getContents()[difficultiesListboxes[i]->getSelectedOption()])); - - for(int i = 0; i < lengths[1]; i++) - factions.push_back(to_string(factionsListboxes[i]->getSelectedOption())); - - - delete[] difficultiesListboxes; - delete[] factionsListboxes; - - StateManager *stateManager = gm->getStateManager(); - int selectedMap = mapListbox->getSelectedOption(); - string mapName = wstringToString(mapListbox->getContents()[selectedMap]); - stateManager->attachAppState(new InGameAppState(difficulties, factions, mapName)); - state->removeAllListboxes(); - state->removeButton("Back"); - state->removeButton("Play"); - } - private: - GuiAppState *state; - Listbox **difficultiesListboxes, **factionsListboxes, *mapListbox = nullptr; - int lengths[2]; - }; + /* class ReturnButton : public Button { public: @@ -87,8 +51,7 @@ namespace battleship{ private: GuiAppState *state; }; - - /* + GameManager *gm = GameManager::getSingleton(); Vector2 pos(gm->getWidth() / 8, gm->getHeight() / 8); difficulties.push_back("Easy"); @@ -127,6 +90,6 @@ namespace battleship{ state->removeButton("Exit"); state->removeButton("Singleplayer"); */ - + ConcreteGuiManager::getSingleton()->readLuaScreenScript("singlePlayerMenu.lua"); } }