Game Development Reference
In-Depth Information
SwitchState() function is called whenever the player tries to use the trigger. It returns true if
the state has been successfully switched, or false otherwise. One reason that leads to unsuc-
cessful switching attempt is that the rest time of the current state has not yet passed. If it is
possible to switch the state, the value of currentState is incremented by 1, or set back to zero
if the current state is the last one in states array. After setting the new state, the trigger iterates
over all TriggerMessage values stored in the list of messages of the new state, and sends each
message once to the specified receiver (lines 51 through 59). The last step before return-
ing true is to record the current time as lastSwitchTime , to be able to compute the rest time
of the current state. Illustration 39 shows the mechanism of manipulating multiple objects
through multiple states of a single trigger.
Illustration 39: Triggering mechanism: when the state changes, the new state sends all messages stored
in messagesToSend array
The idea will be more clear when we discuss the examples. The first example is a switch
that controls an electrical light. The switch as well as the light has two states: on and off.
When the player switches the trigger, two things happen: the light is changed from on to off
or vice-versa, and the switch button is moved upwards or downwards. This means that we
have two states for the switch trigger, and each state has two messages to send: one message
to the light, and another message to the switch object. Next step is to write two scrips that
are capable of receiving the messages and performing actions based on them. The first
script is LightControl shown in Listing 36. This script can receive two messages: SwitchOn
and SwitchOff .
Search WWH ::




Custom Search