除了充盈都做完了
This commit is contained in:
@@ -19,7 +19,7 @@ namespace SLSFramework.General
|
||||
// 队列的入口现在需要一个能接收 Context 的指令工厂
|
||||
// 1. 我们使用一个标准的 Queue<T> 来存储待执行的指令。
|
||||
// 这比 Subject 更能明确地表达“队列”的意图。
|
||||
private readonly Queue<Tuple<CommandBase, CommandContext>> commandQueue = new Queue<Tuple<CommandBase, CommandContext>>();
|
||||
private readonly LinkedList<Tuple<CommandBase, CommandContext>> commandQueue = new LinkedList<Tuple<CommandBase, CommandContext>>();
|
||||
|
||||
// 2. 一个布尔值标志,用于追踪管理器当前是否正在执行一个指令。
|
||||
private bool isBusy = false;
|
||||
@@ -71,10 +71,23 @@ namespace SLSFramework.General
|
||||
}
|
||||
|
||||
public CommandBase AddCommand(CommandBase command, CommandContext context = null)
|
||||
{
|
||||
return AddCommand(command, true, context);
|
||||
}
|
||||
|
||||
public CommandBase AddCommand(CommandBase command, bool insertAtTail, CommandContext context = null)
|
||||
{
|
||||
context ??= new CommandContext();
|
||||
// 将指令和其上下文入队
|
||||
commandQueue.Enqueue(Tuple.Create(command, context));
|
||||
if (insertAtTail)
|
||||
{
|
||||
commandQueue.AddLast(Tuple.Create(command, context));
|
||||
}
|
||||
else
|
||||
{
|
||||
commandQueue.AddFirst(Tuple.Create(command, context));
|
||||
}
|
||||
|
||||
//Debug.Log($"[Queue] 添加指令: {command.GetType()},队列长度: {commandQueue.Count}");
|
||||
// 尝试启动队列处理。
|
||||
// 如果队列当前不忙,这个调用会立即开始处理我们刚刚添加的指令。
|
||||
@@ -100,7 +113,8 @@ namespace SLSFramework.General
|
||||
|
||||
// 4. 标记为“忙碌”,并从队列中取出下一个指令。
|
||||
isBusy = true;
|
||||
var nextEntry = commandQueue.Dequeue();
|
||||
var nextEntry = commandQueue.First.Value;
|
||||
commandQueue.RemoveFirst();
|
||||
var commandToExecute = nextEntry.Item1;
|
||||
var context = nextEntry.Item2;
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using SoftCircuits.Collections;
|
||||
using UnityEngine;
|
||||
|
||||
@@ -7,7 +8,7 @@ namespace SLSFramework.General
|
||||
{
|
||||
public static class DictionaryExtension
|
||||
{
|
||||
public static void Invoke(this IDictionary<string, EventUnit> dictionary)
|
||||
public static void Invoke(this IDictionary<string, PrioritizedAction> dictionary)
|
||||
{
|
||||
foreach (var action in dictionary.Values)
|
||||
{
|
||||
@@ -15,7 +16,7 @@ namespace SLSFramework.General
|
||||
}
|
||||
}
|
||||
|
||||
public static void Invoke<T>(this IDictionary<string, EventUnit<T>> dictionary, T arg)
|
||||
public static void Invoke<T>(this IDictionary<string, PrioritizedAction<T>> dictionary, T arg)
|
||||
{
|
||||
foreach (var action in dictionary.Values)
|
||||
{
|
||||
@@ -23,7 +24,7 @@ namespace SLSFramework.General
|
||||
}
|
||||
}
|
||||
|
||||
public static void Invoke<T1, T2>(this IDictionary<string, EventUnit<T1, T2>> dictionary, T1 arg1, T2 arg2)
|
||||
public static void Invoke<T1, T2>(this IDictionary<string, PrioritizedAction<T1, T2>> dictionary, T1 arg1, T2 arg2)
|
||||
{
|
||||
foreach (var action in dictionary.Values)
|
||||
{
|
||||
@@ -82,6 +83,29 @@ namespace SLSFramework.General
|
||||
}
|
||||
}
|
||||
|
||||
public static class CheckAndEffectDictionaryExtension
|
||||
{
|
||||
public static List<Func<bool>> GetChecks(this IDictionary<string, PrioritizedCheckAndEffect> dictionary)
|
||||
{
|
||||
return dictionary.Values.Select(checkAndEffect => checkAndEffect.check).ToList();
|
||||
}
|
||||
|
||||
public static List<Action> GetEffects(this IDictionary<string, PrioritizedCheckAndEffect> dictionary)
|
||||
{
|
||||
return dictionary.Values.Select(checkAndEffect => checkAndEffect.effect).ToList();
|
||||
}
|
||||
|
||||
public static void InvokeIfAnyConditionChecked(this IDictionary<string, PrioritizedCheckAndEffect> dictionary)
|
||||
{
|
||||
List<Func<bool>> checks = dictionary.Values.Select(checkAndEffect => checkAndEffect.check).ToList();
|
||||
List<Action> effects = dictionary.Values.Select(checkAndEffect => checkAndEffect.effect).ToList();
|
||||
if (checks.Any())
|
||||
{
|
||||
effects.ForEach(effect => effect.Invoke());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static class OrderedDictionaryExtension
|
||||
{
|
||||
/// <summary>
|
||||
|
||||
@@ -6,12 +6,12 @@ using UnityEngine.Events;
|
||||
|
||||
namespace SLSFramework.General
|
||||
{
|
||||
public class EventUnit : IPrioritized
|
||||
public class PrioritizedAction : IPrioritized
|
||||
{
|
||||
private readonly UnityAction action;
|
||||
private readonly Action action;
|
||||
public int Priority { get; set; }
|
||||
|
||||
public EventUnit(UnityAction action, int priority = 0)
|
||||
public PrioritizedAction(Action action, int priority = 0)
|
||||
{
|
||||
this.action = action;
|
||||
this.Priority = priority;
|
||||
@@ -23,12 +23,12 @@ namespace SLSFramework.General
|
||||
}
|
||||
}
|
||||
|
||||
public class EventUnit<T> : IPrioritized
|
||||
public class PrioritizedAction<T> : IPrioritized
|
||||
{
|
||||
private readonly UnityAction<T> action;
|
||||
private readonly Action<T> action;
|
||||
public int Priority { get; set; }
|
||||
|
||||
public EventUnit(UnityAction<T> action, int priority = 0)
|
||||
public PrioritizedAction(Action<T> action, int priority = 0)
|
||||
{
|
||||
this.action = action;
|
||||
this.Priority = priority;
|
||||
@@ -40,12 +40,12 @@ namespace SLSFramework.General
|
||||
}
|
||||
}
|
||||
|
||||
public class EventUnit<T1, T2> : IPrioritized
|
||||
public class PrioritizedAction<T1, T2> : IPrioritized
|
||||
{
|
||||
private readonly UnityAction<T1, T2> action;
|
||||
private readonly Action<T1, T2> action;
|
||||
public int Priority { get; set; }
|
||||
|
||||
public EventUnit(UnityAction<T1, T2> action, int priority = 0)
|
||||
public PrioritizedAction(Action<T1, T2> action, int priority = 0)
|
||||
{
|
||||
this.action = action;
|
||||
this.Priority = priority;
|
||||
@@ -57,12 +57,12 @@ namespace SLSFramework.General
|
||||
}
|
||||
}
|
||||
|
||||
public class EventUnit<T1, T2, T3> : IPrioritized
|
||||
public class PrioritizedAction<T1, T2, T3> : IPrioritized
|
||||
{
|
||||
private readonly UnityAction<T1, T2, T3> action;
|
||||
private readonly Action<T1, T2, T3> action;
|
||||
public int Priority { get; set; }
|
||||
|
||||
public EventUnit(UnityAction<T1, T2, T3> action, int priority = 0)
|
||||
public PrioritizedAction(Action<T1, T2, T3> action, int priority = 0)
|
||||
{
|
||||
this.action = action;
|
||||
this.Priority = priority;
|
||||
@@ -74,6 +74,58 @@ namespace SLSFramework.General
|
||||
}
|
||||
}
|
||||
|
||||
public class PrioritizedFunc<TR> : IPrioritized
|
||||
{
|
||||
private readonly Func<TR> func;
|
||||
public int Priority { get; set; }
|
||||
|
||||
public PrioritizedFunc(Func<TR> func, int priority = 0)
|
||||
{
|
||||
this.func = func;
|
||||
this.Priority = priority;
|
||||
}
|
||||
|
||||
public TR Invoke()
|
||||
{
|
||||
return func.Invoke();
|
||||
}
|
||||
}
|
||||
|
||||
public class PrioritizedFunc<T, TR> : IPrioritized
|
||||
{
|
||||
private readonly Func<T, TR> func;
|
||||
public int Priority { get; set; }
|
||||
|
||||
public PrioritizedFunc(Func<T, TR> func, int priority = 0)
|
||||
{
|
||||
this.func = func;
|
||||
this.Priority = priority;
|
||||
}
|
||||
|
||||
public TR Invoke(T arg)
|
||||
{
|
||||
return func.Invoke(arg);
|
||||
}
|
||||
}
|
||||
|
||||
public class PrioritizedFunc<T1, T2, TR> : IPrioritized
|
||||
{
|
||||
private readonly Func<T1, T2, TR> func;
|
||||
public int Priority { get; set; }
|
||||
|
||||
public PrioritizedFunc(Func<T1, T2, TR> func, int priority = 0)
|
||||
{
|
||||
this.func = func;
|
||||
this.Priority = priority;
|
||||
}
|
||||
|
||||
public TR Invoke(T1 arg1, T2 arg2)
|
||||
{
|
||||
return func.Invoke(arg1, arg2);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public class PrioritizedFunc<T1, T2, T3, TR> : IPrioritized
|
||||
{
|
||||
private readonly Func<T1, T2, T3, TR> func;
|
||||
@@ -90,5 +142,27 @@ namespace SLSFramework.General
|
||||
return func.Invoke(arg1, arg2, arg3);
|
||||
}
|
||||
}
|
||||
|
||||
public class PrioritizedCheckAndEffect : IPrioritized
|
||||
{
|
||||
public readonly Func<bool> check;
|
||||
public readonly Action effect;
|
||||
public int Priority { get; set; }
|
||||
|
||||
public PrioritizedCheckAndEffect(Func<bool> check, Action effect, int priority = 0)
|
||||
{
|
||||
this.check = check;
|
||||
this.effect = effect;
|
||||
this.Priority = priority;
|
||||
}
|
||||
|
||||
public void Invoke()
|
||||
{
|
||||
if (check.Invoke())
|
||||
{
|
||||
effect.Invoke();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user