Game Development Reference
In-Depth Information
3. Add a public reference to
npcCondition
and
npcResponse
. Whenever
this condition evaluates to
true
, the interaction class will dispatch the re-
sponse:
public npcCondition condition;
public npcResponse response;
4. We also add an activated Boolean to allow us to selectively enable and dis-
able interactions based on the state of the game:
public bool active;
5. The method
eval()
is where the brunt of the work in the interaction class
is performed. It first checks if the interaction is active. If it is, and if there is
a condition, it will evaluate the condition and check if that condition is true or
not:
if (active == true)
{
if (condition != null)
{
if (condition.eval() == true)
{ ...
6. Recall that the
npcCondition
reference could be any specialization of
npcCondition
so that the implementation of the
eval()
function could ap-
pear in any child class as long as it has been assigned in the inspector.
7. If the condition returns
true
, we check if a response class has been associ-
ated in the inspector. If it has been dispatched, we dispatch the response!
if (response != null)
rval = response.dispatch();
Congratulations! We have implemented a container class that associates a generic
condition with a generic response. We use the abstract keyword in the base class so
that child classes that derive from
npcCondition
and
npcResponse
can all freely
be connected to
npcInteraction
. Now let's look at how to connect our interac-
tions together.