using Cysharp.Threading.Tasks; using Random = UnityEngine.Random; namespace DanieleMarotta.RiversongCodeShowcase { public class HarvestFertileTileIntentExecutionLogic : IntentExecutionLogic { private IEntityCollection _entityCollection; private IProductStackFactory _productStackFactory; private World _world; private ITileSpace _tileSpace; public override UniTask InitializeAsync(IServiceLocator serviceLocator) { _entityCollection = serviceLocator.GetService(); _productStackFactory = serviceLocator.GetService(); _world = serviceLocator.GetService(); _tileSpace = serviceLocator.GetService(); return UniTask.CompletedTask; } public override IntentExecutionResult Execute(Agent agent, in AgentIntent intent) { if (!_entityCollection.TryGet(agent.HomeId, out var farm)) return IntentExecutionResult.Failure; var tile = _tileSpace.WorldToTile(agent.Position); ref var fertility = ref _world.Fertility.GetValueRW(tile); fertility.CurrentFertility = 0; var product = farm.Definition.FarmProduct; var amount = Random.Range(farm.Definition.MinDroppedAmount, farm.Definition.MaxDroppedAmount + 1); _productStackFactory.CreateOrMerge(tile, product, amount); return IntentExecutionResult.Success; } } }