diff --git a/include/Components/Resource.hpp b/include/Components/Resource.hpp index 07f3888..0bcbda7 100644 --- a/include/Components/Resource.hpp +++ b/include/Components/Resource.hpp @@ -37,9 +37,7 @@ inline void Flecs_Resource(flecs::world& world) world.component() .member("MaxHealth") - .member("Health") - .member("RenewalTicks") - .member("Renewal"); + .member("Health"); world.component() .is_a(); @@ -50,9 +48,6 @@ inline void Flecs_Resource(flecs::world& world) world.component() .add(); - world.component() - .add(); - // harvesting resource to world inventory world.system() .kind(flecs::OnUpdate) @@ -70,11 +65,10 @@ inline void Flecs_Resource(flecs::world& world) }); // decrease health if ResourceHealth component - world.system() + world.system() .kind(flecs::OnUpdate) .without() .each([](ResourceHealth& health, ResourceTick tick){ - if (tick.) health.Health -= tick.Finished(); }); @@ -91,7 +85,17 @@ inline void Flecs_Resource(flecs::world& world) }); // 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) @@ -104,4 +108,21 @@ inline void Resource_Ore_Helper(const flecs::entity& entity, uint16_t resourceID 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(); } \ No newline at end of file diff --git a/tests/Components/test_Resource.cpp b/tests/Components/test_Resource.cpp index aa2cac7..1b0544a 100644 --- a/tests/Components/test_Resource.cpp +++ b/tests/Components/test_Resource.cpp @@ -41,3 +41,94 @@ TEST_SUITE("Resource") { CHECK(inv.GetItemsAmount(stoneID) >= 1); } } + +TEST_SUITE("Resource - Health & Renewing") { + TEST_CASE("tree resource loses health each gather cycle") { + WorldConfig config{}; + uint16_t woodID = config.RegisterItem("Wood"); + + WorldInstance world{ config }; + + auto entity = world.GetEcsWorld().entity(); + Resource_Tree_Helper(entity, woodID, 1, 3, 5); + + world.ProcessFrame(); + + auto& inv = world.GetEcsWorld().ensure(); + CHECK(inv.GetItemsAmount(woodID) == 1); + + auto health = entity.get(); + CHECK(health.Health == 2); + } + + TEST_CASE("tree enters renewing state when health reaches 0") { + WorldConfig config{}; + uint16_t woodID = config.RegisterItem("Wood"); + + WorldInstance world{ config }; + + auto entity = world.GetEcsWorld().entity(); + Resource_Tree_Helper(entity, woodID, 1, 2, 5); + + // 2 gather cycles to deplete health + world.ProcessFrame(); + world.ProcessFrame(); + + auto& inv = world.GetEcsWorld().ensure(); + CHECK(inv.GetItemsAmount(woodID) == 2); + CHECK(entity.has()); + + // one more tick should not produce items while renewing + world.ProcessFrame(); + CHECK(inv.GetItemsAmount(woodID) == 2); + } + + TEST_CASE("tree restores health after renewal period") { + WorldConfig config{}; + uint16_t woodID = config.RegisterItem("Wood"); + + WorldInstance world{ config }; + + auto entity = world.GetEcsWorld().entity(); + Resource_Tree_Helper(entity, woodID, 1, 1, 5); + + // 1 gather cycle depletes health + world.ProcessFrame(); + CHECK(entity.has()); + + // 5 ticks for renewal + for (int i = 0; i < 5; ++i) + world.ProcessFrame(); + + CHECK_FALSE(entity.has()); + CHECK(entity.has()); + + auto health = entity.get(); + CHECK(health.Health == health.MaxHealth); + } + + TEST_CASE("tree resumes gathering after renewal") { + WorldConfig config{}; + uint16_t woodID = config.RegisterItem("Wood"); + + WorldInstance world{ config }; + + auto entity = world.GetEcsWorld().entity(); + Resource_Tree_Helper(entity, woodID, 1, 1, 5); + + // deplete + world.ProcessFrame(); + auto& inv = world.GetEcsWorld().ensure(); + CHECK(inv.GetItemsAmount(woodID) == 1); + + // renew (5 ticks) + for (int i = 0; i < 5; ++i) + world.ProcessFrame(); + + CHECK_FALSE(entity.has()); + + // should gather again + world.ProcessFrame(); + CHECK(inv.GetItemsAmount(woodID) == 2); + } +}