Game Development Reference
In-Depth Information
How to do it...
To be able to control a ParticleEmitter object from our script system, we need a new
class to handle the ParticleEmitter object:
1. Start by creating a new class called PlayEffect , which implements
ScriptObject .
2. The PlayEffect class needs a Boolean called emitAllParticles , a
ParticleEmitter field called effect , and a Boolean to control whether it's
enabled or not (default it to true ).
3. The trigger method should call onTrigger if the object is enabled.
4. The onTrigger method should enable effect and if emitAllParticles
is true , it should call emitter.emitAllParticles() .
Apart from the setter methods, this is all that's needed for the PlayEffect class. Now,
we can look at the Timer class by performing the following steps:
1. We create a new class called Timer , which implements ScriptObject .
2. It will use a simple callback interface to keep track of events:
public interface TimerEvent{
public Object[] call();
}
3. It needs two Boolean fields. One called enabled and another called running . It
also needs to keep track of time with three floats called time , lastTime , and
maxTime .
4. Finally, we will store the events in HashMap<Float, TimerEvent> .
5. We need a method to add events to the timer. Call it addTimerEvent and add
inputs for time in seconds to execute the event, as well as a TimerEvent object
with the code to execute it. After TimerEvent is placed in the timerEvents
map, we check whether the supplied time value is higher than the current
maxTime and set maxTime to time if true, as shown in the following code:
public void addTimerEvent(float time, TimerEvent
callback){
timerEvents.put(time, callback);
if(time >maxTime ){
Search WWH ::




Custom Search