#pragma once #include #include "flecs.h" #include "Tick.hpp" #include "Inventory.hpp" struct ResourceInfo { uint16_t ResourceID; }; struct ResourceHealth { uint16_t MaxHealth{}; uint16_t Health{}; }; struct ResourceTick : public TickAccumulator {}; struct Renewing {}; struct RenewingTick : public TickAccumulator {}; struct FullyGrown {}; inline void Flecs_Resource(flecs::world& world) { world.component() .member("ResourceID"); world.component() .member("MaxHealth") .member("Health"); world.component() .is_a(); world.component() .is_a(); world.component() .add(); // harvesting resource to inventory world.system() .kind(flecs::OnUpdate) .without() .each([](ResourceInfo info, ResourceTick tick, WorldInventory& worldInventory, Inventory* optionalInventory) { if (tick.Finished()) { bool pushToLocalInventory = optionalInventory && !optionalInventory->IsFull(info.ResourceID); if (pushToLocalInventory) optionalInventory->AddItems(info.ResourceID, 1); else worldInventory.AddItems(info.ResourceID, 1); } }); // decrease health if ResourceHealth component world.system() .kind(flecs::OnUpdate) .without() .each([](ResourceHealth& health, ResourceTick tick){ health.Health -= tick.Finished(); }); // checking if we have to renew the resource world.system() .kind(flecs::OnUpdate) .with() .without() .each([](flecs::entity entity, ResourceHealth health) { if (health.Health == 0) { entity.remove(); entity.add(); entity.ensure().AccumulatedTick = 0; } }); // finish renewing world.system("Finish Renewing") .kind(flecs::OnUpdate) .with() .each([](flecs::entity entity, ResourceHealth& health, RenewingTick& tick) { if (tick.Finished()) { health.Health = health.MaxHealth; entity.remove(); entity.add(); entity.ensure().AccumulatedTick = 0; } }); } inline void Resource_Ore_Helper(const flecs::entity& entity, uint16_t resourceID, uint16_t gatherTicks) { ResourceInfo info{}; info.ResourceID = resourceID; ResourceTick tick{}; tick.MaxTick = gatherTicks; entity.set(info); entity.set(tick); } inline void Resource_Tree_Helper(const flecs::entity& entity, uint16_t resourceID, uint16_t gatherTicks, uint16_t maxHealth, uint16_t renewalTicks) { Resource_Ore_Helper(entity, resourceID, gatherTicks); ResourceHealth health{}; health.MaxHealth = maxHealth; health.Health = maxHealth; RenewingTick tick{}; tick.MaxTick = renewalTicks; entity.set(health); entity.set(tick); entity.add(); }