44using System . Threading ;
55using System . Threading . Tasks ;
66using JetBrains . Annotations ;
7+ using Nonatomic . VSM2 . Data ;
78using Nonatomic . VSM2 . Logging ;
89using Nonatomic . VSM2 . NodeGraph ;
910using Nonatomic . VSM2 . StateGraph . States ;
@@ -14,7 +15,9 @@ namespace Nonatomic.VSM2.StateGraph
1415 public class StateMachine
1516 {
1617 public event Action < State , StateMachineModel > OnComplete ;
17-
18+
19+ public ISharedData SharedData { get ; }
20+
1821 public State State => _currentNode ? . State ;
1922 public StateMachineModel Model { get ; private set ; }
2023 public bool IsComplete { get ; private set ; }
@@ -24,14 +27,17 @@ public class StateMachine
2427 private Dictionary < string , List < StateTransitionModel > > _transitionLookup = new ( ) ;
2528 private Dictionary < JumpId , StateNodeModel > _jumpNodeLookup = new ( ) ;
2629 private CancellationTokenSource _cancellationTokenSource = new ( ) ;
27-
30+
2831 public StateMachine ( StateMachineModel model , GameObject gameObject )
2932 {
30- Model = StateMachineModel . CreateInstance ( model ) ;
31- Model . Initialize ( gameObject , this ) ;
32-
33- CreateNodeLookupTable ( ) ;
34- CreateTransitionLookupTable ( ) ;
33+ SharedData = new SharedData ( ) ;
34+ Initialize ( model , gameObject ) ;
35+ }
36+
37+ public StateMachine ( StateMachineModel model , GameObject gameObject , ISharedData sharedData = null )
38+ {
39+ SharedData = sharedData ?? new SharedData ( ) ;
40+ Initialize ( model , gameObject ) ;
3541 }
3642
3743 public void Update ( )
@@ -80,7 +86,7 @@ public void Exit()
8086 _currentNode . Exit ( ) ;
8187 IsComplete = true ;
8288 }
83-
89+
8490 public void JumpTo ( JumpId jumpId )
8591 {
8692 if ( ! _jumpNodeLookup . TryGetValue ( jumpId , out var nextNode ) )
@@ -113,6 +119,15 @@ public void OnDestroy()
113119 Model = null ;
114120 }
115121
122+ private void Initialize ( StateMachineModel model , GameObject gameObject )
123+ {
124+ Model = StateMachineModel . CreateInstance ( model ) ;
125+ Model . Initialize ( gameObject , this , SharedData ) ;
126+
127+ CreateNodeLookupTable ( ) ;
128+ CreateTransitionLookupTable ( ) ;
129+ }
130+
116131 private void CreateTransitionLookupTable ( )
117132 {
118133 _transitionLookup . Clear ( ) ;
@@ -127,7 +142,7 @@ private void CreateTransitionLookupTable()
127142 _transitionLookup [ transition . OriginNodeId ] . Add ( transition ) ;
128143 }
129144 }
130-
145+
131146 private void CreateNodeLookupTable ( )
132147 {
133148 _nodeLookup . Clear ( ) ;
0 commit comments