Game Development Reference
In-Depth Information
for (var key in data) {
value = this[key];
data[key] = (value * 1);
}
}
window.game.Hero = Hero;
}());
Other than the HP property, all class properties are named identical to data saved to local storage. The loadData
method will access the persistent PlayerData object and copy the values over to the instance of the hero. These
instance properties will be used during the battle.
The Hero class only has three methods used during a level. The takeDamage method is called when an enemy
attacks the hero. The amount of damage is passed into the method, which will be determined by the power value
of the attacking enemy. The hero's current defense value will counter against this attack and soften the blow. If this
result is below 0, it is forced to be 0 so that a negative value is not added, which would result in added hit points. These
situations can happen with high defense stats against weak enemies and is referred to as a miss. Finally, another check
is needed to prevent negative hit points after the attack.
The updateInventory method will be called when the player uses a magic item during battle. It will take two
parameters, the item and the quantity used. Finally, the saveStats method will be called when a level is won and will
put all current stats in the hero instance back into local storage.
You'll notice that in many situations, values are multiplied by 1. this forces the value to be a number and
assures proper calculations throughout the game. Values from local storage can often be evaluated as strings, so this
procedure will prevent unwanted results.
Note
Now that all game data is set, and the Enemy and Hero classes are ready to use, let's take a step back and prepare
the game menu scenes.
Building the Game Menus
There are only four scenes in The Villager RPG. Two are menu screens for starting the game and selecting the level
you wish to play. These two menus will be covered in this section.
Creating the Main Menu Scene
The main title screen is simple. It consists of a background, a title sprite, and two buttons. One button will allow the
user to continue with their current progress, and the other is for creating a brand new game. Listing 14-19 shows the
entire GameMenu class.
 
 
Search WWH ::




Custom Search