Time for action – avoiding hardcoded values
To separate and load our gameplay-relevant data, we need to follow these steps:
Add a new file called
gameplay.json
in theResources
folder with the following content:{ "hitpoints": 100, "damage": 25, "battlefield": { "enemy": { "x": 100, "y": 100 }, "pirate": { "x": 300, "y": 100 } } }
Open the
Ship.h
file.Add a property called
maxHitpoints
, as shown in the following line of code:@property int maxHitpoints;
Inside the
Ship
initializer, replace the piece of code where we sethitpoints
with the following lines of code:self.maxHitpoints = [(NSNumber *) [Assets dictionaryFromJSON:@"gameplay.json"][@"hitpoints"] intValue]; self.hitpoints = self.maxHitpoints;
Inside the
hit
method, replace the hardcoded damage value to load from thegameplay.json
file, as shown in the following code:self.hitpoints = self.hitpoints - [(NSNumber *) [Assets dictionaryFromJSON:...