-
Notifications
You must be signed in to change notification settings - Fork 150
Expand file tree
/
Copy pathStateMachine.cs
More file actions
969 lines (834 loc) · 32.6 KB
/
StateMachine.cs
File metadata and controls
969 lines (834 loc) · 32.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using UnityHFSM.Inspection;
/**
* Hierarchical Finite State Machine for Unity
* by Inspiaaa and contributors
*
* Version: 2.2.0
*/
namespace UnityHFSM
{
/// <summary>
/// Main finite state machine class. It can be used as a child state of another state machine
/// in order to create a hierarchical state machine.
/// </summary>
public class StateMachine<TOwnId, TStateId, TEvent> :
StateBase<TOwnId>,
ITriggerable<TEvent>,
IStateMachine<TStateId>,
IActionable<TEvent>
{
/// <summary>
/// A bundle of a state together with the outgoing transitions and trigger transitions.
/// It's useful, as you only need to do one Dictionary lookup for these three items.
/// => Much better performance
/// </summary>
private class StateBundle
{
// By default, these fields are all null and only get a value when you need them.
// => Lazy evaluation => Memory efficient, when you only need a subset of features
public StateBase<TStateId> state;
public List<TransitionBase<TStateId>> transitions;
public Dictionary<TEvent, List<TransitionBase<TStateId>>> triggerToTransitions;
public void AddTransition(TransitionBase<TStateId> t)
{
transitions = transitions ?? new List<TransitionBase<TStateId>>();
transitions.Add(t);
}
public void AddTriggerTransition(TEvent trigger, TransitionBase<TStateId> transition)
{
triggerToTransitions = triggerToTransitions
?? new Dictionary<TEvent, List<TransitionBase<TStateId>>>();
List<TransitionBase<TStateId>> transitionsOfTrigger;
if (!triggerToTransitions.TryGetValue(trigger, out transitionsOfTrigger))
{
transitionsOfTrigger = new List<TransitionBase<TStateId>>();
triggerToTransitions.Add(trigger, transitionsOfTrigger);
}
transitionsOfTrigger.Add(transition);
}
}
/// <summary>
/// Represents a delayed / pending transition.
/// </summary>
/// <remarks>
/// This struct is mutable and its methods mutate the state of the struct. This requires great
/// caution ("mutable structs are evil"), but has lead to a significant increase in performance.
/// </remarks>
private struct PendingTransition
{
// The following fields have been arranged so that they minimise the size of this struct type,
// specifically for small TStateId types (see automatic sequential layout of structs).
// Optional (may be null), used for callbacks when the transition succeeds.
public ITransitionListener listener;
public TStateId targetState;
// As this type is not nullable (it is a value type), an additional field is required
// to see if the pending transition has been set yet.
public bool isPending;
public bool isExitTransition;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Clear()
{
// It suffices just to clear this field, as the other fields are not checked when
// isPending is false.
this.isPending = false;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void SetToExit(ITransitionListener listener = null)
{
this.listener = listener;
this.isExitTransition = true;
this.isPending = true;
// The targetState is irrelevant in this case.
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void SetToState(TStateId target, ITransitionListener listener = null)
{
this.listener = listener;
this.targetState = target;
this.isExitTransition = false;
this.isPending = true;
}
}
// A cached empty list of transitions (For improved readability, less GC).
private static readonly List<TransitionBase<TStateId>> noTransitions
= new List<TransitionBase<TStateId>>(0);
private static readonly Dictionary<TEvent, List<TransitionBase<TStateId>>> noTriggerTransitions
= new Dictionary<TEvent, List<TransitionBase<TStateId>>>(0);
/// <summary>
/// Event that is raised when the active state changes.
/// </summary>
/// <remarks>
/// It is triggered when the state machine enters its initial state, and after a transition is performed.
/// Note that it is not called when the state machine exits.
/// </remarks>
public event Action<StateBase<TStateId>> StateChanged;
private (TStateId state, bool hasState) startState = (default, false);
private PendingTransition pendingTransition = default;
private bool rememberLastState = false;
// Central storage of states.
private readonly Dictionary<TStateId, StateBundle> stateBundlesByName
= new Dictionary<TStateId, StateBundle>();
private StateBase<TStateId> activeState = null;
private List<TransitionBase<TStateId>> activeTransitions = noTransitions;
private Dictionary<TEvent, List<TransitionBase<TStateId>>> activeTriggerTransitions = noTriggerTransitions;
private readonly List<TransitionBase<TStateId>> transitionsFromAny
= new List<TransitionBase<TStateId>>();
private readonly Dictionary<TEvent, List<TransitionBase<TStateId>>> triggerTransitionsFromAny
= new Dictionary<TEvent, List<TransitionBase<TStateId>>>();
public StateBase<TStateId> ActiveState
{
get
{
EnsureIsInitializedFor("Trying to get the active state");
return activeState;
}
}
public TStateId ActiveStateName => ActiveState.name;
public TStateId PendingStateName => pendingTransition.targetState;
public StateBase<TStateId> PendingState => GetState(PendingStateName);
public bool HasPendingTransition => pendingTransition.isPending;
public IStateTimingManager ParentFsm => fsm;
public bool IsRootFsm => fsm == null;
/// <summary>
/// Initialises a new instance of the StateMachine class.
/// </summary>
/// <param name="needsExitTime">(Only for hierarchical states):
/// Determines whether the state machine as a state of a parent state machine is allowed to instantly
/// exit on a transition (false), or if it should wait until an explicit exit transition occurs.</param>
/// <param name="rememberLastState">(Only for hierarchical states):
/// If true, the state machine will return to its last active state when it enters, instead
/// of to its original start state.</param>
/// <inheritdoc cref="StateBase{T}(bool, bool)"/>
public StateMachine(bool needsExitTime = false, bool isGhostState = false, bool rememberLastState = false)
: base(needsExitTime: needsExitTime, isGhostState: isGhostState)
{
this.rememberLastState = rememberLastState;
}
/// <summary>
/// Throws an exception if the state machine is not initialised yet.
/// </summary>
/// <param name="context">String message for which action the fsm should be initialised for.</param>
private void EnsureIsInitializedFor(string context)
{
if (activeState == null)
throw UnityHFSM.Exceptions.Common.NotInitialized(this, context);
}
/// <summary>
/// Notifies the state machine that the active state can cleanly exit. If a transition is pending,
/// the state machine will execute it now.
/// </summary>
/// <remarks>
/// This signal is only valid for this exact point in time. It does not tell the state machine that
/// it is still safe to perform a transition at a later point in the future; it is not "saved" or
/// remembered. <para />
/// As it only has an effect when a transition is pending and transitions are only ever
/// checked after the <c>OnEnter</c> call, calling this method during <c>OnEnter</c>
/// has no effect.
/// </remarks>
public void StateCanExit()
{
if (!pendingTransition.isPending)
return;
ITransitionListener listener = pendingTransition.listener;
if (pendingTransition.isExitTransition)
{
pendingTransition = default;
listener?.BeforeTransition();
PerformVerticalTransition();
listener?.AfterTransition();
}
else
{
TStateId state = pendingTransition.targetState;
// When the pending state is a ghost state, ChangeState() will have
// to try all outgoing transitions, which may overwrite the pendingState.
// That's why it is first cleared, and not afterwards, as that would overwrite
// a new, valid pending state.
pendingTransition = default;
ChangeState(state, listener);
}
}
/// <summary>
/// Instantly changes to the target state.
/// </summary>
/// <param name="name">The name / identifier of the active state.</param>
/// <param name="listener">Optional object that receives callbacks before and after changing state.</param>
private void ChangeState(TStateId name, ITransitionListener listener = null)
{
listener?.BeforeTransition();
activeState?.OnExit();
StateBundle bundle;
if (!stateBundlesByName.TryGetValue(name, out bundle) || bundle.state == null)
{
throw UnityHFSM.Exceptions.Common.StateNotFound(this, name.ToString(), context: "Switching states");
}
activeTransitions = bundle.transitions ?? noTransitions;
activeTriggerTransitions = bundle.triggerToTransitions ?? noTriggerTransitions;
activeState = bundle.state;
activeState.OnEnter();
for (int i = 0, count = activeTransitions.Count; i < count; i++)
{
activeTransitions[i].OnEnter();
}
foreach (List<TransitionBase<TStateId>> transitions in activeTriggerTransitions.Values)
{
for (int i = 0, count = transitions.Count; i < count; i++)
{
transitions[i].OnEnter();
}
}
listener?.AfterTransition();
StateChanged?.Invoke(activeState);
if (activeState.isGhostState)
{
TryAllDirectTransitions();
}
}
/// <summary>
/// Signals to the parent fsm that this fsm can exit which allows the parent
/// fsm to transition to the next state.
/// </summary>
private void PerformVerticalTransition()
{
fsm?.StateCanExit();
}
/// <summary>
/// Requests a state change, respecting the <c>needsExitTime</c> property of the active state.
/// </summary>
/// <param name="name">The name / identifier of the target state.</param>
/// <param name="forceInstantly">Overrides the <c>needsExitTime</c> of the active state if true,
/// therefore forcing an immediate state change.</param>
/// <param name="listener">Optional object that receives callbacks before and after the transition.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void RequestStateChange(
TStateId name,
bool forceInstantly = false,
ITransitionListener listener = null)
{
if (!activeState.needsExitTime || forceInstantly)
{
pendingTransition = default;
ChangeState(name, listener);
}
else
{
pendingTransition.SetToState(name, listener);
activeState.OnExitRequest();
// If it can exit, the activeState would call
// -> state.fsm.StateCanExit() which in turn would call
// -> fsm.ChangeState(...)
}
}
/// <summary>
/// Requests a "vertical transition", allowing the state machine to exit
/// to allow the parent fsm to transition to the next state. It respects the
/// needsExitTime property of the active state.
/// </summary>
/// <param name="forceInstantly">Overrides the <c>needsExitTime</c> of the active state if true,
/// therefore forcing an immediate state change.</param>
/// <param name="listener">Optional object that receives callbacks before and after the transition.</param>
public void RequestExit(bool forceInstantly = false, ITransitionListener listener = null)
{
if (!activeState.needsExitTime || forceInstantly)
{
pendingTransition.Clear();
listener?.BeforeTransition();
PerformVerticalTransition();
listener?.AfterTransition();
}
else
{
pendingTransition.SetToExit(listener);
activeState.OnExitRequest();
}
}
/// <summary>
/// Checks if a transition can take place, and if this is the case, transition to the
/// "to" state and return true. Otherwise, it returns false.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private bool TryTransition(TransitionBase<TStateId> transition)
{
if (transition.isExitTransition)
{
if (fsm == null || !fsm.HasPendingTransition || !transition.ShouldTransition())
return false;
RequestExit(transition.forceInstantly, transition as ITransitionListener);
return true;
}
else
{
if (!transition.ShouldTransition())
return false;
RequestStateChange(transition.to, transition.forceInstantly, transition as ITransitionListener);
return true;
}
}
/// <summary>
/// Tries the "global" transitions that can transition from any state.
/// </summary>
/// <returns>Returns true if a transition occurred.</returns>
private bool TryAllGlobalTransitions()
{
for (int i = 0, count = transitionsFromAny.Count; i < count; i++)
{
TransitionBase<TStateId> transition = transitionsFromAny[i];
// Don't transition to the "to" state, if that state is already the active state.
if (EqualityComparer<TStateId>.Default.Equals(transition.to, activeState.name))
continue;
if (TryTransition(transition))
return true;
}
return false;
}
/// <summary>
/// Tries the "normal" transitions that transition from one specific state to another.
/// </summary>
/// <returns>Returns true if a transition occurred.</returns>
private bool TryAllDirectTransitions()
{
for (int i = 0, count = activeTransitions.Count; i < count; i++)
{
TransitionBase<TStateId> transition = activeTransitions[i];
if (TryTransition(transition))
return true;
}
return false;
}
/// <summary>
/// Calls <c>OnEnter</c> if it is the root state machine, therefore initialising the state machine.
/// </summary>
public override void Init()
{
if (!IsRootFsm) return;
OnEnter();
}
/// <summary>
/// Initialises the state machine and must be called before <c>OnLogic</c> is called.
/// It sets the activeState to the selected startState.
/// </summary>
public override void OnEnter()
{
if (!startState.hasState)
{
throw UnityHFSM.Exceptions.Common.MissingStartState(this, context: "Running OnEnter of the state machine.");
}
// Clear any previous pending transition from the last run.
pendingTransition.Clear();
ChangeState(startState.state);
for (int i = 0, count = transitionsFromAny.Count; i < count; i++)
{
transitionsFromAny[i].OnEnter();
}
foreach (List<TransitionBase<TStateId>> transitions in triggerTransitionsFromAny.Values)
{
for (int i = 0, count = transitions.Count; i < count; i++)
{
transitions[i].OnEnter();
}
}
}
/// <summary>
/// Runs one logic step. It performs at most one transition itself and
/// calls the active state's logic function (after the state transition,
/// if one occurred).
/// </summary>
public override void OnLogic()
{
EnsureIsInitializedFor("Running OnLogic");
if (TryAllGlobalTransitions())
goto runOnLogic;
if (TryAllDirectTransitions())
goto runOnLogic;
runOnLogic:
activeState?.OnLogic();
}
public override void OnExit()
{
if (activeState == null)
return;
if (rememberLastState)
{
startState = (activeState.name, true);
}
activeState.OnExit();
// By setting the activeState to null, the state's onExit method won't be called
// a second time when the state machine enters again (and changes to the start state).
activeState = null;
}
public override void OnExitRequest()
{
if (activeState.needsExitTime)
activeState.OnExitRequest();
}
/// <summary>
/// Defines the entry point of the state machine.
/// </summary>
/// <param name="name">The name / identifier of the start state.</param>
public void SetStartState(TStateId name)
{
startState = (name, true);
}
/// <summary>
/// Gets the StateBundle belonging to the <c>name</c> state "slot" if it exists.
/// Otherwise, it will create a new StateBundle, that will be added to the Dictionary,
/// and return the newly created instance.
/// </summary>
private StateBundle GetOrCreateStateBundle(TStateId name)
{
StateBundle bundle;
if (!stateBundlesByName.TryGetValue(name, out bundle))
{
bundle = new StateBundle();
stateBundlesByName.Add(name, bundle);
}
return bundle;
}
/// <summary>
/// Adds a new node / state to the state machine.
/// </summary>
/// <param name="name">The name / identifier of the new state.</param>
/// <param name="state">The new state instance,
/// e.g. <see cref="State"/>, <see cref="CoState"/>, <see cref="StateMachine"/>.</param>
public void AddState(TStateId name, StateBase<TStateId> state)
{
state.fsm = this;
state.name = name;
state.Init();
StateBundle bundle = GetOrCreateStateBundle(name);
bundle.state = state;
if (stateBundlesByName.Count == 1 && !startState.hasState)
{
SetStartState(name);
}
}
/// <summary>
/// Initialises a transition, i.e. sets its <c>fsm</c> attribute, and then calls its <c>Init</c> method.
/// </summary>
/// <param name="transition"></param>
private void InitTransition(TransitionBase<TStateId> transition)
{
transition.fsm = this;
transition.Init();
}
/// <summary>
/// Adds a new transition between two states.
/// </summary>
/// <param name="transition">The transition instance.</param>
public void AddTransition(TransitionBase<TStateId> transition)
{
InitTransition(transition);
StateBundle bundle = GetOrCreateStateBundle(transition.from);
bundle.AddTransition(transition);
}
/// <summary>
/// Adds a new transition that can happen from any possible state.
/// </summary>
/// <param name="transition">The transition instance; The "from" field can be
/// left empty, as it has no meaning in this context.</param>
public void AddTransitionFromAny(TransitionBase<TStateId> transition)
{
InitTransition(transition);
transitionsFromAny.Add(transition);
}
/// <summary>
/// Adds a new trigger transition between two states that is only checked
/// when the specified trigger is activated.
/// </summary>
/// <param name="trigger">The name / identifier of the trigger.</param>
/// <param name="transition">The transition instance,
/// e.g. <see cref="Transition"/>, <see cref="TransitionAfter"/>, ...</param>
public void AddTriggerTransition(TEvent trigger, TransitionBase<TStateId> transition)
{
InitTransition(transition);
StateBundle bundle = GetOrCreateStateBundle(transition.from);
bundle.AddTriggerTransition(trigger, transition);
}
/// <summary>
/// Adds a new trigger transition that can happen from any possible state, but is only
/// checked when the specified trigger is activated.
/// </summary>
/// <param name="trigger">The name / identifier of the trigger</param>
/// <param name="transition">The transition instance; The "from" field can be
/// left empty, as it has no meaning in this context.</param>
public void AddTriggerTransitionFromAny(TEvent trigger, TransitionBase<TStateId> transition)
{
InitTransition(transition);
List<TransitionBase<TStateId>> transitionsOfTrigger;
if (!triggerTransitionsFromAny.TryGetValue(trigger, out transitionsOfTrigger))
{
transitionsOfTrigger = new List<TransitionBase<TStateId>>();
triggerTransitionsFromAny.Add(trigger, transitionsOfTrigger);
}
transitionsOfTrigger.Add(transition);
}
/// <summary>
/// Adds two transitions:
/// If the condition of the transition instance is true, it transitions from the "from"
/// state to the "to" state. Otherwise, it performs a transition in the opposite direction,
/// i.e. from "to" to "from".
/// </summary>
/// <remarks>
/// Internally the same transition instance will be used for both transitions
/// by wrapping it in a <see cref="ReverseTransition"/>.
/// For the reverse transition the <c>afterTransition</c> callback is called before the transition
/// and the <c>onTransition</c> callback afterwards. If this is not desired then replicate the behaviour
/// of the two-way transitions by creating two separate transitions.
/// </remarks>
public void AddTwoWayTransition(TransitionBase<TStateId> transition)
{
InitTransition(transition);
AddTransition(transition);
ReverseTransition<TStateId> reverse = new ReverseTransition<TStateId>(transition, false);
InitTransition(reverse);
AddTransition(reverse);
}
/// <summary>
/// Adds two transitions that are only checked when the specified trigger is activated:
/// If the condition of the transition instance is true, it transitions from the "from"
/// state to the "to" state. Otherwise, it performs a transition in the opposite direction,
/// i.e. from "to" to "from".
/// </summary>
/// <remarks>
/// Internally the same transition instance will be used for both transitions
/// by wrapping it in a <see cref="ReverseTransition"/>.
/// For the reverse transition the <c>afterTransition</c> callback is called before the transition
/// and the <c>onTransition</c> callback afterwards. If this is not desired then replicate the behaviour
/// of the two-way transitions by creating two separate transitions.
/// </remarks>
public void AddTwoWayTriggerTransition(TEvent trigger, TransitionBase<TStateId> transition)
{
InitTransition(transition);
AddTriggerTransition(trigger, transition);
ReverseTransition<TStateId> reverse = new ReverseTransition<TStateId>(transition, false);
InitTransition(reverse);
AddTriggerTransition(trigger, reverse);
}
/// <summary>
/// Adds a new exit transition from a state. It represents an exit point that
/// allows the fsm to exit and the parent fsm to continue to the next state.
/// It is only checked if the parent fsm has a pending transition.
/// </summary>
/// <param name="transition">The transition instance. The "to" field can be
/// left empty, as it has no meaning in this context.</param>
public void AddExitTransition(TransitionBase<TStateId> transition)
{
transition.isExitTransition = true;
AddTransition(transition);
}
/// <summary>
/// Adds a new exit transition that can happen from any possible state.
/// It represents an exit point that allows the fsm to exit and the parent fsm to continue
/// to the next state. It is only checked if the parent fsm has a pending transition.
/// </summary>
/// <param name="transition">The transition instance. The "from" and "to" fields can be
/// left empty, as they have no meaning in this context.</param>
public void AddExitTransitionFromAny(TransitionBase<TStateId> transition)
{
transition.isExitTransition = true;
AddTransitionFromAny(transition);
}
/// <summary>
/// Adds a new exit transition from a state that is only checked when the specified trigger
/// is activated.
/// It represents an exit point that allows the fsm to exit and the parent fsm to continue
/// to the next state. It is only checked if the parent fsm has a pending transition.
/// </summary>
/// <param name="transition">The transition instance. The "to" field can be
/// left empty, as it has no meaning in this context.</param>
public void AddExitTriggerTransition(TEvent trigger, TransitionBase<TStateId> transition)
{
transition.isExitTransition = true;
AddTriggerTransition(trigger, transition);
}
/// <summary>
/// Adds a new exit transition that can happen from any possible state and is only checked
/// when the specified trigger is activated.
/// It represents an exit point that allows the fsm to exit and the parent fsm to continue
/// to the next state. It is only checked if the parent fsm has a pending transition.
/// </summary>
/// <param name="transition">The transition instance. The "from" and "to" fields can be
/// left empty, as they have no meaning in this context.</param>
public void AddExitTriggerTransitionFromAny(TEvent trigger, TransitionBase<TStateId> transition)
{
transition.isExitTransition = true;
AddTriggerTransitionFromAny(trigger, transition);
}
/// <summary>
/// Activates the specified trigger, checking all targeted trigger transitions to see whether
/// a transition should occur.
/// </summary>
/// <param name="trigger">The name / identifier of the trigger.</param>
/// <returns>True when a transition occurred, otherwise false.</returns>
private bool TryTrigger(TEvent trigger)
{
EnsureIsInitializedFor("Checking all trigger transitions of the active state");
List<TransitionBase<TStateId>> triggerTransitions;
if (triggerTransitionsFromAny.TryGetValue(trigger, out triggerTransitions))
{
for (int i = 0, count = triggerTransitions.Count; i < count; i++)
{
TransitionBase<TStateId> transition = triggerTransitions[i];
if (EqualityComparer<TStateId>.Default.Equals(transition.to, activeState.name))
continue;
if (TryTransition(transition))
return true;
}
}
if (activeTriggerTransitions.TryGetValue(trigger, out triggerTransitions))
{
for (int i = 0, count = triggerTransitions.Count; i < count; i++)
{
TransitionBase<TStateId> transition = triggerTransitions[i];
if (TryTransition(transition))
return true;
}
}
return false;
}
/// <summary>
/// Activates the specified trigger in all active states of the hierarchy, checking all targeted
/// trigger transitions to see whether a transition should occur.
/// </summary>
/// <param name="trigger">The name / identifier of the trigger.</param>
public void Trigger(TEvent trigger)
{
// If a transition occurs, then the trigger should not be activated
// in the new active state, that the state machine just switched to.
if (TryTrigger(trigger)) return;
(activeState as ITriggerable<TEvent>)?.Trigger(trigger);
}
/// <summary>
/// Only activates the specified trigger locally in this state machine.
/// </summary>
/// <param name="trigger">The name / identifier of the trigger.</param>
public void TriggerLocally(TEvent trigger)
{
TryTrigger(trigger);
}
/// <summary>
/// Runs an action on the currently active state.
/// </summary>
/// <param name="trigger">Name of the action.</param>
public virtual void OnAction(TEvent trigger)
{
EnsureIsInitializedFor("Running OnAction of the active state");
(activeState as IActionable<TEvent>)?.OnAction(trigger);
}
/// <summary>
/// Runs an action on the currently active state and lets you pass one data parameter.
/// </summary>
/// <param name="trigger">Name of the action.</param>
/// <param name="data">Any custom data for the parameter.</param>
/// <typeparam name="TData">Type of the data parameter.
/// Should match the data type of the action that was added via <c>AddAction<T>(...).</c></typeparam>
public virtual void OnAction<TData>(TEvent trigger, TData data)
{
EnsureIsInitializedFor("Running OnAction of the active state");
(activeState as IActionable<TEvent>)?.OnAction<TData>(trigger, data);
}
/// <summary>
/// Checks if currently active state has this action.
/// </summary>
/// <param name="trigger">Name of the action.</param>
/// <returns></returns>
public virtual bool HasAction(TEvent trigger)
{
EnsureIsInitializedFor("Running HasAction of the active state");
return (activeState as IActionable<TEvent>)?.HasAction(trigger) ?? false;
}
public StateBase<TStateId> GetState(TStateId name)
{
StateBundle bundle;
if (!stateBundlesByName.TryGetValue(name, out bundle) || bundle.state == null)
{
throw UnityHFSM.Exceptions.Common.StateNotFound(this, name.ToString(), context: "Getting a state");
}
return bundle.state;
}
/// <summary>
/// Only for state machines using string types: Returns a nested state machine with the given name.
/// This is a convenience function when working with string hierarchical state machines.
/// </summary>
public StateMachine<string, string, string> this[TStateId name]
{
get
{
StateBase<TStateId> state = GetState(name);
StateMachine<string, string, string> subFsm = state as StateMachine<string, string, string>;
if (subFsm == null)
{
throw UnityHFSM.Exceptions.Common.QuickIndexerMisusedForGettingState(this, name.ToString());
}
return subFsm;
}
}
public override string GetActiveHierarchyPath()
{
if (activeState == null)
{
// When the state machine is not active, then the active hierarchy path
// is empty.
return "";
}
return $"{name}/{activeState.GetActiveHierarchyPath()}";
}
/// <summary>Returns a list of the names of all currently defined states.</summary>
/// <remarks>Warning: this is an expensive operation.</remarks>
public IReadOnlyList<TStateId> GetAllStateNames()
{
return stateBundlesByName.Values
.Where(bundle => bundle.state != null)
.Select(bundle => bundle.state.name)
.ToArray();
}
/// <summary>Returns a list of all currently defined states.</summary>
/// <remarks>Warning: this is an expensive operation.</remarks>
public IReadOnlyList<StateBase<TStateId>> GetAllStates()
{
return stateBundlesByName.Values
.Where(bundle => bundle.state != null)
.Select(bundle => bundle.state)
.ToArray();
}
public TStateId GetStartStateName()
{
if (!startState.hasState)
{
throw UnityHFSM.Exceptions.Common.MissingStartState(
this,
context: "Getting the start state",
solution: "Make sure that there is at least one state in the state machine before running "
+ "GetStartStateName() by calling fsm.AddState(...).");
}
return startState.state;
}
/// <summary>Returns a list of all added state transitions.</summary>
/// <remarks>Warning: this is an expensive operation.</remarks>
public IReadOnlyList<TransitionBase<TStateId>> GetAllTransitions()
{
return stateBundlesByName.Values
.Where(bundle => bundle.transitions != null)
.SelectMany(bundle => bundle.transitions)
.ToArray();
}
/// <summary>Returns a list of all added state "transitions from any".</summary>
public IReadOnlyList<TransitionBase<TStateId>> GetAllTransitionsFromAny()
{
return transitionsFromAny.ToArray();
}
/// <summary>Returns all added trigger transitions, grouped by their trigger events.</summary>
/// <remarks>Warning: this is an expensive operation.</remarks>
public IReadOnlyDictionary<TEvent, IReadOnlyList<TransitionBase<TStateId>>> GetAllTriggerTransitions()
{
var transitionsByEvent = new Dictionary<TEvent, List<TransitionBase<TStateId>>>();
foreach (var bundle in stateBundlesByName.Values)
{
if (bundle.triggerToTransitions == null)
continue;
foreach ((TEvent trigger, List<TransitionBase<TStateId>> transitions) in bundle.triggerToTransitions)
{
if (!transitionsByEvent.TryGetValue(trigger, out List<TransitionBase<TStateId>> transitionsForEvent))
{
transitionsForEvent = new List<TransitionBase<TStateId>>();
transitionsByEvent.Add(trigger, transitionsForEvent);
}
transitionsForEvent.AddRange(transitions);
}
}
var immutableCopy = new Dictionary<TEvent, IReadOnlyList<TransitionBase<TStateId>>>();
foreach ((TEvent trigger, List<TransitionBase<TStateId>> transitions) in transitionsByEvent)
{
immutableCopy.Add(trigger, transitions);
}
return immutableCopy;
}
/// <summary>Returns all added "trigger transitions from any", grouped by their trigger events.</summary>
/// <remarks>Warning: this is an expensive operation.</remarks>
public IReadOnlyDictionary<TEvent, IReadOnlyList<TransitionBase<TStateId>>> GetAllTriggerTransitionsFromAny()
{
var immutableCopy = new Dictionary<TEvent, IReadOnlyList<TransitionBase<TStateId>>>();
foreach ((TEvent trigger, List<TransitionBase<TStateId>> transitions) in triggerTransitionsFromAny)
{
immutableCopy.Add(trigger, transitions);
}
return immutableCopy;
}
public override void AcceptVisitor(IStateVisitor visitor)
{
visitor.VisitStateMachine(this);
}
}
// Overloaded classes to allow for an easier usage of the StateMachine for common cases.
// E.g. new StateMachine() instead of new StateMachine<string, string, string>()
/// <inheritdoc />
public class StateMachine<TStateId, TEvent> : StateMachine<TStateId, TStateId, TEvent>
{
/// <inheritdoc />
public StateMachine(bool needsExitTime = false, bool isGhostState = false, bool rememberLastState = false)
: base(needsExitTime: needsExitTime, isGhostState: isGhostState, rememberLastState: rememberLastState)
{
}
}
/// <inheritdoc />
public class StateMachine<TStateId> : StateMachine<TStateId, TStateId, string>
{
public StateMachine(bool needsExitTime = false, bool isGhostState = false, bool rememberLastState = false)
: base(needsExitTime: needsExitTime, isGhostState: isGhostState, rememberLastState: rememberLastState)
{
}
}
/// <inheritdoc />
public class StateMachine : StateMachine<string, string, string>
{
/// <inheritdoc />
public StateMachine(bool needsExitTime = false, bool isGhostState = false, bool rememberLastState = false)
: base(needsExitTime: needsExitTime, isGhostState: isGhostState, rememberLastState: rememberLastState)
{
}
}
}