From 86f73ca2793f31957bfa7e4b68ac76aa2a9248ae Mon Sep 17 00:00:00 2001 From: devZoGok Date: Tue, 24 Jun 2025 12:23:56 +0300 Subject: [PATCH] added color change upon changing a unit's player --- engineer.cpp | 2 +- game.cpp | 17 --------------- game.h | 1 - gameObject.cpp | 59 ++++++++++++++++++++++++++++++++++++-------------- gameObject.h | 2 ++ 5 files changed, 46 insertions(+), 35 deletions(-) diff --git a/engineer.cpp b/engineer.cpp index f509ba9..937cbcb 100644 --- a/engineer.cpp +++ b/engineer.cpp @@ -73,7 +73,7 @@ namespace battleship{ } if(hackStatus >= 100){ - Game::getSingleton()->changeUnitPlayer(order.targets[0].unit, player); + order.targets[0].unit->changePlayer(player); removeOrder(0); } } diff --git a/game.cpp b/game.cpp index 2eb58de..4b2256d 100644 --- a/game.cpp +++ b/game.cpp @@ -144,23 +144,6 @@ namespace battleship{ paused = !paused; } - void Game::changeUnitPlayer(Unit *unit, Player *newPlayer){ - Player *oldPlayer = unit->getPlayer(); - vector &oldPlayerUnits = oldPlayer->getUnits(); - int oldId = -1; - - for(int i = 0; i < oldPlayerUnits.size(); i++) - if(oldPlayerUnits[i] == unit){ - oldId = i; - break; - } - - oldPlayerUnits.erase(oldPlayerUnits.begin() + oldId); - newPlayer->addUnit(unit); - unit->setPlayer(newPlayer); - unit->halt(); - } - vector Game::parseTechTable(int tid, string key, string numVarKey, string varKey){ sol::state_view SOL_LUA_VIEW = generateView(); SOL_LUA_VIEW.script(numVarKey + " = #" + key + "[" + to_string(tid + 1) + "]." + varKey); diff --git a/game.h b/game.h index e5a283d..d2bd4e5 100644 --- a/game.h +++ b/game.h @@ -23,7 +23,6 @@ namespace battleship{ void updateLuaPlayers(bool); void togglePause(); void removeAllElements(); - void changeUnitPlayer(Unit*, Player*); void initTechnologies(); float calcAbilFromTech(Ability::Type, std::vector, int, int); bool isUnitUnlocked(std::vector, int); diff --git a/gameObject.cpp b/gameObject.cpp index f895437..3370a4e 100644 --- a/gameObject.cpp +++ b/gameObject.cpp @@ -79,6 +79,29 @@ namespace battleship{ delete hitbox; } + //TODO use a vector for the color nodes + void GameObject::useColor(Material *colorMat){ + if(hitbox) model->dettachChild(hitbox); + model->setMaterial(model->getMaterial()); + if(hitbox) model->attachChild(hitbox); + + sol::table gameObjTable = generateView()[GameObject::getGameObjTableName()][id + 1]; + sol::table colNodeTbl = gameObjTable["colorNodes"]; + int numColorNodes = colNodeTbl.size(); + + for(int i = 0; i < numColorNodes; i++){ + string name = colNodeTbl[i + 1]; + Node *node = model->findDescendant(name, true); + + if(!node) continue; + + vector meshes = node->getMeshes(); + + for(Mesh *mesh : meshes) + mesh->setMaterial(colorMat); + } + } + void GameObject::initProperties(){ sol::table sizeTable = generateView()[GameObject::getGameObjTableName()][id + 1]["size"]; width = sizeTable["x"]; @@ -123,22 +146,8 @@ namespace battleship{ model->setMaterial(mat); sol::optional colNodeOpt = gameObjTable["colorNodes"]; - if(player && colNodeOpt != sol::nullopt){ - sol::table colNodeTbl = gameObjTable["colorNodes"]; - int numColorNodes = colNodeTbl.size(); - - for(int i = 0; i < numColorNodes; i++){ - string name = colNodeTbl[i + 1]; - Node *node = model->findDescendant(name, true); - - if(!node) continue; - - vector meshes = node->getMeshes(); - - for(Mesh *mesh : meshes) - mesh->setMaterial(player->getColorMaterial()); - } - } + if(player && colNodeOpt != sol::nullopt) + useColor(player->getColorMaterial()); root->getRootNode()->attachChild(model); } @@ -179,6 +188,24 @@ namespace battleship{ return within; } + //TODO improve for other game objs, too + void GameObject::changePlayer(Player *newPlayer){ + vector &oldPlayerUnits = player->getUnits(); + int oldId = -1; + + for(int i = 0; i < oldPlayerUnits.size(); i++) + if(oldPlayerUnits[i] == (Unit*)this){ + oldId = i; + break; + } + + oldPlayerUnits.erase(oldPlayerUnits.begin() + oldId); + newPlayer->addUnit((Unit*)this); + setPlayer(newPlayer); + useColor(newPlayer->getColorMaterial()); + ((Unit*)this)->halt(); + } + string GameObject::getGameObjTableName(){ switch(type){ case GameObject::Type::UNIT: diff --git a/gameObject.h b/gameObject.h index 8be2d56..e88e899 100644 --- a/gameObject.h +++ b/gameObject.h @@ -29,6 +29,7 @@ namespace battleship{ void updateGameStats(Unit*); static sf::Sound* prepareSfx(sf::SoundBuffer*, std::string); bool pointWithinObj(vb01::Vector3, float = 0, float = 0, bool = false, float = 0); + void changePlayer(Player *newPlayer); inline vb01::Vector2 getScreenPos(){return screenPos;} inline vb01::Vector3 getCorner(int i){return corners[i];} inline bool isSelectable(){return selectable;} @@ -50,6 +51,7 @@ namespace battleship{ inline vb01::Node* getHitbox(){return hitbox;} inline bool isRemove(){return remove;} protected: + virtual void useColor(vb01::Material*); virtual void initProperties(); virtual void destroyModel(); virtual void initModel(bool = true);