mirror of
https://github.com/devZoGok/PlanetFleet.git
synced 2026-08-26 19:43:29 +00:00
Set max unit groups and fixed unit destruction bug
This commit is contained in:
+42
-6
@@ -829,21 +829,57 @@ namespace battleship{
|
||||
if(controlPressed)
|
||||
{
|
||||
// Ctrl + group number + Q assigns the current selection of units
|
||||
unitGroups[pendingGroup] = mainPlayer->getSelectedUnits();
|
||||
|
||||
// If there are currently maximum allowed units in existence then do not allow creation of a new unique group, only allow overwritting an existing group
|
||||
if(static_cast<int>(unitGroups.size()) >= maxUnitGroups)
|
||||
{
|
||||
// See if pendingGroup is equal to any key and overwrite the key if so
|
||||
auto it = unitGroups.find(pendingGroup);
|
||||
if(it != unitGroups.end())
|
||||
{
|
||||
unitGroups[pendingGroup] = mainPlayer->getSelectedUnits();
|
||||
}
|
||||
// Iterate through all the keys to see if pendingGroup is equal to any key and overwrite the key if so
|
||||
// for(const auto& [key, value]: unitGroups)
|
||||
// {
|
||||
// if(key == pendingGroup)
|
||||
// {
|
||||
// unitGroups[pendingGroup] = mainPlayer->getSelectedUnits();
|
||||
// }
|
||||
// }
|
||||
// If it can't overwrite then the pendingGroup will just be reset since this will bypass the rest of the if statements
|
||||
}
|
||||
else
|
||||
{
|
||||
unitGroups[pendingGroup] = mainPlayer->getSelectedUnits();
|
||||
}
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
// Pressing a number and Q without shifting replaces the unit selection with the unit group for the number pressed
|
||||
if(!shiftPressed)
|
||||
{
|
||||
deselectUnits();
|
||||
mainPlayer->selectUnits(unitGroups[pendingGroup]);
|
||||
auto it = unitGroups.find(pendingGroup);
|
||||
if(it != unitGroups.end())
|
||||
{
|
||||
deselectUnits();
|
||||
mainPlayer->selectUnits(it->second);
|
||||
}
|
||||
// deselectUnits();
|
||||
// mainPlayer->selectUnits(unitGroups[pendingGroup]);
|
||||
}
|
||||
// If shift is pressed then shift plus a number and Q adds the selection to the current selection
|
||||
// If shift is pressed then shift plus a number and Q adds the selection to the current group of the existing key
|
||||
else
|
||||
{
|
||||
mainPlayer->selectUnits(unitGroups[pendingGroup]);
|
||||
unitGroups[pendingGroup] = mainPlayer->getSelectedUnits();
|
||||
auto it = unitGroups.find(pendingGroup);
|
||||
if(it != unitGroups.end())
|
||||
{
|
||||
mainPlayer->selectUnits(it->second);
|
||||
unitGroups[pendingGroup] = mainPlayer->getSelectedUnits();
|
||||
}
|
||||
// mainPlayer->selectUnits(unitGroups[pendingGroup]);
|
||||
// unitGroups[pendingGroup] = mainPlayer->getSelectedUnits();
|
||||
}
|
||||
}
|
||||
// Reset the pending group
|
||||
|
||||
@@ -51,6 +51,7 @@ namespace battleship{
|
||||
inline std::vector<vb01::Node*> getGuiRects(){return guiRects;}
|
||||
inline std::vector<vb01::Text*> getGuiTexts(){return guiTexts;}
|
||||
inline Player* getPlayer(){return mainPlayer;}
|
||||
inline std::unordered_map<int, std::vector<Unit*>>& getUnitGroups(){return unitGroups;} // Returns the unitGroups by reference
|
||||
// inline std::vector<Unit*>& getUnitGroup(int i){return unitGroups[i];}
|
||||
inline void setBuildableStructSelected(bool bss){this->buildableStructSelected = bss;}
|
||||
inline bool isBuildableStructSelected(){return buildableStructSelected;}
|
||||
@@ -96,6 +97,7 @@ namespace battleship{
|
||||
bool shiftPressed = false;
|
||||
bool controlPressed = false;
|
||||
int pendingGroup = -1; // The group value entered to save and select a group of units. -1 means no units have been selected
|
||||
int maxUnitGroups = 100; // There can be a max of 100 unit groups at a time
|
||||
bool selectMouseClicked = false;
|
||||
bool orderMouseClicked = false;
|
||||
bool buildableStructSelected = false;
|
||||
|
||||
@@ -12,6 +12,8 @@
|
||||
#include <glm.hpp>
|
||||
#include <ext.hpp>
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
#include "unit.h"
|
||||
#include "weapon.h"
|
||||
#include "util.h"
|
||||
@@ -64,6 +66,7 @@ namespace battleship{
|
||||
destroySound();
|
||||
destroyHitbox();
|
||||
destroyModel();
|
||||
removeFromUnitGroup();
|
||||
}
|
||||
|
||||
void Unit::initProperties(){
|
||||
@@ -172,6 +175,23 @@ namespace battleship{
|
||||
losLightNode = nullptr;
|
||||
}
|
||||
|
||||
/// @brief Check if unit is in a unit group and if so remove it
|
||||
void Unit::removeFromUnitGroup(){
|
||||
ActiveGameState *activeState = (ActiveGameState*)GameManager::getSingleton()->getStateManager()->getAppStateByType(AppStateType::ACTIVE_STATE);
|
||||
std::unordered_map<int, std::vector<Unit*>>& unitGroups = activeState->getUnitGroups();
|
||||
|
||||
// Iterate through all the individual unit groups and see if this unit is in any of them
|
||||
for(auto& [key, value]: unitGroups)
|
||||
{
|
||||
if(std::find(value.begin(), value.end(), this) != value.end())
|
||||
{
|
||||
//std::erase(value, this);
|
||||
value.erase(std::remove(value.begin(), value.end(), this), value.end());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
int Unit::getNumFreeGarrisonSlots(){
|
||||
int numFreeSlots = 0;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user