Guest User

Untitled

a guest
May 30th, 2017
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.57 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. namespace SpartanFist
  5. {
  6. [AddComponentMenu("Interactions/Switches/Switch")]
  7. public class Switch : MonoBehaviour
  8. {
  9. [Header("References"), Tooltip("(optional) If this is pointed at something, it'll be enabled/disabled based on current switch state")]
  10. public GameObject switchOnMesh;
  11. [Tooltip("(optional) If this is pointed at something, it'll be enabled/disabled based on current switch state")]
  12. public GameObject switchOffMesh;
  13.  
  14. public virtual bool SwitchState
  15. {
  16. get
  17. {
  18. return curSwitchState;
  19. }
  20. set
  21. {
  22. if (value != curSwitchState)
  23. {
  24. if (value) { SwitchOn(); }
  25. else { SwitchOff(); }
  26. }
  27. }
  28. }
  29. protected bool curSwitchState;
  30.  
  31. [Header("Switch Sounds")]
  32. public AudioClip onSound;
  33. public AudioClip offSound;
  34.  
  35. [Header("Switch Stuff")]
  36. public bool defaultSwitchState;
  37. public bool stateIsSaved = false;
  38.  
  39. #region PoolManager Functions
  40. public virtual void OnSpawned()
  41. {
  42. RestoreSwitchState();
  43.  
  44. SwitchState = defaultSwitchState;
  45. // (don't touch lastSwitchState - we want to resume default switch state, which might mean turning on/off from where we were when despawned)
  46. }
  47.  
  48. public virtual void OnDespawned()
  49. {
  50. }
  51. #endregion
  52.  
  53. #region Standard Functions
  54. public virtual void Awake()
  55. {
  56. RestoreSwitchState();
  57.  
  58. SwitchState = defaultSwitchState;
  59. }
  60.  
  61. public virtual void Start()
  62. {
  63. // (stubbed out in case Switches need to touch external objects during their setup)
  64. }
  65.  
  66. public virtual void Update()
  67. {
  68. if (SwitchState)
  69. {
  70. if ((switchOnMesh != null) && !switchOnMesh.activeSelf)
  71. {
  72. switchOnMesh.SetActive(true);
  73. }
  74. if ((switchOffMesh != null) && switchOffMesh.activeSelf)
  75. {
  76. switchOffMesh.SetActive(false);
  77. }
  78. }
  79. else
  80. {
  81. if ((switchOffMesh != null) && !switchOffMesh.activeSelf)
  82. {
  83. switchOffMesh.SetActive(true);
  84. }
  85. if ((switchOnMesh != null) && switchOnMesh.activeSelf)
  86. {
  87. switchOnMesh.SetActive(false);
  88. }
  89. }
  90. }
  91. #endregion
  92.  
  93. #region Switch Functions
  94. private void RestoreSwitchState()
  95. {
  96. if (!stateIsSaved)
  97. {
  98. return;
  99. }
  100.  
  101. // @TODO: If we want these to save/load, we'd need a system for that first
  102. // int savedState = 0;
  103. // if (PersistenceManager.TryGetObjectState(this.gameObject, out savedState))
  104. // {
  105. // if (savedState > 0)
  106. // {
  107. // defaultSwitchState = true;
  108. // }
  109. // else
  110. // {
  111. // defaultSwitchState = false;
  112. // }
  113. // }
  114. }
  115.  
  116. private void SaveSwitchState()
  117. {
  118. if (!stateIsSaved)
  119. {
  120. return;
  121. }
  122.  
  123. // @TODO: If we want these to save/load, we'd need a system for that first
  124. // int savedState = 0;
  125. // if (SwitchState)
  126. // {
  127. // savedState = 1;
  128. // }
  129. //
  130. // PersistenceManager.SetObjectState(this.gameObject, savedState);
  131. }
  132.  
  133. /// <summary>
  134. /// This turns the switch on, with whatever effects that may have
  135. /// </summary>
  136. protected virtual void SwitchOn()
  137. {
  138. curSwitchState = true;
  139. if (onSound != null) { AudioSourceEx.PlayClipAtPointModulated(onSound, gameObject.transform.position); }
  140.  
  141. SaveSwitchState();
  142. }
  143.  
  144. /// <summary>
  145. /// This turns the switch off, with whatever effects that may have
  146. /// </summary>
  147. protected virtual void SwitchOff()
  148. {
  149. curSwitchState = false;
  150. if (offSound != null) { AudioSourceEx.PlayClipAtPointModulated(offSound, gameObject.transform.position); }
  151.  
  152. SaveSwitchState();
  153. }
  154. #endregion
  155. }
  156. }
Advertisement
Add Comment
Please, Sign In to add comment