Using a state design template for cocos2d

The game I create is a puzzle game. You start with a menu in which the user decides to, for example, go for a single player or multiplayer. If they choose Single Player, they have the opportunity to play in 3 different modes.

  • Practice

No timer

  • Stress

The player must complete the puzzle within 10 seconds, until they succeed in making the puzzle within 10 seconds and they will "die"

  • Battle time

The player has 2 minutes to complete as many puzzles as possible.

You see that the real game does not change, but the only thing that changes is how time is controlled. I read your articles, and I found that the state template would be quite enjoyable, and now the only problem I am facing is how I could implement this template.

Should I create sub-states both in the abstraction of the menu state and in the abstraction of the game, or do I just need to create a single abstraction of the state of the game, and then ignore calls like "handleMenuSelection"?

I can’t find any good online learning apps covering this in cocos2d. I can find many small demos, but it’s hard to convert them into a large application when I never touched a design pattern other than OOP design.

Btw. your links were very helpful, opening my mind to new ideas :)

+4
source share
2 answers

It is good to have one singleton class (common object) that supports all statistics.

Example: Say MyGame is used to store all the characteristics of a game. // in MyGame.h

typedef enum { kGameMode_Practice = 1001, kGameMode_Stress, kGameMode_TimeBattle, }GameMode; @interface MyGame: NSObject { GameMode mGameMode; int mHighScore; } @property(nonatomic,assign) GameMode gameMode; 

@property (nonatomic, assign) int highScore;

 +(MyGame*)sharedGameObject; 

// In MyGame.mm

 static MyGame *gGame = nil; @implementation MyGame @synthesize gameMode=mGameMode; @synthesize highScore=mHighScore; +(MyGame*)sharedGameObject { if(!gGame) { gGame = [[MyGame alloc] init]; } return gGame; } -(void)saveData //Call this from applicationWillResignActive { NSUserDefaults *userDafs = [NSUserDefaults standardUserDefaults]; [userDafs setInteger:self.highScore forKey:@"highScore"]; [userDafs setInteger:self.gameMode forKey:@"gameMode"]; [[NSUserDefaults standardUserDefaults] synchronize]; } -(void)loadData //call this from UIApplication didFinishLaunchingWithOptions { NSUserDefaults *userDafs = [NSUserDefaults standardUserDefaults]; self.highScore = [userDafs integerForKey:@"highScore"] self.gameMode = (GameMode)[userDafs integerForKey:@"gameMode"] } 

// You can set the game mode when you select the menu button

  [MyGame sharedGameObject].gameMode = kGameMode_Practice 

// Check anywhere in the game

  if([MyGame sharedGameObject].gameMode == kGameMode_Practice) 

Also save this value when the application terminates and load it when the application starts.

  [[MyGame sharedGameObject] saveData]; 

Depending on the game mode, you can change the game. Use a single common class for the game logic and check the game mode and make settings .. when the design 3 has a separate class for 3 types, then a change in one needs to be updated in all files in the future. It’s good to have as much code as possible.

+4
source

It is difficult to offer a good design, given that there is very little information. Trying to guess a bit, I suggest you read about the "Strategy Development Pattern" and the State Design Pattern ( another link ), as it may be suitable for what you are doing, and this will be a clean way to control several game modes.

+2
source

Source: https://habr.com/ru/post/1488543/


All Articles