Files
riversong-code-showcase/Source/Riversong/Game/World/Agents/Intents/HarvestFertileTileIntentExecutionLogic.cs
2026-05-21 16:04:49 +02:00

43 lines
1.5 KiB
C#

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<IEntityCollection>();
_productStackFactory = serviceLocator.GetService<IProductStackFactory>();
_world = serviceLocator.GetService<World>();
_tileSpace = serviceLocator.GetService<ITileSpace>();
return UniTask.CompletedTask;
}
public override IntentExecutionResult Execute(Agent agent, in AgentIntent intent)
{
if (!_entityCollection.TryGet<Building>(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;
}
}
}