/* name: FSM.cs * author: sam petro * created: 8 Feb 2007 * last updated: 8 Feb 2007 * * description: the finite state machine (FSM) which will control * the AI for the enemy ships * */ using System; using System.Collections.Generic; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Audio; using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Input; using Microsoft.Xna.Framework.Storage; using Microsoft.Xna.Framework.Content; using ToolshedGames.InfiniteVelocity.AI; namespace ToolshedGames.InfiniteVelocity.AI { public class FSM //created 8 feb 2007 { //--- PRIVATE DATA private entity pOwner; //a ref to the instance's owner private State pCurrentState; //a reference to the current state private State pPreviousState; //a reference to the previous state private State pGlobalState; //the global state called by the FSM //--- CONSTRUCTORS -------------------// /// /// constructor. initializes the current state, /// previous state, and glo /// /// the owner of the FSM public FSM(entity owner) { pOwner = owner; pCurrentState = null; pPreviousState = null; pGlobalState = null; } ~FSM() { } //destructor //--- FSM initializers ----------------------// /// /// Set the current state. /// used during initialization /// /// current state of to the entity public void SetCurrentState(State cs) { pCurrentState = cs; } /// /// set the global state /// used during initialization /// do not initialize if entity does not have a current state /// /// global state of the entity public void SetGlobalState(State gs) {pGlobalState = gs;} /// /// set the previous state /// possibly to be used in AI stack? /// /// previous state of the entity public void SetPreviousState(State ps) {pPreviousState = ps;} //--- GET states --------------// REVAMP IN C# STYLE??? State CurrentState() { return pCurrentState; } State GlobalState() { return pGlobalState; } State PreviousState() { return pPreviousState; } //--- UPDATE ------------------// /// /// update the FSM. updates the owner's global /// then current states. /// public void Update() { //if there's a global state, execute it if (pGlobalState != null) pGlobalState.Execute(pOwner); //execute the current state too if (pCurrentState != null) pCurrentState.Execute(pOwner); } /* public bool HandleMessage(ref Telegram msg) { //!!!!! CHECK VALIDITY OF STATEMENTS * // AND ADD DATESTAMP //check to see that the current state is valid //and can handle the message if (pCurrentState != null && pCurrentState.OnMessage(pOwner, msg)) return true; //if not, check for global state, send message to //global state, if implemented if (pGlobalState != null && pGlobalState.OnMessage(pOwner, msg)) return true; //if both checks fail return false; }*/ //--- STATE CHANGES ---------------------------// /// /// transitions into the new state from the old /// stores old state, calls its exit function /// sets new state as current, calls enter function /// /// new state to become current public void ChangeState(State pNewState) { //assert and throw error? (see AI book code) //keep a record of previous state pPreviousState = pCurrentState; //call the exit method pCurrentState.Exit(pOwner); //change state to new state pCurrentState = pNewState; //call the enter method of the new state pNewState.Enter(pOwner); } //POSSIBLE IMPLEMENTATION OF AI STACK??? //perhaps a new stack function set???? /// /// reverts to previous state /// useful for returning to global states /// public void RevertToPreviousState() { ChangeState(pPreviousState); } } }