1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-11-25 22:42:04 +02:00

PlayerState now stores all objects owned by player

This commit is contained in:
Ivan Savenko
2024-08-24 22:21:26 +00:00
parent 0fd9dbf240
commit a481f07daf
21 changed files with 184 additions and 113 deletions

View File

@@ -10,6 +10,9 @@
#include "StdInc.h"
#include "CPlayerState.h"
#include "mapObjects/CGDwelling.h"
#include "mapObjects/CGTownInstance.h"
#include "mapObjects/CGHeroInstance.h"
#include "gameState/QuestInfo.h"
#include "texts/CGeneralTextHandler.h"
#include "VCMI_Lib.h"
@@ -90,4 +93,66 @@ int PlayerState::getResourceAmount(int type) const
return vstd::atOrDefault(resources, static_cast<size_t>(type), 0);
}
std::vector<const CGDwelling* > PlayerState::getDwellings() const
{
std::vector<const CGDwelling* > result;
for (auto const & object : ownedObjects)
{
auto dwelling = dynamic_cast<const CGDwelling *>(object);
auto town = dynamic_cast<const CGTownInstance *>(object);
if (dwelling && !town)
result.push_back(dwelling);
}
return result;
}
template<typename T>
std::vector<T> PlayerState::getObjectsOfType() const
{
std::vector<T> result;
for (auto const & object : ownedObjects)
{
auto casted = dynamic_cast<T>(object);
if (casted)
result.push_back(casted);
}
return result;
}
std::vector<const CGHeroInstance *> PlayerState::getHeroes() const
{
return getObjectsOfType<const CGHeroInstance *>();
}
std::vector<const CGTownInstance *> PlayerState::getTowns() const
{
return getObjectsOfType<const CGTownInstance *>();
}
std::vector<CGHeroInstance *> PlayerState::getHeroes()
{
return getObjectsOfType<CGHeroInstance *>();
}
std::vector<CGTownInstance *> PlayerState::getTowns()
{
return getObjectsOfType<CGTownInstance *>();
}
std::vector<const CGObjectInstance *> PlayerState::getOwnedObjects() const
{
return {ownedObjects.begin(), ownedObjects.end()};
}
void PlayerState::addOwnedObject(CGObjectInstance * object)
{
ownedObjects.push_back(object);
}
void PlayerState::removeOwnedObject(CGObjectInstance * object)
{
vstd::erase(ownedObjects, object);
}
VCMI_LIB_NAMESPACE_END