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(); _agentCommonLogic = serviceLocator.GetService(); 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; } } }