/// <summary> /// Orchestrator function that runs a CNCF Serverless workflow. /// </summary> public static Task <JToken> DurableWorkflowRunner(IDurableOrchestrationContext context) { var args = context.GetInput <StartWorkflowArgs>(); var host = new DurableFunctionsHost(context, args.TelemetryUri); return(StateMachineRunner.RunAsync(args.Definition, host, args.Input, args.Actions)); }
public void Init() { go = new GameObject("stateTest"); behaviour1 = go.AddComponent<ClassWithBasicStates>(); behaviour2 = go.AddComponent<ClassWithBasicStates>(); behaviour3 = go.AddComponent<ClassWithBasicStates>(); engine = go.AddComponent<StateMachineRunner>(); }
public void Init() { go = new GameObject("stateTest"); behaviour1 = go.AddComponent <ClassWithBasicStates>(); behaviour2 = go.AddComponent <ClassWithBasicStates>(); behaviour3 = go.AddComponent <ClassWithBasicStates>(); engine = go.AddComponent <StateMachineRunner>(); }
/// <summary> /// Inspects a MonoBehaviour for state methods as definied by the supplied Enum, and returns a stateMachine instance used to trasition states. /// </summary> /// <param name="component">The component with defined state methods</param> /// <param name="startState">The default starting state</param> /// <returns>A valid stateMachine instance to manage MonoBehaviour state transitions</returns> public static StateMachine <T> Initialize(MonoBehaviour component, T startState) { var engine = component.GetComponent <StateMachineRunner>(); if (engine == null) { engine = component.gameObject.AddComponent <StateMachineRunner>(); } return(engine.Initialize <T>(component, startState)); }
public async Task HelloWorld() { var definition = File.ReadAllText("ExampleWorkflows//helloworld.json"); var workflow = JsonConvert.DeserializeObject <StateMachine>(definition); var json = await StateMachineRunner.RunAsync(workflow, TheHost); Assert.IsNotNull(json); Assert.AreEqual(JTokenType.Object, json.Type); Assert.IsTrue(((JObject)json).ContainsKey("result")); Assert.AreEqual("Hello World!", json["result"].Value <string>()); }
public void Init() { go = new GameObject("stateTest"); behaviour = go.AddComponent <MonoBehaviour>(); engine = go.AddComponent <StateMachineRunner>(); }
public void Init() { go = new GameObject("stateTest"); behaviour = go.AddComponent<ClassDerivedFromSuperClass>(); engine = go.AddComponent<StateMachineRunner>(); }
public void Init() { behaviour = new ClassWithBasicStates(); engine = StateMachineRunner.Instance; }
public void Init() { go = new GameObject("stateTest"); behaviour = new ClassDerivedFromSuperClass(); engine = StateMachineRunner.Instance; }
public void Init() { go = new GameObject("stateTest"); behaviour = go.AddComponent<MonoBehaviour>(); engine = go.AddComponent<StateMachineRunner>(); }
public StateMachine(StateMachineRunner engine, BaseState stateScript, string[] states) { _engine = engine; _stateScript = stateScript; _stateLookup = new Dictionary <string, StateMapping> (); foreach (var state in states) { var mapping = new StateMapping(state); _stateLookup.Add(state, mapping); } // Reflect methods var methods = _stateScript.GetType().GetMethods(BindingFlags.Instance | BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.NonPublic); for (int i = 0; i < methods.Length; i++) { if (methods[i].GetCustomAttributes(typeof(CompilerGeneratedAttribute), true).Length != 0) { continue; } int lastIdx = methods[i].Name.LastIndexOf('_'); if (lastIdx < 0) { continue; } string name = methods[i].Name.Substring(0, lastIdx); if (!states.Contains(name)) { continue; } StateMapping targetState = _stateLookup[name]; switch (methods[i].Name.Substring(lastIdx + 1)) { case "Enter": if (methods[i].ReturnType == typeof(IEnumerator)) { targetState.hasEnterRoutine = true; targetState.EnterRoutine = CreateDelegate <Func <IEnumerator> > (methods[i], _stateScript); } else { targetState.hasEnterRoutine = false; targetState.EnterCall = CreateDelegate <Action> (methods[i], _stateScript); } break; case "Exit": if (methods[i].ReturnType == typeof(IEnumerator)) { targetState.hasExitRoutine = true; targetState.ExitRoutine = CreateDelegate <Func <IEnumerator> > (methods[i], _stateScript); } else { targetState.hasExitRoutine = false; targetState.ExitCall = CreateDelegate <Action> (methods[i], _stateScript); } break; case "Finally": targetState.Finally = CreateDelegate <Action> (methods[i], _stateScript); break; case "Update": targetState.Update = CreateDelegate <Action> (methods[i], _stateScript); break; case "LateUpdate": targetState.LateUpdate = CreateDelegate <Action> (methods[i], _stateScript); break; case "FixedUpdate": targetState.FixedUpdate = CreateDelegate <Action> (methods[i], _stateScript); break; } } _currentState = new StateMapping(null); }
public void Init() { go = new GameObject("stateTest"); behaviour = go.AddComponent <ClassDerivedFromSuperClass>(); engine = go.AddComponent <StateMachineRunner>(); }
public StateMachine(StateMachineRunner engine, MonoBehaviour component) { this.engine = engine; this.component = component; //Define States var values = Enum.GetValues(typeof(T)); if (values.Length < 1) { throw new ArgumentException("Enum provided to Initialize must have at least 1 visible definition"); } stateLookup = new Dictionary <object, StateMapping>(); for (int i = 0; i < values.Length; i++) { var mapping = new StateMapping((Enum)values.GetValue(i)); stateLookup.Add(mapping.state, mapping); } //Reflect methods var methods = component.GetType().GetMethods(BindingFlags.Instance | BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.NonPublic); //Bind methods to states var separator = "_".ToCharArray(); for (int i = 0; i < methods.Length; i++) { if (methods[i].GetCustomAttributes(typeof(CompilerGeneratedAttribute), true).Length != 0) { continue; } var names = methods[i].Name.Split(separator); //Ignore functions without an underscore if (names.Length <= 1) { continue; } Enum key; try { key = (Enum)Enum.Parse(typeof(T), names[0]); } catch (ArgumentException) { //Not an method as listed in the state enum continue; } var targetState = stateLookup[key]; switch (names[1]) { case "Enter": if (methods[i].ReturnType == typeof(IEnumerator)) { targetState.hasEnterRoutine = true; targetState.EnterRoutine = CreateDelegate <Func <IEnumerator> >(methods[i], component); } else { targetState.hasEnterRoutine = false; targetState.EnterCall = CreateDelegate <Action>(methods[i], component); } break; case "Exit": if (methods[i].ReturnType == typeof(IEnumerator)) { targetState.hasExitRoutine = true; targetState.ExitRoutine = CreateDelegate <Func <IEnumerator> >(methods[i], component); } else { targetState.hasExitRoutine = false; targetState.ExitCall = CreateDelegate <Action>(methods[i], component); } break; case "Finally": targetState.Finally = CreateDelegate <Action>(methods[i], component); break; case "Update": targetState.Update = CreateDelegate <Action>(methods[i], component); break; case "LateUpdate": targetState.LateUpdate = CreateDelegate <Action>(methods[i], component); break; case "FixedUpdate": targetState.FixedUpdate = CreateDelegate <Action>(methods[i], component); break; case "OnCollisionEnter": targetState.OnCollisionEnter = CreateDelegate <Action <Collision> >(methods[i], component); break; } } //Create nil state mapping currentState = new StateMapping(null); }