loading player data from the single player menu to load the game

This commit is contained in:
devZoGok
2025-03-06 20:19:02 +02:00
parent f787ba49b3
commit 48c38e9ec9
12 changed files with 123 additions and 115 deletions
+4 -2
View File
@@ -52,6 +52,8 @@ ListboxType = {
LAND_TEXTURES = 7, LAND_TEXTURES = 7,
CPU_DIFFICULTIES = 8, CPU_DIFFICULTIES = 8,
FACTIONS = 9, FACTIONS = 9,
CONSOLE = 10, COLORS = 10,
TRADE_OFFERS = 11 TEAMS = 11,
CONSOLE = 12,
TRADE_OFFERS = 13
} }
+30 -4
View File
@@ -1,23 +1,49 @@
playerListboxPos = {x = 110, y = 10, z = 0}
Pos = {x = playerListboxPos.x, y = playerListboxPos.y, z = playerListboxPos.z} 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"} difficulties = {"Easy", "Medium", "Hard"}
space = 10 colors = {'White', 'Black', 'Red'}
teams = {'0', '1', '2', '3', '4'}
gui = { 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, size = Size,
guiType = GuiType.LISTBOX, guiType = GuiType.LISTBOX,
name = 'factions',
numMaxDisplay = 3, numMaxDisplay = 3,
lines = factions, lines = factions,
listboxType = ListboxType.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, size = Size,
guiType = GuiType.LISTBOX, guiType = GuiType.LISTBOX,
name = 'difficulties',
numMaxDisplay = 3, numMaxDisplay = 3,
lines = difficulties, lines = difficulties,
listboxType = ListboxType.CPU_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
},
} }
+1 -17
View File
@@ -1,17 +1,6 @@
Size = {x = 100, y = 20} Size = {x = 100, y = 20}
playerListboxPos = {x = 110, y = 10, z = 0}
factions = {"America inc.", "European Republic", "Asian Co-Prosperity Sphere"}
numGui = 6
gui = { gui = {
{
pos = playerListboxPos,
size = Size,
guiType = GuiType.LISTBOX,
numMaxDisplay = 3,
lines = factions,
listboxType = ListboxType.FACTIONS
},
{ {
pos = {x = 110, y = 310, z = 0}, pos = {x = 110, y = 310, z = 0},
size = Size, size = Size,
@@ -27,12 +16,7 @@ gui = {
buttonType = ButtonType.PLAY, buttonType = ButtonType.PLAY,
name = 'Play', name = 'Play',
numDependencies = 4, numDependencies = 4,
dependencies = { dependencies = {{id = 0}}
{id = 0},
{id = 1},
{id = 2},
{id = 3}
}
}, },
{ {
pos = {x = 110 + Size.x + 10, y = 460, z = 0}, pos = {x = 110 + Size.x + 10, y = 460, z = 0},
+10 -20
View File
@@ -156,21 +156,9 @@ namespace battleship{
button = new ExportButton(pos, size); button = new ExportButton(pos, size);
break; break;
case PLAY:{ case PLAY:{
int did = guiTable["dependencies"][1]["id"]; int mid = guiTable["dependencies"][1]["id"];
vector<Listbox*> difficultyListboxes = vector<Listbox*>{(Listbox*)guiElements[did].second};
vector<Listbox*> 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"];
Listbox *mapListbox = (MapListbox*)guiElements[mid].second; Listbox *mapListbox = (MapListbox*)guiElements[mid].second;
button = new PlayButton(mapListbox, pos, size, name, true);
button = new PlayButton(difficultyListboxes, factionsListboxes, mapListbox, pos, size, name, true);
break; break;
} }
case RESUME: case RESUME:
@@ -308,6 +296,10 @@ namespace battleship{
Listbox *listbox = nullptr; Listbox *listbox = nullptr;
string fontPath = fontBasePath + "batang.ttf"; string fontPath = fontBasePath + "batang.ttf";
sol::optional<string> nameOpt = guiTable["name"];
string name = "";
if(nameOpt != sol::nullopt) name = guiTable["name"];
switch(listboxType){ switch(listboxType){
case CONTROLS:{ case CONTROLS:{
@@ -389,13 +381,9 @@ namespace battleship{
listbox = new LandTextureListbox(pos, size, lines, maxDisplay, fontPath); listbox = new LandTextureListbox(pos, size, lines, maxDisplay, fontPath);
break; break;
case CPU_DIFFICULTIES: 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 FACTIONS:
case COLORS:
case TEAMS:
numLines = lines.size(); numLines = lines.size();
closable = true; closable = true;
maxDisplay = (numLines > numMaxDisplay ? numMaxDisplay : numLines); maxDisplay = (numLines > numMaxDisplay ? numMaxDisplay : numLines);
@@ -421,6 +409,8 @@ namespace battleship{
int typeArr[2]{(int)GuiElementType::LISTBOX, (int)listboxType}; int typeArr[2]{(int)GuiElementType::LISTBOX, (int)listboxType};
guiElements.push_back(make_pair(typeArr, (void*)listbox)); guiElements.push_back(make_pair(typeArr, (void*)listbox));
listbox->setName(name);
return listbox; return listbox;
} }
+2
View File
@@ -65,6 +65,8 @@ namespace battleship{
LAND_TEXTURES, LAND_TEXTURES,
CPU_DIFFICULTIES, CPU_DIFFICULTIES,
FACTIONS, FACTIONS,
COLORS,
TEAMS,
CONSOLE, CONSOLE,
TRADE_OFFERS TRADE_OFFERS
}; };
+4 -26
View File
@@ -45,39 +45,17 @@ namespace battleship{
Console::execute(wstringToString(textbox->getText())); Console::execute(wstringToString(textbox->getText()));
} }
InGameAppState::InGameAppState(vector<string> difficultyLevels, vector<string> factions) : AbstractAppState( InGameAppState::InGameAppState() : AbstractAppState(
AppStateType::IN_GAME_STATE, AppStateType::IN_GAME_STATE,
configData::calcSumBinds(AppStateType::IN_GAME_STATE, true), configData::calcSumBinds(AppStateType::IN_GAME_STATE, true),
configData::calcSumBinds(AppStateType::IN_GAME_STATE, false), configData::calcSumBinds(AppStateType::IN_GAME_STATE, false),
GameManager::getSingleton()->getPath() + scripts[(int)ScriptFiles::OPTIONS]){ GameManager::getSingleton()->getPath() + scripts[(int)ScriptFiles::OPTIONS]
this->playerId = 1; ),
this->difficultyLevels = difficultyLevels; playerId(0){}
this->factions = factions;
}
InGameAppState::~InGameAppState() {
}
void InGameAppState::onAttached() { void InGameAppState::onAttached() {
AbstractAppState::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(); Camera *cam = Root::getSingleton()->getCamera();
cam->setPosition(Map::getSingleton()->getSpawnPoint(playerId - 1) + Vector3(1, 1, 1) * configData::CAMERA_DISTANCE); 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()); cam->lookAt(Vector3(0, -1, -1).norm(), Vector3(0, 1, -1).norm());
+3 -3
View File
@@ -39,8 +39,8 @@ namespace battleship{
private: private:
}; };
InGameAppState(std::vector<std::string>, std::vector<std::string>); InGameAppState();
~InGameAppState(); ~InGameAppState(){}
void onAttached(); void onAttached();
void onDettached(); void onDettached();
void update(); void update();
@@ -50,7 +50,7 @@ namespace battleship{
inline ActiveGameState* getActiveState(){return activeState;} inline ActiveGameState* getActiveState(){return activeState;}
private: private:
bool isMainMenuActive = false; bool isMainMenuActive = false;
std::vector<std::string> difficultyLevels, factions, modelPaths; std::vector<std::string> modelPaths;
Player *mainPlayer; Player *mainPlayer;
int playerId; int playerId;
ActiveGameState* activeState; ActiveGameState* activeState;
+1 -1
View File
@@ -384,7 +384,7 @@ namespace battleship{
void Map::loadSpawnPoints(){ void Map::loadSpawnPoints(){
sol::state_view SOL_LUA_VIEW = generateView(); sol::state_view SOL_LUA_VIEW = generateView();
int numSpawnPoints = getNumSpawnPoints(); int numSpawnPoints = getNumMapSpawnPoints();
for(int i = 0; i < numSpawnPoints; i++){ for(int i = 0; i < numSpawnPoints; i++){
sol::table posTable = SOL_LUA_VIEW[mapTable]["spawnPoints"][i + 1]; sol::table posTable = SOL_LUA_VIEW[mapTable]["spawnPoints"][i + 1];
+3 -6
View File
@@ -20,10 +20,7 @@ namespace battleship{
MapListbox::MapListbox(Vector3 pos, Vector2 size, std::vector<string> &lines, int maxDisplay, bool adg, string fontPath, bool closeable) : MapListbox::MapListbox(Vector3 pos, Vector2 size, std::vector<string> &lines, int maxDisplay, bool adg, string fontPath, bool closeable) :
Listbox(pos, size, lines, maxDisplay, fontPath, closeable), Listbox(pos, size, lines, maxDisplay, fontPath, closeable),
addPlayerGui(adg) { addPlayerGui(adg) {}
openUp();
close();
}
void MapListbox::onClose(){ void MapListbox::onClose(){
if(addPlayerGui){ if(addPlayerGui){
@@ -38,14 +35,14 @@ namespace battleship{
int numSpawnPoints = Map::getSingleton()->getNumMapSpawnPoints(mapName); int numSpawnPoints = Map::getSingleton()->getNumMapSpawnPoints(mapName);
sol::state_view SOL_LUA_VIEW = generateView(); 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(); int prevNumListboxes = guiManager->getListboxes().size();
SOL_LUA_VIEW.script("lineId = " + to_string(i)); SOL_LUA_VIEW.script("lineId = " + to_string(i));
guiManager->parseLuaScript("playerSelection.lua"); guiManager->parseLuaScript("playerSelection.lua");
std::vector<Listbox*> listboxes = guiManager->getListboxes(); std::vector<Listbox*> listboxes = guiManager->getListboxes();
int diffNumListboxes = guiManager->getListboxes().size() - prevNumListboxes; int diffNumListboxes = listboxes.size() - prevNumListboxes;
for(int j = 0; j < diffNumListboxes; j++) for(int j = 0; j < diffNumListboxes; j++)
cpuPlayerListboxes.push_back(listboxes[listboxes.size() - 1 - j]); cpuPlayerListboxes.push_back(listboxes[listboxes.size() - 1 - j]);
+58 -33
View File
@@ -14,42 +14,67 @@ namespace battleship{
using namespace vb01Gui; using namespace vb01Gui;
using namespace gameBase; using namespace gameBase;
PlayButton::PlayButton(vector<Listbox*> difficulties, vector<Listbox*> 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) { 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) {}
difficultiesListboxes = difficulties;
factionsListboxes = factions;
this->mapListbox = mapListbox;
}
void PlayButton::onClick() { void PlayButton::onClick() {
std::vector<string> difficulties, factions;
for(int i = 0; i < difficultiesListboxes.size(); i++) ConcreteGuiManager *guiManager = ConcreteGuiManager::getSingleton();
difficulties.push_back(wstringToString(difficultiesListboxes[i]->getContents()[difficultiesListboxes[i]->getSelectedOption()])); vector<Listbox*> 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++) for(int i = 0; i < numPlayers; i++){
factions.push_back(to_string(factionsListboxes[i]->getSelectedOption())); int factionChoice = factionsListboxes[i]->getSelectedOption();
Game *game = Game::getSingleton(); if(factionChoice == 0) continue;
game->initTechnologies();
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(); map->loadPlayerGameObjects();
string mapName = wstringToString(mapListbox->getContents()[selectedMap]);
Map *map = Map::getSingleton(); guiManager->readLuaScreenScript("inGame.lua");
map->load(mapName);
StateManager *stateManager = GameManager::getSingleton()->getStateManager();
int numPlayers = map->getNumSpawnPoints(); stateManager->attachAppState(new InGameAppState());
}
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));
}
} }
+1 -3
View File
@@ -10,12 +10,10 @@ namespace vb01Gui{
namespace battleship{ namespace battleship{
class PlayButton : public vb01Gui::Button { class PlayButton : public vb01Gui::Button {
public: public:
PlayButton(std::vector<vb01Gui::Listbox*>, std::vector<vb01Gui::Listbox*>, vb01Gui::Listbox*, vb01::Vector3, vb01::Vector2, std::string, bool); PlayButton(vb01Gui::Listbox*, vb01::Vector3, vb01::Vector2, std::string, bool);
void onClick(); void onClick();
private: private:
std::vector<vb01Gui::Listbox*> difficultiesListboxes, factionsListboxes;
vb01Gui::Listbox *mapListbox = nullptr; vb01Gui::Listbox *mapListbox = nullptr;
int lengths[2];
}; };
} }
+6
View File
@@ -2,6 +2,8 @@
#include "gameManager.h" #include "gameManager.h"
#include "concreteGuiManager.h" #include "concreteGuiManager.h"
#include <listbox.h>
#include <glfw3.h> #include <glfw3.h>
namespace battleship{ namespace battleship{
@@ -21,5 +23,9 @@ namespace battleship{
void SinglePlayerButton::onClick() { void SinglePlayerButton::onClick() {
ConcreteGuiManager::getSingleton()->readLuaScreenScript("singlePlayerMenu.lua"); ConcreteGuiManager::getSingleton()->readLuaScreenScript("singlePlayerMenu.lua");
Listbox *listbox = ConcreteGuiManager::getSingleton()->getListboxes()[0];
listbox->openUp();
listbox->close();
} }
} }