Hello! I'm new on this forum.
I wanted to ask you how can I create a script for a traffic light on my roller coasters.
Thank You!
Bye.
public class LightScript
{
private SceneObjectLight redLight;
private SceneObjectLight yellowLight;
private SceneObjectLight greenLight;
public void Init(Simulator sim)
{
SceneObject light = sim.getSceneObject("Light");
redLight = light.getLightForName("red");
yellowLight = light.getLightForName("yellow");
greenLight = light.getLightForName("green");
}
public void Red()
{
redLight.setEnabled(true);
}
public void Yellow()
{
redLight.setEnabled(false);
yellowLight.setEnabled(on);
}
public void Green()
{
yellowLight.setEnabled(false);
greenLight.setEnabled(on);
}
public void Off()
{
greenLight.setEnabled(false);
}
}
private static final double TimeRed = 2000;
private static final double TimeYellow = 2000;
private static final double TimeGreen = 2000;
private static final int STATE_GREEN = 0;
private static final int STATE_YELLOW= 1;
private static final int STATE_RED = 2;
private double TimeStartDelay = 0;
private byte state = STATE_GREEN;
// Put the traffic light code in here
public void onNextFrame(float tick)
{
switch (state)
{
case STATE_GREEN:
Green();
if (wait(TimeGreen))
{
state = STATE_YELLOW;
}
break;
case STATE_YELLOW:
Yellow();
if (wait(TimeYellow))
{
state = STATE_Red;
}
break;
case STATE_Red:
Red();
if (wait(TimeRed))
{
state = STATE_Green;
}
break;
}
}
private bool wait(double time)
{
if (TimeStartDelay == 0) {
TimeStartDelay = System.currentTimeMillis();
}
if (TimeStartDelay + time < System.currentTimeMillis()) {
TimeStartDelay = 0;
return true;
}
return false;
}
private TrackTrigger beforeDoorTrigger;
private TrackTrigger behindDoorTrigger;
private static final String scriptName = "DoorScript";
public bool onInit()
{
sco = sim.getSceneObjectForEntityId(getParentEntityId());
if (sco == null)
{
System.err.println(scriptName + ": the script is not part of a scene object");
return false;
}
Vector3f pos = new Vector3f();
sco.getTranslation(pos);
final float range = 10;
// create triggers...
TrackPos trackPos = sim.findNearestCoasterTrack(pos, range);
if (trackPos == null)
{
System.err.println(scriptName + ": no coaster found within a range of "+ range + " meters");
return false;
}
else
{
Coaster coaster = trackPos.getCoaster();
System.out.println(scriptName + ": Coaster found: " + coaster);
System.out.println("Coaster Style is: " + coaster.getCoasterStyleId());
// create a trigger 5 meters in front of the track pos
beforeDoorTrigger = TrackTrigger.createTrackTriggerAtOffset(trackPos, -5.0);
// create a trigger 5 meters after the track pos
behindDoorTrigger = TrackTrigger.createTrackTriggerAtOffset(trackPos, +5.0);
// tell the triggers that they should call our onTrainEntering/onTrainLeaving methods
// We have to implement the TrackTriggerListener interface
beforeDoorTrigger.addTrackTriggerListener(this);
behindDoorTrigger.addTrackTriggerListener(this);
}
return true;
}
/**
* This method is part of the TrackTriggerListener Interface implementation
*
* It will be called if the interface is registered for a trigger using the
* addTrackTriggerListener method.
*/
public void onTrainEntering(TrackTrigger trigger, Train train)
{
if (trigger == beforeDoorTrigger)
{
//train is seen before the door
}
else if (trigger == behindDoorTrigger)
{
//train is seen behind the door
}
}
Return to NoLimits Coaster 2 Scripting