added color change upon changing a unit's player

This commit is contained in:
devZoGok
2025-06-24 12:23:56 +03:00
parent 7c4d26e08c
commit 86f73ca279
5 changed files with 46 additions and 35 deletions
+1 -1
View File
@@ -73,7 +73,7 @@ namespace battleship{
} }
if(hackStatus >= 100){ if(hackStatus >= 100){
Game::getSingleton()->changeUnitPlayer(order.targets[0].unit, player); order.targets[0].unit->changePlayer(player);
removeOrder(0); removeOrder(0);
} }
} }
-17
View File
@@ -144,23 +144,6 @@ namespace battleship{
paused = !paused; paused = !paused;
} }
void Game::changeUnitPlayer(Unit *unit, Player *newPlayer){
Player *oldPlayer = unit->getPlayer();
vector<Unit*> &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<int> Game::parseTechTable(int tid, string key, string numVarKey, string varKey){ vector<int> Game::parseTechTable(int tid, string key, string numVarKey, string varKey){
sol::state_view SOL_LUA_VIEW = generateView(); sol::state_view SOL_LUA_VIEW = generateView();
SOL_LUA_VIEW.script(numVarKey + " = #" + key + "[" + to_string(tid + 1) + "]." + varKey); SOL_LUA_VIEW.script(numVarKey + " = #" + key + "[" + to_string(tid + 1) + "]." + varKey);
-1
View File
@@ -23,7 +23,6 @@ namespace battleship{
void updateLuaPlayers(bool); void updateLuaPlayers(bool);
void togglePause(); void togglePause();
void removeAllElements(); void removeAllElements();
void changeUnitPlayer(Unit*, Player*);
void initTechnologies(); void initTechnologies();
float calcAbilFromTech(Ability::Type, std::vector<int>, int, int); float calcAbilFromTech(Ability::Type, std::vector<int>, int, int);
bool isUnitUnlocked(std::vector<int>, int); bool isUnitUnlocked(std::vector<int>, int);
+43 -16
View File
@@ -79,6 +79,29 @@ namespace battleship{
delete hitbox; 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<Mesh*> meshes = node->getMeshes();
for(Mesh *mesh : meshes)
mesh->setMaterial(colorMat);
}
}
void GameObject::initProperties(){ void GameObject::initProperties(){
sol::table sizeTable = generateView()[GameObject::getGameObjTableName()][id + 1]["size"]; sol::table sizeTable = generateView()[GameObject::getGameObjTableName()][id + 1]["size"];
width = sizeTable["x"]; width = sizeTable["x"];
@@ -123,22 +146,8 @@ namespace battleship{
model->setMaterial(mat); model->setMaterial(mat);
sol::optional<sol::table> colNodeOpt = gameObjTable["colorNodes"]; sol::optional<sol::table> colNodeOpt = gameObjTable["colorNodes"];
if(player && colNodeOpt != sol::nullopt){ if(player && colNodeOpt != sol::nullopt)
sol::table colNodeTbl = gameObjTable["colorNodes"]; useColor(player->getColorMaterial());
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<Mesh*> meshes = node->getMeshes();
for(Mesh *mesh : meshes)
mesh->setMaterial(player->getColorMaterial());
}
}
root->getRootNode()->attachChild(model); root->getRootNode()->attachChild(model);
} }
@@ -179,6 +188,24 @@ namespace battleship{
return within; return within;
} }
//TODO improve for other game objs, too
void GameObject::changePlayer(Player *newPlayer){
vector<Unit*> &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(){ string GameObject::getGameObjTableName(){
switch(type){ switch(type){
case GameObject::Type::UNIT: case GameObject::Type::UNIT:
+2
View File
@@ -29,6 +29,7 @@ namespace battleship{
void updateGameStats(Unit*); void updateGameStats(Unit*);
static sf::Sound* prepareSfx(sf::SoundBuffer*, std::string); static sf::Sound* prepareSfx(sf::SoundBuffer*, std::string);
bool pointWithinObj(vb01::Vector3, float = 0, float = 0, bool = false, float = 0); bool pointWithinObj(vb01::Vector3, float = 0, float = 0, bool = false, float = 0);
void changePlayer(Player *newPlayer);
inline vb01::Vector2 getScreenPos(){return screenPos;} inline vb01::Vector2 getScreenPos(){return screenPos;}
inline vb01::Vector3 getCorner(int i){return corners[i];} inline vb01::Vector3 getCorner(int i){return corners[i];}
inline bool isSelectable(){return selectable;} inline bool isSelectable(){return selectable;}
@@ -50,6 +51,7 @@ namespace battleship{
inline vb01::Node* getHitbox(){return hitbox;} inline vb01::Node* getHitbox(){return hitbox;}
inline bool isRemove(){return remove;} inline bool isRemove(){return remove;}
protected: protected:
virtual void useColor(vb01::Material*);
virtual void initProperties(); virtual void initProperties();
virtual void destroyModel(); virtual void destroyModel();
virtual void initModel(bool = true); virtual void initModel(bool = true);