Creating a finite state machine agent
Now that we've created a finite state machine, we can simply replace the decision tree that powers our indirect soldier agent with a finite state machine. The initialization, updating, and termination of our FSM are identical to the decision tree.
With an abstract data structure, we can now create both decision-tree-based agents and finite state machine agents simultaneously:
IndirectSoldierAgent.lua
:
local soldier; local soldierController; local soldierFSM; local soldierUserData; function Agent_Initialize(agent) ... soldierFSM = SoldierLogic_FiniteStateMachine( soldierUserData); end function Agent_Update(agent, deltaTimeInMillis) if (soldierUserData.alive) then soldierFSM:Update(deltaTimeInMillis); end soldierController:Update(agent, deltaTimeInMillis); end