Game Development Reference
In-Depth Information
Damage Floors Revisited
I covered damage floors all the way back in Chapter 4. There, I discussed that you can change the damage that such
terrain causes, based on Regions. Terrain tags work even better, and you can differentiate between types of floor
damage in that way as well. However, what happens if you have (to give a hyperbolic example) more than 20 different
types of floor damage? First, have a flash from the past (Chapter 4, to be exact).
#--------------------------------------------------------------------------
# * Get Base Value for Floor Damage
#--------------------------------------------------------------------------
def basic_floor_damage
return 2 if $game_variables[3] == 1
return 5 if $game_variables[3] == 2
return 10 if $game_variables[3] == 3
return 25 if $game_variables[3] == 4
end
I gave a list of four values of $game_variables[3] (the variable used to hold the Region ID in the exercise). You
would have to write 16 more expressions in the form return x if $game_variables[3] = y to meet our lofty goal.
However, Ruby provides a neater way of handling such a situation. Enter the case method. Here's what the RMVXA
Ruby reference has to say on case : case expressions execute branching for a single expression via matching . While we're
on the subject of bringing back older exercises to expand upon, let's repackage this one into a module. We'll call the
module FloorDamage . It will contain a method called value that will return a certain damage number, based on the
value of our Region ID variable. Look at the following code to see case in action.
module FloorDamage
module_function
def value
case $game_variables[3]
when 1
2
when 2
5
when 3
10
when 4
25
else
0
end
end
end
We declare a case parameter (in this case, our variable) and then stipulate various conditions, as necessary. I
won't actually expand this to 20 values of our variable, but you can probably already see that this can save a lot of time
and space when used in the right situations.
 
Search WWH ::




Custom Search