状态机
public interface IState
{
void Enter();
void Exit();
void LogicUpdate();
void PhysicsUpdate();
}
using System.Collections.Generic;
using UnityEngine;
/**
* 状态机类
* 支持所有的状态类,并对他们进行管理和切换
* 负责进行当前状态的更新
*/
public class StateMachine : MonoBehaviour
{
IState currentState;
public Dictionary<System.Type, PlayerState> stateTable;
void Update()
{
currentState.LogicUpdate();
}
private void FixedUpdate()
{
currentState.PhysicsUpdate();
}
protected void SwitchOn(IState newState)
{
currentState = newState;
currentState.Enter();
}
public void SwitchState(IState newState)
{
currentState.Exit();
SwitchOn(newState);
}
public void SwitchState(System.Type state)
{
currentState.Exit();
SwitchOn(stateTable[state]);
}
}
Comments | NOTHING