Game State Implementation Using Protocol And Base Class

94 views Asked by At

From what I've read so far, it seems that Objective C does NOT have abstract classes. I'm trying to implement a game state manager that is similar to what Apple just announced in GameKit (GKState and GKStateMachine).

My solution so far involves creating a base state class called BaseGameState and it adheres to a protocol I've created called GameState. For each state that I need for gameplay, I'm going to subclass the BaseGameState class. My BaseGameState class is just there so that I can subclass it and will not really do anything, but I want my state machine to work with one type. The class that controls which state I'm in is called GameStateMachine and it will contain an array of objects that are subclassed from BaseGameState.

To me, this seems like a horrible solution and I'm wondering if there's a standard method for doing this outside of using GameKit classes (which I cannot currently use because I need this app to target Yosemite).

1

There are 1 answers

1
Sven On BEST ANSWER

You don't need a base class, especially if it doesn't provide any implementation. This also is more flexible as it gives the user the freedom to use a different base class or extend an existing class with an implementation of your protocol.

Just have your GameStateMachine store an array of any object conforming to your GameState protocol - that is id <GameState>.