Game Development Reference
In-Depth Information
Taming the inheritance monster
Now that we have seen the basic building blocks of a class, let us write a whole class
and even go over an example of how inheritance can help us manage complexity.
Imagine we are writing an MMO RPG ( Massively Multiplayer Online Role Playing
Game ). If you have played one, you would have definitely come across all kinds of
monsters that basically have the same behavior, that is, you engage them in a battle
and there is a fight to the death, unless you have taken on something more than you
can handle, in which case you would make a break for it!
Monsters may vary in strength based on their level, basically how much you can hurt
them or how much they can hurt you. A more interesting difference would be in the
way they behave. For example, do they chase after you when you try to run, do they
attack you as soon as you get near, or wait for you to attack first? And in the case
of multiple players slaying a monster, does it attack back only the first attacker or
switch to attack the player that attacked last? Or it could be smart enough to figure
out the weakest of the players first, and so on.
How can we model this in our game implementation? Let us go through a simple
example of implementing this in an object-oriented way. The first thing we would
do is take all the commonality of a monster and create what is called a base class. For
any specific kind of monster we would like to create, we subclass from the base and
code the specific behavior into it. Sounds easy, but you don't actually get how to go
about it, do you? Well, let's jump right into the example.
Let's say we want to implement two kinds of monsters, one called PassiveMonster
and another called ActiveMonster .
The following illustrates the specific behavior:
Monster
PassiveMonster
ActiveMonster
Waits for someone to attack first
Does not chase
Attacks only the first attacker
Starts attacking anyone thats close
Chase the attacker
Attacks the last attacker
 
Search WWH ::




Custom Search