From b895a85378c4e9e7a36a46e9072db923ad132846 Mon Sep 17 00:00:00 2001 From: devZoGok Date: Sun, 8 Oct 2023 15:08:45 +0300 Subject: [PATCH] implemented central Game class --- Assets/Models/Maps/111/111.lua | 6 ++++-- CMakeLists.txt | 2 +- activeGameState.cpp | 7 ++++--- addResourceCommand.cpp | 4 ++-- addUnitCommand.cpp | 3 ++- explosion.cpp | 25 +++++++++++++------------ explosion.h | 9 +++------ fx.h | 17 +++++++++++++++++ game.cpp | 25 +++++++++++++++++++++++++ game.h | 33 +++++++++++++++++++++++++++++++++ gameObjectFactory.cpp | 4 ++-- gameObjectFactory.h | 2 +- inGameAppState.cpp | 18 ++++++------------ inGameAppState.h | 13 +------------ map.cpp | 15 +++++---------- map.h | 14 +++----------- mapEditorAppState.cpp | 22 ++++++++++------------ mapEditorAppState.h | 4 ++++ playButton.cpp | 17 +++++++++++++++-- player.cpp | 19 +++++++++---------- player.h | 9 +++++++-- projectile.cpp | 10 +++++----- resourceDeposit.cpp | 2 +- resourceDeposit.h | 2 +- unit.cpp | 16 ---------------- unit.h | 1 - 26 files changed, 174 insertions(+), 125 deletions(-) create mode 100644 fx.h create mode 100644 game.cpp create mode 100644 game.h diff --git a/Assets/Models/Maps/111/111.lua b/Assets/Models/Maps/111/111.lua index 3e0cc3d..4e198c3 100644 --- a/Assets/Models/Maps/111/111.lua +++ b/Assets/Models/Maps/111/111.lua @@ -9,7 +9,9 @@ spawnPoints = { }, players = { { -spawnPoint = 0, +numResourceDeposits = 0, +resourceDeposits = { +}, numUnits = 0, } }, @@ -332,4 +334,4 @@ skybox = "Skybox1", terrain = {model = "111.xml", albedo = "111.jpg"}, waterbodies = { {pos = {x = 7.296803, y = -0.948949, z = 6.370515}, size = {x = 42.873222, y = 42.948956}, albedo = "water.jpg"},} -} \ No newline at end of file +} diff --git a/CMakeLists.txt b/CMakeLists.txt index 8c565ac..f502891 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -20,7 +20,7 @@ set(STATES activeGameState.cpp inGameAppState.cpp guiAppState.cpp mapEditorAppSt set(UTIL util.cpp binds.h) set(CONTROLLERS gameObjectFrameController.cpp cameraController.cpp) set(CONSOLE console.cpp abstractCommand.cpp addUnitCommand.cpp addResourceCommand.cpp) -set(CORE gameManager.cpp defConfigs.cpp ${CONSOLE} ${CONTROLLERS}) +set(CORE gameManager.cpp game.cpp defConfigs.cpp ${CONSOLE} ${CONTROLLERS}) set(PROJECTILES projectile.cpp) set(FX explosion.cpp) set(VEHICLES vehicle.cpp engineer.cpp) diff --git a/activeGameState.cpp b/activeGameState.cpp index 1c0ad9e..79c98d1 100755 --- a/activeGameState.cpp +++ b/activeGameState.cpp @@ -18,6 +18,7 @@ #include "inGameAppState.h" #include "defConfigs.h" #include "structure.h" +#include "game.h" #include "util.h" #include "tooltip.h" #include "gameObjectFrameController.h" @@ -38,9 +39,9 @@ namespace battleship{ configData::calcSumBinds(AppStateType::ACTIVE_STATE, false), GameManager::getSingleton()->getPath() + "Scripts/options.lua"){ this->guiState = guiState; - this->playerId = playerId; - mainPlayer = Map::getSingleton()->getPlayer(playerId); + + mainPlayer = Game::getSingleton()->getPlayer(playerId); initDragbox(); @@ -138,7 +139,7 @@ namespace battleship{ void ActiveGameState::renderUnits() { vector units; - for (Player *p : Map::getSingleton()->getPlayers()) + for (Player *p : Game::getSingleton()->getPlayers()) for (Unit *u : p->getUnits()) units.push_back(u); diff --git a/addResourceCommand.cpp b/addResourceCommand.cpp index a0f4c37..faada13 100644 --- a/addResourceCommand.cpp +++ b/addResourceCommand.cpp @@ -1,5 +1,5 @@ #include "addResourceCommand.h" -#include "map.h" +#include "game.h" #include "player.h" namespace battleship{ @@ -21,7 +21,7 @@ namespace battleship{ } void AddResourceCommand::addResource(){ - Player *player = Map::getSingleton()->getPlayer(playerId); + Player *player = Game::getSingleton()->getPlayer(playerId); switch(resourceId){ case 0: diff --git a/addUnitCommand.cpp b/addUnitCommand.cpp index f225ba8..a805aff 100644 --- a/addUnitCommand.cpp +++ b/addUnitCommand.cpp @@ -5,6 +5,7 @@ #include "console.h" #include "addUnitCommand.h" #include "inGameAppState.h" +#include "game.h" #include "player.h" #include "gameObjectFactory.h" @@ -47,7 +48,7 @@ namespace battleship{ } void AddUnitCommand::addUnit(){ - Player* player = Map::getSingleton()->getPlayer(playerId); + Player* player = Game::getSingleton()->getPlayer(playerId); player->addUnit(GameObjectFactory::createUnit(player, unitId, pos, rot)); } diff --git a/explosion.cpp b/explosion.cpp index 8fc21d6..7fd6575 100644 --- a/explosion.cpp +++ b/explosion.cpp @@ -3,14 +3,15 @@ #include "explosion.h" #include "util.h" #include "inGameAppState.h" +#include "game.h" using namespace std; using namespace vb01; namespace battleship{ - void detonate(InGameAppState *inGameState, Vector3 pos, Vector3 dir){ - sf::SoundBuffer *sfxBuffer = new sf::SoundBuffer(); - sf::Sound *sfx = nullptr; + void detonate(Vector3 pos, Vector3 dir){ + sf::SoundBuffer *sfxBuffer = new sf::SoundBuffer(); + sf::Sound *sfx = nullptr; string p = GameManager::getSingleton()->getPath() + "Sounds/Explosions/explosion0" + to_string(rand() % 4) + ".ogg"; if(sfxBuffer->loadFromFile(p.c_str())){ @@ -22,12 +23,12 @@ namespace battleship{ fx.initTime = getTime(); fx.time = 2500; fx.sfx = sfx; - inGameState->addFx(fx); + Game::getSingleton()->addFx(fx); } - void detonateDepthCharge(InGameAppState *inGameState, Vector3){ - sf::SoundBuffer *sfxBuffer = new sf::SoundBuffer(); - sf::Sound *sfx = nullptr; + void detonateDepthCharge(){ + sf::SoundBuffer *sfxBuffer = new sf::SoundBuffer(); + sf::Sound *sfx = nullptr; string p = GameManager::getSingleton()->getPath() + "Sounds/Destroyers/depthCharge.ogg"; if(sfxBuffer->loadFromFile(p.c_str())){ @@ -39,12 +40,12 @@ namespace battleship{ fx.initTime = getTime(); fx.time = 2000; fx.sfx = sfx; - inGameState->addFx(fx); + Game::getSingleton()->addFx(fx); } - void detonateTorpedo(InGameAppState *inGameState, Vector3){ - sf::SoundBuffer *sfxBuffer = new sf::SoundBuffer(); - sf::Sound *sfx = nullptr; + void detonateTorpedo(){ + sf::SoundBuffer *sfxBuffer = new sf::SoundBuffer(); + sf::Sound *sfx = nullptr; string p = GameManager::getSingleton()->getPath() + "Sounds/Submarines/torpedo.ogg"; if(sfxBuffer->loadFromFile(p.c_str())){ @@ -56,6 +57,6 @@ namespace battleship{ fx.initTime = getTime(); fx.time = 250; fx.sfx = sfx; - inGameState->addFx(fx); + Game::getSingleton()->addFx(fx); } } diff --git a/explosion.h b/explosion.h index 48a0b6d..f18c035 100644 --- a/explosion.h +++ b/explosion.h @@ -1,15 +1,12 @@ -#pragma once #ifndef EXPLOSION_H #define EXPLOSION_H #include namespace battleship{ - class InGameAppState; - - void detonate(InGameAppState*, vb01::Vector3, vb01::Vector3); - void detonateDepthCharge(InGameAppState*, vb01::Vector3); - void detonateTorpedo(InGameAppState*, vb01::Vector3); + void detonate(vb01::Vector3, vb01::Vector3); + void detonateDepthCharge(); + void detonateTorpedo(); } #endif diff --git a/fx.h b/fx.h new file mode 100644 index 0000000..03b2f12 --- /dev/null +++ b/fx.h @@ -0,0 +1,17 @@ +#ifndef FX_H +#define FX_H + +#include + +namespace sf{ + class Sound; +} + +namespace battleship{ + struct Fx { + vb01::s64 initTime, time; + sf::Sound *sfx = nullptr; + }; +} + +#endif diff --git a/game.cpp b/game.cpp new file mode 100644 index 0000000..5c3b159 --- /dev/null +++ b/game.cpp @@ -0,0 +1,25 @@ +#include "game.h" +#include "player.h" +#include "projectile.h" + +namespace battleship{ + static Game *game = nullptr; + + Game* Game::getSingleton(){ + if(!game) + game = new Game(); + + return game; + } + + void Game::update(){ + for(Player *p : players) + p->update(); + + for(Projectile *proj : projectiles) + proj->update(); + } + + void Game::togglePause(){ + } +} diff --git a/game.h b/game.h new file mode 100644 index 0000000..9e43f08 --- /dev/null +++ b/game.h @@ -0,0 +1,33 @@ +#ifndef GAME_H +#define GAME_H + +#include "fx.h" + +#include + +namespace battleship{ + class Player; + class Projectile; + + class Game{ + public: + static Game* getSingleton(); + void update(); + void togglePause(); + inline void addFx(Fx f){fx.push_back(f);} + inline void addPlayer(Player *pl){players.push_back(pl);} + inline void addProjectile(Projectile *proj){projectiles.push_back(proj);} + inline std::vector& getPlayers(){return players;} + inline Player* getPlayer(int id){return players[id];} + inline int getNumPlayers(){return players.size();} + private: + Game(){} + + bool paused = false; + std::vector fx; + std::vector players; + std::vector projectiles; + }; +} + +#endif diff --git a/gameObjectFactory.cpp b/gameObjectFactory.cpp index ccbf7a2..3565b60 100644 --- a/gameObjectFactory.cpp +++ b/gameObjectFactory.cpp @@ -31,7 +31,7 @@ namespace battleship{ return nullptr; } - ResourceDeposit* GameObjectFactory::createResourceDeposit(int id, Vector3 pos, Quaternion rot){ - return new ResourceDeposit(id, pos, rot); + ResourceDeposit* GameObjectFactory::createResourceDeposit(Player *player, int id, Vector3 pos, Quaternion rot){ + return new ResourceDeposit(player, id, pos, rot); } } diff --git a/gameObjectFactory.h b/gameObjectFactory.h index d4ba61a..655804d 100644 --- a/gameObjectFactory.h +++ b/gameObjectFactory.h @@ -15,7 +15,7 @@ namespace battleship{ enum GameObjectType{UNIT, PROJECTILE, RESOURCE_DEPOSIT}; static Unit* createUnit(Player*, int, vb01::Vector3, vb01::Quaternion); static Projectile* createProjectile(Unit*, int, vb01::Vector3, vb01::Quaternion); - static ResourceDeposit* createResourceDeposit(int, vb01::Vector3, vb01::Quaternion); + static ResourceDeposit* createResourceDeposit(Player*, int, vb01::Vector3, vb01::Quaternion); private: }; } diff --git a/inGameAppState.cpp b/inGameAppState.cpp index 219dca5..cea73e2 100755 --- a/inGameAppState.cpp +++ b/inGameAppState.cpp @@ -11,6 +11,7 @@ #include "defConfigs.h" #include "inGameAppState.h" +#include "game.h" #include "console.h" #include "gameObjectFrameController.h" #include "concreteGuiManager.h" @@ -45,7 +46,7 @@ namespace battleship{ Console::execute(wstringToString(textbox->getText())); } - InGameAppState::InGameAppState(vector difficultyLevels, vector factions, string mapName) : AbstractAppState( + InGameAppState::InGameAppState(vector difficultyLevels, vector factions) : AbstractAppState( AppStateType::IN_GAME_STATE, configData::calcSumBinds(AppStateType::IN_GAME_STATE, true), configData::calcSumBinds(AppStateType::IN_GAME_STATE, false), @@ -53,7 +54,6 @@ namespace battleship{ this->playerId = 1; this->difficultyLevels = difficultyLevels; this->factions = factions; - this->mapName = mapName; } InGameAppState::~InGameAppState() { @@ -62,9 +62,6 @@ namespace battleship{ void InGameAppState::onAttached() { AbstractAppState::onAttached(); - Map *map = Map::getSingleton(); - map->load(mapName); - for (int i = 0; i < factions.size(); i++) { int faction, difficulty; @@ -80,16 +77,13 @@ namespace battleship{ } faction = factions[i][0] - 48; - Player *p = new Player(difficulty, faction, i); - p->setId(map->getNumPlayers() - 1); - map->addPlayer(p); } Camera *cam = Root::getSingleton()->getCamera(); - cam->setPosition(map->getSpawnPoint(playerId - 1) + Vector3(1, 1, 1) * 40); + cam->setPosition(Map::getSingleton()->getSpawnPoint(playerId - 1) + Vector3(1, 1, 1) * 40); cam->lookAt(Vector3(-1, -1, -1).norm(), Vector3(-1, 1, -1).norm()); - mainPlayer = map->getPlayer(playerId); + mainPlayer = Game::getSingleton()->getPlayer(playerId); GameManager *gm = GameManager::getSingleton(); StateManager *stateManager = gm->getStateManager(); @@ -101,7 +95,7 @@ namespace battleship{ void InGameAppState::onDettached() {} void InGameAppState::update() { - Map::getSingleton()->update(); + Game::getSingleton()->update(); } void InGameAppState::toggleMainMenu() { @@ -122,7 +116,7 @@ namespace battleship{ guiManager->readLuaScreenScript("inGame.lua", activeState->getButtons()); - for(Player *p : Map::getSingleton()->getPlayers()) + for(Player *p : Game::getSingleton()->getPlayers()) for(Unit *u : p->getUnits()) u->reinit(); } diff --git a/inGameAppState.h b/inGameAppState.h index cfd1c8f..5bf6860 100755 --- a/inGameAppState.h +++ b/inGameAppState.h @@ -13,11 +13,6 @@ #include namespace battleship{ - struct Fx { - s64 initTime, time; - sf::Sound *sfx = nullptr; - }; - class InGameAppState : public gameBase::AbstractAppState { public: class ResumeButton : public vb01Gui::Button { @@ -43,7 +38,7 @@ namespace battleship{ private: }; - InGameAppState(std::vector, std::vector, std::string); + InGameAppState(std::vector, std::vector); ~InGameAppState(); void onAttached(); void onDettached(); @@ -51,15 +46,9 @@ namespace battleship{ void onAction(int, bool); void onAnalog(int, float); std::vector getSelectedUnits(Player*); - inline std::vector& getProjectiles(){return projectiles;} - inline void addFx(Fx fx){this->fx.push_back(fx);} - inline void addProjectile(Projectile *p){projectiles.push_back(p);} private: bool isMainMenuActive = false; std::vector difficultyLevels, factions, modelPaths; - std::vector projectiles; - std::vector fx; - std::string mapName; Player *mainPlayer; int playerId; ActiveGameState* activeState; diff --git a/map.cpp b/map.cpp index 1f591c7..27a1fd6 100755 --- a/map.cpp +++ b/map.cpp @@ -42,8 +42,6 @@ namespace battleship{ } void Map::update(){ - for(Player *p : players) - p->update(); } void Map::loadSkybox(){ @@ -123,7 +121,7 @@ namespace battleship{ } } - void Map::loadPlayers(){ + void Map::loadPlayerGameObjects(Player *player){ AssetManager *assetManager = AssetManager::getSingleton(); string path = GameManager::getSingleton()->getPath(); assetManager->load(path + DEFAULT_TEXTURE); @@ -135,10 +133,10 @@ namespace battleship{ for(int i = 0; i < numPlayers; i++){ if(i == 0){ - int numNpcObjs = SOL_LUA_STATE[mapTable][playerInd][i + 1]["numNpcGameObjs"]; + int numNpcObjs = SOL_LUA_STATE[mapTable][playerInd][i + 1]["numResourceDeposits"]; for(int j = 0; j < numNpcObjs; j++){ - sol::table npcObjTable = SOL_LUA_STATE[mapTable][playerInd][i + 1]["npcGameObjs"][j + 1]; + sol::table npcObjTable = SOL_LUA_STATE[mapTable][playerInd][i + 1]["resourceDeposits"][j + 1]; int id = npcObjTable["id"]; sol::table posTable = npcObjTable["pos"]; @@ -147,15 +145,13 @@ namespace battleship{ sol::table rotTable = npcObjTable["rot"]; Quaternion rot = Quaternion(rotTable["w"], rotTable["x"], rotTable["y"], rotTable["z"]); - addNpcGameObject((GameObject*)GameObjectFactory::createResourceDeposit(id, pos, rot)); + player->addResourceDeposit(GameObjectFactory::createResourceDeposit(player, id, pos, rot)); } } //int spawnPointId = SOL_LUA_STATE[mapTable]["spawnPointInd"][i + 1][spawnPointId]; int numUnits = SOL_LUA_STATE[mapTable][playerInd][i + 1]["numUnits"]; - players.push_back(new Player(0, 0, 0, spawnPoints[i])); - for(int j = 0; j < numUnits; j++){ sol::table unitTable = SOL_LUA_STATE[mapTable][playerInd][i + 1]["units"][j + 1]; @@ -167,7 +163,7 @@ namespace battleship{ int id = unitTable["id"]; - players[i]->addUnit(GameObjectFactory::createUnit(players[i], id, pos, rot)); + player->addUnit(GameObjectFactory::createUnit(player, id, pos, rot)); } } } @@ -249,7 +245,6 @@ namespace battleship{ int numWaterbodies = SOL_LUA_STATE[mapTable]["numWaterBodies"]; loadSpawnPoints(); - loadPlayers(); loadSkybox(); loadCells(); loadTerrainObject(-1); diff --git a/map.h b/map.h index dd91ca5..17e4833 100755 --- a/map.h +++ b/map.h @@ -1,4 +1,3 @@ -#pragma once #ifndef MAP_H #define MAP_H @@ -16,7 +15,8 @@ namespace vb01{ namespace battleship{ class Player; - class GameObject; + class Unit; + class ResourceDeposit; struct Cell; class Map { @@ -48,19 +48,14 @@ namespace battleship{ void unload(); int getCellId(vb01::Vector3); bool isPointWithinTerrainObject(vb01::Vector3, int); + void loadPlayerGameObjects(Player*); inline std::string getMapName(){return mapName;} inline vb01::Node* getNodeParent(){return terrainNode;} inline vb01::Vector3 getCellSize(){return CELL_SIZE;} - inline std::vector getPlayers() {return players;} - inline Player* getPlayer(int i){return players[i];} - inline void addPlayer(Player *p){players.push_back(p);} - inline int getNumPlayers(){return players.size();} inline int getNumSpawnPoints(){return spawnPoints.size();} inline vb01::Vector3 getSpawnPoint(int i){return spawnPoints[i];} inline void addSpawnPoint(vb01::Vector3 sp){spawnPoints.push_back(sp);} inline std::vector& getCells(){return cells;} - inline void addNpcGameObject(GameObject *obj){npcGameObjects.push_back(obj);} - inline std::vector getNpcGameObjects(){return npcGameObjects;} private: std::string mapTable = "map"; vb01::Node *terrainNode = nullptr, *cellNode = nullptr; @@ -68,14 +63,11 @@ namespace battleship{ std::string mapName; vb01::Vector3 CELL_SIZE = vb01::Vector3(7, 7, 7), mapSize; std::vector spawnPoints; - std::vector players; std::vector cells; - std::vector npcGameObjects; Map(){} void preprareScene(); void loadSpawnPoints(); - void loadPlayers(); void loadSkybox(); void loadCells(); void loadTerrainObject(int); diff --git a/mapEditorAppState.cpp b/mapEditorAppState.cpp index c74ec88..2072162 100644 --- a/mapEditorAppState.cpp +++ b/mapEditorAppState.cpp @@ -45,7 +45,7 @@ namespace battleship{ map->load(name, newMap); if(newMap){ - map->addPlayer(new Player(0, 0, 0)); + addPlayer(); map->addSpawnPoint(Vector3::VEC_ZERO); generatePlane(size); } @@ -401,7 +401,7 @@ namespace battleship{ string mapScript = "map = {\nnumWaterBodies = " + to_string(numWaterBodies) + ",\n"; mapScript += "size = {x = " + to_string(mapSize.x) + ", y = 100, z = " + to_string(mapSize.y) + "},\n"; mapScript += "impassibleNodeValue = " + to_string(IMPASS_NODE_VAL) + ",\n"; - mapScript += "numPlayers = " + to_string(map->getNumPlayers()) + ",\n"; + mapScript += "numPlayers = " + to_string(players.size()) + ",\n"; int numSpawnPoints = map->getNumSpawnPoints(); mapScript += "numSpawnPoints = " + to_string(numSpawnPoints) + ",\n"; @@ -414,13 +414,13 @@ namespace battleship{ mapScript += "},\nplayers = {\n"; - for(int i = 0; i < map->getNumPlayers(); i++){ + for(int i = 0; i < players.size(); i++){ mapScript += "{\n"; if(i > 0) mapScript += "spawnPoint = 0,\n"; else{ - vector npcGameObjs = map->getNpcGameObjects(); + vector npcGameObjs; mapScript += "numNpcGameObjs = " + to_string(npcGameObjs.size()) + ",\n"; mapScript += "npcGameObjs = {\n"; @@ -437,14 +437,14 @@ namespace battleship{ mapScript += "},\n"; } - int numUnits = map->getPlayer(i)->getNumberOfUnits(); + int numUnits = players[i]->getNumberOfUnits(); mapScript += "numUnits = " + to_string(numUnits) + ",\n"; if(numUnits > 0){ mapScript += "units = {\n"; for(int j = 0; j < numUnits; j++){ - Unit *unit = map->getPlayer(i)->getUnit(j); + Unit *unit = getPlayer(i)->getUnit(j); string idStr = "id = " + to_string(unit->getId()); @@ -605,17 +605,15 @@ namespace battleship{ switch((Bind)bind){ case Bind::LOOK_AROUND: if(ufCtr->isPlacingFrames()){ - Map *map = Map::getSingleton(); - Player *player = map->getPlayer(0); + Player *player = mapEditor->getPlayer(0); GameObjectFrame &frame = ufCtr->getGameObjectFrame(0); Model *model = frame.getModel(); Vector3 pos = model->getPosition(); Quaternion rot = model->getOrientation(); + GameObject *obj; - if(frame.getType() == GameObject::Type::RESOURCE_DEPOSIT){ - ResourceDeposit *dep = GameObjectFactory::createResourceDeposit(frame.getId(), pos, rot); - map->addNpcGameObject((GameObject*)dep); - } + if(frame.getType() == GameObject::Type::RESOURCE_DEPOSIT) + player->addResourceDeposit(GameObjectFactory::createResourceDeposit(player, frame.getId(), pos, rot)); else player->addUnit(GameObjectFactory::createUnit(player, frame.getId(), pos, rot)); } diff --git a/mapEditorAppState.h b/mapEditorAppState.h index 8502f69..e19b1f8 100644 --- a/mapEditorAppState.h +++ b/mapEditorAppState.h @@ -2,6 +2,7 @@ #define MAP_EDITOR_APP_STATE_H #include "map.h" +#include "player.h" #include @@ -39,6 +40,8 @@ namespace battleship{ void exportMap(); void prepareTerrainObjects(int = 0, int = -1); void togglePush(bool); + inline void addPlayer(){players.push_back(new Player(0, 0, 0));} + inline Player* getPlayer(int id){return players[id];} inline vb01::Node* getSelectedNode(){return selectedTerrainNode;} inline float getGuiThreshold(){return guiThreshold;} inline float getCircleRadius(){return circleRadius;} @@ -63,6 +66,7 @@ namespace battleship{ void prepareTerrainObject(vb01::u32**, Map::Cell*, int[3], float, bool); Map *map; + std::vector players; vb01::Node *selectedTerrainNode = nullptr; TransformAxis transformAxis = X_AXIS; vb01Gui::Listbox *skyListbox = nullptr; diff --git a/playButton.cpp b/playButton.cpp index 0a9d147..ecf803e 100644 --- a/playButton.cpp +++ b/playButton.cpp @@ -2,6 +2,7 @@ #include "gameManager.h" #include "inGameAppState.h" #include "concreteGuiManager.h" +#include "game.h" #include @@ -28,10 +29,22 @@ namespace battleship{ 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)); + Map *map = Map::getSingleton(); + map->load(mapName); + Game::getSingleton()->addPlayer(new Player(0, 0, 0)); + + int numPlayers = map->getNumSpawnPoints(); + + for(int i = 0; i < numPlayers; i++){ + Player *player = new Player(0, 0, 0); + map->loadPlayerGameObjects(player); + Game::getSingleton()->addPlayer(player); + } + + StateManager *stateManager = GameManager::getSingleton()->getStateManager(); + stateManager->attachAppState(new InGameAppState(difficulties, factions)); ConcreteGuiManager::getSingleton()->readLuaScreenScript("inGame.lua"); } diff --git a/player.cpp b/player.cpp index 8076040..04106ac 100755 --- a/player.cpp +++ b/player.cpp @@ -1,18 +1,14 @@ #include #include "player.h" - -using namespace vb01; -using namespace gameBase; -using namespace std; +#include "resourceDeposit.h" namespace battleship{ - Player::Player(int difficulty, int faction, int id, Vector3 spawnPoint) { - this->difficulty = difficulty; - this->faction = faction; - this->spawnPoint = spawnPoint; - this->id = id; + using namespace vb01; + using namespace gameBase; + using namespace std; + Player::Player(int diff, int fac, int i, Vector3 sp) : difficulty(diff), faction(fac), id(i), spawnPoint(sp) { //SOL_LUA_STATE.script("players[" + to_string(id + 1) + "] = Player:new({id = " + to_string(id + 1) + "})"); } @@ -24,6 +20,9 @@ namespace battleship{ for(Unit *u : units) u->update(); + + for(ResourceDeposit *rd : resourceDeposits) + rd->update(); } void Player::issueOrder(Order::TYPE type, vector targets, bool append){ @@ -39,7 +38,7 @@ namespace battleship{ } } - bool Player::isThisPlayersUnit(Unit *u) { + bool Player::isThisPlayersUnit(GameObject *u) { bool foundUnit = false; if (units.size() > 0) { diff --git a/player.h b/player.h index 932372d..f6d5c3c 100755 --- a/player.h +++ b/player.h @@ -1,4 +1,3 @@ -#pragma once #ifndef PLAYER_H #define PLAYER_H @@ -8,6 +7,9 @@ #include "unit.h" namespace battleship{ + class ResourceDeposit; + class Projectile; + class Player { public: Player(int, int, int, vb01::Vector3 = vb01::Vector3::VEC_ZERO); @@ -16,13 +18,14 @@ namespace battleship{ void issueOrder(Order::TYPE, std::vector, bool); void deselectUnit(int); void deselectUnits(); - bool isThisPlayersUnit(Unit*); + bool isThisPlayersUnit(GameObject*); const inline std::vector& getSelectedUnits(){return selectedUnits;} inline Unit* getSelectedUnit(int i){return selectedUnits[i];} inline int getNumSelectedUnits(){return selectedUnits.size();} inline void selectUnit(Unit *u){selectedUnits.push_back(u);} inline void selectUnits(std::vector units){selectedUnits = units;} inline void addUnit(Unit *u){units.push_back(u);} + inline void addResourceDeposit(ResourceDeposit *rd){resourceDeposits.push_back(rd);} inline Unit* getUnit(int i){return units[i];} inline std::vector& getUnits(){return units;} inline void setId(int i){id=i;} @@ -43,6 +46,8 @@ namespace battleship{ private: int refineds = 0, wealth = 0, research = 0, faction, difficulty,side,id; std::vector units, selectedUnits; + std::vector projectiles; + std::vector resourceDeposits; vb01::Vector3 spawnPoint; }; } diff --git a/projectile.cpp b/projectile.cpp index 47b3ccf..8703acc 100755 --- a/projectile.cpp +++ b/projectile.cpp @@ -8,7 +8,7 @@ #include -#include "map.h" +#include "game.h" #include "unit.h" #include "util.h" #include "projectile.h" @@ -103,7 +103,7 @@ namespace battleship{ void Projectile::explode(Node *collNode) { exploded = true; - for (Player *p : Map::getSingleton()->getPlayers()) { + for (Player *p : Game::getSingleton()->getPlayers()) { for (Unit *u : p->getUnits()) if (collNode == u->getNode()) u->takeDamage(damage); @@ -113,11 +113,11 @@ namespace battleship{ InGameAppState *inGameState = ((InGameAppState*)sm->getAppStateByType((int)AppStateType::IN_GAME_STATE)); if(id == 8) - detonateTorpedo(inGameState, pos); + detonateTorpedo(); else if(weaponTypeId == 1 && (id == 2 || id == 3)) - detonateDepthCharge(inGameState, pos); + detonateDepthCharge(); else - detonate(inGameState, pos, -dirVec); + detonate(pos, -dirVec); } void Projectile::debug(){ diff --git a/resourceDeposit.cpp b/resourceDeposit.cpp index 563e6af..4b9d848 100644 --- a/resourceDeposit.cpp +++ b/resourceDeposit.cpp @@ -3,7 +3,7 @@ namespace battleship{ using namespace vb01; - ResourceDeposit::ResourceDeposit(int id, Vector3 pos, Quaternion rot) : GameObject(GameObject::Type::RESOURCE_DEPOSIT, id, nullptr, pos, rot){ + ResourceDeposit::ResourceDeposit(Player *player, int id, Vector3 pos, Quaternion rot) : GameObject(GameObject::Type::RESOURCE_DEPOSIT, id, player, pos, rot){ initModel(); placeAt(pos); orientAt(rot); diff --git a/resourceDeposit.h b/resourceDeposit.h index 5881834..38667b7 100644 --- a/resourceDeposit.h +++ b/resourceDeposit.h @@ -6,7 +6,7 @@ namespace battleship{ class ResourceDeposit : public GameObject{ public: - ResourceDeposit(int, vb01::Vector3, vb01::Quaternion); + ResourceDeposit(Player*, int, vb01::Vector3, vb01::Quaternion); void update(); private: }; diff --git a/unit.cpp b/unit.cpp index 18368ec..12d0033 100755 --- a/unit.cpp +++ b/unit.cpp @@ -192,17 +192,6 @@ namespace battleship{ removeOrder(orders.size() - 1); } - std::vector Unit::getProjectiles(){ - std::vector projectiles; - InGameAppState *inGameState = ((InGameAppState*)GameManager::getSingleton()->getStateManager()->getAppStateByType((int)AppStateType::IN_GAME_STATE)); - - for(Projectile *p: inGameState->getProjectiles()) - if(p->getUnit() == this) - projectiles.push_back(p); - - return projectiles; - } - void Unit::addOrder(Order order){ orders.push_back(order); } @@ -220,9 +209,4 @@ namespace battleship{ orderLineDispTime = getTime(); } - - void Unit::addProjectile(Projectile *p) { - StateManager *stateManager = GameManager::getSingleton()->getStateManager(); - ((InGameAppState*)stateManager->getAppStateByType((int)AppStateType::IN_GAME_STATE))->addProjectile(p); - } } diff --git a/unit.h b/unit.h index 05252d0..11859df 100755 --- a/unit.h +++ b/unit.h @@ -55,7 +55,6 @@ namespace battleship{ virtual void halt(); void toggleSelection(bool); void setOrder(Order); - void addProjectile(Projectile*); std::vector getProjectiles(); virtual void addOrder(Order); virtual void reinit();