CCG/Kind: Interior Diesel Cab Script

From TrainzOnline
< CCG(Difference between revisions)
Jump to: navigation, search
(New page: ==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 switchlig...)
 
 
(4 intermediate revisions by 2 users not shown)
Line 7: Line 7:
  
 
  include "defaultlocomotivecabin.gs"
 
  include "defaultlocomotivecabin.gs"
 
+
 
  class DD40CabinData isclass CabinData
 
  class DD40CabinData isclass CabinData
 
  {
 
  {
  public bool animatingFan;
+
    public bool animatingFan;
  public bool animatingiper;
+
    public bool animatingWiper;
  public bool switchOn3;
+
    public bool switchOn3;
  public bool switchOn4;
+
    public bool switchOn4;
  public bool switchOn5;
+
    public bool switchOn5;
  public bool switchOn6;
+
    public bool switchOn6;
  public bool switchOn7;
+
    public bool switchOn7;
  public bool switchOn8;
+
    public bool switchOn8;
  };
+
};
 
+
  class DD40Cabin isclass
+
class DD40Cabin isclass DefaultLocomotiveCabin
DefaultLocomotiveCabin
+
{
  {
+
 
     // Switches
 
     // Switches
 
     CabinControl cabin_fan_switch;
 
     CabinControl cabin_fan_switch;
Line 32: Line 31:
 
     CabinControl switch7;
 
     CabinControl switch7;
 
     CabinControl switch8;
 
     CabinControl switch8;
 
+
 
     // Lights
 
     // Lights
 
     CabinControl    cabin_fan_light;
 
     CabinControl    cabin_fan_light;
Line 42: Line 41:
 
     CabinControl    light7;
 
     CabinControl    light7;
 
     CabinControl    light8;
 
     CabinControl    light8;
 
+
 
     thread void SlowFanDown(void);
 
     thread void SlowFanDown(void);
 
     thread void SpeedFanUp(void);
 
     thread void SpeedFanUp(void);
 
+
 
     void UpdateFan(void);
 
     void UpdateFan(void);
 
     void UpdateWipers(void);
 
     void UpdateWipers(void);
 
     thread void RunAnimation(void);
 
     thread void RunAnimation(void);
 
+
 
     float fanSpeed;
 
     float fanSpeed;
 
     bool isFanSpeedingUp;
 
     bool isFanSpeedingUp;
 
     bool isFanSlowingDown;
 
     bool isFanSlowingDown;
 
+
 
     //! Attach this cabin to a game object (i.e. a locomotive).
 
     //! Attach this cabin to a game object (i.e. a locomotive).
 
     //
 
     //
 
     // Param: obj Game object to attach this cabin to (usually a Locomotive).
 
     // Param: obj Game object to attach this cabin to (usually a Locomotive).
 
     //
 
     //
 +
 
     void Attach(GameObject obj)
 
     void Attach(GameObject obj)
 
     {
 
     {
      inherited(obj);
+
        inherited(obj);
 
+
      // get cabin data
+
        // get cabin data
      CabinData cd = loco.GetCabinData();
+
        CabinData cd = loco.GetCabinData();
      if(cd)
+
        if(cd)
      {
+
        {
        // reset the controls from saved values
+
            // reset the controls from saved values
        DD40CabinData ddcd = cast<DD40CabinData>cd;
+
            DD40CabinData ddcd = cast<DD40CabinData>cd;
        float value = 0.0;
+
            float value = 0.0;
 
+
 
+
            // ANIMATING FAN
    // ANIMATING FAN
+
            if (ddcd.animatingFan)
    if (ddcd.animatingFan)
+
            {
    {
+
                fanSpeed = 1.0;
      fanSpeed = 1.0;
+
                SetMeshAnimationSpeed("fan", 1.0);
      SetMeshAnimationSpeed("fan", 1.0);
+
                StartMeshAnimationLoop("fan");
  StartMeshAnimationLoop("fan");
+
                value = 1.0;
  value = 1.0;
+
            }
}
+
            else
else
+
                value = 0.0;
value = 0.0;
+
 
+
            cabin_fan_switch.SetValue(value);
cabin_fan_switch.SetValue(value);
+
            cabin_fan_light.SetValue(value);
cabin_fan_light.SetValue(value);
+
 
+
            // ANIMATING WIPER
// ANIMATING IPER
+
            if (ddcd.animatingWiper)
if (ddcd.animatingWiper)
+
            {
{
+
                value = 1.0;
  value = 1.0;
+
                SetMeshAnimationSpeed("wipers", 1.0);
  SetMeshAnimationSpeed("wipers", 1.0);
+
                StartMeshAnimationLoop("wipers");
  StartMeshAnimationLoop("wipers");
+
            }
}
+
            else
else
+
                value = 0.0;
value = 0.0;
+
   
 
+
            window_wipers.SetValue(value);
  window_wipers.SetValue(value);
+
            window_wipers_light.SetValue(value);
window_wipers_light.SetValue(value);
+
   
 
+
            // SWITCH 3
  // SWITCH 3
+
            if (ddcd.switchOn3)
if (ddcd.switchOn3)
+
                value = 1.0;
value = 1.0;
+
            else
else
+
                value = 0.0;
value = 0.0;
+
   
  switch3.SetValue(value);
+
            switch3.SetValue(value);
light3.SetValue(value);
+
            light3.SetValue(value);
 
+
   
  // SWITCH 4
+
            // SWITCH 4
if (ddcd.switchOn4)
+
            if (ddcd.switchOn4)
value = 1.0;
+
                value = 1.0;
else
+
            else
value = 0.0;
+
                value = 0.0;
  switch4.SetValue(value);
+
   
light4.SetValue(value);
+
            switch4.SetValue(value);
 
+
            light4.SetValue(value);
  // SWITCH 5
+
   
if (ddcd.switchOn5)
+
            // SWITCH 5
value = 1.0;
+
            if (ddcd.switchOn5)
else
+
                value = 1.0;
value = 0.0;
+
            else
  switch5.SetValue(value);
+
                value = 0.0;
light5.SetValue(value);
+
   
 
+
            switch5.SetValue(value);
// SWITCH 6
+
            light5.SetValue(value);
if (ddcd.switchOn6)
+
value = 1.0;
+
            // SWITCH 6
else
+
            if (ddcd.switchOn6)
value = 0.0;
+
                value = 1.0;
  switch6.SetValue(value);
+
            else
light6.SetValue(value);
+
                value = 0.0;
 
+
   
  // SWITCH 7
+
            switch6.SetValue(value);
if (ddcd.switchOn7)
+
            light6.SetValue(value);
value = 1.0;
+
   
else
+
            // SWITCH 7
value = 0.0;
+
            if (ddcd.switchOn7)
  switch7.SetValue(value);
+
                value = 1.0;
light7.SetValue(value);
+
            else
 
+
                value = 0.0;
// SWITCH 8
+
   
if (ddcd.switchOn8)
+
            switch7.SetValue(value);
value = 1.0;
+
            light7.SetValue(value);
else
+
value = 0.0;
+
            // SWITCH 8
  switch8.SetValue(value);
+
            if (ddcd.switchOn8)
light8.SetValue(value);
+
                value = 1.0;
      }
+
            else
 
+
                value = 0.0;
      else
+
   
      {
+
            switch8.SetValue(value);
        DD40CabinData ddd = new DD40CabinData();
+
            light8.SetValue(value);
        loco.SetCabinData(ddd);
+
        }
      }
+
 +
        else
 +
        {
 +
            DD40CabinData ddd = new DD40CabinData();
 +
            loco.SetCabinData(ddd);
 +
        }
 
     }
 
     }
 +
 
     void UserPressKey(string s)
 
     void UserPressKey(string s)
 
     {
 
     {
      if(s == "cabin-fans")
+
        if(s == "cabin-fans")
      {
+
        {
        DD40CabinData cd = cast<DD40CabinData>loco.GetCabinData();
+
            DD40CabinData cd = cast<DD40CabinData>loco.GetCabinData();
        cd.animatingFan = !cd.animatingFan;
+
            cd.animatingFan = !cd.animatingFan;
 
+
 
             float value;
 
             float value;
 
             if (cd.animatingFan)
 
             if (cd.animatingFan)
            value = 1.0;
+
                value = 1.0;
 
             else
 
             else
            value = 0.0;
+
                value = 0.0;
 
+
 
             cabin_fan_switch.SetValue(value);
 
             cabin_fan_switch.SetValue(value);
 
             cabin_fan_light.SetValue(value);
 
             cabin_fan_light.SetValue(value);
 
+
        UpdateFan();
+
            UpdateFan();
      }
+
        }
      if(s == "wipers")
+
      {
+
        if(s == "wipers")
        DD40CabinData cd = cast<DD40CabinData>loco.GetCabinData();
+
        {
        cd.animatingiper = !cd.animatingiper;
+
            DD40CabinData cd = cast<DD40CabinData>loco.GetCabinData();
 
+
            cd.animatingWiper = !cd.animatingWiper;
 +
 
             float value;
 
             float value;
 
             if (cd.animatingWiper)
 
             if (cd.animatingWiper)
            value = 1.0;
+
                value = 1.0;
 
             else
 
             else
            value = 0.0;
+
                value = 0.0;
 
+
 
             window_wipers.SetValue(value);
 
             window_wipers.SetValue(value);
 
             window_wipers_light.SetValue(value);
 
             window_wipers_light.SetValue(value);
 
+
 
             UpdateWipers();
 
             UpdateWipers();
 
         }
 
         }
 
     }
 
     }
 
+
 
     public void Init(void)
 
     public void Init(void)
 
     {
 
     {
      inherited();
+
        inherited();
 
+
      cabin_fan_switch = GetNamedControl("fan_switch");
+
        cabin_fan_switch = GetNamedControl("fan_switch");
      window_wipers = GetNamedControl("wiper_switch");
+
        window_wipers = GetNamedControl("wiper_switch");
 
+
      switch3 = GetNamedControl("switch_3");
+
        switch3 = GetNamedControl("switch_3");
      switch4 = GetNamedControl("switch_4");
+
        switch4 = GetNamedControl("switch_4");
      switch5 = GetNamedControl("switch_5");
+
        switch5 = GetNamedControl("switch_5");
      switch6 = GetNamedControl("switch_6");
+
        switch6 = GetNamedControl("switch_6");
      switch7 = GetNamedControl("switch_7");
+
        switch7 = GetNamedControl("switch_7");
      switch8 = GetNamedControl("switch_8");
+
        switch8 = GetNamedControl("switch_8");
 
+
 
+
        cabin_fan_light = GetNamedControl("switchlight0");
      cabin_fan_light = GetNamedControl("switchlight0");
+
        window_wipers_light = GetNamedControl("sw itchlight1");
      window_wipers_light = GetNamedControl("sw itchlight1");
+
 
+
        light3 = GetNamedControl("switchlight2");
      light3 = GetNamedControl("switchlight2");
+
        light4 = GetNamedControl("switchlight3");
      light4 = GetNamedControl("switchlight3");
+
        light5 = GetNamedControl("switchlight4");
      light5 = GetNamedControl("switchlight4");
+
        light6 = GetNamedControl("switchlight5");
      light6 = GetNamedControl("switchlight5");
+
        light7 = GetNamedControl("switchlight6");
      light7 = GetNamedControl("switchlight6");
+
        light8 = GetNamedControl("switchlight7");
      light8 = GetNamedControl("switchlight7");
+
 
+
        RunAnimation();
          RunAnimation();
+
    }
      }
+
      void UserSetControl(CabinControl p_control, float p_value)
+
    void UserSetControl(CabinControl p_control, float p_value)
      {
+
    {
 
         DD40CabinData cd = cast<DD40CabinData>loco.GetCabinData();
 
         DD40CabinData cd = cast<DD40CabinData>loco.GetCabinData();
 
+
      if (p_control == cabin_fan_switch)
+
        if (p_control == cabin_fan_switch)
      {
+
        {
        bool wantFanAnimation = p_value > 0.5;
+
            bool wantFanAnimation = p_value > 0.5;
        if (wantFanAnimation != cd.animatingFan)
+
            if (wantFanAnimation != cd.animatingFan)
        {
+
            {
          cd.animatingFan = wantFanAnimation;
+
                cd.animatingFan = wantFanAnimation;
          UpdateFan();
+
                UpdateFan();
        }
+
            }
              if (wantFanAnimation)
+
            if (wantFanAnimation)
              cabin_fan_light.SetValue(1.0);
+
                cabin_fan_light.SetValue(1.0);
              else
+
            else
              cabin_fan_light.SetValue(0.0);
+
                cabin_fan_light.SetValue(0.0);
          }
+
        }
 
+
          else if (p_control == window_wipers)
+
        else if (p_control == window_wipers)
          {
+
        {
             bool wantiperAnimation = p_value > 0.5;
+
             bool wantWiperAnimation = p_value > 0.5;
        if (wantWiperAnimation != cd.animatingWiper)
+
            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)
 
         {
 
         {
          cd.animatingiper = 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;
 
             bool isOn = p_value > 0.5;
 
             float value = 0.0;
 
             float value = 0.0;
 
+
 
             if (isOn) value = 1.0;
 
             if (isOn) value = 1.0;
 +
 
             light3.SetValue(value);
 
             light3.SetValue(value);
 
             cd.switchOn3 = value;
 
             cd.switchOn3 = value;
          }
+
        }
 
+
 
         else if (p_control == switch4)
 
         else if (p_control == switch4)
 
         {
 
         {
          bool isOn = p_value > 0.5;
+
            bool isOn = p_value > 0.5;
          float value = 0.0;
+
            float value = 0.0;
          if (isOn) value = 1.0;
+
 
+
            if (isOn) value = 1.0;
 +
 
             light4.SetValue(value);
 
             light4.SetValue(value);
 
             cd.switchOn4 = value;
 
             cd.switchOn4 = value;
 
         }
 
         }
 
+
 
       else if (p_control == switch5)
 
       else if (p_control == switch5)
      {
+
        {
        bool isOn = p_value > 0.5;
+
            bool isOn = p_value > 0.5;
        float value = 0.0;
+
            float value = 0.0;
        if (isOn) value = 1.0;
+
 
+
            if (isOn) value = 1.0;
 +
 
             light5.SetValue(value);
 
             light5.SetValue(value);
 
             cd.switchOn5 = value;
 
             cd.switchOn5 = value;
 
         }
 
         }
 
+
      else if (p_control == switch6)
+
        else if (p_control == switch6)
 
         {
 
         {
          bool isOn = p_value > 0.5;
+
            bool isOn = p_value > 0.5;
          float value = 0.0;
+
            float value = 0.0;
          if (isOn) value = 1.0;
+
 
+
            if (isOn) value = 1.0;
 +
 
             light6.SetValue(value);
 
             light6.SetValue(value);
 
             cd.switchOn6 = value;
 
             cd.switchOn6 = value;
 
         }
 
         }
 
+
 
         else if (p_control == switch7)
 
         else if (p_control == switch7)
 
         {
 
         {
          bool isOn = p_value > 0.5;
+
            bool isOn = p_value > 0.5;
          float value = 0.0;
+
            float value = 0.0;
          if (isOn) value = 1.0;
+
 
+
            if (isOn) value = 1.0;
 +
 
             light7.SetValue(value);
 
             light7.SetValue(value);
 
             cd.switchOn7 = value;
 
             cd.switchOn7 = value;
 
         }
 
         }
 
+
 
         else if (p_control == switch8)
 
         else if (p_control == switch8)
 
         {
 
         {
          bool isOn = p_value > 0.5;
+
            bool isOn = p_value > 0.5;
          float value = 0.0;
+
            float value = 0.0;
          if (isOn) value = 1.0;
+
 
+
            if (isOn) value = 1.0;
 +
 
             light8.SetValue(value);
 
             light8.SetValue(value);
 
             cd.switchOn8 = value;
 
             cd.switchOn8 = value;
Line 310: Line 323:
 
         inherited(p_control, p_value);
 
         inherited(p_control, p_value);
 
     }
 
     }
 
+
  thread void SlowFanDown(void)
+
    thread void SlowFanDown(void)
 
     {
 
     {
      DD40CabinData cd = cast<DD40CabinData>loco.GetCabinData();
+
        DD40CabinData cd = cast<DD40CabinData>loco.GetCabinData();
 
+
 
         if (isFanSlowingDown)
 
         if (isFanSlowingDown)
        return;
+
            return;
          isFanSpeedingUp = false;
+
          // Slow it down...
+
        isFanSpeedingUp = false;
          while (fanSpeed > 0.1)
+
        // Slow it down...
          {
+
        while (fanSpeed > 0.1)
            fanSpeed = fanSpeed - 0.1;
+
        {
            SetMeshAnimationSpeed("fan", fanSpeed);
+
            fanSpeed = fanSpeed - 0.1;
              Sleep(0.5);
+
            SetMeshAnimationSpeed("fan", fanSpeed);
              if (!isFanSlowingDown)
+
            Sleep(0.5);
              return;
+
            if (!isFanSlowingDown)
          }
+
                return;
 
+
        }
          fanSpeed = 0.0;
+
          StopMeshAnimation(vfan");
+
        fanSpeed = 0.0;
      }
+
        StopMeshAnimation(vfan");
 
+
    }
      thread void SpeedFanUp(void)
+
      {
+
    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();
 
         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.
 
         // Don't need to worry about else, as it will be handled when the loop is done.
 
         if (cd.animatingWiper)
 
         if (cd.animatingWiper)
 
         {
 
         {
          SetMeshAnimationSpeed("wipers", 1.0);
+
            SetMeshAnimationSpeed("wipers", 1.0);
          StartMeshAnimationLoop("wipers");
+
            StartMeshAnimationLoop("wipers");
 
         }
 
         }
 
   }
 
   }
 
+
 
     thread void RunAnimation(void)
 
     thread void RunAnimation(void)
 
     {
 
     {
      DD40CabinData cd = cast<DD40CabinData>loco.GetCabinData();
+
        DD40CabinData cd = cast<DD40CabinData>loco.GetCabinData();
 
+
        wait()
+
        wait()
        {
+
        {
          on "Animation-Event", "wiperstop":
+
            on "Animation-Event", "wiperstop":
          if (!cd.animatingWiper)
+
            if (!cd.animatingWiper)
          StopMeshAnimation("wipers");
+
                StopMeshAnimation("wipers");
          continue;
+
            continue;
        }
+
        }
    }
+
    }
  } ;
+
  };
 +
 
  
 +
[[CCG/Kind:_Interior#DD40_Cab_Interior|Diesel Cab Config.txt]]
  
  
 
==Return to CCG Index==
 
==Return to CCG Index==
[[Content Creator's Guide]]]
+
[[Content Creator's Guide]]

Latest revision as of 13:02, 18 June 2013

[edit] 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


[edit] Return to CCG Index

Content Creator's Guide

Personal tools