using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/**
A Finite State Machine System based on Chapter 3.1 of Game Programming Gems 1 by Eric Dybsand
Written by Roberto Cezar Bianchini, July 2010
How to use:
1. Place the labels for the transitions and the states of the Finite State System
in the corresponding enums.
2. Write new class(es) inheriting from FSMState and fill each one with pairs (transition-state).
These pairs represent the state S2 the FSMSystem should be if while being on state S1, a
transition T is fired and state S1 has a transition from it to S2. Remember this is a Deterministic FSM.
You can't have one transition leading to two different states.
Method Reason is used to determine which transition should be fired.
You can write the code to fire transitions in another place, and leave this method empty if you
feel it's more appropriate to your project.
Method Act has the code to perform the actions the NPC is supposed do if it's on this state.
You can write the code for the actions in another place, and leave this method empty if you
feel it's more appropriate to your project.
3. Create an instance of FSMSystem class and add the states to it.
4. Call Reason and Act (or whichever methods you have for firing transitions and making the NPCs
behave in your game) from your Update or FixedUpdate methods.
Asynchronous transitions from Unity Engine, like OnTriggerEnter, SendMessage, can also be used,
just call the Method PerformTransition from your FSMSystem instance with the correct Transition
when the event occurs.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
AND NON-INFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
/// <summary>
/// Place the labels for the Transitions in this enum.
/// Don't change the first label, NullTransition as FSMSystem class uses it.
/// </summary>
public enum Transition
{
NullTransition = 0, // Use this transition to represent a non-existing transition in your system
}
/// <summary>
/// Place the labels for the States in this enum.
/// Don't change the first label, NullTransition as FSMSystem class uses it.
/// </summary>
public enum StateID
{
NullStateID = 0, // Use this ID to represent a non-existing State in your system
}
/// <summary>
/// This class represents the States in the Finite State System.
/// Each state has a Dictionary with pairs (transition-state) showing
/// which state the FSM should be if a transition is fired while this state
/// is the current state.
/// Method Reason is used to determine which transition should be fired .
/// Method Act has the code to perform the actions the NPC is supposed do if it's on this state.
/// </summary>
public abstract class FSMState
{
protected Diction
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/**
A Finite State Machine System based on Chapter 3.1 of Game Programming Gems 1 by Eric Dybsand
Written by Roberto Cezar Bianchini, July 2010
How to use:
1. Place the labels for the transitions and the states of the Finite State System
in the corresponding enums.
2. Write new class(es) inheriting from FSMState and fill each one with pairs (transition-state).
These pairs represent the state S2 the FSMSystem should be if while being on state S1, a
transition T is fired and state S1 has a transition from it to S2. Remember this is a Deterministic FSM.
You can't have one transition leading to two different states.
Method Reason is used to determine which transition should be fired.
You can write the code to fire transitions in another place, and leave this method empty if you
feel it's more appropriate to your project.
Method Act has the code to perform the actions the NPC is supposed do if it's on this state.
You can write the code for the actions in another place, and leave this method empty if you
feel it's more appropriate to your project.
3. Create an instance of FSMSystem class and add the states to it.
4. Call Reason and Act (or whichever methods you have for firing transitions and making the NPCs
behave in your game) from your Update or FixedUpdate methods.
Asynchronous transitions from Unity Engine, like OnTriggerEnter, SendMessage, can also be used,
just call the Method PerformTransition from your FSMSystem instance with the correct Transition
when the event occurs.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
AND NON-INFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
/// <summary>
/// Place the labels for the Transitions in this enum.
/// Don't change the first label, NullTransition as FSMSystem class uses it.
/// </summary>
public enum Transition
{
NullTransition = 0, // Use this transition to represent a non-existing transition in your system
}
/// <summary>
/// Place the labels for the States in this enum.
/// Don't change the first label, NullTransition as FSMSystem class uses it.
/// </summary>
public enum StateID
{
NullStateID = 0, // Use this ID to represent a non-existing State in your system
}
/// <summary>
/// This class represents the States in the Finite State System.
/// Each state has a Dictionary with pairs (transition-state) showing
/// which state the FSM should be if a transition is fired while this state
/// is the current state.
/// Method Reason is used to determine which transition should be fired .
/// Method Act has the code to perform the actions the NPC is supposed do if it's on this state.
/// </summary>
public abstract class FSMState
{
protected Diction