Files
factory-hole-core/include/Components/Resource.hpp
2026-02-15 22:48:35 +09:00

107 lines
2.4 KiB
C++

#pragma once
#include <stdint.h>
#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<ResourceInfo>()
.member<uint16_t>("ResourceID");
world.component<ResourceHealth>()
.member<uint16_t>("MaxHealth")
.member<uint16_t>("Health")
.member<uint16_t>("RenewalTicks")
.member<uint16_t>("Renewal");
world.component<ResourceTick>()
.is_a<TickAccumulator>();
world.component<RenewingTick>()
.is_a<TickAccumulator>();
world.component<Renewing>()
.add<Freezes, ResourceTick>();
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)
{
ResourceInfo info{};
info.ResourceID = resourceID;
ResourceTick tick{};
tick.MaxTick = gatherTicks;
entity.set<ResourceInfo>(info);
entity.set<ResourceTick>(tick);
}