Game Development Reference
In-Depth Information
options. Having a general solution is more flexible and allows us to experiment more
To implement this we will use an object-oriented design pattern called an “inter-
face.” Some languages, such as Java, have this built in as part of the language; in others
it can be approximated with a regular class. Before we look at the force generator code,
I will briefly review the concept of an interface, and its relative, polymorphism.
In programming, an interface is a specification of how one software component in-
teracts with others. In an object-oriented language it normally refers to a class: an
interface is a specification of the methods, constants, data types, and exceptions (i.e.,
errors) that a class will expose. The interface itself is not a class; it is a specification
that any number of classes can fulfill. When a class fulfills the specification, it is said
that it implements the interface (in fact, Java uses the explicit implements keyword to
denote a class that implements an interface).
What is powerful about interfaces is their use in polymorphism. Polymorphism is
the ability of a language to use some software component on the basis that it fulfills
some specification. For our purposes the components are classes and the specification
is an interface. If we write some code that needs to use another part of the system,
we can define an interface for the interaction and make the calling code use only
the elements in the interface. We can later change what the code is interacting with,
and as long as it implements the same interface, the calling code will never know the
This replaceability is key for our needs: we have an interface for a force generator,
and through polymorphism we don't need to know what kind of force the force gen-
erator represents, as long as it implements the interface we need to extract the relevant
information. This is a helpful way to avoid having different parts of the program ex-
tremely reliant on the way other parts are implemented: we create an interface, and
as long as a class implements it, the calling code need know nothing more about it.
In C++ there is no dedicated interface structure in the language. Instead we use a
base class, with a selection of pure virtual functions. This ensures that we can't create
an instance of the base class. Each class that derives from the base class then has to
implement all its methods before it can be instantiated.
The force generator interface needs to provide only a current force. This can then be
accumulated and applied to the object. The interface we will use looks like this:
Excerpt from include/cyclone/pfgen.h
* A force generator can be asked to add a force to one or more
* particles.
Search Nedrilad ::

Custom Search