diff --git a/activeGameState.cpp b/activeGameState.cpp index 919aed1..8b6081a 100755 --- a/activeGameState.cpp +++ b/activeGameState.cpp @@ -48,6 +48,7 @@ namespace battleship{ } ActiveGameState::~ActiveGameState() { + removeDragbox(); } void ActiveGameState::initDragbox(){ @@ -66,6 +67,11 @@ namespace battleship{ root->getGuiNode()->attachChild(dragboxNode); } + void ActiveGameState::removeDragbox(){ + Root::getSingleton()->getRootNode()->dettachChild(dragboxNode); + delete dragboxNode; + } + void ActiveGameState::onAttached() { AbstractAppState::onAttached(); ConcreteGuiManager::getSingleton()->readLuaScreenScript("activeGameState.lua"); diff --git a/activeGameState.h b/activeGameState.h index 15bff27..2298261 100755 --- a/activeGameState.h +++ b/activeGameState.h @@ -33,6 +33,7 @@ namespace battleship{ private: void updateGameObjHoveredOn(); void initDragbox(); + void removeDragbox(); void deselectUnits(); void renderUnits(); void updateSelectionBox(); diff --git a/inGameAppState.cpp b/inGameAppState.cpp index 0489035..82b51c8 100755 --- a/inGameAppState.cpp +++ b/inGameAppState.cpp @@ -91,7 +91,12 @@ namespace battleship{ stateManager->attachAppState(activeState); } - void InGameAppState::onDettached() {} + void InGameAppState::onDettached() { + StateManager *sm = GameManager::getSingleton()->getStateManager(); + ActiveGameState *activeState = (ActiveGameState*)sm->getAppStateByType(int(AppStateType::ACTIVE_STATE)); + sm->dettachAppState(activeState); + delete activeState; + } void InGameAppState::update() { Game::getSingleton()->update(); diff --git a/map.cpp b/map.cpp index e9f87cf..48f02dc 100755 --- a/map.cpp +++ b/map.cpp @@ -7,6 +7,7 @@ #include #include "map.h" +#include "game.h" #include "player.h" #include "gameObject.h" #include "gameObjectFactory.h" @@ -262,7 +263,59 @@ namespace battleship{ } } - void Map::unload() {} + void Map::unloadSkybox(){ + Root::getSingleton()->removeSkybox(); + //unload skybox assets + } + + void Map::unloadCells(){ + while(cellNode->getNumChildren() > 0){ + Node *node = cellNode->getChild(0); + cellNode->dettachChild(node); + //delete node; + } + + cells.clear(); + } + + void Map::unloadTerrainObjects(){ + //unload terrain assets + while(terrainNode->getNumChildren() > 0){ + Node *node = terrainNode->getChild(0); + terrainNode->dettachChild(node); + //delete node; + } + } + + void Map::unloadPlayerObjects(){ + for(Player *pl : Game::getSingleton()->getPlayers()){ + while(pl->getNumUnits() > 0){ + pl->removeUnit(0); + } + + while(pl->getNumResourceDeposits() > 0){ + pl->removeResourceDeposit(0); + } + } + } + + void Map::destroyScene(){ + Node *rootNode = Root::getSingleton()->getRootNode(); + rootNode->dettachChild(terrainNode); + rootNode->dettachChild(cellNode); + + delete terrainNode; + delete cellNode; + } + + void Map::unload(){ + unloadSkybox(); + unloadCells(); + unloadTerrainObjects(); + unloadPlayerObjects(); + spawnPoints.clear(); + destroyScene(); + } template int Map::bsearch(vector haystack, T needle, float eps){ T haystackMidVal; diff --git a/map.h b/map.h index 17e4833..5280b9a 100755 --- a/map.h +++ b/map.h @@ -71,6 +71,11 @@ namespace battleship{ void loadSkybox(); void loadCells(); void loadTerrainObject(int); + void unloadTerrainObjects(); + void unloadCells(); + void unloadSkybox(); + void unloadPlayerObjects(); + void destroyScene(); template int bsearch(std::vector, T, float); }; } diff --git a/playButton.cpp b/playButton.cpp index 224eb26..31a74e9 100644 --- a/playButton.cpp +++ b/playButton.cpp @@ -42,9 +42,9 @@ namespace battleship{ Game::getSingleton()->addPlayer(player); } + ConcreteGuiManager::getSingleton()->readLuaScreenScript("inGame.lua"); + 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 51d2d4a..a7db17c 100755 --- a/player.cpp +++ b/player.cpp @@ -114,6 +114,11 @@ namespace battleship{ units.erase(units.begin() + id); } + void Player::removeResourceDeposit(int id){ + delete resourceDeposits[id]; + resourceDeposits.erase(resourceDeposits.begin() + id); + } + vector Player::getSelectedUnitsByClass(UnitClass uc){ vector selectedUnits = getSelectedUnits(), unitsByClass; diff --git a/player.h b/player.h index 6f506fc..61f0dc8 100755 --- a/player.h +++ b/player.h @@ -18,6 +18,7 @@ namespace battleship{ void issueOrder(Order::TYPE, vb01::Vector3, std::vector, bool); void removeUnit(Unit*); void removeUnit(int); + void removeResourceDeposit(int); bool isThisPlayersUnit(GameObject*); std::vector getSelectedUnitsByClass(UnitClass); void selectUnits(std::vector); @@ -30,6 +31,7 @@ namespace battleship{ inline void addUnit(Unit *u){units.push_back(u);} inline std::vector& getResourceDeposits(){return resourceDeposits;} inline void addResourceDeposit(ResourceDeposit *rd){resourceDeposits.push_back(rd);} + inline int getNumResourceDeposits(){return resourceDeposits.size();} inline Unit* getUnit(int i){return units[i];} inline std::vector& getUnits(){return units;} inline void setTeam(int t){team = t;} diff --git a/statsButton.cpp b/statsButton.cpp index 660b50a..e0a760a 100644 --- a/statsButton.cpp +++ b/statsButton.cpp @@ -1,14 +1,26 @@ #include "statsButton.h" #include "gameManager.h" #include "concreteGuiManager.h" +#include "inGameAppState.h" +#include "map.h" + +#include namespace battleship{ using namespace std; using namespace vb01; + using namespace gameBase; StatsButton::StatsButton(Vector2 pos, Vector2 size, string name, int trigger, string imagePath) : Button(pos, size, name, GameManager::getSingleton()->getPath() + "Fonts/batang.ttf", trigger, true, imagePath){} void StatsButton::onClick(){ + Map::getSingleton()->unload(); + + StateManager *sm = GameManager::getSingleton()->getStateManager(); + InGameAppState *inGameState = (InGameAppState*)sm->getAppStateByType(int(AppStateType::IN_GAME_STATE)); + sm->dettachAppState(inGameState); + delete inGameState; + ConcreteGuiManager::getSingleton()->readLuaScreenScript("statistics.lua"); } }