riversong code showcase
This commit is contained in:
52
Source/Riversong/Game/World/Agents/AgentsCleanUpSystem.cs
Normal file
52
Source/Riversong/Game/World/Agents/AgentsCleanUpSystem.cs
Normal file
@@ -0,0 +1,52 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace DanieleMarotta.RiversongCodeShowcase
|
||||
{
|
||||
[GameSystemGroup(typeof(DefaultAgentsSystemGroup))]
|
||||
[UpdateAfter(typeof(AgentManagerSystem))]
|
||||
public class AgentsCleanUpSystem : GameSystem, IUpdatable
|
||||
{
|
||||
[InjectService]
|
||||
private IEntityCollection _entityCollection;
|
||||
|
||||
public AgentsCleanUpSystem(IServiceLocator serviceLocator) : base(serviceLocator)
|
||||
{
|
||||
}
|
||||
|
||||
public void Update()
|
||||
{
|
||||
foreach (IAgentSourceEntity agentSource in _entityCollection.GetInternalEntityList(typeof(ConstructionSite))) CleanUpAgentSource(agentSource);
|
||||
foreach (IAgentSourceEntity agentSource in _entityCollection.GetInternalEntityList(typeof(Building))) CleanUpAgentSource(agentSource);
|
||||
foreach (IAgentSourceEntity agentSource in _entityCollection.GetInternalEntityList(typeof(CritterHerd))) CleanUpAgentSource(agentSource);
|
||||
}
|
||||
|
||||
private void CleanUpAgentSource(IAgentSourceEntity agentSource)
|
||||
{
|
||||
ref var agentSourceState = ref agentSource.GetAgentSourceStateRW();
|
||||
|
||||
for (var i = agentSourceState.AgentIds.Length - 1; i >= 0; i--)
|
||||
{
|
||||
var id = agentSourceState.AgentIds[i];
|
||||
|
||||
var agent = _entityCollection.Get<Agent>(id);
|
||||
if (agent is not { LifecycleState: AgentLifecycleState.DeSpawning }) continue;
|
||||
|
||||
ref var jobState = ref agent.GetJobStateRW();
|
||||
var job = jobState.Job;
|
||||
|
||||
var count = agentSourceState.AgentCount[(int)job];
|
||||
if (count > 0)
|
||||
{
|
||||
agentSourceState.AgentCount[(int)job] = (byte)(count - 1);
|
||||
}
|
||||
else
|
||||
{
|
||||
var home = _entityCollection.Get<Building>(agent.HomeId);
|
||||
Debug.LogError($"Agent count <= 0 when de-spawning agent. Home: '{home.Definition.name}', Job: '{job}'");
|
||||
}
|
||||
|
||||
agentSourceState.AgentIds.RemoveAtSwapBack(i);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user