CCG/Kind: Interior Diesel Cab Script

From TrainzOnline
< CCG
Jump to: navigation, search

Diesel Cab Script

DD40Cabin.gs

This is the DD40 interior script file.

This sets up the fan and wiper animations to be switch controlled and controls the visibility of switchlights.

include "defaultlocomotivecabin.gs"

class DD40CabinData isclass CabinData
{
   public bool animatingFan;
   public bool animatingWiper;
   public bool switchOn3;
   public bool switchOn4;
   public bool switchOn5;
   public bool switchOn6;
   public bool switchOn7;
   public bool switchOn8;
};

class DD40Cabin isclass DefaultLocomotiveCabin
{
   // Switches
   CabinControl cabin_fan_switch;
   CabinControl window_wipers;
   CabinControl switch3;
   CabinControl switch4;
   CabinControl switch5;
   CabinControl switch6;
   CabinControl switch7;
   CabinControl switch8;

   // Lights
   CabinControl     cabin_fan_light;
   CabinControl     window_wipers_light;
   CabinControl     light3;
   CabinControl     light4;
   CabinControl     light5;
   CabinControl     light6;
   CabinControl     light7;
   CabinControl     light8;

   thread void SlowFanDown(void);
   thread void SpeedFanUp(void);

   void UpdateFan(void);
   void UpdateWipers(void);
   thread void RunAnimation(void);

   float fanSpeed;
   bool isFanSpeedingUp;
   bool isFanSlowingDown;

   //! Attach this cabin to a game object (i.e. a locomotive).
   //
   // Param: obj Game object to attach this cabin to (usually a Locomotive).
   //

   void Attach(GameObject obj)
   {
       inherited(obj);

       // get cabin data
       CabinData cd = loco.GetCabinData();
       if(cd)
       {
           // reset the controls from saved values
           DD40CabinData ddcd = cast<DD40CabinData>cd;
           float value = 0.0;

           // ANIMATING FAN
           if (ddcd.animatingFan)
           {
               fanSpeed = 1.0;
               SetMeshAnimationSpeed("fan", 1.0);
               StartMeshAnimationLoop("fan");
               value = 1.0;
           }
           else
               value = 0.0;

           cabin_fan_switch.SetValue(value);
           cabin_fan_light.SetValue(value);

           // ANIMATING WIPER
           if (ddcd.animatingWiper)
           {
               value = 1.0;
               SetMeshAnimationSpeed("wipers", 1.0);
               StartMeshAnimationLoop("wipers");
           }
           else
               value = 0.0;

           window_wipers.SetValue(value);
           window_wipers_light.SetValue(value);

           // SWITCH 3
           if (ddcd.switchOn3)
               value = 1.0;
           else
               value = 0.0;

           switch3.SetValue(value);
           light3.SetValue(value);

           // SWITCH 4
           if (ddcd.switchOn4)
               value = 1.0;
           else
               value = 0.0;

           switch4.SetValue(value);
           light4.SetValue(value);

           // SWITCH 5
           if (ddcd.switchOn5)
               value = 1.0;
           else
               value = 0.0;

           switch5.SetValue(value);
           light5.SetValue(value);

           // SWITCH 6
           if (ddcd.switchOn6)
               value = 1.0;
           else
               value = 0.0;

           switch6.SetValue(value);
           light6.SetValue(value);

           // SWITCH 7
           if (ddcd.switchOn7)
               value = 1.0;
           else
               value = 0.0;

           switch7.SetValue(value);
           light7.SetValue(value);

            // SWITCH 8
           if (ddcd.switchOn8)
               value = 1.0;
           else
               value = 0.0;

           switch8.SetValue(value);
           light8.SetValue(value);
       }

       else
       {
           DD40CabinData ddd = new DD40CabinData();
           loco.SetCabinData(ddd);
       }
   }

   void UserPressKey(string s)
   {
       if(s == "cabin-fans")
       {
           DD40CabinData cd = cast<DD40CabinData>loco.GetCabinData();
           cd.animatingFan = !cd.animatingFan;

           float value;
           if (cd.animatingFan)
               value = 1.0;
           else
               value = 0.0;

           cabin_fan_switch.SetValue(value);
           cabin_fan_light.SetValue(value);

           UpdateFan();
       }

       if(s == "wipers")
       {
           DD40CabinData cd = cast<DD40CabinData>loco.GetCabinData();
           cd.animatingWiper = !cd.animatingWiper;

           float value;
           if (cd.animatingWiper)
               value = 1.0;
           else
               value = 0.0;

           window_wipers.SetValue(value);
           window_wipers_light.SetValue(value);

           UpdateWipers();
       }
   }

