// return true to change state public FSMToken.ExcuteResult Excute(int index, ref int lastToken, FSMHandle handle, Stack<State> stack, object externalArg) { FSMToken.ExcuteResult excuteResult = null; object lastRetVal = externalArg; foreach (var token in tokens) { if (tokens.IndexOf(token) < index) { continue; } { excuteResult = token.Excute(lastRetVal, handle, stack); lastRetVal = excuteResult.retVal; if (token.name == FSMKeywords.Sleep) { lastToken = tokens.IndexOf(token); } } if (excuteResult.stopSentence) { break; } } return excuteResult; }
public override ExcuteResult Excute(object lastRetVal, FSMHandle handle, Stack<State> stack) { if (methodInfo == null) { Debug.LogError(name + " has no method"); } object retValue = Excute(ToParam(lastRetVal), handle, stack); ExcuteResult ret = new ExcuteResult(); if (methodInfo.ReturnType == typeof(Boolean)) { if (retValue is bool && (bool)retValue == false) { ret.stopParagraph = false; ret.stopSentence = true; } } else { ret.retVal = retValue; } return ret; }
public override ExcuteResult Excute(object lastRetVal, FSMHandle handle, Stack<State> stack) { Dictionary<string, object> variables = null; int count = str.Length - str.Replace("$", "").Length; foreach (var entity in stack) { if (--count == 0) { variables = entity.variables; break; } } if (variables == null) { Debug.LogError("must not be null"); } if (isSetter) { if (!variables.ContainsKey(name)) { variables.Add(name, lastRetVal); } else { variables[name] = lastRetVal; } } ExcuteResult ret = new ExcuteResult(); ret.retVal = variables[name]; return ret; }
public void OnCreate(FSMHandle handle, Stack<State> stack, object stateArg) { variables = new Dictionary<string, object>(); //LogTool.Log(Name + ".OnCreate"); paragraph.title.Excute(0, ref lastToken, handle, stack, stateArg); ExcuteEvent("create", handle, stack, null); }
public override ExcuteResult Excute(object lastRetVal, FSMHandle handle, Stack<State> stack) { ExcuteResult ret = new ExcuteResult(); ret.stopParam = lastRetVal; ret.stopTokenType = type; ret.stopParagraph = true; ret.stopSentence = true; return ret; }
public override ExcuteResult Excute(object lastRetVal, FSMHandle handle, Stack<State> stack) { if (!string.IsNullOrEmpty(paramName)) { var variables = stack.Peek().variables; if (!variables.ContainsKey(paramName)) { variables[paramName] = null; } variables[paramName] = lastRetVal; } return new ExcuteResult(); }
object Excute(object[] parameters, FSMHandle handle, Stack<State> stack) { object ret = methodInfo.Invoke(handle, parameters); if (isNegative) { if (ret.GetType() == typeof(bool)) { ret = !(bool)ret; } else { Debug.LogError("undefined operation"); } } return ret; }
public void OnDestroy(FSMHandle handle, Stack<State> stack) { //LogTool.Log(Name + ".OnDestroy"); ExcuteEvent("destroy", handle, stack, null); foreach (var entity in variables) { if (entity.Value is Object) { Object.Destroy(entity.Value as Object); } else { TryDestroy(entity); } } variables = null; }
public FSM(string path, FSMHandle handle) { this.handle = handle; stack = new Stack<State>(); FSMPage page = FSMParser.Get(path, handle.GetType()); //Debug.LogWarning(page); this.name = path; this.initStateName = page.InitStateName; states = new Dictionary<string, State>(); foreach (var entity in page.Paragraphs) { states.Add(entity.Key, new State(entity.Value, this)); } SetState(initStateName, null); }
public FiniteStateMachine(StateAction[] stateActions, TransientAction[] openingActions, TransientAction[] closingActions, T startingState) { int nrOfStates = Enum.GetValues(typeof(T)).Length; if (stateActions.Length != nrOfStates) { throw new Exception("Invalid number of state actions, " + nrOfStates + "expected, " + stateActions.Length + " recieved."); } if (openingActions.Length != nrOfStates) { throw new Exception("Invalid number of opening actions, " + nrOfStates + "expected, " + openingActions.Length + " recieved."); } if (closingActions.Length != nrOfStates) { throw new Exception("Invalid number of closing actions, " + nrOfStates + "expected, " + closingActions.Length + " recieved."); } this.stateActions = stateActions; this.openingActions = openingActions; this.closingActions = closingActions; handle = new FSMHandle(startingState); }
public void OnExit(FSMHandle handle, Stack<State> stack) { ExcuteEvent("exit", handle, stack, null); }
private bool Excute(FSMSentence sentence, int tokenIndex, FSMHandle handle, Stack<State> stack, object externalArg) { FSMToken.ExcuteResult excuteResult = sentence.Excute(tokenIndex, ref lastToken, handle, stack, externalArg); if (excuteResult == null) { Debug.LogError("excuteResult null on " + sentence + ", token:" + tokenIndex); } if (excuteResult.stopParagraph) { if (excuteResult.stopTokenType == FSMToken.E_TokenType.ChangeState) { string nextState = excuteResult.stopParam as string; fsm.SetState(nextState, excuteResult.stopParamArg); } else if (excuteResult.stopTokenType == FSMToken.E_TokenType.PushState) { string nextState = excuteResult.stopParam as string; fsm.PushState(nextState, excuteResult.stopParamArg); } else if (excuteResult.stopTokenType == FSMToken.E_TokenType.PopState) { fsm.PopState(); } else if (excuteResult.stopTokenType == FSMToken.E_TokenType.SetTimer) { float? waitTime = Cast.To<float>(excuteResult.stopParam); SetTimer(waitTime.Value, handle, stack); } else if (excuteResult.stopTokenType == FSMToken.E_TokenType.Sleep) { float? waitTime = Cast.To<float>(excuteResult.stopParam); Sleep(waitTime.Value, handle, stack); } else { Debug.LogError("why stop in stopParam " + excuteResult.stopParam + "(" + excuteResult.stopParam.GetType() + ")"); } } return excuteResult.stopParagraph; }
public void Update(FSMHandle handle, Stack<State> stack) { foreach (var sentence in paragraph.sentences) { //LogTool.Log("[sentence] " + sentence); bool stop = Excute(sentence, 0, handle, stack, null); if (stop) { lastSentence = sentence; return; } } }
public override ExcuteResult Excute(object lastRetVal, FSMHandle handle, Stack<State> stack) { ExcuteResult ret = new ExcuteResult(); ret.retVal = name; return ret; }
public void OnTimer(FSMHandle handle, Stack<State> stack) { ExcuteEvent("timer", handle, stack, null); }
public void OnEvent(string eventName, FSMHandle handle, Stack<State> stack, object eventArg) { ExcuteEvent(eventName, handle, stack, eventArg); }
private void SetTimer(float waitTime, FSMHandle handle, Stack<State> stack) { remainTimeForTimer = waitTime; routineForTimer = delegate() { if (fsm.IsCurrentState(this)) { Debug.LogWarning("OnTimer~~"); this.OnTimer(handle, stack); } }; }
public abstract ExcuteResult Excute(object lastRetVal, FSMHandle handle, Stack<State> stack);
private void Sleep(float waitTime, FSMHandle handle, Stack<State> stack) { Assert.IsTrue(!isWaiting); isWaiting = true; remainTimeForSleep = waitTime; routineForSleep = delegate() { isWaiting = false; if (fsm.IsCurrentState(this)) { FSMSentence sentence = lastSentence; int token = lastToken; if (sentence.HasToken(token + 1)) { Excute(sentence, token + 1, handle, stack, null); } } }; }
public void InitFSM(string scriptPath, FSMHandle handle, bool enableLog) { fsm = new FSM(scriptPath, handle); fsm.EnableLog(enableLog); }
private void ExcuteEvent(string eventName, FSMHandle handle, Stack<State> stack, object externalArg) { if (isWaiting) { return; } if (paragraph.events.ContainsKey(eventName)) { var sentence = paragraph.events[eventName]; bool stop = Excute(sentence, 0, handle, stack, externalArg); if (stop) { lastSentence = sentence; return; } } }