From 48c38e9ec93367c490cc2da1e0a3b7e848549465 Mon Sep 17 00:00:00 2001 From: devZoGok Date: Thu, 6 Mar 2025 20:19:02 +0200 Subject: [PATCH] loading player data from the single player menu to load the game --- Assets/Scripts/Gui/main.lua | 6 +- Assets/Scripts/Gui/playerSelection.lua | 34 +++++++-- Assets/Scripts/Gui/singlePlayerMenu.lua | 18 +---- concreteGuiManager.cpp | 30 +++----- concreteGuiManager.h | 2 + inGameAppState.cpp | 30 ++------ inGameAppState.h | 6 +- map.cpp | 2 +- mapListbox.cpp | 9 +-- playButton.cpp | 91 ++++++++++++++++--------- playButton.h | 4 +- singlePlayerButton.cpp | 6 ++ 12 files changed, 123 insertions(+), 115 deletions(-) diff --git a/Assets/Scripts/Gui/main.lua b/Assets/Scripts/Gui/main.lua index 9075486..0affb41 100644 --- a/Assets/Scripts/Gui/main.lua +++ b/Assets/Scripts/Gui/main.lua @@ -52,6 +52,8 @@ ListboxType = { LAND_TEXTURES = 7, CPU_DIFFICULTIES = 8, FACTIONS = 9, - CONSOLE = 10, - TRADE_OFFERS = 11 + COLORS = 10, + TEAMS = 11, + CONSOLE = 12, + TRADE_OFFERS = 13 } diff --git a/Assets/Scripts/Gui/playerSelection.lua b/Assets/Scripts/Gui/playerSelection.lua index a81fde2..20bc65d 100644 --- a/Assets/Scripts/Gui/playerSelection.lua +++ b/Assets/Scripts/Gui/playerSelection.lua @@ -1,23 +1,49 @@ +playerListboxPos = {x = 110, y = 10, z = 0} Pos = {x = playerListboxPos.x, y = playerListboxPos.y, z = playerListboxPos.z} -Size = {x = 100, y = 20} +Size = {x = 150, y = 20} +gap = 10 +gapFactor = 3 + +factions = {'None', "America inc.", "European Republic", "Asian Co-Prosperity Sphere"} difficulties = {"Easy", "Medium", "Hard"} -space = 10 +colors = {'White', 'Black', 'Red'} +teams = {'0', '1', '2', '3', '4'} gui = { { - pos = {x = Pos.x, y = Pos.y + (#factions * Size.y + space) * (lineId + 1), z = Pos.z}, + pos = {x = Pos.x, y = Pos.y + (gapFactor * Size.y + gap) * (lineId + 1), z = Pos.z}, size = Size, guiType = GuiType.LISTBOX, + name = 'factions', numMaxDisplay = 3, lines = factions, listboxType = ListboxType.FACTIONS }, { - pos = {x = Pos.x + Size.x + space, y = Pos.y + (#difficulties * Size.y + space) * (lineId + 1), z = Pos.z}, + pos = {x = Pos.x + Size.x + gap, y = Pos.y + (gapFactor * Size.y + gap) * (lineId + 1), z = Pos.z}, size = Size, guiType = GuiType.LISTBOX, + name = 'difficulties', numMaxDisplay = 3, lines = difficulties, listboxType = ListboxType.CPU_DIFFICULTIES }, + { + pos = {x = Pos.x + 2 * (Size.x + gap), y = Pos.y + (gapFactor * Size.y + gap) * (lineId + 1), z = Pos.z}, + size = Size, + guiType = GuiType.LISTBOX, + name = 'colors', + numMaxDisplay = 3, + lines = colors, + listboxType = ListboxType.COLORS + }, + { + pos = {x = Pos.x + 3 * (Size.x + gap), y = Pos.y + (gapFactor * Size.y + gap) * (lineId + 1), z = Pos.z}, + size = Size, + guiType = GuiType.LISTBOX, + name = 'teams', + numMaxDisplay = 3, + lines = teams, + listboxType = ListboxType.TEAMS + }, } diff --git a/Assets/Scripts/Gui/singlePlayerMenu.lua b/Assets/Scripts/Gui/singlePlayerMenu.lua index 3fdd312..3653569 100644 --- a/Assets/Scripts/Gui/singlePlayerMenu.lua +++ b/Assets/Scripts/Gui/singlePlayerMenu.lua @@ -1,17 +1,6 @@ Size = {x = 100, y = 20} -playerListboxPos = {x = 110, y = 10, z = 0} -factions = {"America inc.", "European Republic", "Asian Co-Prosperity Sphere"} -numGui = 6 gui = { - { - pos = playerListboxPos, - size = Size, - guiType = GuiType.LISTBOX, - numMaxDisplay = 3, - lines = factions, - listboxType = ListboxType.FACTIONS - }, { pos = {x = 110, y = 310, z = 0}, size = Size, @@ -27,12 +16,7 @@ gui = { buttonType = ButtonType.PLAY, name = 'Play', numDependencies = 4, - dependencies = { - {id = 0}, - {id = 1}, - {id = 2}, - {id = 3} - } + dependencies = {{id = 0}} }, { pos = {x = 110 + Size.x + 10, y = 460, z = 0}, diff --git a/concreteGuiManager.cpp b/concreteGuiManager.cpp index 9b04a87..6527717 100644 --- a/concreteGuiManager.cpp +++ b/concreteGuiManager.cpp @@ -156,21 +156,9 @@ namespace battleship{ button = new ExportButton(pos, size); break; case PLAY:{ - int did = guiTable["dependencies"][1]["id"]; - vector difficultyListboxes = vector{(Listbox*)guiElements[did].second}; - - vector factionsListboxes; - int numPlayers = 2; - - for(int i = 0; i < numPlayers; i++){ - int did = guiTable["dependencies"][i + 2]["id"]; - factionsListboxes.push_back((Listbox*)guiElements[did].second); - } - - int mid = guiTable["dependencies"][4]["id"]; + int mid = guiTable["dependencies"][1]["id"]; Listbox *mapListbox = (MapListbox*)guiElements[mid].second; - - button = new PlayButton(difficultyListboxes, factionsListboxes, mapListbox, pos, size, name, true); + button = new PlayButton(mapListbox, pos, size, name, true); break; } case RESUME: @@ -308,6 +296,10 @@ namespace battleship{ Listbox *listbox = nullptr; string fontPath = fontBasePath + "batang.ttf"; + sol::optional nameOpt = guiTable["name"]; + string name = ""; + + if(nameOpt != sol::nullopt) name = guiTable["name"]; switch(listboxType){ case CONTROLS:{ @@ -389,13 +381,9 @@ namespace battleship{ listbox = new LandTextureListbox(pos, size, lines, maxDisplay, fontPath); break; case CPU_DIFFICULTIES: - numLines = lines.size(); - closable = true; - maxDisplay = (numLines > numMaxDisplay ? numMaxDisplay : numLines); - - listbox = new Listbox(pos, size, lines, maxDisplay, fontPath); - break; case FACTIONS: + case COLORS: + case TEAMS: numLines = lines.size(); closable = true; maxDisplay = (numLines > numMaxDisplay ? numMaxDisplay : numLines); @@ -421,6 +409,8 @@ namespace battleship{ int typeArr[2]{(int)GuiElementType::LISTBOX, (int)listboxType}; guiElements.push_back(make_pair(typeArr, (void*)listbox)); + listbox->setName(name); + return listbox; } diff --git a/concreteGuiManager.h b/concreteGuiManager.h index 45c24e5..1f0dec5 100644 --- a/concreteGuiManager.h +++ b/concreteGuiManager.h @@ -65,6 +65,8 @@ namespace battleship{ LAND_TEXTURES, CPU_DIFFICULTIES, FACTIONS, + COLORS, + TEAMS, CONSOLE, TRADE_OFFERS }; diff --git a/inGameAppState.cpp b/inGameAppState.cpp index 26a4c23..ea83225 100755 --- a/inGameAppState.cpp +++ b/inGameAppState.cpp @@ -45,39 +45,17 @@ namespace battleship{ Console::execute(wstringToString(textbox->getText())); } - InGameAppState::InGameAppState(vector difficultyLevels, vector factions) : AbstractAppState( + InGameAppState::InGameAppState() : AbstractAppState( AppStateType::IN_GAME_STATE, configData::calcSumBinds(AppStateType::IN_GAME_STATE, true), configData::calcSumBinds(AppStateType::IN_GAME_STATE, false), - GameManager::getSingleton()->getPath() + scripts[(int)ScriptFiles::OPTIONS]){ - this->playerId = 1; - this->difficultyLevels = difficultyLevels; - this->factions = factions; - } - - InGameAppState::~InGameAppState() { - } + GameManager::getSingleton()->getPath() + scripts[(int)ScriptFiles::OPTIONS] + ), + playerId(0){} void InGameAppState::onAttached() { AbstractAppState::onAttached(); - for (int i = 0; i < factions.size(); i++) { - int faction, difficulty; - - if (i == 0) - difficulty = -1; - else { - if (difficultyLevels[i - 1] == "Easy") - difficulty = 0; - else if (difficultyLevels[i - 1] == "Medium") - difficulty = 1; - else - difficulty = 2; - } - - faction = factions[i][0] - 48; - } - Camera *cam = Root::getSingleton()->getCamera(); cam->setPosition(Map::getSingleton()->getSpawnPoint(playerId - 1) + Vector3(1, 1, 1) * configData::CAMERA_DISTANCE); cam->lookAt(Vector3(0, -1, -1).norm(), Vector3(0, 1, -1).norm()); diff --git a/inGameAppState.h b/inGameAppState.h index 0bb77e4..811b345 100755 --- a/inGameAppState.h +++ b/inGameAppState.h @@ -39,8 +39,8 @@ namespace battleship{ private: }; - InGameAppState(std::vector, std::vector); - ~InGameAppState(); + InGameAppState(); + ~InGameAppState(){} void onAttached(); void onDettached(); void update(); @@ -50,7 +50,7 @@ namespace battleship{ inline ActiveGameState* getActiveState(){return activeState;} private: bool isMainMenuActive = false; - std::vector difficultyLevels, factions, modelPaths; + std::vector modelPaths; Player *mainPlayer; int playerId; ActiveGameState* activeState; diff --git a/map.cpp b/map.cpp index fde0027..50c268e 100755 --- a/map.cpp +++ b/map.cpp @@ -384,7 +384,7 @@ namespace battleship{ void Map::loadSpawnPoints(){ sol::state_view SOL_LUA_VIEW = generateView(); - int numSpawnPoints = getNumSpawnPoints(); + int numSpawnPoints = getNumMapSpawnPoints(); for(int i = 0; i < numSpawnPoints; i++){ sol::table posTable = SOL_LUA_VIEW[mapTable]["spawnPoints"][i + 1]; diff --git a/mapListbox.cpp b/mapListbox.cpp index 54f05f1..19338a2 100644 --- a/mapListbox.cpp +++ b/mapListbox.cpp @@ -20,10 +20,7 @@ namespace battleship{ MapListbox::MapListbox(Vector3 pos, Vector2 size, std::vector &lines, int maxDisplay, bool adg, string fontPath, bool closeable) : Listbox(pos, size, lines, maxDisplay, fontPath, closeable), - addPlayerGui(adg) { - openUp(); - close(); - } + addPlayerGui(adg) {} void MapListbox::onClose(){ if(addPlayerGui){ @@ -38,14 +35,14 @@ namespace battleship{ int numSpawnPoints = Map::getSingleton()->getNumMapSpawnPoints(mapName); sol::state_view SOL_LUA_VIEW = generateView(); - for(int i = 0; i < numSpawnPoints - 1; i++){ + for(int i = 0; i < numSpawnPoints; i++){ int prevNumListboxes = guiManager->getListboxes().size(); SOL_LUA_VIEW.script("lineId = " + to_string(i)); guiManager->parseLuaScript("playerSelection.lua"); std::vector listboxes = guiManager->getListboxes(); - int diffNumListboxes = guiManager->getListboxes().size() - prevNumListboxes; + int diffNumListboxes = listboxes.size() - prevNumListboxes; for(int j = 0; j < diffNumListboxes; j++) cpuPlayerListboxes.push_back(listboxes[listboxes.size() - 1 - j]); diff --git a/playButton.cpp b/playButton.cpp index c3457ea..a91bc52 100644 --- a/playButton.cpp +++ b/playButton.cpp @@ -14,42 +14,67 @@ namespace battleship{ using namespace vb01Gui; using namespace gameBase; - PlayButton::PlayButton(vector difficulties, vector factions, Listbox *mapListbox, Vector3 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; - } + PlayButton::PlayButton(Listbox *ml, Vector3 pos, Vector2 size, string name, bool separate) : Button(pos, size, name, GameManager::getSingleton()->getPath() + "Fonts/batang.ttf", GLFW_KEY_P, separate), mapListbox(ml) {} - void PlayButton::onClick() { - std::vector difficulties, factions; + void PlayButton::onClick() { - for(int i = 0; i < difficultiesListboxes.size(); i++) - difficulties.push_back(wstringToString(difficultiesListboxes[i]->getContents()[difficultiesListboxes[i]->getSelectedOption()])); + ConcreteGuiManager *guiManager = ConcreteGuiManager::getSingleton(); + vector factionsListboxes, difficultiesListboxes, colorsListboxes, teamsListboxes; + + for(Listbox *listbox : guiManager->getListboxes()){ + string name = listbox->getName(); + + if(name == "factions") factionsListboxes.push_back(listbox); + else if(name == "difficulties") difficultiesListboxes.push_back(listbox); + else if(name == "colors") colorsListboxes.push_back(listbox); + else if(name == "teams") teamsListboxes.push_back(listbox); + } + + Game *game = Game::getSingleton(); + game->initTechnologies(); + + int selectedMap = mapListbox->getSelectedOption(); + string mapName = wstringToString(mapListbox->getContents()[selectedMap]); + + Map *map = Map::getSingleton(); + map->load(mapName); + int numPlayers = map->getNumSpawnPoints(); - for(int i = 0; i < factionsListboxes.size(); i++) - factions.push_back(to_string(factionsListboxes[i]->getSelectedOption())); + for(int i = 0; i < numPlayers; i++){ + int factionChoice = factionsListboxes[i]->getSelectedOption(); - Game *game = Game::getSingleton(); - game->initTechnologies(); + if(factionChoice == 0) continue; + + int faction = factionChoice - 1; + + bool cpuPlayer = (i > 0); + string diffStr = (cpuPlayer ? wstringToString(difficultiesListboxes[i]->getContents()[colorsListboxes[i]->getSelectedOption()]) : ""); + int difficulty = -1; + + if(diffStr == "Easy") difficulty = 0; + else if(diffStr == "Medium") difficulty = 1; + else if(diffStr == "Hard") difficulty = 2; + + string colorStr = wstringToString(colorsListboxes[i]->getContents()[colorsListboxes[i]->getSelectedOption()]); + Vector3 color; + + if(colorStr == "Black") color = Vector3::VEC_ZERO; + else if(colorStr == "Red") color = Vector3::VEC_I; + else if(colorStr == "Green") color = Vector3::VEC_J; + else if(colorStr == "Blue") color = Vector3::VEC_K; + else if(colorStr == "White") color = Vector3::VEC_IJK; + + int team = stoi(teamsListboxes[i]->getContents()[teamsListboxes[i]->getSelectedOption()]); + + string name = (cpuPlayer ? "CPU player #" + to_string(i) : "Player"); + game->addPlayer(new Player(difficulty, faction, team, color, cpuPlayer, map->getSpawnPoint(i), name)); + } - int selectedMap = mapListbox->getSelectedOption(); - string mapName = wstringToString(mapListbox->getContents()[selectedMap]); - Map *map = Map::getSingleton(); - map->load(mapName); - - int numPlayers = map->getNumSpawnPoints(); - - for(int i = 0; i < numPlayers; i++){ - bool cpuPlayer = (i < numPlayers - 1); - string name = (cpuPlayer ? "CPU player #" + to_string(i) : "Player"); - game->addPlayer(new Player(0, 0, i, Vector3(i, i, i), cpuPlayer, map->getSpawnPoint(i), name)); - } - - map->loadPlayerGameObjects(); - - ConcreteGuiManager::getSingleton()->readLuaScreenScript("inGame.lua"); - - StateManager *stateManager = GameManager::getSingleton()->getStateManager(); - stateManager->attachAppState(new InGameAppState(difficulties, factions)); - } + map->loadPlayerGameObjects(); + + guiManager->readLuaScreenScript("inGame.lua"); + + StateManager *stateManager = GameManager::getSingleton()->getStateManager(); + stateManager->attachAppState(new InGameAppState()); + } } diff --git a/playButton.h b/playButton.h index 2c2565a..d265cdc 100644 --- a/playButton.h +++ b/playButton.h @@ -10,12 +10,10 @@ namespace vb01Gui{ namespace battleship{ class PlayButton : public vb01Gui::Button { public: - PlayButton(std::vector, std::vector, vb01Gui::Listbox*, vb01::Vector3, vb01::Vector2, std::string, bool); + PlayButton(vb01Gui::Listbox*, vb01::Vector3, vb01::Vector2, std::string, bool); void onClick(); private: - std::vector difficultiesListboxes, factionsListboxes; vb01Gui::Listbox *mapListbox = nullptr; - int lengths[2]; }; } diff --git a/singlePlayerButton.cpp b/singlePlayerButton.cpp index f0ff156..ef29559 100644 --- a/singlePlayerButton.cpp +++ b/singlePlayerButton.cpp @@ -2,6 +2,8 @@ #include "gameManager.h" #include "concreteGuiManager.h" +#include + #include namespace battleship{ @@ -21,5 +23,9 @@ namespace battleship{ void SinglePlayerButton::onClick() { ConcreteGuiManager::getSingleton()->readLuaScreenScript("singlePlayerMenu.lua"); + + Listbox *listbox = ConcreteGuiManager::getSingleton()->getListboxes()[0]; + listbox->openUp(); + listbox->close(); } }