riversong code showcase
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
using Cysharp.Threading.Tasks;
|
||||
using Unity.Mathematics;
|
||||
|
||||
namespace DanieleMarotta.RiversongCodeShowcase
|
||||
{
|
||||
public class MakeLiveIntentExecutionLogic : IntentExecutionLogic
|
||||
{
|
||||
private ITileSpace _tileSpace;
|
||||
|
||||
private IAgentVisualizationCollection _visualizationCollection;
|
||||
|
||||
public override UniTask InitializeAsync(IServiceLocator serviceLocator)
|
||||
{
|
||||
_tileSpace = serviceLocator.GetService<ITileSpace>();
|
||||
_visualizationCollection = serviceLocator.GetService<IAgentVisualizationCollection>();
|
||||
|
||||
return UniTask.CompletedTask;
|
||||
}
|
||||
|
||||
public override IntentExecutionResult Execute(Agent agent, in AgentIntent intent)
|
||||
{
|
||||
switch (agent.LifecycleState)
|
||||
{
|
||||
case AgentLifecycleState.DeSpawning:
|
||||
return IntentExecutionResult.Failure;
|
||||
|
||||
case AgentLifecycleState.Live:
|
||||
return IntentExecutionResult.Success;
|
||||
}
|
||||
|
||||
if (!_visualizationCollection.TryGetVisualization(agent.Id, out _)) return IntentExecutionResult.InProgress;
|
||||
|
||||
agent.LifecycleState = AgentLifecycleState.Live;
|
||||
|
||||
SetInitialHeading(agent);
|
||||
|
||||
return IntentExecutionResult.Success;
|
||||
}
|
||||
|
||||
private void SetInitialHeading(Agent agent)
|
||||
{
|
||||
ref var path = ref agent.GetPathRW();
|
||||
if (path.StepCount <= 1) return;
|
||||
|
||||
var p0 = _tileSpace.TileToWorld(path.Steps[0]);
|
||||
var p1 = _tileSpace.TileToWorld(path.Steps[1]);
|
||||
agent.Heading = math.normalizesafe(p1 - p0, math.forward());
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user