Game Development Reference
In-Depth Information
Chapter 21
Storing and Recalling Game Data
Many games consist of different levels. Especially in casual games such as puzzles and maze
games, a game may have several hundreds of levels. Until now, your games have relied on
randomness to keep the gameplay interesting. Although randomness is a powerful tool to achieve
replayability, in a lot of cases game designers want more control over how a game progresses. This
control is generally achieved by designing levels . Each level is its own game world in which the
player has to achieve some sort of objective.
With the tools you've seen until now, you can imagine that for each level in a game you can write a
specific class in which you fill that specific level with game objects and add the behavior you want.
This approach has a few disadvantages. The most important disadvantage is that you're mixing the
game logic (gameplay, win condition, and so on), with the game content . This means every time you
want to add another level to the game, you have to write a new class, which leads to a lot of classes
that need to be retrieved when a game is loaded in the browser. Furthermore, if a game designer wants
to add a level to a game that you built, the designer needs in-depth knowledge of how your code works.
And any mistake the designer makes in writing the code will result in bugs or crashes in your game.
A much better approach is to store level information separately from the actual game code. While
the game is loading, this level information is retrieved. Ideally, the information needs to be stored
in a simple format that non-programmers can understand and work with. That way, levels can be
designed by someone without that person having to know how the game converts the data into
playable game levels. JavaScript is a very suitable language for representing structured information.
This is so easy primarily because of the object literals you can define in JavaScript. Have a look at
the following example:
var ticTacToeSaveGame = {
scorePlayerX : 2,
scorePlayerO : 1,
currentStatus : ["x x",
"oox",
"o "],
turn : "x"
}
277
 
Search WWH ::




Custom Search