【发布时间】:2014-08-13 15:05:58
【问题描述】:
我的问题非常具体,很难搜索,所以到目前为止我还没有成功。 我一直在思考我的这个特殊问题,但我似乎不够聪明。也许其他人是:
这应该是 Unity-Game 的状态机,其中 Game 类通过使用简单的 StateMachine 将其状态类型作为通用参数来管理关卡。 当我将游戏内脚本 SceneMaster 添加到 StateMachine 本身的结构中时,它会变得复杂。
Level(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());
}
}
【问题讨论】:
-
我并没有完全按照你说的是你的问题,但如果我理解你试图去的方向,这似乎是一个我可以用工厂模式或抽象模式解决的问题工厂模式? stackoverflow.com/q/5739611/1718702
-
嗯,谢谢大家的帮助。我真的不知道工厂对我有什么好处,因为我的问题不是创建实例(无论如何都包含唯一代码),但也许这只是我缺乏创造力。我想问题无法以我尝试的方式解决。我尝试引入的任何接口来替代 Level
以使其成为非泛型(我需要 LevelMachine)最终在一个或另一个位置不兼容。所以现在我选择在 Level中添加一个 S 类型的 var,通过强制转换为 S,在接收 ILevelMaster 的方法中分配它。无论如何,非常感谢!
标签: c# generics unity3d game-engine state-machine