Guest User

Untitled

a guest
May 30th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.38 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. namespace SpartanFist
  5. {
  6. [AddComponentMenu("Interactions/Switches/Floor Switch")]
  7.  
  8. public class DepressionSwitch : Switch
  9. {
  10. [Header("References"), Tooltip("(optional) this is the mesh we're depressing down or popping back up with switch state")]
  11. public GameObject switchMeshObj;
  12.  
  13. protected float curSwitchBlendTime = 0.0f;
  14. protected float baseButtonY = 0.0f;
  15.  
  16. /* Magic Numbers */
  17.  
  18. protected const float SwitchDepressionAmount = 0.4f;
  19. protected const float SwitchMoveTime = 0.5f;
  20.  
  21. /* Standard Functions */
  22.  
  23. public override void Awake()
  24. {
  25. base.Awake();
  26.  
  27. if (switchMeshObj != null)
  28. {
  29. Vector3 curSwitchPos = switchMeshObj.transform.localPosition;
  30. baseButtonY = curSwitchPos.y;
  31.  
  32. if (defaultSwitchState) { curSwitchPos.y = baseButtonY - SwitchDepressionAmount; }
  33. else { curSwitchPos.y = baseButtonY; }
  34. switchMeshObj.transform.localPosition = curSwitchPos;
  35. }
  36. }
  37.  
  38. public override void Update()
  39. {
  40. base.Update();
  41.  
  42. if (curSwitchBlendTime > 0.0f)
  43. {
  44. float deltaTime = PhysicsManager.GetDeltaTimeFor(gameObject);
  45. curSwitchBlendTime -= deltaTime;
  46. if (curSwitchBlendTime <= 0.0f)
  47. {
  48. curSwitchBlendTime = 0.0f;
  49. }
  50.  
  51. if (switchMeshObj != null)
  52. {
  53. float tVal = curSwitchBlendTime / SwitchMoveTime;
  54.  
  55. Vector3 curSwitchPos = switchMeshObj.transform.localPosition;
  56. if (SwitchState)
  57. {
  58. // (for switch on, we want to attack into the state faster)
  59. tVal *= tVal;
  60. tVal += tVal;
  61. tVal = 1.0f - tVal;
  62.  
  63. curSwitchPos.y = baseButtonY + Mathf.Lerp(0.0f, -SwitchDepressionAmount, tVal);
  64. }
  65. else
  66. {
  67. // (for switch off, we want to ease out of the state slowly)
  68. tVal = 1.0f - tVal;
  69. tVal *= tVal;
  70.  
  71. curSwitchPos.y = baseButtonY + Mathf.Lerp(-SwitchDepressionAmount, 0.0f, tVal);
  72. }
  73. switchMeshObj.transform.localPosition = curSwitchPos;
  74. }
  75. }
  76. }
  77.  
  78. /// <summary>
  79. /// This is someone tapping their foot down on the switch
  80. /// </summary>
  81. protected override void SwitchOn()
  82. {
  83. base.SwitchOn();
  84.  
  85. curSwitchBlendTime = SwitchMoveTime;
  86. }
  87.  
  88. /// <summary>
  89. /// This is someone walking off the switch
  90. /// </summary>
  91. protected override void SwitchOff()
  92. {
  93. base.SwitchOff();
  94.  
  95. curSwitchBlendTime = SwitchMoveTime;
  96. }
  97. }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment