Game Development Reference
In-Depth Information
Figure 6-19. CMOD so far, with completed weapon functionality
Note One issue to consider here, regarding both the punch and the gun weapons, concerns code
duplication across multiple classes. These classes, as given here, duplicate some functionality, especially
within the Fire method. In general, code duplication is not recommended practice, unless essential, because
it leads to lengthy source files and increases the possibility of error.
One way to solve this here would be by moving some of the common functionality into the base class,
allowing both classes to inherent the behavior. But doing this puts greater restrictions on what weapons can
be, and makes the base class “heavier” in terms of functionality. Further, we might want to add new weapons
later that do not share this behavior and which work entirely differently. You could solve this by introducing
more classes and levels of inheritance, or by overriding functions differently.
In short, finding the right balance between inheritance, levels of inheritance, and code duplication
(where unavoidable) is part of creating a suitable class hierarchy for your project.
Don't forget to create a weapon power-up that gives the Player the -weapon when collected (see book
companion files, if you're not sure how to do that). Or else, enable the Weapon_Gun.Collected public
member for the gun in the Object Inspector, to assign the gun to the Player automatically at level start-up. Don't
forget to set the gun ammo to 0 or above, and not -1; the gun cannot fire infinitely! See Chapter 4 for more
information on how to create collectable power-ups.
 
Search WWH ::




Custom Search