A simple and easy FSM state machine

State machine in our project many applications, such as standing and walking state machine like a running state because the states defined so limited types of finite state machines (finite state machine) can be called when certain conditions are met and the state between the state single transition transition state is a result of a variety of produce singlet result time and follow after the end of the state machine may be a fusion condition is satisfied mutually exclusive results only principle the final result of the implementation of only one state
then how to switch between one state do so there is a managed state fsmCore class switching state which method and process state update status since there must be switched on with a particular order, so the current state and the pre-state (on state) is essentially switched state data transmission so you want to switch incoming became a state when switching data class is also indispensable to the point status and its own state and incoming data is first to determine whether the ongoing state of the same state and the ongoing state is empty if not more condition determination does not continue directly returned Perform the same plane as the switching state is ineffective; if so, proceed with the handover will be assigned to the current state of the pre-state and then passed the state assigned to the current state of the state, of course, but also to achieve pre-judge the state is empty If you do not empty the last stop on a state methods weekend classes for incoming data switching state data class assignment method fsmCore would end up
switching state after the completion of the state to accomplish some of the things to do such as walking to keep running to maintain the desired method without switching state of the state is necessary to use continuous update status is in the process of the current state is not called persistent state is empty
before it comes to text data type (data) there distinguish categories of enumeration type and Object object ; Idel state will have many roles take into account the code reuse can switch / case to distinguish the type of data in the end is type Idel whose write state is not easily obtained, but specific data object class object data type can be inferred var var V = (typeof (Data)) Data to address such an action would not have to write the same state as Idel multi-role
A common abstract parent class StateBase state between a state in which each state has to have Enter Stay Exit and
attach the code
public class FsmCore
{
// Called before the Start The IS First Frame Update
StateBase currentBase; // current state
StateBase previousBase ; // pre state
StateEventDate date; // data type
///
/// change state
///
///
///
///
public void ChangeStatemethod (StateBase state, StateEventDate currentDate, StateEventDate previousDate)
{
// if then the state of the switch to meet the current state and consistent
IF (= null && currentBase currentBase state ==!)
{
return;
}
// put a stop state
IF (previousBase = null!)
{
previousBase.Exit (previousDate);
}
// assign the current state of the pre-state
= currentBase previousBase;
// status assigned to the current state of the incoming
currentBase = State;
// first enters
currentBase.Entry (currentDate);
// data assignment
DATE = currentDate;
}
///
/// Update Status
///
void UpdateState public ()
{
IF (currentBase = null!)
currentBase.Stay (DATE);
}
///
/// initialization
///
///
///
///
public void the Start (StateBase State, StateEventDate currentDate, previousDate StateEventDate)
{
ChangeStatemethod (State, currentDate, previousDate);
}
}
public abstract class StateBase
{
    ///
    /// first enters
    ///
    ///
    public abstract void the Entry (StateEventDate EventDate);
    ///
    /// performed for each frame of status
    ///
    ///
    public abstract void Stay (StateEventDate EventDate);
    ///
    last execution leave ///
    // /
    ///
    public abstract void Exit (StateEventDate EventDate);
}
---------------------
Disclaimer: This article is CSDN blogger "PJ_Mr_Zhang" original articles, follow CC 4.0 by-sa copyright agreement, reproduced, please attach the original source link and this statement.
Original link: https: //blog.csdn.net/PJ_Mr_Zhang/article/details/99471806

Guess you like

Origin www.cnblogs.com/PJ-Mr-Zhang/p/11348381.html