diff --git a/include/Components/Inventory.hpp b/include/Components/Inventory.hpp index c5bc8ca..e298097 100644 --- a/include/Components/Inventory.hpp +++ b/include/Components/Inventory.hpp @@ -47,7 +47,7 @@ public: Slots[i] = 0; } - void Assert(uint16_t id) + void Assert(uint16_t id) const { DEV_ASSERT(id < Slots.GetSize() && id != Item::null); } diff --git a/include/Components/Resource.hpp b/include/Components/Resource.hpp index 950802e..07f3888 100644 --- a/include/Components/Resource.hpp +++ b/include/Components/Resource.hpp @@ -16,8 +16,6 @@ struct ResourceHealth { uint16_t MaxHealth{}; uint16_t Health{}; - uint16_t RenewalTicks{}; - uint16_t Renewal{}; }; struct ResourceTick : public TickAccumulator @@ -26,6 +24,12 @@ struct ResourceTick : public TickAccumulator struct Renewing {}; +struct RenewingTick : public TickAccumulator +{}; + +struct FullyGrown +{}; + inline void Flecs_Resource(flecs::world& world) { world.component() @@ -40,17 +44,53 @@ inline void Flecs_Resource(flecs::world& world) world.component() .is_a(); + world.component() + .is_a(); + world.component() .add(); - world.system() - .without() - .each([&world](ResourceInfo info, ResourceTick tick) { - if (tick.Finished()) { - auto& worldInventory = world.ensure(); - worldInventory.AddItems(info.ResourceID, 1); + world.component() + .add(); + + // harvesting resource to world inventory + world.system() + .kind(flecs::OnUpdate) + .without() + .each([](ResourceInfo info, ResourceTick tick, WorldInventory& worldInventory) { + worldInventory.AddItems(info.ResourceID, tick.Finished()); + }); + + // harvesting resource to local inventory + world.system() + .kind(flecs::OnUpdate) + .without() + .each([](ResourceInfo info, ResourceTick tick, Inventory& worldInventory) { + worldInventory.AddItems(info.ResourceID, tick.Finished()); + }); + + // decrease health if ResourceHealth component + world.system() + .kind(flecs::OnUpdate) + .without() + .each([](ResourceHealth& health, ResourceTick tick){ + if (tick.) + health.Health -= tick.Finished(); + }); + + // checking if we have to renew the resource + world.system() + .kind(flecs::OnUpdate) + .without() + .each([](flecs::entity entity, ResourceHealth health, RenewingTick& tick) { + if (health.Health == 0) { + entity.remove(); + entity.add(); + tick.AccumulatedTick = 0; } }); + + // finish renewing } diff --git a/src/Core/WorldInstance.cpp b/src/Core/WorldInstance.cpp index f43bcd3..4ecda52 100644 --- a/src/Core/WorldInstance.cpp +++ b/src/Core/WorldInstance.cpp @@ -22,6 +22,7 @@ void WorldInstance::RegisterTypes(flecs::world &world) Flecs_Item(world); Flecs_Configs(world); Flecs_Tick(world); + Flecs_Inventory(world); Flecs_Resource(world); } diff --git a/tests/Components/test_Resource.cpp b/tests/Components/test_Resource.cpp new file mode 100644 index 0000000..aa2cac7 --- /dev/null +++ b/tests/Components/test_Resource.cpp @@ -0,0 +1,43 @@ +#include +#include "Components/Configs/WorldConfig.hpp" +#include "Core/WorldInstance.h" +#include "Components/Resource.hpp" +#include "Components/Inventory.hpp" + +TEST_SUITE("Resource") { + TEST_CASE("basic resource gathering produces item after one tick") { + WorldConfig config{}; + uint16_t stoneID = config.RegisterItem("Stone"); + + WorldInstance world{ config }; + + // Create a resource entity with gatherTicks = 1 + auto entity = world.GetEcsWorld().entity(); + Resource_Ore_Helper(entity, stoneID, 1); + + world.ProcessFrame(); + + auto& inv = world.GetEcsWorld().ensure(); + CHECK(inv.GetItemsAmount(stoneID) >= 1); + } + + TEST_CASE("resource with 20 tick gather time produces item after 20 ticks") { + WorldConfig config{}; + uint16_t stoneID = config.RegisterItem("Stone"); + + WorldInstance world{ config }; + + auto entity = world.GetEcsWorld().entity(); + Resource_Ore_Helper(entity, stoneID, 20); + + for (int i = 0; i < 19; ++i) + world.ProcessFrame(); + + auto& inv = world.GetEcsWorld().ensure(); + CHECK(inv.GetItemsAmount(stoneID) == 0); + + world.ProcessFrame(); + + CHECK(inv.GetItemsAmount(stoneID) >= 1); + } +} diff --git a/tests/test_SharedBuffer.cpp b/tests/Util/test_SharedBuffer.cpp similarity index 100% rename from tests/test_SharedBuffer.cpp rename to tests/Util/test_SharedBuffer.cpp