Game Development Reference
In-Depth Information
•
condition_fartherThanThresh
: This is a condition script that checks if
the distance from object A to object B is greater than a parameter. If it is, it
returns true.
•
response_changeState
: This is a response script that changes the state
of an NPC to the state parameter.
Implementing the npcCondition script
The
npcCondition
class is the base class for all questions that an NPC might ask
about the player or the state of the world.
1. Create a new class in the editor using the new class wizard. Name it
npcCondition
.
2. Open the script in MonoDevelop, and change the class declaration. Add the
word
abstract
in front of
public
so that it reads the following. We use the
keyword
abstract
because this class will be used to just declare an inter-
face. We will use this class as a common base for all of the condition classes
our game will have for NPCs:
abstract public class npcCondition :
MonoBehaviour{
3. Using the
abstract
keyword, declare an interface method named
eval()
with the following syntax:
abstract public bool eval();
4. By designing
npcCondition
as an interface class, we are telling Unity that
there will be a base class named
npcCondition
. Other conditions will be
added to the game, and they will have different behaviors for sure, but one
common element is that they all have a method named
eval()
, which they
all implement in a unique way. In this way, each condition class can special-
ize its behavior through the
eval()
method, and no matter what is being
evaluated, by returning
true
, we can pass a message to the
DecisionM-
gr
class that a condition has become true and that we need to invoke a re-
sponse script.