NL2 SCRIPT : Ouverture de porte lateral (trigger automatique)

yoyo212

Membre
27 Mai 2010
1 628
1
51
Suisse
www.dawaweb.ch
Fonctionne avec le Test Trigger Track

Code:
import com.nolimitscoaster.*;
import nlvm.math3d.*;

public class DoorScriptModifie extends Script implements TrackTriggerListener
{
  private SceneObject sco;
  private SceneObjectElement leftDoor;
  private SceneObjectElement rightDoor;
  
  private static final int STATE_IDLE = 0;
  private static final int STATE_OPENING = 1;
  private static final int STATE_CLOSING = 2;
  
  // Ouverture limitée à 2m de chaque côté
  private static final float TRANSLATION = 2.0f;
  //Temps d'ouverture de 1,5s
  private static final float DUREE_OUVERTURE = 2.0f;
  //Temps d'accélération et de ralentissement
  private static final float DUREE_ACCELERATION = 0.5f;
  //Variable servant lors de l'ouverture ou la fermeture des portes
  private static float startTime = 0.0f;
  
  private int state; 
  private float trans;
  
  private TrackTrigger beforeDoorTrigger;
  private TrackTrigger behindDoorTrigger;
  
  private StaticSound doorSound;
  
  private static final String scriptName = "DoorScript";
  
  // must be constant string expression and marked as final so that the compiler will no complain with a warning when used for
  // StaticSound.loadFromFile
  private final static String doorSoundFile = "doorsound.ogg";
  
  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;
    }
    //Récupération du sous objet porte gauche
    leftDoor = sco.getElementForName("left");
    if (leftDoor == null) 
    {
      System.err.println(scriptName + ": element not found: left");
      return false;
    }    
    //Récupération du sous objet porte droite
    rightDoor = sco.getElementForName("right");
    if (rightDoor == null) 
    {
      System.err.println(scriptName + ": element not found: right");
      return false;
    }  
	//Initialisation du son d'ouverture de la porte
    doorSound = StaticSound.loadFromFile(doorSoundFile, 0);
    if (doorSound == null)
    {
      System.err.println(scriptName + ": could not load sound '" + doorSoundFile + "'");
      return false;
    }

	//Vecteur représentant la position de l'objet dans l'espace
    Vector3f pos = new Vector3f();
    sco.getTranslation(pos);

	//Déplacement du son au niveau de l'objet
    doorSound.setPosition(pos);

    // Setting to 'Local' will enable environmental (reverb) effects if this sound's position should be e.g. in a tunnel
    doorSound.setEnvironmentMode(StaticSound.E_ENVMODE_LOCAL);

    // These parameters affect the distance attenuation
    doorSound.setDistanceParameters(2.0f, 0.8f);

    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);
    }
    
    state = STATE_IDLE;
    trans = 0;
    
    return true;
  }
  
  public void onNextFrame(float fTickTime)
  {
    switch (state)
    {
    case STATE_IDLE:
      // do nothing
      break;
    case STATE_OPENING: //Etat gérant l'ouverture de la porte
      {
	    //Calcul de la position de la porte
		startTime += fTickTime;
        trans = (float)Tools.switchRampC1(DUREE_OUVERTURE, TRANSLATION, DUREE_ACCELERATION, startTime);
		//Si on atteint l'ouverture maximale autorisée, on bloque la translation
        if (trans >= TRANSLATION)
        {
          trans = TRANSLATION;
          state = STATE_IDLE;
		  startTime = 0;
        }
		//Construction de deux vecteurs
		Vector3f leftTrans = new Vector3f(-trans,0,0);//La translation se fait sur l'axe X
		Vector3f rightTrans = new Vector3f(trans,0,0);//La translation se fait sur l'axe X
		//On applique la translation aux sous objets porte gauche et porte droite
		leftDoor.setTranslation(leftTrans);
		rightDoor.setTranslation(rightTrans);
      }
      break;    
    case STATE_CLOSING: //Etat gérant la fermeture de la porte
      {
	    //Calcul de la position de la porte
		startTime += fTickTime;
        trans = TRANSLATION - (float)Tools.switchRampC1(DUREE_OUVERTURE, TRANSLATION, DUREE_ACCELERATION, startTime);
		//Si on atteint l'ouverture minimale, on bloque la translation
        if (trans <= 0)
        {
          trans = 0;
          state = STATE_IDLE;
		  startTime = 0;
        }
		//Construction de deux vecteurs
		Vector3f leftTrans = new Vector3f(-trans,0,0);//La translation se fait sur l'axe X
		Vector3f rightTrans = new Vector3f(trans,0,0);//La translation se fait sur l'axe X
		//On applique la translation aux sous objets porte gauche et porte droite
		leftDoor.setTranslation(leftTrans);
		rightDoor.setTranslation(rightTrans);
      }
      break;
    }
  }
  
  /**
   * 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)
    {
      if (train.getSpeed() > 0)
      {
        state = STATE_OPENING;
        doorSound.play();

        //printTrainPositionAndRotation(train);
      }
    }
    else if (trigger == behindDoorTrigger)
    {
      if (train.getSpeed() < 0)
      {
        state = STATE_OPENING;
        doorSound.play();
      }
    }
  }
  
  /**
   * 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 onTrainLeaving(TrackTrigger trigger, Train train)
  {
    if (trigger == beforeDoorTrigger)
    {
      if (train.getSpeed() < 0)
      {
        state = STATE_CLOSING;
        doorSound.play();
      }
    }
    else if (trigger == behindDoorTrigger)
    {
      if (train.getSpeed() > 0)
      {
        state = STATE_CLOSING;
        doorSound.play();
      }
    }
  }

  // This method demonstrates how to get the position and rotation of the first bogey
  private void printTrainPositionAndRotation(Train train)
  {
    if (train.getBogieCount() > 0)
    {
      Vector3f pos = new Vector3f();
      Vector3f front = new Vector3f();
      
      train.getBogieOrientationAndPosition(0, front, null, null, pos);

      System.out.println(scriptName + ": First bogey is at " + pos);

      double azimuthRad = Math.atan2(front.x, -front.z);
      double elevationRad = Math.atan2(front.y, Math.sqrt(front.x* front.x + front.z* front.z));

      System.out.println(scriptName + ": Yaw is " + Math.toDegrees(azimuthRad) + " degrees"); 
      System.out.println(scriptName + ": Pitch is " + Math.toDegrees(elevationRad) + " degrees"); 
    }
  }
}