42 lines
1.5 KiB
C#
42 lines
1.5 KiB
C#
using Cysharp.Threading.Tasks;
|
|
using Unity.Mathematics;
|
|
using UnityEngine;
|
|
|
|
namespace DanieleMarotta.RiversongCodeShowcase
|
|
{
|
|
public class FollowPathIntentExecutionLogic : IntentExecutionLogic
|
|
{
|
|
private ITileSpace _tileSpace;
|
|
|
|
private IAgentCommonLogic _agentCommonLogic;
|
|
|
|
public override UniTask InitializeAsync(IServiceLocator serviceLocator)
|
|
{
|
|
_tileSpace = serviceLocator.GetService<ITileSpace>();
|
|
_agentCommonLogic = serviceLocator.GetService<IAgentCommonLogic>();
|
|
|
|
return UniTask.CompletedTask;
|
|
}
|
|
|
|
public override IntentExecutionResult OnStartingIntent(Agent agent, in AgentIntent intent)
|
|
{
|
|
agent.PathIndex = 0;
|
|
|
|
return IntentExecutionResult.Success;
|
|
}
|
|
|
|
public override IntentExecutionResult Execute(Agent agent, in AgentIntent intent)
|
|
{
|
|
ref var path = ref agent.GetPathRW();
|
|
if (path.StepCount <= 1) return IntentExecutionResult.Success;
|
|
|
|
var nextTile = path.Steps[agent.PathIndex + 1];
|
|
var targetPosition = (float3)_tileSpace.TileToWorld(nextTile);
|
|
var targetReached = _agentCommonLogic.MoveAgent(agent, targetPosition, Time.deltaTime);
|
|
|
|
var lastStepReached = targetReached && ++agent.PathIndex >= path.StepCount - 1 - intent.StopBeforeGoalStepCount;
|
|
return lastStepReached ? IntentExecutionResult.Success : IntentExecutionResult.InProgress;
|
|
}
|
|
}
|
|
}
|