57 lines
1.4 KiB
C++
57 lines
1.4 KiB
C++
#include "ScrapSystem.h"
|
|
|
|
#include "DespawnAtComponent.h"
|
|
#include "EntityAdmin.h"
|
|
#include "PositionComponent.h"
|
|
#include "ScrapDataComponent.h"
|
|
|
|
ScrapSystem::ScrapSystem(EntityAdmin& admin)
|
|
: m_admin(admin)
|
|
{
|
|
}
|
|
|
|
entt::entity ScrapSystem::spawn(QVector2D position, int amount, Tick despawnAt)
|
|
{
|
|
return m_admin.spawnScrap(position, amount, despawnAt);
|
|
}
|
|
|
|
void ScrapSystem::tickDespawn(Tick currentTick)
|
|
{
|
|
std::vector<entt::entity> expired;
|
|
m_admin.forEach<DespawnAtComponent>(
|
|
[&expired, currentTick](entt::entity e, DespawnAtComponent& d)
|
|
{
|
|
if (d.tick <= currentTick)
|
|
{
|
|
expired.push_back(e);
|
|
}
|
|
});
|
|
|
|
for (entt::entity e : expired)
|
|
{
|
|
m_admin.destroy(e);
|
|
}
|
|
}
|
|
|
|
std::optional<int> ScrapSystem::consume(entt::entity entity)
|
|
{
|
|
if (!m_admin.isValid(entity) || !m_admin.hasAll<ScrapDataComponent>(entity))
|
|
{
|
|
return std::nullopt;
|
|
}
|
|
int amount = m_admin.get<ScrapDataComponent>(entity).amount;
|
|
m_admin.destroy(entity);
|
|
return amount;
|
|
}
|
|
|
|
std::vector<ScrapInfo> ScrapSystem::allScrapInfo() const
|
|
{
|
|
std::vector<ScrapInfo> result;
|
|
m_admin.forEach<ScrapDataComponent>(
|
|
[&result, this](entt::entity e, const ScrapDataComponent& /*sd*/)
|
|
{
|
|
result.push_back(ScrapInfo{e, m_admin.get<PositionComponent>(e).value});
|
|
});
|
|
return result;
|
|
}
|