Skip to main content
 首页 » 编程设计

c#之有限状态机和封闭

2024年02月23日15落叶无声

<分区>

最近在我的工作中,我“强制”学习正确的编程。 我正在阅读很多燕鸥、DDD、TDD 等。 推荐《Clean Code》和《Refactoring to Patterns》这两本书,因为它们非常好。

最后,这让我拿起了一些我按时实现的游戏原型(prototype),并尝试在正确应用 OOP 的情况下重做,因为当我这样做时,只是按照我的直觉(没有研究任何东西)。

在尝试为这些游戏实现有限状态机时,我遇到了几个问题。

public class StateMachine 
{ 
    private IState _globalState; 
    private IState _currentState; 
    private readonly List<IState> _previousStates; 
 
   public StateMachine(IState globalState, IState currentState) 
    { 
       _globalState = globalState; 
       _currentState = currentState; 
       _previousStates = new List<IState>(); 
    } 
 
    public void Execute() 
    { 
        if (_globalState != null) 
            _globalState.Update(); 
        if (_currentState != null) 
            _currentState.Update(); 
    } 
 
    public void ChangeState(IState state) 
    { 
        _previousStates.Add(_currentState); 
        _currentState.Exit(); 
        _currentState = state; 
        _currentState.Enter(); 
    } 
 
    public void BackToPreviousStates() 
    { 
        _currentState.Exit(); 
        _currentState = _previousStates[_previousStates.Count-1]; 
        _previousStates.Remove(_currentState); 
        _currentState.Enter(); 
    } 
 
    public void SetCurrentState(IState state) 
    { 
        _currentState = state; 
    } 
 
    public void SetGlobalState(IState state) 
    { 
        _globalState = state; 
    } 
} 

不像网上的一些例子,我没有踩播放器调用State的Update方法,也没有在Prora StateMachine中作为ViniGodoy的例子(http://sofiaia.wordpress.com/2008/07/11和 ... -states/) 因为我不明白为什么。

是一个状态的例子

public class RunAway : TargetState 
{ 
    public RunAway(IEntity entity, IEntity target) 
        : base(entity, target) 
    { 
    } 
 
    public override void Exit() 
    { 
      //  throw new NotImplementedException(); 
    } 
 
    public override void Enter() 
    { 
       // throw new NotImplementedException(); 
    } 
 
    public override void Update() 
    { 
        if (Target.X > Entity.X) 
        { 
            Entity.Move(Key.Down); 
        } 
 
 
        Entity.StateMachine.BackToPreviousStates(); 
 
    } 
} 

我对这条线的第一个怀疑只是 Entity.StateMachine.BackToPreviousStates (),因为这违反了 OOP 的模式,所以我封闭了我的实体(玩家或敌人)的状态机。

另一个问题是在某些情况下我会

public override void Update() 
    { 
        var enemy; 
            foreach (IEntity entity in Entity.World.Entities){ 
 
            if(entity .GetType() == typeOf(Enemy)) 
{ 
          enemy =(Enemy) entity; 
             break; 
 } 
  } 
 
   if (enemy  != null) 
        Entity.StateMachine.SetCurrentState(new RunAway(Entity, enemy)); 
 
    } 

我违反了几个隧道,State 在构造函数中接收一个 Entity 类并处理 World(实体也在其构造函数中接收的游戏的主要类)和 StateMachine,我仍在实例化一个新的 State而不是单例模式 (http://www.ai-junkie.com/books/toc_pgaibe.html) 中推荐的 usal。 States 我没有使用 Static,因为我不知道如何应用,因为各种状态不仅需要玩家或 StateMachine 的信息,还需要特定的上下文(例如 Runaway State 需要一个他会逃跑的敌人)。

我想知道如何改进这个实现并且不违反封装....

[编辑]

为清楚起见

public abstract class State : IState 
{ 
    private readonly IEntity _entity; 
 
    protected IEntity Entity 
    { 
        get { return _entity; } 
    } 
 
    protected State(IEntity entidade) 
    { 
        _entity = entidade; 
    } 
 
    public abstract void Exit(); 
    public abstract void Enter(); 
    public abstract void Update(); 
} 
 
 
public class Sleep: State 
{ 
    private int _time; 
 
    public Sleep(IEntity entidade) : base(entidade) 
    { 
    } 
 
    public override void Exit() 
    { 
       // _time = 0; 
    } 
 
    public override void Enter() 
    { 
 
        _time = 0; 
    } 
 
    public override void Update() 
    { 
        _time++; 
 
        if (_time == 20) 
            Entity.StateMachine.ChangeState(new Awake()); //<-- My Questions: Singletons ou instance? 
                                                         //      how to enclosing it? using Entity.ChangeState(new Awake()) ? 
 
 
    } 
}