类在不同的地方暴露不同的抽象层次

时间:2014-08-13 15:05:58

标签: c# generics unity3d game-engine state-machine

我的问题非常具体,难以搜索,所以到目前为止我还没有成功。 我一直在考虑我的这个特殊问题,但我似乎不够聪明。也许其他人是:

这应该是Unity-Game的状态机,其中Game类通过使用简单的StateMachine将其状态类型作为通用参数来管理级别。 当我将In-Game脚本SceneMaster添加到StateMachine本身的结构时,它会变得复杂。

级别(LevelMachine状态)应该将SceneMaster作为Actor,但LevelMachine不应该关心LevelStates(Level级别)的特定类型。

足够的谈话,也许有人甚至有一个更合乎逻辑,更简单的解决方案。 非常感谢您提供任何帮助

//LevelMachine takes States of type Level
public class LevelMachine : StateMachine<Level> {} //<---here is the problem, I don't want Level to be generic here

//Level is a State of LevelMachine, Level should be acting on a LevelMaster
public abstract class Level<S> : StateObject<LevelMaster<S>> where S : LevelMaster<S> {}



public interface ILevelMaster : IStateObject {}
//LevelMaster itself is a StateMachine as well, it takes states of type LevelState
public abstract class LevelMaster<S> : StateMachine<LevelState<S>>, ILevelMaster where S : ILevelMaster {}

//StateObject takes an Actor of type LevelMaster
public abstract class LevelState<A> : StateObject<A> where A : LevelMaster<A> {}



public class Level01_State : LevelState<Level01_Master> {
    public Level01_State (Level01_Master actor) : base (actor) {}
}

public class Level01_Master : SceneMaster<Level01_Master> {}



public class LEVEL01 : Level<Level01_Master> {
    public void Act () {
        Debug.Log (Actor); //Actor is supposed to be of type Level01_Master
    }
}

public class Game {
    //levelMachine should not care about what kinds of LevelStates the Levels in it take
    public static LevelMachine levelMachine = new LevelMachine();

    public static initialize() {
        levelMachine.AddState (new LEVEL01());
    }
}

1 个答案:

答案 0 :(得分:0)

适配器模式和工厂模式的组合可以帮助您。对于适配器模式,请访问http://dofactory.com/net/adapter-design-pattern链接,并访问工厂模式,访问http://dofactory.com/net/factory-method-design-pattern链接。