Skip to content

Instantly share code, notes, and snippets.

@hclarke
Last active March 11, 2017 19:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hclarke/7d225a5adad7a186e47a861d3f89dde5 to your computer and use it in GitHub Desktop.
Save hclarke/7d225a5adad7a186e47a861d3f89dde5 to your computer and use it in GitHub Desktop.
protected delegate State State(bool? prev);
protected readonly State PASS = x => PASS;
protected readonly State FAIL = x => FAIL;
protected readonly State TICK = x => TICK;
Stack<State> stateStack = new Stack<State>();
protected abstract State GetBaseState(); //put your behaviour here
protected void Update() {
bool? prev = null;
if(stateStack.Count == 0) stateStack.Push(GetBaseState());
while(stateStack.count > 0) {
var top = stateStack.Pop();
var res = top(prev);
if(res == PASS) {
prev = true;
continue;
}
if(res == FAIL) {
prev = false;
continue;
}
stateStack.Push(top);
if(res == TICK) {
break;
}
else {
stateStack.Push(res);
prev = null;
continue;
}
}
}
protected State Sequence(params State[] states) {
int i = 0;
State self = null;
return self = prev => {
if(i >= states.Length) return PASS;
if(prev == false) return FAIL;
return states[i++];
};
}
protected State Selector(params State[] states) {
int i = 0;
State self = null;
return self = prev => {
if(i >= states.Length) return FAIL;
if(prev == true) return PASS;
return states[i++];
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment