namespace TurnTheGameOn.IKDriver { using System.Collections; using System.Collections.Generic; using UnityEngine; [System.Serializable] public class CameraStateSettings { public string stateName; [Range (0.0f, 1.0f)] public float volumelimit = 1.0f; [Range (0.0f, 1.0f)] public float shiftVolume = 1.0f; public GameObject [] enabledObjects; public GameObject [] disabledObjects; public Behaviour [] enabledBehaviours; public Behaviour [] disabledBehaviours; } public class IKD_CameraSwitchToggle : MonoBehaviour { public IKD_VehicleController vehicleController; public IKD_VehicleAudio vehicleAudio; public string currentState { get; private set; } public int currentStateIndex { get; private set; } public CameraStateSettings [] cameraStateSettings; public Camera carCamera; public Camera helmetCamera; public Camera lookBackCamera; public Transform vehicle; public string startState = "ChaseCamera"; private GameObject uiHud; void Awake () { uiHud = GameObject.Find ("UI HUD"); } void Start () { OnChangeState (startState); } public void SetNextState () { currentStateIndex = currentStateIndex == cameraStateSettings.Length - 1 ? 0 : currentStateIndex += 1; currentState = cameraStateSettings [currentStateIndex].stateName; SetStateSettings (currentStateIndex); } public void OnChangeState (string newState) { currentState = newState; for (int i = 0; i < cameraStateSettings.Length; i++) { if (currentState == cameraStateSettings [i].stateName) { SetStateSettings (i); currentStateIndex = i; break; } } SetEnabledCamera (); } void SetStateSettings (int index) { // set enabledObjects for (int i = 0; i < cameraStateSettings [index].enabledObjects.Length; i++) { if (cameraStateSettings [index].enabledObjects != null) { if (cameraStateSettings [index].enabledObjects [i] != null) cameraStateSettings [index].enabledObjects [i].SetActive (true); } } // set disabledObjects for (int i = 0; i < cameraStateSettings [index].disabledObjects.Length; i++) { if (cameraStateSettings [index].disabledObjects != null) { if (cameraStateSettings [index].disabledObjects [i] != null) cameraStateSettings [index].disabledObjects [i].SetActive (false); } } // set enabledBehaviours for (int i = 0; i < cameraStateSettings [index].enabledBehaviours.Length; i++) { if (cameraStateSettings [index].enabledBehaviours != null) { if (cameraStateSettings [index].enabledBehaviours [i]) cameraStateSettings [index].enabledBehaviours [i].enabled = true; } } // set disabledBehaviours for (int i = 0; i < cameraStateSettings [index].disabledBehaviours.Length; i++) { if (cameraStateSettings [index].disabledBehaviours != null) { if (cameraStateSettings [index].disabledBehaviours [i]) cameraStateSettings [index].disabledBehaviours [i].enabled = false; } } // set audioLimiter vehicleAudio.volumeLimiter = cameraStateSettings [index].volumelimit; vehicleAudio.shiftSource.volume = cameraStateSettings [index].shiftVolume; } public void SetEnabledCamera () { if (carCamera.enabled) { helmetCamera.enabled = currentState == "HelmetCamera" ? true : false; carCamera.enabled = currentState == "ChaseCamera" ? true : false; } else if (helmetCamera.enabled) { carCamera.enabled = currentState == "ChaseCamera" ? true : false; helmetCamera.enabled = currentState == "HelmetCamera" ? true : false; } else { carCamera.enabled = currentState == "ChaseCamera" ? true : false; helmetCamera.enabled = currentState == "HelmetCamera" ? true : false; } transform.parent = currentState == "ChaseCamera" ? null : currentState == "HelmetCamera" ? vehicle : null; if (!vehicleController) vehicleController = GetComponent <IKD_VehicleCamera> ().carController; if (uiHud) { if (currentState == "ChaseCamera") { uiHud.SetActive (true); } else { uiHud.SetActive (false); } } } /// <summary> /// Enter the camera 'Look Back' state /// </summary> public void LookBackCamera () { carCamera.enabled = false; helmetCamera.enabled = false; lookBackCamera.gameObject.SetActive (true); } /// <summary> /// Exit the Camera 'Look Back' state /// </summary> public void OnLookBackKeyUp () { lookBackCamera.gameObject.SetActive (false); SetEnabledCamera (); } /// <summary> /// Cycles through the configured vehicle cameras /// </summary> public void CycleCamera() { SetNextState (); SetEnabledCamera (); } } }