diff --git a/CMakeLists.txt b/CMakeLists.txt index a7a592d..d9efa87 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -28,7 +28,7 @@ set(GUI tooltip.cpp concreteGuiManager.cpp ${BUTTONS} ${LISTBOXES}) set(STATES activeGameState.cpp inGameAppState.cpp guiAppState.cpp mapEditorAppState.cpp) set(UTIL util.cpp binds.h) set(CONTROLLERS gameObjectFrameController.cpp cameraController.cpp) -set(CONSOLE console.cpp abstractCommand.cpp addUnitCommand.cpp addResourceCommand.cpp addTechnologyCommand.cpp) +set(CONSOLE console.cpp abstractCommand.cpp addUnitCommand.cpp addResourceCommand.cpp addTechnologyCommand.cpp toggleDebugCommand.cpp) set(CORE gameManager.cpp game.cpp fxManager.cpp defConfigs.cpp ${CONSOLE} ${CONTROLLERS}) set(PROJECTILES projectile.cpp cruiseMissile.cpp) set(VEHICLES vehicle.cpp engineer.cpp resourceRover.cpp) diff --git a/abstractCommand.cpp b/abstractCommand.cpp index 5807c62..4c19c45 100644 --- a/abstractCommand.cpp +++ b/abstractCommand.cpp @@ -6,29 +6,28 @@ namespace battleship{ using namespace std; - vector AbstractCommand::explodeString(string commandStr){ - vector spaceIds; - vector fullCommand; - - for(int i = 0; i < commandStr.length(); i++) - if(commandStr[i] == ' ') - spaceIds.push_back(i); - - fullCommand.push_back(commandStr.substr(0, spaceIds[0])); - - for(int i = 0; i < spaceIds.size(); i++){ - bool lastSpace = (i == spaceIds.size() - 1); - string argument = commandStr.substr(spaceIds[i] + 1, lastSpace ? string::npos : spaceIds[i + 1] - spaceIds[i] - 1); - fullCommand.push_back(argument); - } - - return fullCommand; - } - void AbstractCommand::handle(){ if(cmdStr.find(" ") == -1) return; - arguments = explodeString(cmdStr); + vector spaceIds; + arguments.clear(); + + for(int i = 0; i < cmdStr.length(); i++) + if(cmdStr[i] == ' ') + spaceIds.push_back(i); + + arguments.push_back(cmdStr.substr(0, spaceIds[0])); + + for(int i = 0; i < spaceIds.size(); i++){ + bool lastSpace = (i == spaceIds.size() - 1); + string argument = cmdStr.substr(spaceIds[i] + 1, lastSpace ? string::npos : spaceIds[i + 1] - spaceIds[i] - 1); + arguments.push_back(argument); + } + } + + void AbstractCommand::execute(){ + handle(); + validate(); } } diff --git a/abstractCommand.h b/abstractCommand.h index b02f6eb..8e3a97f 100644 --- a/abstractCommand.h +++ b/abstractCommand.h @@ -8,14 +8,12 @@ namespace battleship{ class AbstractCommand{ protected: AbstractCommand(std::string str) : cmdStr(str){} - virtual void execute(){} - virtual void validate(){} virtual void handle(); + virtual void validate(){} + virtual void execute(); std::string cmdStr; std::vector arguments; - private: - std::vector explodeString(std::string); }; } diff --git a/addResourceCommand.cpp b/addResourceCommand.cpp index 3a36278..15bfe6a 100644 --- a/addResourceCommand.cpp +++ b/addResourceCommand.cpp @@ -21,14 +21,10 @@ namespace battleship{ resourceAmmount = atoi(arguments[2].c_str()); } - void AddResourceCommand::addResource(){ + void AddResourceCommand::execute(){ + AbstractCommand::execute(); + Player *player = Game::getSingleton()->getPlayer(playerId); player->updateResource(ResourceType(resourceId), resourceAmmount, true); } - - void AddResourceCommand::execute(){ - AbstractCommand::handle(); - validate(); - addResource(); - } } diff --git a/addResourceCommand.h b/addResourceCommand.h index b5c55bb..711a829 100644 --- a/addResourceCommand.h +++ b/addResourceCommand.h @@ -10,7 +10,6 @@ namespace battleship{ void execute(); private: void validate(); - void addResource(); int playerId, resourceId, resourceAmmount; }; diff --git a/addTechnologyCommand.cpp b/addTechnologyCommand.cpp index 7ff5025..73468c7 100644 --- a/addTechnologyCommand.cpp +++ b/addTechnologyCommand.cpp @@ -30,13 +30,8 @@ namespace battleship{ return; } - void AddTechnologyCommand::addTechnology(){ + void AddTechnologyCommand::execute(){ + AbstractCommand::execute(); Game::getSingleton()->getPlayer(playerId)->addTechnology(techId); } - - void AddTechnologyCommand::execute(){ - AbstractCommand::handle(); - validate(); - addTechnology(); - } } diff --git a/addTechnologyCommand.h b/addTechnologyCommand.h index d3fb32c..f60cd45 100644 --- a/addTechnologyCommand.h +++ b/addTechnologyCommand.h @@ -10,7 +10,6 @@ namespace battleship{ void execute(); private: void validate(); - void addTechnology(); int playerId, techId; }; diff --git a/addUnitCommand.cpp b/addUnitCommand.cpp index d3d0f51..c46452f 100644 --- a/addUnitCommand.cpp +++ b/addUnitCommand.cpp @@ -54,14 +54,10 @@ namespace battleship{ return; } - void AddUnitCommand::addUnit(){ + void AddUnitCommand::execute(){ + AbstractCommand::execute(); + Player* player = Game::getSingleton()->getPlayer(playerId); player->addUnit(GameObjectFactory::createUnit(player, unitId, pos, rot, 100)); } - - void AddUnitCommand::execute(){ - AbstractCommand::handle(); - validate(); - addUnit(); - } } diff --git a/addUnitCommand.h b/addUnitCommand.h index a0d5429..b91467f 100644 --- a/addUnitCommand.h +++ b/addUnitCommand.h @@ -17,8 +17,6 @@ namespace battleship{ bool posEnabled, rotEnabled; vb01::Vector3 pos = vb01::Vector3::VEC_ZERO; vb01::Quaternion rot = vb01::Quaternion::QUAT_W; - - void addUnit(); }; } diff --git a/console.cpp b/console.cpp index 58206f7..0b2c38c 100755 --- a/console.cpp +++ b/console.cpp @@ -2,13 +2,14 @@ #include "addUnitCommand.h" #include "addResourceCommand.h" #include "addTechnologyCommand.h" +#include "toggleDebugCommand.h" using namespace std; namespace battleship{ void Console::execute(string cmdStr) { int space = cmdStr.find(" "); - string cmdName, argsStr; + string cmdName = cmdStr, argsStr = ""; if(space != -1){ cmdName = cmdStr.substr(0, space); @@ -21,5 +22,7 @@ namespace battleship{ AddResourceCommand(argsStr).execute(); else if(cmdName == "add-technology") AddTechnologyCommand(argsStr).execute(); + else if(cmdName == "toggle-debug") + ToggleDebugCommand(argsStr).execute(); } } diff --git a/external/vb01 b/external/vb01 index e39f9e3..23304a1 160000 --- a/external/vb01 +++ b/external/vb01 @@ -1 +1 @@ -Subproject commit e39f9e358df6d065335c57a1adb30889e5f4ad02 +Subproject commit 23304a10e98de6193470254c73946f16a84317da diff --git a/game.cpp b/game.cpp index 203c333..4e80bb4 100644 --- a/game.cpp +++ b/game.cpp @@ -118,22 +118,24 @@ namespace battleship{ AssetManager::getSingleton()->load(gm->getPath() + gop, true); AssetManager::getSingleton()->load(gm->getPath() + vfxp, true); - vector gameObjs; + if(debug){ + vector gameObjs; - for(Player *pl : Game::getSingleton()->getPlayers()){ - for(Unit *u : pl->getUnits()) - gameObjs.push_back((GameObject*)u); + for(Player *pl : Game::getSingleton()->getPlayers()){ + for(Unit *u : pl->getUnits()) + gameObjs.push_back((GameObject*)u); - for(Projectile *proj : pl->getProjectiles()) - gameObjs.push_back((GameObject*)proj); + for(Projectile *proj : pl->getProjectiles()) + gameObjs.push_back((GameObject*)proj); - for(ResourceDeposit *dep : pl->getResourceDeposits()) - gameObjs.push_back((GameObject*)dep); + for(ResourceDeposit *dep : pl->getResourceDeposits()) + gameObjs.push_back((GameObject*)dep); + } + + for(GameObject *obj : gameObjs) + obj->reinit(); } - for(GameObject *obj : gameObjs) - obj->reinit(); - gm->getStateManager()->attachAppState(activeState); } diff --git a/game.h b/game.h index 5899048..26aa1d5 100644 --- a/game.h +++ b/game.h @@ -28,8 +28,10 @@ namespace battleship{ float calcAbilFromTech(Ability::Type, std::vector, int, int); bool isUnitUnlocked(std::vector, int); std::vector findTradeOffers(Player*, Player*); + inline void setDebug(bool d){this->debug = d;} inline void addTradeOffer(TradeOffer *to){tradeOffers.push_back(to);} inline void addPlayer(Player *pl){players.push_back(pl);} + inline bool isDebug(){return debug;} inline std::vector& getPlayers(){return players;} inline Player* getPlayer(int id){return players[id];} inline int getNumPlayers(){return players.size();} @@ -41,7 +43,7 @@ namespace battleship{ void endGame(bool); std::vector parseTechTable(int, std::string, std::string, std::string); - bool paused = false, ended = false; + bool paused = false, ended = false, debug = false; std::vector technologies; std::vector abilities; std::vector players; diff --git a/gameObject.cpp b/gameObject.cpp index d752e77..ebd5b64 100644 --- a/gameObject.cpp +++ b/gameObject.cpp @@ -51,16 +51,18 @@ namespace battleship{ //TODO remove neccessity to create a material for an invisible mesh void GameObject::initHitbox(){ - Box *box = new Box(Vector3(width, height, length)); - box->setWireframe(true); - Material *mat = new Material(Root::getSingleton()->getLibPath() + "texture"); mat->addBoolUniform("texturingEnabled", false); + mat->addBoolUniform("lightingEnabled", false); mat->addVec4Uniform("diffuseColor", Vector4(1, 1, 1, 1)); + + Box *box = new Box(Vector3(width, height, length)); + box->setWireframe(true); box->setMaterial(mat); sol::table gameObjTable = generateView()[GameObject::getGameObjTableName()][id + 1]; sol::table offsetPosTable = gameObjTable["hitboxOffset"]; + hitbox = new Node(Vector3(offsetPosTable["x"], offsetPosTable["y"], offsetPosTable["z"])); hitbox->attachMesh(box); hitbox->setVisible(true); @@ -68,7 +70,6 @@ namespace battleship{ } void GameObject::destroyHitbox(){ - hitbox->dettachMesh(0); model->dettachChild(hitbox); delete hitbox; } @@ -81,6 +82,9 @@ namespace battleship{ } void GameObject::destroyModel(){ + if(model->getMaterial()) + delete model->getMaterial(); + Root::getSingleton()->getRootNode()->dettachChild(model); delete model; } @@ -88,19 +92,17 @@ namespace battleship{ //TODO improve DEFAULT_TEXTURE handling void GameObject::initModel(bool textured){ sol::table gameObjTable = generateView()[GameObject::getGameObjTableName()][id + 1]; - string basePath = gameObjTable["basePath"]; - string meshPath = gameObjTable["meshPath"]; - + string basePath = gameObjTable["basePath"], meshPath = gameObjTable["meshPath"]; model = new Model(basePath + meshPath); - Root *root = Root::getSingleton(); - string libPath = root->getLibPath(); - Material *mat = new Material(libPath + "texture"); + Root *root = Root::getSingleton(); + Material *mat = new Material(root->getLibPath() + "texture"); if(textured){ string albedoPath = gameObjTable["albedoPath"].get_or(configData::DEFAULT_TEXTURE); string f[]{albedoPath == configData::DEFAULT_TEXTURE ? GameManager::getSingleton()->getPath() + albedoPath : basePath + albedoPath}; Texture *diffuseTexture = new Texture(f, 1, false); + mat->addBoolUniform("texturingEnabled", true); mat->addBoolUniform("lightingEnabled", true); mat->addBoolUniform("constLightingEnabled", false); diff --git a/toggleDebugCommand.cpp b/toggleDebugCommand.cpp new file mode 100644 index 0000000..5ebf9c9 --- /dev/null +++ b/toggleDebugCommand.cpp @@ -0,0 +1,15 @@ +#include "toggleDebugCommand.h" +#include "game.h" + +namespace battleship{ + void ToggleDebugCommand::validate(){ + if(!arguments.empty()) return; + } + + void ToggleDebugCommand::execute(){ + AbstractCommand::execute(); + + Game *game = Game::getSingleton(); + game->setDebug(!game->isDebug()); + } +} diff --git a/toggleDebugCommand.h b/toggleDebugCommand.h new file mode 100644 index 0000000..794a795 --- /dev/null +++ b/toggleDebugCommand.h @@ -0,0 +1,16 @@ +#ifndef TOGGLE_DEBUG_COMMAND_H +#define TOGGLE_DEBUG_COMMAND_H + +#include "abstractCommand.h" + +namespace battleship{ + class ToggleDebugCommand : public AbstractCommand{ + public: + ToggleDebugCommand(std::string str) : AbstractCommand(str){} + void validate(); + void execute(); + private: + }; +} + +#endif diff --git a/unit.cpp b/unit.cpp index 53ef3d8..17eaa84 100755 --- a/unit.cpp +++ b/unit.cpp @@ -322,9 +322,11 @@ namespace battleship{ vector currTechs = player->getTechnologies(); string name = unitTable["name"]; - health = unitTable["health"]; health += game->calcAbilFromTech(Ability::Type::HEALTH, currTechs, (int)GameObject::type, id); vehicle = unitTable["isVehicle"]; - maxHealth = health; + health += game->calcAbilFromTech(Ability::Type::HEALTH, currTechs, (int)GameObject::type, id); + maxHealth = unitTable["health"]; + + if(health == 0) health = maxHealth; lineOfSight = unitTable["lineOfSight"]; lineOfSight += game->calcAbilFromTech(Ability::Type::LINE_OF_SIGHT, currTechs, (int)GameObject::type, id); unitClass = (UnitClass)unitTable["unitClass"]; @@ -441,6 +443,9 @@ namespace battleship{ } void Unit::reinit(){ + if(losLightNode) + destroyLosLight(); + destroyWeapons(); destroyHitbox(); destroyModel(); diff --git a/unit.h b/unit.h index eaa0725..a09e3c7 100755 --- a/unit.h +++ b/unit.h @@ -176,7 +176,7 @@ namespace battleship{ UnitType type; std::vector orders; std::string guiScreen = ""; - int health, maxHealth, playerId, lenHpBar = 200; + int health = 0, maxHealth, playerId, lenHpBar = 200; vb01::s64 orderLineDispTime = 0, lastFireTime = 0; float lineOfSight; std::vector armorTypes; diff --git a/vehicle.cpp b/vehicle.cpp index 6a68d11..57fba4e 100644 --- a/vehicle.cpp +++ b/vehicle.cpp @@ -94,6 +94,11 @@ namespace battleship{ garrisonCategory = unitTable["garrisonCategory"]; } + void Vehicle::reinit(){ + Unit::reinit(); + initProperties(); + } + void Vehicle::navigate(float destOffset){ Vector3 hypVec = (pathPoints[0] - pos); float hypAngle = hypVec.norm().getAngleBetween(upVec) - PI / 2; diff --git a/vehicle.h b/vehicle.h index 6794d7b..cff6990 100644 --- a/vehicle.h +++ b/vehicle.h @@ -34,6 +34,7 @@ namespace battleship{ void removePathpoint(int = 0); void removeAllPathpoints(); void select(); + void reinit(); protected: std::vector pathPoints; bool pursuingTarget = false;