Game Development Reference
In-Depth Information
In this way, we will need only to call
eval()
on
npcCondition
in the code, without
the need to know specifically the type of condition being evaluated. This simpli-
fies our code complexity immensely, allowing our condition code to be polymorphic,
which is a good thing!
Implementing the npcResponse script
The
npcResponse
class is the base class for all responses that an NPC might in-
voke when a condition is found to be
true
.
1. Create a new class in the editor using the new class wizard. Name it
npcRe-
sponse
.
2. As seen previously, open the script in MonoDevelop, and change the class
declaration; add the world
abstract
in front of the public so that it reads the
following:
abstract public class npcResponse :
MonoBehaviour{
3. Using the
abstract
keyword, declare an interface method named
dis-
patch()
with the following syntax:
abstract public bool dispatch();
4. As seen previously, by designing
npcResponse
as an interface class, we
are telling Unity that there will be a base class named
npcResponse
. Other
responses will be added to the game, and they will have different behavi-
ors for sure, but one common element is that they all have a method named
dispatch()
, which they all implement in a unique way.
In this way, we will need only to call
dispatch()
on
npcResponse
in the code,
without the need to know specifically the implementation of response that is being
dispatched. This simplifies our code complexity immensely and allows our response
code to be polymorphic as with the conditions.