riversong code showcase
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Cysharp.Threading.Tasks;
|
||||
|
||||
namespace DanieleMarotta.RiversongCodeShowcase
|
||||
{
|
||||
[Service(typeof(IFailedPathCache))]
|
||||
public class FailedPathCacheSystem : GameSystem, IInitializable, IDisposable, IFailedPathCache
|
||||
{
|
||||
[InjectService]
|
||||
private ISignalBus _signalBus;
|
||||
|
||||
private HashSet<ulong> _failedPaths = new();
|
||||
|
||||
public FailedPathCacheSystem(IServiceLocator serviceLocator) : base(serviceLocator)
|
||||
{
|
||||
}
|
||||
|
||||
public UniTask InitializeAsync()
|
||||
{
|
||||
_signalBus.Subscribe<BuildingDeletedSignal>(InvalidateCacheOnSignal);
|
||||
_signalBus.Subscribe<RoadTileUpdatedSignal>(InvalidateCacheOnSignal);
|
||||
|
||||
return UniTask.CompletedTask;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_signalBus.Unsubscribe<BuildingDeletedSignal>(InvalidateCacheOnSignal);
|
||||
_signalBus.Unsubscribe<RoadTileUpdatedSignal>(InvalidateCacheOnSignal);
|
||||
}
|
||||
|
||||
private void InvalidateCacheOnSignal<T>(T signal)
|
||||
{
|
||||
Invalidate();
|
||||
}
|
||||
|
||||
public void FailPath(int sourceId, int targetId)
|
||||
{
|
||||
_failedPaths.Add(MakeKey(sourceId, targetId));
|
||||
}
|
||||
|
||||
public bool IsKnownFailedPath(int sourceId, int targetId)
|
||||
{
|
||||
return _failedPaths.Contains(MakeKey(sourceId, targetId));
|
||||
}
|
||||
|
||||
public void Invalidate()
|
||||
{
|
||||
_failedPaths.Clear();
|
||||
}
|
||||
|
||||
private ulong MakeKey(int sourceId, int targetId)
|
||||
{
|
||||
return sourceId < targetId ? ((ulong)sourceId << 32) | (uint)targetId : ((ulong)targetId << 32) | (uint)sourceId;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user