riversong code showcase
This commit is contained in:
@@ -0,0 +1,59 @@
|
||||
using System;
|
||||
using Cysharp.Threading.Tasks;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Pool;
|
||||
using Random = UnityEngine.Random;
|
||||
|
||||
namespace DanieleMarotta.RiversongCodeShowcase
|
||||
{
|
||||
public class ResourceNodeHarvestingSystem : GameSystem, IInitializable, IDisposable
|
||||
{
|
||||
[InjectService]
|
||||
private GameConfig _config;
|
||||
|
||||
[InjectService]
|
||||
private ISignalBus _signalBus;
|
||||
|
||||
[InjectService]
|
||||
private IProductStackFactory _productStackFactory;
|
||||
|
||||
[InjectService]
|
||||
private World _world;
|
||||
|
||||
[InjectService]
|
||||
private IGameDatabase _gameDatabase;
|
||||
|
||||
public ResourceNodeHarvestingSystem(IServiceLocator serviceLocator) : base(serviceLocator)
|
||||
{
|
||||
}
|
||||
|
||||
public UniTask InitializeAsync()
|
||||
{
|
||||
_signalBus.Subscribe<RawResourceHarvestedSignal>(OnRawResourceHarvested);
|
||||
|
||||
return UniTask.CompletedTask;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_signalBus.Unsubscribe<RawResourceHarvestedSignal>(OnRawResourceHarvested);
|
||||
}
|
||||
|
||||
private void OnRawResourceHarvested(RawResourceHarvestedSignal signal)
|
||||
{
|
||||
if (!_world.RawResources.DeleteResourceNode(signal.RawResourceId, _config.GeneralSettings.BaseElevation, out var resourceNode))
|
||||
{
|
||||
Debug.LogError("Could not find harvested Resource Node");
|
||||
return;
|
||||
}
|
||||
|
||||
var definition = _gameDatabase.WithId<ResourceNodeDefinition>(resourceNode.DefinitionId);
|
||||
var amount = Random.Range(definition.MinDroppedAmount, definition.MaxDroppedAmount + 1);
|
||||
_productStackFactory.CreateOrMerge(resourceNode.Tile, definition.Product, amount);
|
||||
|
||||
using var resourceNodesScope = ListPool<ResourceNode>.Get(out var resourceNodes);
|
||||
resourceNodes.Add(resourceNode);
|
||||
_signalBus.Raise(new RawResourcesRemovedSignal(resourceNodes, RawResourcesRemovedSignal.RemovalReason.Harvested));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user