Game Development Reference
In-Depth Information
How to do it...
Now, we can implement the ScriptObject in a class called Trigger . This will have
six steps:
1. Add the following fields to the Trigger class:
private boolean enabled;
private float delay;
private boolean triggered;
private float timer;
private HashMap<String, ScriptObject> targets;
2. The enabled and delay fields should have getters and setters and targets
should have a addTarget and removeTarget method publically available.
3. In the trigger method, we add the following functionality:
If enabled is false it shouldn't do anything.
Otherwise timer should be set to 0 and triggered to
true.
4. If the script is enabled in the update method, we should perform the following
steps:
1. If triggered is true and delay is more than 0, the timer should be in-
creased by tpf.
2. Then if the timer is more than or equal to delay, it should call onTrig-
ger() .
5. If delay is 0 and triggered is true , the timer should also call onTrigger() .
6. In the onTrigger method, we should parse through all the values of target-
sMap and call the trigger on them. Then triggered should be set to false .
Now, perform the following set of steps to control the Trigger class.
1. We define a new class called ScriptAppState , which extends AbstractAp-
pState .
2. It should have a List<ScriptObject> called scriptObjects , together
with methods to add and remove ScriptObjects from List .
Search WWH ::




Custom Search