riversong code showcase

This commit is contained in:
Daniele Marotta
2026-05-21 15:52:18 +02:00
commit 4c9eea1c02
462 changed files with 23406 additions and 0 deletions

View File

@@ -0,0 +1,59 @@
using System;
using System.Collections.Generic;
using UnityEngine.InputSystem;
namespace DanieleMarotta.RiversongCodeShowcase
{
[Service(typeof(ICancelAction))]
[GameSystemGroup(typeof(LateGameSystemGroup))]
public class CancelActionSystem : GameSystem, IUpdatable, ICancelAction
{
private readonly List<(int, Func<CancelActionType, bool>)> _handlers = new();
private Comparison<(int, Func<CancelActionType, bool>)> _handlerComparison;
public CancelActionSystem(IServiceLocator serviceLocator) : base(serviceLocator)
{
}
public void Update()
{
if (!TryGetCancelActionType(out var cancelActionType)) return;
foreach (var (_, handler) in _handlers)
if (handler.Invoke(cancelActionType))
return;
}
public void AddHandler(int priority, Func<CancelActionType, bool> cancelAction)
{
_handlerComparison ??= (handler, otherHandler) =>
{
var (handlerPriority, _) = handler;
var (otherHandlerPriority, _) = otherHandler;
return otherHandlerPriority.CompareTo(handlerPriority);
};
_handlers.Add((priority, cancelAction));
_handlers.Sort(_handlerComparison);
}
private static bool TryGetCancelActionType(out CancelActionType cancelActionType)
{
if (Keyboard.current.escapeKey.wasPressedThisFrame)
{
cancelActionType = CancelActionType.EscapeKey;
return true;
}
if (Mouse.current.rightButton.wasPressedThisFrame)
{
cancelActionType = CancelActionType.RightMouseButton;
return true;
}
cancelActionType = CancelActionType.None;
return false;
}
}
}