Game Development Reference
In-Depth Information
It's not much to look at, but it is basic-level Ruby programming at its finest. You'll be happy to know that you
can copy-paste this for use in your game. However, programming is, at its heart, a journey of problem-solving and
discovery. Let me walk you through these lines of code.
We are creating a
module to contain methods related to damage formulas for use in our game.
This particular module is named Winter (you could rename it anything else, and it would be fine).
module_function is needed for the module to work correctly. Alternately, you could add self
to each of the method definitions ( def self.phys , for example) for the same effect.
Below we have
def phys along with parentheses filled with several variables. Methods are
expressed in the form method (parameters). Notice that the method is on a higher indentation
level compared to module Winter. Indents are necessary in Ruby coding, to determine what
goes where. Everything below the module is part of it, so it has to be indented. Likewise,
everything under def phys until the second end is contained within said method; hence, it is
a higher indentation level than def phys.
1.
As noted a few lines later in a comment tag, p is the power multiplier for the user's
ATK stat.
2. a and b are the standard uses in a damage formula (user and target).
3. v is used for any variables you decide to use within a formula. You can use the value of
a variable by expressing it as $game_variables[n] within a damage formula.
Next, we have an
if statement that's true if a.atk is greater than b.def . If the statement is true,
then the first formula is the one to be used, If the statement is false, then the second formula is
used instead.
1.
return is used within Ruby to exit a method early (in a case such as the one
preceding, where we have the formula we want already). While it is not strictly
necessary to use at any point of the code I placed in the previous page, it does no
harm either, so I left it as is.
2.
The first end in the method terminates the if statement. The second end terminates
the method itself.
def sitva is the other method present here. Sitva is what I called magic in my own game.
You can rename sitva to magic , and it will make no difference.
1.
As noted in the comment tags following this method, bd represents the base damage
of the spell in question, and p is the power multiplier for the caster's MAT stat.
2.
You'll notice that there's no if statement for this method, as there's only the one
damage formula to plug. I took my inspiration from many Japanese RPGs, in which
spell damage tends to be a little more constant than physical damage. (So, barring the
target having a massive amount of MDF compared to the user's MAT, damage spells
should always do some good damage.)
Because you have only the method itself, you only need the one end statement.
3.
end past the def sitva comment tags. That end closes out the
Last, you'll notice a solitary
module itself.
 
Search WWH ::




Custom Search