Game Development Reference
In-Depth Information
Object orientation begins with the concept of a class . But what exactly is that? Of course, we've coded
classes before in previous chapters using C# script files—and they all worked! But it's important to
understand more deeply the mechanics or underpinning philosophy of how it works. It's not enough
to follow along with examples in a book and to copy and paste code. In short, a class is an abstract or
template entity—something that exists in theory or in principle . We when look at the world around us,
the analytical mind restlessly breaks things down into neat categories or groups in search of a deeper
understanding. We don't just observe a random flux of atoms; instead, we see tables, and chairs, and
trees, and people, and discrete objects that have clear beginnings and endings. We recognize all these
things when we see them because of a general or abstract picture we hold in our minds.
For example, we recognize a table when we see one, because we have an abstract understanding
of a table . That is, we know enough about everything tables have in common to recognize individual
instances of a table when we see them in the real world. There is, in our minds, a general template
or pattern of an ideal table. And this helps us to recognize particular real-world tables when we see
them. The ideal table is a class . And the real-world specific tables are instances or instantiations of
that class. In Unity, classes are defined in script files . Instances are made in the scene by way of
components —that is, classes are instantiated in the scene as components on a game object. We
just drag and drop scripts into the scene to make instantiations. Now, perhaps none of this is news
to you—you may already know about objects and instances, but everything we've said so far poses
a logistical problem for us when creating weapons. Let's see what that is.
Object Orientation: Inheritance
CMOD supports two weapon types, as we've seen. This immediately suggests that we need to
create two separate C# classes: one for the fists/punch ( Weapon_Punch.cs ) and another for the
gun ( Weapon_Gun.cs ). This is correct, but a problem introduces itself regarding code and feature
duplication. The problem is that although the fist and gun weapons are separate and distinct objects,
there are still many similarities between the weapon types. Specifically, both are weapons , both deal
a specified amount of damage to enemies, both have a recovery rate (the amount of time that should
elapse before the weapon can be reused after being fired), and both have a range (the distance from
the enemy at which the weapon is effective). These are numerous and significant features held in
common , and not just across the two weapons we're creating for CMOD in this book, but across
almost all weapons imaginable. We could, of course, disregard these similarities entirely and simply
jump into implementing our weapons straightaway, coding these properties for each weapon. This
approach, however, is inefficient because it means we're adding the same kinds of properties to two
separate classes. We're unnecessarily duplicating our workload and increasing the size of our code.
Instead, we can solve this problem using class inheritance to develop a base class for all weapons.
Whenever we identify two separate classes— X and Y —that share lots of behavior and functionality
in common, we've usually found good candidates for inheritance. Class inheritance allows you to
create a third class, Z , known as a base class , which defines all behaviors common to X and Y.
The classes X and Y ( subclasses ) can then inherit that functionality from the base class Z, to save
you having to code it twice, once for X and again for Y. Base class Z is therefore a distillation of all
commonalities between X and Y. It's not a class intended to be instantiated on its own . Its purpose
is to be inherited by other classes that wish to reuse and recycle its behavior as though it were their
own. This kind of class is more formally known as an abstract base class . So, let's start coding
the Player weapons here, with the base class (see Listing 6-1, which demonstrates a base class
Weapon.cs ; comments follow).
 
Search WWH ::




Custom Search