Skip to content

Commit 8d299ff

Browse files
authored
Funcs to replace actions by type (#17)
1 parent b3e323f commit 8d299ff

File tree

1 file changed

+76
-0
lines changed

1 file changed

+76
-0
lines changed

src/FsmUtil.cs

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -666,6 +666,7 @@ public static void InsertMethod(this FsmState state, int index, Action<FsmStateA
666666
/// <param name="stateName">The name of the state in which the method is added</param>
667667
/// <param name="method">The method that will be invoked</param>
668668
/// <param name="index">The index to place the action in</param>
669+
/// <param name="everyFrame">Whether to execute 'method' on every update frame</param>
669670
/// <returns>bool that indicates whether the insertion was successful</returns>
670671
[PublicAPI]
671672
public static void InsertMethod(this PlayMakerFSM fsm, string stateName, Action method, int index, bool everyFrame = false) => fsm.GetState(stateName)!.InsertMethod(index, method, everyFrame);
@@ -911,6 +912,81 @@ public static void ReplaceAllActions(this FsmState state, params FsmStateAction[
911912
{
912913
action.Init(state);
913914
}
915+
}
916+
917+
/// <summary>
918+
/// Replace all actions of type <typeparamref name="T"/> with new actions.
919+
/// </summary>
920+
/// <typeparam name="T">The type of actions to replace.</typeparam>
921+
/// <param name="state">The state containing actions to replace.</param>
922+
/// <param name="newActionGenerator">Function used to generate the new actions.</param>
923+
[PublicAPI]
924+
public static void ReplaceActionsOfType<T>(this FsmState state, Func<T, FsmStateAction> newActionGenerator)
925+
where T : FsmStateAction
926+
{
927+
for (int i = 0; i < state.actions.Length; i++)
928+
{
929+
if (state.Actions[i] is T typedAction)
930+
{
931+
state.ReplaceAction(i, newActionGenerator(typedAction));
932+
}
933+
}
934+
}
935+
936+
/// <summary>
937+
/// Replace the first action of type <typeparamref name="T"/> with a new action.
938+
/// </summary>
939+
/// <typeparam name="T">The type of the action to replace.</typeparam>
940+
/// <param name="state">The state holding the actions.</param>
941+
/// <param name="newAction">The new action.</param>
942+
[PublicAPI]
943+
public static void ReplaceFirstActionOfType<T>(this FsmState state, FsmStateAction newAction) where T : FsmStateAction
944+
{
945+
int foundIndex = -2;
946+
947+
for (int i = 0; i < state.actions.Length; i++)
948+
{
949+
if (state.actions[i] is T)
950+
{
951+
foundIndex = i;
952+
break;
953+
}
954+
}
955+
956+
if (foundIndex == -2)
957+
{
958+
return;
959+
}
960+
961+
state.ReplaceAction(newAction, foundIndex);
962+
}
963+
964+
/// <summary>
965+
/// Replace the last action of type <typeparamref name="T"/> with a new action.
966+
/// </summary>
967+
/// <typeparam name="T">The type of the action to replace.</typeparam>
968+
/// <param name="state">The state holding the actions.</param>
969+
/// <param name="newAction">The new action.</param>
970+
[PublicAPI]
971+
public static void ReplaceLastActionOfType<T>(this FsmState state, FsmStateAction newAction) where T : FsmStateAction
972+
{
973+
int foundIndex = -2;
974+
975+
for (int i = state.actions.Length - 1; i >= 0; i--)
976+
{
977+
if (state.actions[i] is T)
978+
{
979+
foundIndex = i;
980+
break;
981+
}
982+
}
983+
984+
if (foundIndex == -2)
985+
{
986+
return;
987+
}
988+
989+
state.ReplaceAction(newAction, foundIndex);
914990
}
915991

916992
#endregion Replace

0 commit comments

Comments
 (0)