Skip to content

Commit

Permalink
v1.0.43 ActionKit 每个 Action 增加 ID
Browse files Browse the repository at this point in the history
  • Loading branch information
liangxiegame committed Apr 23, 2023
1 parent 2caa5c0 commit 450f2d0
Show file tree
Hide file tree
Showing 16 changed files with 105 additions and 21 deletions.
Binary file modified QFramework.Toolkits.unitypackage
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"Id": "",
"Version": "v1.0.42",
"Version": "v1.0.43",
"Type": 0,
"AccessRight": 0,
"DownloadUrl": "",
Expand All @@ -11,10 +11,10 @@
],
"DocUrl": "https://liangxiegame.com",
"Readme": {
"version": "v1.0.42",
"content": "ActionKit:修复 Deinit 后可能多跑一帧的问题(学猫叫的鹦鹉提供反馈)",
"version": "v1.0.43",
"content": "v1.0.43 ActionKit 每个 Action 增加 ID",
"author": "liangxie",
"date": "2023 年 04 月 2015:52",
"date": "2023 年 04 月 2322:47",
"PackageId": ""
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ namespace QFramework
#endif
public class ActionKit : Architecture<ActionKit>
{
public static ulong ID_GENERATOR = 0;

#if UNITY_EDITOR
[MethodAPI]
[APIDescriptionCN("延时回调")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,48 +20,101 @@ public enum ActionStatus

public interface IActionController
{
ulong ActionID { get; set; }

IAction Action { get; set; }

bool Paused { get; set; }
void Reset();
void Deinit();
}

public interface IAction<TStatus> : IActionController
public interface IAction<TStatus>
{
ulong ActionID { get; set; }
TStatus Status { get; set; }
void OnStart();
void OnExecute(float dt);
void OnFinish();

bool Deinited { get; set; }

bool Deinited { get; set; }

bool Paused { get; set; }
void Reset();
void Deinit();
}


public interface IAction : IAction<ActionStatus>
{
}


public struct ActionController : IActionController
{
public ulong ActionID { get; set; }
public IAction Action { get; set; }

public bool Paused
{
get => Action.Paused;
set => Action.Paused = value;
}

public void Reset()
{
if (Action.ActionID == ActionID)
{
Action.Reset();
}
}

public void Deinit()
{
if (Action.ActionID == ActionID)
{
Action.Deinit();
}
}
}


public static class IActionExtensions
{
public static IActionController Start(this IAction self, MonoBehaviour monoBehaviour,
Action<IAction> onFinish = null)
Action<IActionController> onFinish = null)
{
return monoBehaviour.ExecuteByUpdate(self, onFinish);
monoBehaviour.ExecuteByUpdate(self, onFinish);

return new ActionController()
{
Action = self,
ActionID = self.ActionID,
};
}

public static IActionController Start(this IAction self, MonoBehaviour monoBehaviour,
Action onFinish)
{
return monoBehaviour.ExecuteByUpdate(self, _ => onFinish());
monoBehaviour.ExecuteByUpdate(self, _ => onFinish());

return new ActionController()
{
Action = self,
ActionID = self.ActionID,
};
}

public static IActionController StartGlobal(this IAction self, Action<IAction> onFinish = null)
public static IActionController StartGlobal(this IAction self, Action<IActionController> onFinish = null)
{
IActionExecutor executor = null;
if (executor.UpdateAction(self, 0, onFinish)) return self;
if (executor.UpdateAction(self, 0, onFinish))
{
return new ActionController()
{
Action = self,
ActionID = self.ActionID,
};
}

void Update()
{
Expand All @@ -74,7 +127,11 @@ void Update()
ActionKit.OnUpdate.Register(Update);


return self;
return new ActionController()
{
Action = self,
ActionID = self.ActionID,
};
}


Expand Down Expand Up @@ -110,7 +167,7 @@ public static bool Execute(this IAction self, float dt)
else if (self.Status == ActionStatus.Started)
{
if (self.Paused) return false;

self.OnExecute(dt);

if (self.Status == ActionStatus.Finished)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,22 @@ namespace QFramework
{
public interface IActionExecutor
{
void Execute(IAction action,Action<IAction> onFinish = null);
void Execute(IAction action,Action<IActionController> onFinish = null);
}


public static class IActionExecutorExtensions
{
public static bool UpdateAction(this IActionExecutor self,IAction action,float dt,Action<IAction> onFinish = null)
public static bool UpdateAction(this IActionExecutor self,IAction action,float dt,Action<IActionController> onFinish = null)
{
if (!action.Deinited && action.Execute(dt))
{
onFinish?.Invoke(action);
onFinish?.Invoke(new ActionController()
{
Action = action,
ActionID = action.ActionID
});

action.Deinit();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ private Callback()
public static Callback Allocate(Action callback)
{
var callbackAction = mSimpleObjectPool.Allocate();
callbackAction.ActionID = ActionKit.ID_GENERATOR++;
callbackAction.Reset();
callbackAction.Deinited = false;
callbackAction.mCallback = callback;
Expand All @@ -32,6 +33,7 @@ public static Callback Allocate(Action callback)

public bool Paused { get; set; }
public bool Deinited { get; set; }
public ulong ActionID { get; set; }
public ActionStatus Status { get; set; }

public void OnStart()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public class Condition : IAction
public static Condition Allocate(Func<bool> condition)
{
var conditionAction = mSimpleObjectPool.Allocate();
conditionAction.ActionID = ActionKit.ID_GENERATOR++;
conditionAction.Deinited = false;
conditionAction.Reset();
conditionAction.mCondition = condition;
Expand All @@ -30,6 +31,7 @@ public static Condition Allocate(Func<bool> condition)

public bool Paused { get; set; }
public bool Deinited { get; set; }
public ulong ActionID { get; set; }
public ActionStatus Status { get; set; }
public void OnStart()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ internal class CoroutineAction : IAction
public static CoroutineAction Allocate(Func<IEnumerator> coroutineGetter)
{
var coroutineAction = mPool.Allocate();
coroutineAction.ActionID = ActionKit.ID_GENERATOR++;
coroutineAction.Deinited = false;
coroutineAction.Reset();
coroutineAction.mCoroutineGetter = coroutineGetter;
Expand All @@ -48,6 +49,7 @@ public void Reset()
}

public bool Deinited { get; set; }
public ulong ActionID { get; set; }
public ActionStatus Status { get; set; }
public void OnStart()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ protected Custom()
public static Custom<TData> Allocate()
{
var custom = mSimpleObjectPool.Allocate();
custom.ActionID = ActionKit.ID_GENERATOR++;
custom.Deinited = false;
custom.Reset();
return custom;
Expand Down Expand Up @@ -66,6 +67,7 @@ public void Reset()
}

public bool Deinited { get; set; }
public ulong ActionID { get; set; }
public ActionStatus Status { get; set; }

public void OnStart()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ private Delay()
public static Delay Allocate(float delayTime, System.Action onDelayFinish = null)
{
var retNode = mPool.Allocate();
retNode.ActionID = ActionKit.ID_GENERATOR++;
retNode.Deinited = false;
retNode.Reset();
retNode.DelayTime = delayTime;
Expand All @@ -48,8 +49,9 @@ public static Delay Allocate(Func<float> delayTimeFactory, System.Action onDelay
retNode.CurrentSeconds = 0.0f;
return retNode;
}



public ulong ActionID { get; set; }
public ActionStatus Status { get; set; }

public void OnStart()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ internal class DelayFrame : IAction
{
public bool Paused { get; set; }
public bool Deinited { get; set; }
public ulong ActionID { get; set; }
public ActionStatus Status { get; set; }

private static SimpleObjectPool<DelayFrame> mSimpleObjectPool =
Expand All @@ -25,6 +26,7 @@ internal class DelayFrame : IAction
public static DelayFrame Allocate(int frameCount, Action onDelayFinish = null)
{
var delayFrame = mSimpleObjectPool.Allocate();
delayFrame.ActionID = ActionKit.ID_GENERATOR++;
delayFrame.Reset();
delayFrame.Deinited = false;
delayFrame.mDelayedFrameCount = frameCount;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public class Lerp : IAction
public static Lerp Allocate(float a, float b, float duration, Action<float> onLerp = null,Action onLerpFinish = null)
{
var retNode = mPool.Allocate();
retNode.ActionID = ActionKit.ID_GENERATOR++;
retNode.Deinited = false;
retNode.Reset();
retNode.A = a;
Expand Down Expand Up @@ -56,6 +57,7 @@ public void Deinit()
}
}

public ulong ActionID { get; set; }
public ActionStatus Status { get; set; }
public void OnStart()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,14 @@ internal class Parallel : IParallel
public static Parallel Allocate()
{
var parallel = mSimpleObjectPool.Allocate();
parallel.ActionID = ActionKit.ID_GENERATOR++;
parallel.Deinited = false;
parallel.Reset();
return parallel;
}
public bool Paused { get; set; }
public bool Deinited { get; set; }
public ulong ActionID { get; set; }
public ActionStatus Status { get; set; }
public void OnStart()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ private Repeat()
public static Repeat Allocate(int repeatCount = -1)
{
var repeat = mSimpleObjectPool.Allocate();
repeat.ActionID = ActionKit.ID_GENERATOR++;
repeat.mSequence = Sequence.Allocate();
repeat.Deinited = false;
repeat.Reset();
Expand All @@ -40,6 +41,7 @@ public static Repeat Allocate(int repeatCount = -1)

public bool Paused { get; set; }
public bool Deinited { get; set; }
public ulong ActionID { get; set; }
public ActionStatus Status { get; set; }

public void OnStart()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ private Sequence()
public static Sequence Allocate()
{
var sequence = mSimpleObjectPool.Allocate();
sequence.ActionID = ActionKit.ID_GENERATOR++;
sequence.Reset();
sequence.Deinited = false;
return sequence;
Expand All @@ -41,6 +42,7 @@ public static Sequence Allocate()

public bool Deinited { get; set; }

public ulong ActionID { get; set; }
public ActionStatus Status { get; set; }

public void OnStart()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ internal class MonoUpdateActionExecutor : MonoBehaviour, IActionExecutor
{
private Action mOnUpdate = () => { };

public void Execute(IAction action,Action<IAction> onFinish = null)
public void Execute(IAction action,Action<IActionController> onFinish = null)
{
if (action.Status == ActionStatus.Finished) action.Reset();
if (this.UpdateAction(action, 0, onFinish)) return;
Expand All @@ -39,7 +39,7 @@ private void Update()

public static class MonoUpdateActionExecutorExtension
{
public static IAction ExecuteByUpdate<T>(this T self, IAction action,Action<IAction> onFinish = null) where T : MonoBehaviour
public static IAction ExecuteByUpdate<T>(this T self, IAction action,Action<IActionController> onFinish = null) where T : MonoBehaviour
{
if (action.Status == ActionStatus.Finished) action.Reset();
self.gameObject.GetOrAddComponent<MonoUpdateActionExecutor>().Execute(action,onFinish);
Expand Down

0 comments on commit 450f2d0

Please sign in to comment.