Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- namespace SpartanFist
- {
- [AddComponentMenu("Interactions/Switches/Switch")]
- public class Switch : MonoBehaviour
- {
- [Header("References"), Tooltip("(optional) If this is pointed at something, it'll be enabled/disabled based on current switch state")]
- public GameObject switchOnMesh;
- [Tooltip("(optional) If this is pointed at something, it'll be enabled/disabled based on current switch state")]
- public GameObject switchOffMesh;
- public virtual bool SwitchState
- {
- get
- {
- return curSwitchState;
- }
- set
- {
- if (value != curSwitchState)
- {
- if (value) { SwitchOn(); }
- else { SwitchOff(); }
- }
- }
- }
- protected bool curSwitchState;
- [Header("Switch Sounds")]
- public AudioClip onSound;
- public AudioClip offSound;
- [Header("Switch Stuff")]
- public bool defaultSwitchState;
- public bool stateIsSaved = false;
- #region PoolManager Functions
- public virtual void OnSpawned()
- {
- RestoreSwitchState();
- SwitchState = defaultSwitchState;
- // (don't touch lastSwitchState - we want to resume default switch state, which might mean turning on/off from where we were when despawned)
- }
- public virtual void OnDespawned()
- {
- }
- #endregion
- #region Standard Functions
- public virtual void Awake()
- {
- RestoreSwitchState();
- SwitchState = defaultSwitchState;
- }
- public virtual void Start()
- {
- // (stubbed out in case Switches need to touch external objects during their setup)
- }
- public virtual void Update()
- {
- if (SwitchState)
- {
- if ((switchOnMesh != null) && !switchOnMesh.activeSelf)
- {
- switchOnMesh.SetActive(true);
- }
- if ((switchOffMesh != null) && switchOffMesh.activeSelf)
- {
- switchOffMesh.SetActive(false);
- }
- }
- else
- {
- if ((switchOffMesh != null) && !switchOffMesh.activeSelf)
- {
- switchOffMesh.SetActive(true);
- }
- if ((switchOnMesh != null) && switchOnMesh.activeSelf)
- {
- switchOnMesh.SetActive(false);
- }
- }
- }
- #endregion
- #region Switch Functions
- private void RestoreSwitchState()
- {
- if (!stateIsSaved)
- {
- return;
- }
- // @TODO: If we want these to save/load, we'd need a system for that first
- // int savedState = 0;
- // if (PersistenceManager.TryGetObjectState(this.gameObject, out savedState))
- // {
- // if (savedState > 0)
- // {
- // defaultSwitchState = true;
- // }
- // else
- // {
- // defaultSwitchState = false;
- // }
- // }
- }
- private void SaveSwitchState()
- {
- if (!stateIsSaved)
- {
- return;
- }
- // @TODO: If we want these to save/load, we'd need a system for that first
- // int savedState = 0;
- // if (SwitchState)
- // {
- // savedState = 1;
- // }
- //
- // PersistenceManager.SetObjectState(this.gameObject, savedState);
- }
- /// <summary>
- /// This turns the switch on, with whatever effects that may have
- /// </summary>
- protected virtual void SwitchOn()
- {
- curSwitchState = true;
- if (onSound != null) { AudioSourceEx.PlayClipAtPointModulated(onSound, gameObject.transform.position); }
- SaveSwitchState();
- }
- /// <summary>
- /// This turns the switch off, with whatever effects that may have
- /// </summary>
- protected virtual void SwitchOff()
- {
- curSwitchState = false;
- if (offSound != null) { AudioSourceEx.PlayClipAtPointModulated(offSound, gameObject.transform.position); }
- SaveSwitchState();
- }
- #endregion
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment