我在试着想出一种我可以使用的模式。
我希望能够有一个中间人模块,它接受游戏的一种状态。给定该状态,调用驻留在anotehr模块中的某个方法。
我可以使用什么模式呢?
例如,我希望能够接受"computer always wins“的状态,并且基于该状态类型,我将调用someOtherModule.makeComputerMove()。在未来,也许我们希望能够将游戏设置为一种计算机并不总是获胜的模式。好吧,然后我们可以发送一个"normal game"状态或者类似的状态,它只是从不同的用例模块调用computerAlwaysWins.makeComputerMove(),比如normalGame.makeComputerMove()
明白了吗?
我想不出任何模式来提供这样的thing...probably,因为我不太了解其中的许多模式。
发布于 2015-12-10 16:31:56
你应该结合使用状态模式和观察者。
public class GameStateContext {
PlayerState Player {get;set; }
// other properties that need to be shared
}
public interface IGameController {
void GoToState(State state)
}
public interface IGameState {
void Start();
void Update();
}
public abstract class GameStateBase : IGameState {
protected GameStateContext _context;
protected IGameController _parent;
public GameStateBase(GameStateContext context, IGameController parent) {
this._context = context;
this._parent = parent;
}
public virtual void Start() {
}
public virtual void Update() {
}
}
public class BonusLevelState : GameStateBase {
public public MainMenuState (GameStateContext context, IGameController parent) : base (context, parent) {
}
public override void Update() {
if(_context.Player.Health == 0) {
_parent.GoToState(GameStates.GameOver);
}
}
}
public GameController : IGameController {
public enum GameStates {
BonusLevel,
InitialState,
....
}
private IGameState currentState;
public GameController() {
// create diferent states
...
currentState = GetState(GameStates.InitialState);
}
public void Update {
currentState.Update();
}
public GoToState(State state) {
currentState = GetState(state);
}
}我希望你能有个主意,祝你好运!
https://stackoverflow.com/questions/34195894
复制相似问题