health
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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<ResourceInfo>()
|
||||
@@ -40,18 +44,54 @@ inline void Flecs_Resource(flecs::world& world)
|
||||
world.component<ResourceTick>()
|
||||
.is_a<TickAccumulator>();
|
||||
|
||||
world.component<RenewingTick>()
|
||||
.is_a<TickAccumulator>();
|
||||
|
||||
world.component<Renewing>()
|
||||
.add<Freezes, ResourceTick>();
|
||||
|
||||
world.system<ResourceInfo, ResourceTick>()
|
||||
.without<ResourceHealth>()
|
||||
.each([&world](ResourceInfo info, ResourceTick tick) {
|
||||
if (tick.Finished()) {
|
||||
auto& worldInventory = world.ensure<WorldInventory>();
|
||||
worldInventory.AddItems(info.ResourceID, 1);
|
||||
world.component<Renewing>()
|
||||
.add<Freezes, ResourceTick>();
|
||||
|
||||
// harvesting resource to world inventory
|
||||
world.system<const ResourceInfo, const ResourceTick, WorldInventory>()
|
||||
.kind(flecs::OnUpdate)
|
||||
.without<Renewing>()
|
||||
.each([](ResourceInfo info, ResourceTick tick, WorldInventory& worldInventory) {
|
||||
worldInventory.AddItems(info.ResourceID, tick.Finished());
|
||||
});
|
||||
|
||||
// harvesting resource to local inventory
|
||||
world.system<const ResourceInfo, const ResourceTick, Inventory>()
|
||||
.kind(flecs::OnUpdate)
|
||||
.without<Renewing>()
|
||||
.each([](ResourceInfo info, ResourceTick tick, Inventory& worldInventory) {
|
||||
worldInventory.AddItems(info.ResourceID, tick.Finished());
|
||||
});
|
||||
|
||||
// decrease health if ResourceHealth component
|
||||
world.system<const ResourceHealth, const ResourceTick>()
|
||||
.kind(flecs::OnUpdate)
|
||||
.without<Renewing>()
|
||||
.each([](ResourceHealth& health, ResourceTick tick){
|
||||
if (tick.)
|
||||
health.Health -= tick.Finished();
|
||||
});
|
||||
|
||||
// checking if we have to renew the resource
|
||||
world.system<const ResourceHealth, RenewingTick>()
|
||||
.kind(flecs::OnUpdate)
|
||||
.without<Renewing>()
|
||||
.each([](flecs::entity entity, ResourceHealth health, RenewingTick& tick) {
|
||||
if (health.Health == 0) {
|
||||
entity.remove<FullyGrown>();
|
||||
entity.add<Renewing>();
|
||||
tick.AccumulatedTick = 0;
|
||||
}
|
||||
});
|
||||
|
||||
// finish renewing
|
||||
|
||||
}
|
||||
|
||||
inline void Resource_Ore_Helper(const flecs::entity& entity, uint16_t resourceID, uint16_t gatherTicks)
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
43
tests/Components/test_Resource.cpp
Normal file
43
tests/Components/test_Resource.cpp
Normal file
@@ -0,0 +1,43 @@
|
||||
#include <doctest/doctest.h>
|
||||
#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<WorldInventory>();
|
||||
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<WorldInventory>();
|
||||
CHECK(inv.GetItemsAmount(stoneID) == 0);
|
||||
|
||||
world.ProcessFrame();
|
||||
|
||||
CHECK(inv.GetItemsAmount(stoneID) >= 1);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user