Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexBlackfrost committed Aug 4, 2023
2 parents 851618d + 23cd72e commit befdcd1
Show file tree
Hide file tree
Showing 3 changed files with 174 additions and 1 deletion.
19 changes: 19 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,21 @@
# Hierarchical-Finite-State-Machine

## [1.1.1] - 04/08/2023

### Added
Nothing was added.
### Changed
Nothing was changed.
### Fixed
* Fixed a bug in EventTransition.cs where multipled events with different args are processed at the same time but only the first one is evaluated. Now all of them are evaluated until one of them mets all the conditions.


## [1.1.0] - 07/12/2022

### Added
Nothing was added.
### Changed
* All the events listened by active state objects are consumed after Update ends.
### Fixed
* Fixed some bugs in processInstantly flag for listening events.
* LateUpdate is not called when state is changed in Update. Updated how events are consumed, now all the events listened by active state objects are consumed after Update ends.
3 changes: 3 additions & 0 deletions Runtime/EventTransition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ private protected override bool ConditionsMet() {
currentArg = default(T);
foreach (T arg in args) {
currentArg = arg;
conditionsMet = true;
foreach (Func<T, bool> condition in conditions) {
if (!condition(arg)) {
conditionsMet = false;
Expand Down Expand Up @@ -169,6 +170,7 @@ private protected override bool ConditionsMet() {
currentArgs = default((T1, T2));
foreach ((T1, T2) argTuple in args) {
currentArgs = argTuple;
conditionsMet = true;
foreach (Func<T1, T2, bool> condition in conditions) {
if (!condition(currentArgs.Item1, currentArgs.Item2)) {
conditionsMet = false;
Expand Down Expand Up @@ -232,6 +234,7 @@ private protected override bool ConditionsMet() {
currentArgs = default((T1, T2, T3));
foreach ((T1, T2, T3) argTuple in args) {
currentArgs = argTuple;
conditionsMet = true;
foreach (Func<T1, T2, T3, bool> condition in conditions) {
if (!condition(argTuple.Item1, argTuple.Item2, argTuple.Item3)) {
conditionsMet = false;
Expand Down
153 changes: 152 additions & 1 deletion Tests/Runtime/HFSMTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1982,7 +1982,7 @@ public void MultipleEventsFiredBeforeProcessing() {
}

[Test]
public void MultipleEventsWithDifferentArgsFiredBeforeProcessing() {
public void MultipleEventsWithDifferentArgsPassedByReferenceFiredBeforeProcessing() {
StringBuilder sb = new StringBuilder();

State stateA = new WriterStateA(sb);
Expand Down Expand Up @@ -2036,6 +2036,157 @@ public void MultipleEventsWithDifferentArgsFiredBeforeProcessing() {
Assert.AreEqual(expected, sb.ToString());
}

[Test]
public void MultipleEventsWithOneDifferentArgsPassedByValueFiredBeforeProcessing() {
StringBuilder sb = new StringBuilder();

State stateA = new WriterStateA(sb);
State stateB = new WriterStateB(sb);
State stateC = new WriterStateC(sb);

StateMachine stateMachineOne = new WriterStateMachineOne(sb, stateA, stateB);
StateMachine stateMachineTwo = new WriterStateMachineTwo(sb, stateC);
StateMachine stateMachineZero = new WriterStateMachineZero(sb, stateMachineOne, stateMachineTwo);

Action<int> transitionEvent = null;
Func<int, bool> transitionConditionAC = (int numberArg) => { return numberArg == 49; };
Func<int, bool> transitionConditionCA = (int numberArg) => { return numberArg == 50; };
transitionEvent += stateA.AddEventTransition<int>(stateC, transitionConditionAC);
transitionEvent += stateC.AddEventTransition<int>(stateA, transitionConditionCA);


stateMachineZero.Init();
stateMachineZero.Update();

transitionEvent.Invoke(48);
transitionEvent.Invoke(49);
transitionEvent.Invoke(50);

stateMachineZero.Update();
stateMachineZero.Update();

string expected = stateMachineZero.GetType() + EnterLogText +
stateMachineOne.GetType() + EnterLogText +
stateA.GetType() + EnterLogText +

stateMachineZero.GetType() + UpdateLogText +
stateMachineOne.GetType() + UpdateLogText +
stateA.GetType() + UpdateLogText +

stateA.GetType() + ExitLogText +
stateMachineOne.GetType() + ExitLogText +
stateMachineTwo.GetType() + EnterLogText +
stateC.GetType() + EnterLogText +

stateMachineZero.GetType() + UpdateLogText +
stateMachineTwo.GetType() + UpdateLogText +
stateC.GetType() + UpdateLogText;

Assert.AreEqual(expected, sb.ToString());
}

[Test]
public void MultipleEventsWithTwoDifferentArgsPassedByValueFiredBeforeProcessing() {
StringBuilder sb = new StringBuilder();

State stateA = new WriterStateA(sb);
State stateB = new WriterStateB(sb);
State stateC = new WriterStateC(sb);

StateMachine stateMachineOne = new WriterStateMachineOne(sb, stateA, stateB);
StateMachine stateMachineTwo = new WriterStateMachineTwo(sb, stateC);
StateMachine stateMachineZero = new WriterStateMachineZero(sb, stateMachineOne, stateMachineTwo);

Action<int, int> transitionEvent = null;
Func<int, int, bool> transitionConditionAC = (int numberArg1, int numberArg2) => { return numberArg1 == 49 && numberArg2 == -49; };
Func<int, int, bool> transitionConditionCA = (int numberArg1, int numberArg2) => { return numberArg1 == 50 && numberArg2 == -50; };
transitionEvent += stateA.AddEventTransition<int, int>(stateC, transitionConditionAC);
transitionEvent += stateC.AddEventTransition<int, int>(stateA, transitionConditionCA);


stateMachineZero.Init();
stateMachineZero.Update();

transitionEvent.Invoke(48, -48);
transitionEvent.Invoke(49, -49);
transitionEvent.Invoke(50, -50);

stateMachineZero.Update();
stateMachineZero.Update();

string expected = stateMachineZero.GetType() + EnterLogText +
stateMachineOne.GetType() + EnterLogText +
stateA.GetType() + EnterLogText +

stateMachineZero.GetType() + UpdateLogText +
stateMachineOne.GetType() + UpdateLogText +
stateA.GetType() + UpdateLogText +

stateA.GetType() + ExitLogText +
stateMachineOne.GetType() + ExitLogText +
stateMachineTwo.GetType() + EnterLogText +
stateC.GetType() + EnterLogText +

stateMachineZero.GetType() + UpdateLogText +
stateMachineTwo.GetType() + UpdateLogText +
stateC.GetType() + UpdateLogText;

Assert.AreEqual(expected, sb.ToString());
}

[Test]
public void MultipleEventsWithThreeDifferentArgsPassedByValueFiredBeforeProcessing() {
StringBuilder sb = new StringBuilder();

State stateA = new WriterStateA(sb);
State stateB = new WriterStateB(sb);
State stateC = new WriterStateC(sb);

StateMachine stateMachineOne = new WriterStateMachineOne(sb, stateA, stateB);
StateMachine stateMachineTwo = new WriterStateMachineTwo(sb, stateC);
StateMachine stateMachineZero = new WriterStateMachineZero(sb, stateMachineOne, stateMachineTwo);

Action<int, int, string> transitionEvent = null;
Func<int, int, string, bool> transitionConditionAC = (int numberArg1, int numberArg2, string stringArg) => {
return numberArg1 == 49 && numberArg2 == -49 && stringArg == "49";
};
Func<int, int, string, bool> transitionConditionCA = (int numberArg1, int numberArg2, string stringArg) => {
return numberArg1 == 50 && numberArg2 == -50 && stringArg == "50";
};
transitionEvent += stateA.AddEventTransition<int, int, string>(stateC, transitionConditionAC);
transitionEvent += stateC.AddEventTransition<int, int, string>(stateA, transitionConditionCA);


stateMachineZero.Init();
stateMachineZero.Update();

transitionEvent.Invoke(48, -48, "48");
transitionEvent.Invoke(49, -49, "49");
transitionEvent.Invoke(50, -50, "50");

stateMachineZero.Update();
stateMachineZero.Update();

string expected = stateMachineZero.GetType() + EnterLogText +
stateMachineOne.GetType() + EnterLogText +
stateA.GetType() + EnterLogText +

stateMachineZero.GetType() + UpdateLogText +
stateMachineOne.GetType() + UpdateLogText +
stateA.GetType() + UpdateLogText +

stateA.GetType() + ExitLogText +
stateMachineOne.GetType() + ExitLogText +
stateMachineTwo.GetType() + EnterLogText +
stateC.GetType() + EnterLogText +

stateMachineZero.GetType() + UpdateLogText +
stateMachineTwo.GetType() + UpdateLogText +
stateC.GetType() + UpdateLogText;

Assert.AreEqual(expected, sb.ToString());
}

[Test]
public void ProcessIntantlyTransitionEvent2Args() {
StringBuilder sb = new StringBuilder();
Expand Down

0 comments on commit befdcd1

Please sign in to comment.