0

My problem is very specific and difficult to search for, so I have not been successful so far. I have been thinking about this special problem of mine for a while now but I don't seem to be intelligent enough. Maybe someone else is:

This is supposed to be a statemachine for a Unity-Game with a Game class managing the levels by using a simple StateMachine taking the type of its states as a generic parameter. It gets complicated when I am adding an In-Game script SceneMaster to the structure which is a StateMachine itself.

Level (State of LevelMachine) should have a SceneMaster as an Actor but LevelMachine should not care about what specific type the LevelStates (states of Level) are.

Enough talk, maybe someone even has a more logical and simpler solution. Thanks a lot in advance for any help

//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());
    }
}
Ralt
  • 2,084
  • 1
  • 26
  • 38
RodK
  • 1
  • 1
  • I'm not quite following what you say is your problem, but if I understand the direction you're trying to go, this seems like a problem I might solve with a factory pattern, or an abstract factory pattern? http://stackoverflow.com/q/5739611/1718702 – HodlDwon Aug 13 '14 at 15:27
  • Well, thank you all for your help. I don't really see how a Factory could do me any good because my problem is not the creation of instances (which contain unique code anyway) but maybe that's just my lack of creativity. I suppose the problem cannot be solved in the manner I tried to. Any interfaces I tried to introduce to substitute Level to make it non-generic (which I need for LevelMachine) ended up incompatible in one or the other spot. So now I opted dirty adding a var of type S in Level which is assigned in a method receiving a ILevelMaster, by casting to S. Anyway, Thanks a bunch! – RodK Aug 13 '14 at 19:10

1 Answers1

0

Combination of Adapter Pattern and Factory Pattern may help you. For adapter pattern visit http://dofactory.com/net/adapter-design-pattern link and for factory pattern visit http://dofactory.com/net/factory-method-design-pattern link.

Dinesh Maind
  • 349
  • 1
  • 7
  • If you could give a quick outline using these patterns that would be awesome. - Now that I am thinking about it I could probably find an interface ILevel capable of substituting Level in LevelMachine. Perhaps it didn't work so far because I had the illusion I could access the correctly typed Level.Actor via levelMachine.Level.Actor, which is of course impossible if LevelMachine is storing the level as type ILevel. Therefore a cast to Level is inevitable. Sorry for bothering you, Thanks! – RodK Aug 13 '14 at 19:37