From 0ee35adc813cb86a972d537fb783bcad82ff4e99 Mon Sep 17 00:00:00 2001 From: knowledgenude Date: Sun, 14 Feb 2021 19:21:26 -0300 Subject: [PATCH] Improved FSM --- Sources/armory/system/FSM.hx | 109 ++++++++++++++--------------------- 1 file changed, 44 insertions(+), 65 deletions(-) diff --git a/Sources/armory/system/FSM.hx b/Sources/armory/system/FSM.hx index 53a66007..f703aea7 100644 --- a/Sources/armory/system/FSM.hx +++ b/Sources/armory/system/FSM.hx @@ -1,49 +1,25 @@ package armory.system; -class FSM { - var state: Null; - var nextState: Null; - var transitions = new Array(); - var tempTransitions = new Array(); +class FSM { + final transitions = new Array>(); + final tempTransitions = new Array>(); + var state: Null>; var entered = false; public function new() {} - /** - * Set the initial / current state of the FSM and return it - * @param state The state to be set - * @return State - **/ - public function setState(state: State) { + public function bindTransition(canEnter: Void -> Bool, fromState: State, toState: State) { + final transition = new Transition(canEnter, fromState, toState); + transitions.push(transition); + syncTransitions(); + } + + public function setInitState(state: State) { this.state = state; syncTransitions(); - return state; } - /** - * Bind multiple transitions to the specified state - * @param canEnter The function that returns true or false to enter the transition - * @param toState The next state the transiiton will return - * @param fromStates The states that are allowed to be changed by the next state - * @return Void - **/ - public function bindTransitions(canEnter: Void -> Bool, toState: State, fromStates: Array) { - for (s in fromStates) { - transitions.push(new Transition(canEnter, s, toState)); - } - - syncTransitions(); - } - - function syncTransitions() { - tempTransitions = []; - - for (t in transitions) { - if (t.isConnected(state)) tempTransitions.push(t); - } - } - - public function run() { + public function update() { if (!entered) { state.onEnter(); entered = true; @@ -51,45 +27,48 @@ class FSM { state.onUpdate(); - for (t in tempTransitions) { - if (t.canEnter()) { - nextState = t.getNextState(); + for (transition in tempTransitions) { + if (transition.canEnter()) { state.onExit(); - state = nextState; + state = transition.toState; entered = false; syncTransitions(); break; } } } -} -class Transition { - final func: Void -> Bool; - final from: State; - final to: State; + public function syncTransitions() { + tempTransitions.resize(0); - public function new(func: Void -> Bool, from: State, to: State) { - this.func = func; - this.from = from; - this.to = to; - } - - public function canEnter() { - return func(); - } - - public function isConnected(state: State) { - return from == state; - } - - public function getNextState() { - return to; + for (transition in transitions) { + if (transition.fromState == state) tempTransitions.push(transition); + } } } -interface State { - function onEnter(): Void; - function onUpdate(): Void; - function onExit(): Void; +class Transition { + public final canEnter: Void -> Bool; + public final fromState: State; + public final toState: State; + + public function new(canEnter: Void -> Bool, fromState: State, toState: State) { + this.canEnter = canEnter; + this.fromState = fromState; + this.toState = toState; + } +} + +class State { + final owner: T; + + public function new(owner: T) { + this.owner = owner; + } + + public function onEnter() {} + + public function onUpdate() {} + + public function onExit() {} } \ No newline at end of file