I am trying to figure out a way to setup my data so I don't need to write repetitive code for each **need **that an agent requires.
For each new **need **, means I have to write another loop with largely the same code except for applying a different tag component. The repeitiveness of the code suggests to me there is probably a better way to set this up.
EntityCommandBuffer _ecb;
public void OnUpdate(ref SystemState ss)
{
_ecb = new(Unity.Collections.Allocator.Temp);
float _rate = 0.01f;
int _threshold = 20;
var delta = SystemAPI.Time.DeltaTime * _rate;
foreach (var (h, e) in SystemAPI.Query<RefRW<Hydration>>().WithEntityAccess()) {
var value = h.ValueRW.Value;
value -= delta;
value = math.clamp(value, 0f, 100f);
h.ValueRW.Value = value;
if (h.ValueRO.Value <= _threshold)
_ecb.AddComponent<ThirstyTag>(e);
else
_ecb.RemoveComponent<ThirstyTag>(e);
}
foreach (var (h, e) in SystemAPI.Query<RefRW<Hunger>>().WithEntityAccess())
{
var value = h.ValueRW.Value;
value -= delta;
value = math.clamp(value, 0f, 100f);
h.ValueRW.Value = value;
if (h.ValueRO.Value <= _threshold)
_ecb.AddComponent<HungryTag>(e);
else
_ecb.RemoveComponent<HungryTag>(e);
}
_ecb.Playback(ss.EntityManager);
_ecb.Dispose();
}