From b58d31465928c463c346f0802378cc7636aac098 Mon Sep 17 00:00:00 2001 From: devZoGok Date: Sat, 7 Oct 2023 18:41:07 +0300 Subject: [PATCH] saving and loading resource deposits --- map.cpp | 18 ++++++++++++++++++ map.h | 4 ++++ mapEditorAppState.cpp | 23 ++++++++++++++++++++++- 3 files changed, 44 insertions(+), 1 deletion(-) diff --git a/map.cpp b/map.cpp index 8ed0c58..1f591c7 100755 --- a/map.cpp +++ b/map.cpp @@ -8,6 +8,7 @@ #include "map.h" #include "player.h" +#include "gameObject.h" #include "gameObjectFactory.h" #include "pathfinder.h" #include "gameManager.h" @@ -133,6 +134,23 @@ namespace battleship{ string playerInd = "players"; for(int i = 0; i < numPlayers; i++){ + if(i == 0){ + int numNpcObjs = SOL_LUA_STATE[mapTable][playerInd][i + 1]["numNpcGameObjs"]; + + for(int j = 0; j < numNpcObjs; j++){ + sol::table npcObjTable = SOL_LUA_STATE[mapTable][playerInd][i + 1]["npcGameObjs"][j + 1]; + int id = npcObjTable["id"]; + + sol::table posTable = npcObjTable["pos"]; + Vector3 pos = Vector3(posTable["x"], posTable["y"], posTable["z"]); + + sol::table rotTable = npcObjTable["rot"]; + Quaternion rot = Quaternion(rotTable["w"], rotTable["x"], rotTable["y"], rotTable["z"]); + + addNpcGameObject((GameObject*)GameObjectFactory::createResourceDeposit(id, pos, rot)); + } + } + //int spawnPointId = SOL_LUA_STATE[mapTable]["spawnPointInd"][i + 1][spawnPointId]; int numUnits = SOL_LUA_STATE[mapTable][playerInd][i + 1]["numUnits"]; diff --git a/map.h b/map.h index f83bc2e..dd91ca5 100755 --- a/map.h +++ b/map.h @@ -16,6 +16,7 @@ namespace vb01{ namespace battleship{ class Player; + class GameObject; struct Cell; class Map { @@ -58,6 +59,8 @@ namespace battleship{ 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; @@ -67,6 +70,7 @@ namespace battleship{ std::vector spawnPoints; std::vector players; std::vector cells; + std::vector npcGameObjects; Map(){} void preprareScene(); diff --git a/mapEditorAppState.cpp b/mapEditorAppState.cpp index b1712a1..c74ec88 100644 --- a/mapEditorAppState.cpp +++ b/mapEditorAppState.cpp @@ -415,7 +415,27 @@ namespace battleship{ mapScript += "},\nplayers = {\n"; for(int i = 0; i < map->getNumPlayers(); i++){ - mapScript += "{\nspawnPoint = 0,\n"; + mapScript += "{\n"; + + if(i > 0) + mapScript += "spawnPoint = 0,\n"; + else{ + vector npcGameObjs = map->getNpcGameObjects(); + mapScript += "numNpcGameObjs = " + to_string(npcGameObjs.size()) + ",\n"; + mapScript += "npcGameObjs = {\n"; + + for(GameObject *npcGameObj : npcGameObjs){ + Vector3 pos = npcGameObj->getPos(); + string posStr = "x = " + to_string(pos.x) + ", y = " + to_string(pos.y) + ", z = " + to_string(pos.z); + + Quaternion rot = npcGameObj->getRot(); + string rotStr = "w = " + to_string(rot.w) + ", x = " + to_string(rot.x) + ", y = " + to_string(rot.y) + ", z = " + to_string(rot.z); + + mapScript += "{id = " + to_string(npcGameObj->getId()) + ", pos = {" + posStr + "}, rot = {" + rotStr + "}},\n"; + } + + mapScript += "},\n"; + } int numUnits = map->getPlayer(i)->getNumberOfUnits(); mapScript += "numUnits = " + to_string(numUnits) + ",\n"; @@ -594,6 +614,7 @@ namespace battleship{ if(frame.getType() == GameObject::Type::RESOURCE_DEPOSIT){ ResourceDeposit *dep = GameObjectFactory::createResourceDeposit(frame.getId(), pos, rot); + map->addNpcGameObject((GameObject*)dep); } else player->addUnit(GameObjectFactory::createUnit(player, frame.getId(), pos, rot));