   public void Init(void)
   {
       inherited();

       cabin_fan_switch = GetNamedControl("fan_switch");
       window_wipers = GetNamedControl("wiper_switch");

       switch3 = GetNamedControl("switch_3");
       switch4 = GetNamedControl("switch_4");
       switch5 = GetNamedControl("switch_5");
       switch6 = GetNamedControl("switch_6");
       switch7 = GetNamedControl("switch_7");
       switch8 = GetNamedControl("switch_8");

       cabin_fan_light = GetNamedControl("switchlight0");
       window_wipers_light = GetNamedControl("sw itchlight1");

       light3 = GetNamedControl("switchlight2");
       light4 = GetNamedControl("switchlight3");
       light5 = GetNamedControl("switchlight4");
       light6 = GetNamedControl("switchlight5");
       light7 = GetNamedControl("switchlight6");
       light8 = GetNamedControl("switchlight7");

       RunAnimation();
   }

   void UserSetControl(CabinControl p_control, float p_value)
   {
       DD40CabinData cd = cast<DD40CabinData>loco.GetCabinData();

       if (p_control == cabin_fan_switch)
       {
           bool wantFanAnimation = p_value > 0.5;
           if (wantFanAnimation != cd.animatingFan)
           {
               cd.animatingFan = wantFanAnimation;
               UpdateFan();
           }
           if (wantFanAnimation)
               cabin_fan_light.SetValue(1.0);
           else
               cabin_fan_light.SetValue(0.0);
       }

       else if (p_control == window_wipers)
       {
           bool wantWiperAnimation = p_value > 0.5;
           if (wantWiperAnimation != cd.animatingWiper)
           {
               cd.animatingWiper = wantWiperAnimation;
               UpdateWipers();
           }
           if (wantWiperAnimation)
               window_wipers_light.SetValue(1.0);
           else
               window_wipers_light.SetValue(0.0);
       }

       else if (p_control == switch3)
       {
           bool isOn = p_value > 0.5;
           float value = 0.0;

           if (isOn) value = 1.0;

           light3.SetValue(value);
           cd.switchOn3 = value;
       }

       else if (p_control == switch4)
       {
           bool isOn = p_value > 0.5;
           float value = 0.0;

           if (isOn) value = 1.0;

           light4.SetValue(value);
           cd.switchOn4 = value;
       }

      else if (p_control == switch5)
       {
           bool isOn = p_value > 0.5;
           float value = 0.0;

           if (isOn) value = 1.0;

           light5.SetValue(value);
           cd.switchOn5 = value;
       }

       else if (p_control == switch6)
       {
           bool isOn = p_value > 0.5;
           float value = 0.0;

           if (isOn) value = 1.0;

           light6.SetValue(value);
           cd.switchOn6 = value;
       }

       else if (p_control == switch7)
       {
           bool isOn = p_value > 0.5;
           float value = 0.0;

           if (isOn) value = 1.0;

           light7.SetValue(value);
           cd.switchOn7 = value;
       }

       else if (p_control == switch8)
       {
           bool isOn = p_value > 0.5;
           float value = 0.0;

           if (isOn) value = 1.0;

           light8.SetValue(value);
           cd.switchOn8 = value;
       }
       else
       inherited(p_control, p_value);
   }

   thread void SlowFanDown(void)
   {
       DD40CabinData cd = cast<DD40CabinData>loco.GetCabinData();

       if (isFanSlowingDown)
           return;

       isFanSpeedingUp = false;
       // Slow it down...
       while (fanSpeed > 0.1)
       {
           fanSpeed = fanSpeed - 0.1;
           SetMeshAnimationSpeed("fan", fanSpeed);
           Sleep(0.5);
           if (!isFanSlowingDown)
               return;
       }

       fanSpeed = 0.0;
       StopMeshAnimation(vfan");
   }

   thread void SpeedFanUp(void)
   {
       DD40CabinData cd = cast<DD40CabinData>loco.GetCabinData();

       if (isFanSpeedingUp)
           return;

       isFanSpeedingUp = true;
       isFanSlowingDown = false;

       // Speed it up...
       while (fanSpeed < 1.0)
       {
           fanSpeed = fanSpeed + 0.1;
           SetMeshAnimationSpeed("fan", fanSpeed);
           StartMeshAnimationLoop("fan");

           Sleep(0.5);
           if (!isFanSpeedingUp)
               return;
       }

       fanSpeed = 1.0;
       SetMeshAnimationSpeed("fan", fanSpeed);
       //isFanSpeedingUp = false;
   }

   void UpdateFan(void)
   {
       DD40CabinData cd = cast<DD40CabinData>loco.GetCabinData();

       if (cd.animatingFan)
           SpeedFanUp();
       else
           SlowFanDown();
   }

   void UpdateWipers(void)
   {
       DD40CabinData cd = cast<DD40CabinData>loco.GetCabinData();

       // Don't need to worry about else, as it will be handled when the loop is done.
       if (cd.animatingWiper)
       {
           SetMeshAnimationSpeed("wipers", 1.0);
           StartMeshAnimationLoop("wipers");
       }
  }

   thread void RunAnimation(void)
   {
       DD40CabinData cd = cast<DD40CabinData>loco.GetCabinData();

       wait()
       {
           on "Animation-Event", "wiperstop":
           if (!cd.animatingWiper)
               StopMeshAnimation("wipers");
           continue;
       }
   }
};


Diesel Cab Config.txt


Return to CCG Index

Content Creator's Guide

Personal tools