From 8c80aedb20865b91c10438c4de8e6e221b06a3f6 Mon Sep 17 00:00:00 2001 From: devZoGok Date: Mon, 31 Oct 2022 11:03:19 +0200 Subject: [PATCH] removed UnitDataManager class --- CMakeLists.txt | 2 +- consoleCommand.cpp | 7 +++- inGameAppState.cpp | 17 ++++++---- unit.cpp | 66 +++++++++++++++++++++++-------------- unit.h | 13 ++++---- unitDataManager.cpp | 79 --------------------------------------------- unitDataManager.h | 53 ------------------------------ 7 files changed, 66 insertions(+), 171 deletions(-) delete mode 100644 unitDataManager.cpp delete mode 100644 unitDataManager.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 9e4cdd4..ab1f66b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -13,7 +13,7 @@ set(GUI tooltip.cpp optionsButton.cpp exitButton.cpp consoleCommand.cpp) set(CORE gameManager.cpp defConfigs.cpp) set(PROJECTILES projectile.cpp guidedMissile.cpp depthCharge.cpp missile.cpp shell.cpp torpedo.cpp) set(FX explosion.cpp) -set(CONTENT player.cpp unitDataManager.cpp unit.cpp pathfinder.cpp map.cpp ${PROJECTILES} ${FX}) +set(CONTENT player.cpp unit.cpp pathfinder.cpp map.cpp ${PROJECTILES} ${FX}) set(GAME_SRC ${STATES} ${GUI} ${CORE} ${CONTENT} ${UTIL}) set(SFML_DIR external/SFML) diff --git a/consoleCommand.cpp b/consoleCommand.cpp index ca52a83..f13f620 100755 --- a/consoleCommand.cpp +++ b/consoleCommand.cpp @@ -4,6 +4,7 @@ #include #include +#include #include "consoleCommand.h" #include "inGameAppState.h" @@ -57,7 +58,11 @@ namespace battleship{ int unitId = atoi(fullCommand[2].c_str()); - if(!(0 <= unitId && unitId <= UnitDataManager::getSingleton()->getNumUnits())) + LuaManager *lm = LuaManager::getSingleton(); + string pathBase = GameManager::getSingleton()->getPath() + "Scripts/"; + lm->buildScript(vector{pathBase + "defPaths.lua", pathBase + "unitData.lua"}); + + if(!(0 <= unitId && unitId <= lm->getInt("numUnits"))) return; } diff --git a/inGameAppState.cpp b/inGameAppState.cpp index 6f1349f..f156d37 100755 --- a/inGameAppState.cpp +++ b/inGameAppState.cpp @@ -6,10 +6,10 @@ #include #include +#include #include "defConfigs.h" #include "inGameAppState.h" -#include "unitDataManager.h" #include "consoleCommand.h" #include "vessel.h" @@ -197,13 +197,16 @@ namespace battleship{ AssetManager *assetManager = AssetManager::getSingleton(); assetManager->load(DEFAULT_TEXTURE); - UnitDataManager *unitDataManager = UnitDataManager::getSingleton(); - int numUnits = unitDataManager->getNumUnits(); - string *basePaths = unitDataManager->getBasePath(); - string *meshPaths = unitDataManager->getMeshPath(); + LuaManager *lm = LuaManager::getSingleton(); + string pathBase = GameManager::getSingleton()->getPath() + "Scripts/"; + lm->buildScript(vector{pathBase + "defPaths.lua", pathBase + "unitData.lua"}); + int numUnits = lm->getInt("numUnits"); - for(int i = 0; i < numUnits; ++i) - assetManager->load(basePaths[i] + meshPaths[i]); + for(int i = 0; i < numUnits; ++i){ + string basePath = lm->getStringFromTable("basePath", vector{Index(i + 1)}); + string meshPath = lm->getStringFromTable("meshPath", vector{Index(i + 1)}); + assetManager->load(basePath + meshPath); + } } void InGameAppState::onDettached() {} diff --git a/unit.cpp b/unit.cpp index 8b89e91..df45cf0 100755 --- a/unit.cpp +++ b/unit.cpp @@ -5,6 +5,7 @@ #include #include +#include #include #include @@ -25,18 +26,7 @@ namespace battleship{ this->id = id; this->player = player; - UnitDataManager *udm = UnitDataManager::getSingleton(); - health = udm->getHealth()[id]; - maxTurnAngle = udm->getMaxTurnAngle()[id]; - range = udm->getRange()[id]; - lineOfSight = udm->getLineOfSight()[id]; - speed = udm->getSpeed()[id]; - unitClass = udm->getUnitClass()[id]; - type = udm->getUnitType()[id]; - width = udm->getUnitCornerPoints()[id][0].x - udm->getUnitCornerPoints()[id][1].x; - height = udm->getUnitCornerPoints()[id][4].y - udm->getUnitCornerPoints()[id][0].y; - length = udm->getUnitCornerPoints()[id][3].z - udm->getUnitCornerPoints()[id][0].z; - + initProperties(); initModel(); placeUnit(pos); initSound(); @@ -46,9 +36,42 @@ namespace battleship{ Unit::~Unit() { } + void Unit::initProperties(){ + LuaManager *lm = LuaManager::getSingleton(); + string pathBase = GameManager::getSingleton()->getPath() + "Scripts/"; + lm->buildScript(vector{pathBase + "defPaths.lua", pathBase + "unitData.lua"}); + + health = lm->getIntFromTable("health", vector{Index(id + 1)}); + maxHealth = health; + + maxTurnAngle = lm->getFloatFromTable("maxTurnAngle", vector{Index(id + 1)}); + range = lm->getFloatFromTable("range", vector{Index(id + 1)}); + lineOfSight = lm->getFloatFromTable("lineOfSight", vector{Index(id + 1)}); + speed = lm->getFloatFromTable("speed", vector{Index(id + 1)}); + unitClass = (UnitClass)lm->getIntFromTable("unitClass", vector{Index(id + 1)}); + anglePrecision = lm->getFloatFromTable("anglePrecision", vector{Index(id + 1)}); + type = (UnitType)lm->getIntFromTable("unitType", vector{Index(id + 1)}); + + string cornerInd = "unitCornerPoints"; + + for(int i = 0; i < 8; i++){ + float x = lm->getFloatFromTable(cornerInd, vector{Index(id + 1), Index(i + 1), Index("x")}); + float y = lm->getFloatFromTable(cornerInd, vector{Index(id + 1), Index(i + 1), Index("y")}); + float z = lm->getFloatFromTable(cornerInd, vector{Index(id + 1), Index(i + 1), Index("z")}); + corners[i] = Vector3(x, y, z); + } + + width = corners[0].x - corners[1].x; + height = corners[4].y - corners[0].y; + length = corners[3].z - corners[0].z; + } + void Unit::initModel(){ - UnitDataManager *udm = UnitDataManager::getSingleton(); - model = new Model(udm->getBasePath()[id] + udm->getMeshPath()[id]); + LuaManager *lm = LuaManager::getSingleton(); + string basePath = lm->getStringFromTable("basePath", vector{Index(id + 1)}); + string meshPath = lm->getStringFromTable("meshPath", vector{Index(id + 1)}); + + model = new Model(basePath + meshPath); Root *root = Root::getSingleton(); string libPath = root->getLibPath(); @@ -64,9 +87,10 @@ namespace battleship{ } void Unit::initSound(){ - UnitDataManager *udm = UnitDataManager::getSingleton(); + LuaManager *lm = LuaManager::getSingleton(); + string name = lm->getStringFromTable("name", vector{Index(id + 1)}); selectionSfxBuffer = new sf::SoundBuffer(); - string p = GameManager::getSingleton()->getPath() + "Sounds/" + udm->getName()[id] + "s/selection.ogg"; + string p = GameManager::getSingleton()->getPath() + "Sounds/" + name + "s/selection.ogg"; if(selectionSfxBuffer->loadFromFile(p.c_str())) selectionSfx = new sf::Sound(*selectionSfxBuffer); @@ -127,7 +151,7 @@ namespace battleship{ void Unit::displayUnitStats() { float shiftedX = screenPos.x - 0.5 * lenHpBar; hpForegroundNode->setPosition(Vector3(shiftedX, screenPos.y, 0)); - hpForeground->setSize(Vector3(health / UnitDataManager::getSingleton()->getHealth()[id] * lenHpBar, 10, 0)); + hpForeground->setSize(Vector3(health / maxHealth * lenHpBar, 10, 0)); hpBackgroundNode->setPosition(Vector3(shiftedX, screenPos.y, .1)); } @@ -173,9 +197,8 @@ namespace battleship{ Vector3 linDest = pathPoints[0] + upVec * offset; Vector3 destDir = (linDest - pos).norm(); float angle = (destDir != Vector3::VEC_ZERO ? dirVec.getAngleBetween(destDir) : -1); - UnitDataManager *udm = UnitDataManager::getSingleton(); - if(angle > udm->getAnglePrecision()[id]){ + if(angle > anglePrecision){ float rotSpeed = (maxTurnAngle > angle ? angle : maxTurnAngle); if(leftVec.getAngleBetween(destDir) > PI / 2) @@ -282,11 +305,6 @@ namespace battleship{ rot = rotQuat; } - Vector3 Unit::getCorner(int i) { - Vector3 corner = UnitDataManager::getSingleton()->getUnitCornerPoints()[id][i]; - return pos + (leftVec * corner.x + upVec * corner.y - dirVec * corner.z); - } - std::vector Unit::getProjectiles(){ std::vector projectiles; InGameAppState *inGameState = ((InGameAppState*)GameManager::getSingleton()->getStateManager()->getAppStateByType((int)AppStateType::IN_GAME_STATE)); diff --git a/unit.h b/unit.h index 04e5fe9..be2bf27 100755 --- a/unit.h +++ b/unit.h @@ -9,7 +9,6 @@ #include -#include "unitDataManager.h" #include "gameManager.h" #include "projectile.h" @@ -44,8 +43,9 @@ namespace battleship{ }; enum class MoveDir {LEFT, UP, FORW}; - enum class Corner {FRONT_LEFT, FRONT_RIGHT, REAR_LEFT, REAR_RIGHT}; + enum class UnitClass {VESSEL, DESTROYER, CRUISER, AIRCRAFT_CARRIER, SUBMARINE, MISSILE_JET, DEMO_JET}; + enum class UnitType {UNDERWATER, SEA_LEVEL, LAND, AIR}; class Unit { public: @@ -59,9 +59,9 @@ namespace battleship{ void placeUnit(vb01::Vector3); void orientUnit(vb01::Quaternion); void addProjectile(Projectile*); - vb01::Vector3 getCorner(int); std::vector getProjectiles(); void addOrder(Order); + inline vb01::Vector3 getCorner(int i){return corners[i];} inline bool isSelected(){return selected;} inline bool isSelectable(){return selectable;} inline bool isDebuggable(){return debugging;} @@ -85,6 +85,7 @@ namespace battleship{ inline vb01::Vector3 getLeftVec() {return leftVec;} inline vb01::Vector3 getUpVec() {return upVec;} private: + void initProperties(); void initModel(); void initSound(); void initUnitStats(); @@ -109,13 +110,13 @@ namespace battleship{ UnitType type; vb01::Vector2 screenPos; std::vector orders; - vb01::Vector3 pos = vb01::Vector3(0, 0, 0), upVec = vb01::Vector3(0, 1, 0), dirVec = vb01::Vector3(0, 0, 1), leftVec = vb01::Vector3(1, 0, 0); + vb01::Vector3 pos = vb01::Vector3(0, 0, 0), upVec = vb01::Vector3(0, 1, 0), dirVec = vb01::Vector3(0, 0, 1), leftVec = vb01::Vector3(1, 0, 0), corners[8]; vb01::Quaternion rot = vb01::Quaternion::QUAT_W; - int health, cost, id, patrolPointId = 0, playerId; + int health, maxHealth, cost, id, patrolPointId = 0, playerId; s64 orderLineDispTime = 0; vb01::Model *model; bool selected = false, selectable, debugging = false, working = true; - float lineOfSight, speed, maxTurnAngle, range, width, height, length; + float lineOfSight, speed, maxTurnAngle, range, width, height, length, anglePrecision; void removeOrder(int); virtual void executeOrders(); diff --git a/unitDataManager.cpp b/unitDataManager.cpp deleted file mode 100644 index 373e699..0000000 --- a/unitDataManager.cpp +++ /dev/null @@ -1,79 +0,0 @@ -#include "unitDataManager.h" -#include "defConfigs.h" - -#include - -#include -#include - -namespace battleship{ - using namespace std; - using namespace vb01; - using namespace gameBase; - - UnitDataManager *unitDataManager = nullptr; - - UnitDataManager* UnitDataManager::getSingleton(){ - if(!unitDataManager) - unitDataManager = new UnitDataManager(); - - return unitDataManager; - } - - UnitDataManager::UnitDataManager(){ - LuaManager *luaManager = LuaManager::getSingleton(); - string path = GameManager::getSingleton()->getPath(); - luaManager->buildScript(vector{path + "Scripts/defPaths.lua", path + "Scripts/unitData.lua"}); - - numUnits = luaManager->getInt("numUnits"); - - health = new int[numUnits]; - speed = new float[numUnits]; - destinationOffset = new float[numUnits]; - unitType = new UnitType[numUnits]; - unitClass = new UnitClass[numUnits]; - anglePrecision = new float[numUnits]; - cost = new int[numUnits]; - maxTurnAngle = new float[numUnits]; - range = new float[numUnits]; - unitAxisLength = new float[numUnits]; - lineOfSight = new float[numUnits]; - name = new string[numUnits]; - meshPath = new string[numUnits]; - basePath = new string[numUnits]; - unitCornerPoints = new Vector3*[numUnits]; - unitCuboidDimensions = new Vector3*[numUnits]; - - for(int i = 0; i < numUnits; i++){ - health[i] = luaManager->getIntFromTable("health", vector{Index(i + 1)}); - speed[i] = luaManager->getFloatFromTable("speed", vector{Index(i + 1)}); - unitClass[i] = (UnitClass)luaManager->getIntFromTable("unitClass", vector{Index(i + 1)}); - destinationOffset[i] = luaManager->getFloatFromTable("destinationOffset", vector{Index(i + 1)}); - unitType[i] = (UnitType)luaManager->getIntFromTable("unitType", vector{Index(i + 1)}); - anglePrecision[i] = luaManager->getFloatFromTable("anglePrecision", vector{Index(i + 1)}); - cost[i] = luaManager->getIntFromTable("cost", vector{Index(i + 1)}); - maxTurnAngle[i] = luaManager->getFloatFromTable("maxTurnAngle", vector{Index(i + 1)}); - range[i] = luaManager->getFloatFromTable("range", vector{Index(i + 1)}); - unitAxisLength[i] = luaManager->getFloatFromTable("unitAxisLength", vector{Index(i + 1)}); - lineOfSight[i] = luaManager->getFloatFromTable("lineOfSight", vector{Index(i + 1)}); - name[i] = luaManager->getStringFromTable("name", vector{Index(i + 1)}); - meshPath[i] = luaManager->getStringFromTable("meshPath", vector{Index(i + 1)}); - basePath[i] = luaManager->getStringFromTable("basePath", vector{Index(i + 1)}); - - unitCornerPoints[i] = new Vector3[8]; - unitCuboidDimensions[i] = new Vector3[8]; - - for(int j = 0; j < 8; j++){ - float cornerX = luaManager->getFloatFromTable("unitCornerPoints", vector{Index(i + 1), Index(j + 1), Index("x")}); - float cornerY = luaManager->getFloatFromTable("unitCornerPoints", vector{Index(i + 1), Index(j + 1), Index("y")}); - float cornerZ = luaManager->getFloatFromTable("unitCornerPoints", vector{Index(i + 1), Index(j + 1), Index("z")}); - unitCornerPoints[i][j] = Vector3(cornerX, cornerY, cornerZ); - - float dimX = luaManager->getFloatFromTable("unitCuboidDimensions", vector{Index(i + 1), Index(j + 1), Index("x")}); - float dimY = luaManager->getFloatFromTable("unitCuboidDimensions", vector{Index(i + 1), Index(j + 1), Index("y")}); - float dimZ = luaManager->getFloatFromTable("unitCuboidDimensions", vector{Index(i + 1), Index(j + 1), Index("z")}); - unitCuboidDimensions[i][j] = Vector3(dimX, dimY, dimZ); - } - } - } -} diff --git a/unitDataManager.h b/unitDataManager.h deleted file mode 100644 index 7d76cff..0000000 --- a/unitDataManager.h +++ /dev/null @@ -1,53 +0,0 @@ -#ifndef UNIT_DATA_MANAGER_H -#define UNIT_DATA_MANAGER_H - -#include - -#include - -namespace battleship{ - enum class UnitClass {VESSEL, DESTROYER, CRUISER, AIRCRAFT_CARRIER, SUBMARINE, MISSILE_JET, DEMO_JET}; - - enum class UnitType {UNDERWATER, SEA_LEVEL, LAND, AIR}; - - class UnitDataManager{ - public: - static UnitDataManager* getSingleton(); - inline int getNumUnits(){return numUnits;} - inline int* getHealth(){return health;} - inline UnitType* getUnitType(){return unitType;} - inline UnitClass* getUnitClass(){return unitClass;} - inline float* getMaxTurnAngle(){return maxTurnAngle;} - inline float* getRange(){return range;} - inline float* getLineOfSight(){return lineOfSight;} - inline float* getSpeed(){return speed;} - inline float* getAnglePrecision(){return anglePrecision;} - inline float* getDestinationOffset(){return destinationOffset;} - inline vb01::Vector3** getUnitCornerPoints(){return unitCornerPoints;} - inline std::string* getName(){return name;} - inline std::string* getBasePath(){return basePath;} - inline std::string* getMeshPath(){return meshPath;} - private: - UnitDataManager(); - - int numUnits; - UnitType *unitType; - UnitClass *unitClass; - int *health; - float *speed; - float *destinationOffset; - float *anglePrecision; - int *cost; - float *maxTurnAngle; - float *range; - vb01::Vector3 **unitCornerPoints; - vb01::Vector3 **unitCuboidDimensions; - float *unitAxisLength; - float *lineOfSight; - std::string *name; - std::string *meshPath; - std::string *basePath; - }; -} - -#endif