58 lines
2.4 KiB
C#
58 lines
2.4 KiB
C#
using Cysharp.Threading.Tasks;
|
|
using Unity.Mathematics;
|
|
using UnityEngine;
|
|
using UnityEngine.Pool;
|
|
|
|
namespace DanieleMarotta.RiversongCodeShowcase
|
|
{
|
|
public class MoveToHarvestPositionIntentExecutionLogic : IntentExecutionLogic
|
|
{
|
|
private GameConfig _config;
|
|
|
|
private World _world;
|
|
|
|
private IAgentCommonLogic _agentCommonLogic;
|
|
|
|
public override UniTask InitializeAsync(IServiceLocator serviceLocator)
|
|
{
|
|
_config = serviceLocator.GetService<GameConfig>();
|
|
_world = serviceLocator.GetService<World>();
|
|
_agentCommonLogic = serviceLocator.GetService<IAgentCommonLogic>();
|
|
|
|
return UniTask.CompletedTask;
|
|
}
|
|
|
|
public override IntentExecutionResult OnStartingIntent(Agent agent, in AgentIntent intent)
|
|
{
|
|
ref var path = ref agent.GetPathRW();
|
|
var tile = path.Steps[^1];
|
|
|
|
using var resourceNodesScope = ListPool<(int, bool)>.Get(out var resourceNodeIds);
|
|
_world.RawResources.GetResourceNodes(TileRect.OneTile(tile), _config.GeneralSettings.BaseElevation, resourceNodeIds);
|
|
|
|
if (resourceNodeIds.Count <= 0) return IntentExecutionResult.Failure;
|
|
|
|
var (id, _) = resourceNodeIds[0];
|
|
_world.RawResources.TryGetResourceNode(id, out var resourceNode);
|
|
|
|
ref var jobState = ref agent.GetJobStateRW();
|
|
jobState.Harvester.ResourceNodeId = id;
|
|
jobState.Harvester.TargetPosition = resourceNode.Position + math.normalizesafe(agent.Position - resourceNode.Position) * intent.GenericFloatParam0;
|
|
|
|
return IntentExecutionResult.Success;
|
|
}
|
|
|
|
public override IntentExecutionResult Execute(Agent agent, in AgentIntent intent)
|
|
{
|
|
ref var jobState = ref agent.GetJobStateRW();
|
|
|
|
var targetReached = _agentCommonLogic.MoveAgent(agent, jobState.Harvester.TargetPosition, Time.deltaTime);
|
|
if (!targetReached) return IntentExecutionResult.InProgress;
|
|
|
|
if (!_world.RawResources.TryGetResourceNode(jobState.Harvester.ResourceNodeId, out var resourceNode)) return IntentExecutionResult.Failure;
|
|
|
|
var desiredHeading = math.normalizesafe(resourceNode.Position - agent.Position, agent.Heading);
|
|
return _agentCommonLogic.UpdateHeading(agent, desiredHeading, Time.deltaTime) ? IntentExecutionResult.Success : IntentExecutionResult.InProgress;
|
|
}
|
|
}
|
|
} |