Online Book Reader

Home Category

iPhone Game Development - Chris Craft [34]

By Root 1552 0
tile should be. Finally, it defines a type: an empty, blank, or arrow tile.

Examining the Game class in detail

The Game class is responsible for defining these methods. The class interface is listed here:

@interface Game : NSObject {

Level *levels[24];

}

+ (Difficulty)getBestDifficulty;

+ (int)getBestLevelForDifficulty:(Difficulty)aDifficulty;

+ (int)getCurrentLevelIndex;

+ (void)setCurrentLevelIndex;

+ (Level*)getLevelAtLevelIndex:(int)aLevelIndex;

@end

Let's look at each of these items in more detail. First, we define a field array of Level:

Level *levels[24];

Since we know there will be exactly 24 levels, we can go ahead and create an array to reference each one. This makes the call to getLevelAtLevelIndex easy; all we need to do is return the level for the specified index, like so:

+ (Level*)getLevelAtLevelIndex:(int)aLevelIndex {

return [Game getInstance].levels[aLevelIndex];

}

Notice that we called [Game getInstance] to get an instance of the Game class. The array of levels is populated here when the Game class is constructed. Later in this section we will show you the method that populates the array.

The methods getCurrentLevelIndex and setCurrentLevelIndex enable us to keep track of the last level the player was on. This way, when he comes back to the game, the last level he has not completed can be automatically loaded. This index is a number between 0 and 24 to make it easier to load levels and reference the levels array:

+ (int)getCurrentLevelIndex {

return [[Helper getUserValueForKey:@”currentlevel”

withDefault:@”0”] intValue];

}

+ (void)setCurrentLevelIndex:(int)aLevelIndex {

int bestIndex = [[Helper getUserValueForKey:@”currentlevel”

withDefault:@”0”] intValue];

if (bestIndex < aLevelIndex)

[Helper setUserValue:[NSString stringWithFormat:@”%d”, aLevelIndex]

forKey:@”bestlevel”];

[Helper setUserValue:[NSString stringWithFormat:@”%d”, aLevelIndex]

forKey:@”currentlevel”];

}

We use our Helper class again to get and set values by their key names. Notice that we do not use a private field; we just go ahead and write and read directly to the user settings. This keeps us from having to decide when to save the value, and you can rest assured the value will be what you set it to even if the game is interrupted by a phone call. We also set the best level index when we write the current level index. This is good practice because it helps us keep data logic out of the Game View later.

Now we'll look at the methods getBestDifficulty and getBestLevelForDifficulty. These methods prevent users from having to perform data logic in the Menu View (covered later in this chapter):

+ (Difficulty)getBestDifficulty {

int bestIndex = [[Helper getUserValueForKey:@”currentlevel”

withDefault:@”0”] intValue];

return (Difficulty)floor(bestIndex / 3);

}

+ (int)getBestLevelForDifficulty:(Difficulty)aDifficulty {

int bestIndex = [[Helper getUserValueForKey:@”currentlevel”

withDefault:@”0”] intValue];

int offset = [self getBestDifficulty] * 8;

return bestIndex - offset;

}

These methods give us a way to find the best level the player has completed. The player is presented with levels in the menus as Bachelors levels 1-8, Masters levels 1-8, and so on. This means that Masters level 1 is actually level index 8, and Bachelors level 1 is actually level index 0. By providing these methods, we ensure that we always calculate levels the same way.

Examining supporting Game classes and more

There are a handful of other classes and a couple of enums that are used to help support the Game class. Now you are going to get a closer look at these supporting items.

As you have seen earlier, the Game class ultimately exposes tile data, and on each tile you encounter a tile type. For the enum TileType, there are three different types of tiles:

Empty is used if the tile is the blank puzzle. There should only be one empty tile per puzzle.

Blank is used if the tile is active but does not have an arrow. Blank tiles

Return Main Page Previous Page Next Page

®Online Book Reader