using System.Collections; using UnityEngine; using UnityEngine.SceneManagement; namespace Invector { using UnityEngine.Events; using vCharacterController; [vClassHeader("Simple GameController Example", openClose = false)] public class vGameController : vMonoBehaviour { [System.Serializable] public class OnRealoadGame : UnityEngine.Events.UnityEvent { } [vHelpBox("Assign your Character Prefab to be instantiate at the SpawnPoint, leave it unassigned to Restart the Scene instead")] public GameObject playerPrefab; [vHelpBox("Assign a empty transform to spawn the Player to a specific location")] public Transform spawnPoint; [vHelpBox("Time to wait until the scene restart or the player will be spawned again")] public float respawnTimer = 4f; [vHelpBox("Check this if you want to destroy the dead body after the respawn")] public bool destroyBodyAfterDead; [vHelpBox("Display a message using the FadeText UI")] public bool displayInfoInFadeText = true; [HideInInspector] public OnRealoadGame OnReloadGame = new OnRealoadGame(); [HideInInspector] public GameObject currentPlayer; private vThirdPersonController currentController; public static vGameController instance; private GameObject oldPlayer; public UnityEvent onSpawn; public bool dontDestroyOnLoad = true; protected virtual void Start() { if (instance == null) { instance = this; if (dontDestroyOnLoad) { DontDestroyOnLoad(this.gameObject); } this.gameObject.name = gameObject.name + " Instance"; } else { Destroy(this.gameObject); return; } SceneManager.sceneLoaded += OnLevelFinishedLoading; if (displayInfoInFadeText && vHUDController.instance) { vHUDController.instance.ShowText("Init Scene"); } FindPlayer(); } /// /// Show/Hide Cursor /// /// public virtual void ShowCursor(bool value) { Cursor.visible = value; } /// /// Lock/Unlock the cursor to the center of screen /// /// public virtual void LockCursor(bool value) { if (value) { Cursor.lockState = CursorLockMode.Locked; } else { Cursor.lockState = CursorLockMode.None; } } protected virtual void OnCharacterDead(GameObject _gameObject) { oldPlayer = _gameObject; if (playerPrefab != null) { StartCoroutine(RespawnRoutine()); } else { if (displayInfoInFadeText && vHUDController.instance) { vHUDController.instance.ShowText("Restarting Scene..."); } Invoke("ResetScene", respawnTimer); } } protected virtual IEnumerator RespawnRoutine() { yield return new WaitForSeconds(respawnTimer); if (playerPrefab != null && spawnPoint != null) { if (oldPlayer != null && destroyBodyAfterDead) { if (displayInfoInFadeText && vHUDController.instance) { vHUDController.instance.ShowText("Player destroyed: " + oldPlayer.name.Replace("(Clone)", "").Replace("Instance", "")); } Destroy(oldPlayer); } else { if (displayInfoInFadeText && vHUDController.instance) { vHUDController.instance.ShowText("Remove Player Components: " + oldPlayer.name.Replace("(Clone)", "").Replace("Instance", "")); } DestroyPlayerComponents(oldPlayer); } yield return new WaitForEndOfFrame(); currentPlayer = Instantiate(playerPrefab, spawnPoint.position, spawnPoint.rotation); currentController = currentPlayer.GetComponent(); currentController.onDead.AddListener(OnCharacterDead); if (displayInfoInFadeText && vHUDController.instance) { vHUDController.instance.ShowText("Respawn player: " + currentPlayer.name.Replace("(Clone)", "")); } OnReloadGame.Invoke(); onSpawn.Invoke(); } } protected virtual void OnLevelFinishedLoading(Scene scene, LoadSceneMode mode) { if (currentController == null) { return; } if (currentController.currentHealth > 0) { if (displayInfoInFadeText && vHUDController.instance) { vHUDController.instance.ShowText("Load Scene: " + scene.name); } return; } if (displayInfoInFadeText && vHUDController.instance) { vHUDController.instance.ShowText("Reload Scene"); } OnReloadGame.Invoke(); FindPlayer(); } protected virtual void FindPlayer() { var player = GameObject.FindObjectOfType(); if (player) { currentPlayer = player.gameObject; currentController = player; player.onDead.AddListener(OnCharacterDead); if (displayInfoInFadeText && vHUDController.instance) { vHUDController.instance.ShowText("Found player: " + currentPlayer.name.Replace("(Clone)", "").Replace("Instance", "")); } } else if (currentPlayer == null && playerPrefab != null && spawnPoint != null) { SpawnAtPoint(spawnPoint); } } protected virtual void DestroyPlayerComponents(GameObject target) { if (!target) { return; } var comps = target.GetComponentsInChildren(); for (int i = 0; i < comps.Length; i++) { Destroy(comps[i]); } var coll = target.GetComponent(); if (coll != null) { Destroy(coll); } var rigidbody = target.GetComponent(); if (rigidbody != null) { Destroy(rigidbody); } var animator = target.GetComponent(); if (animator != null) { Destroy(animator); } } /// /// Set a custom spawn point (or use it as checkpoint to your level) /// /// new point to spawn public virtual void SetSpawnSpoint(Transform newSpawnPoint) { spawnPoint = newSpawnPoint; } /// /// Set the default player prefab /// /// public void SetPlayerPrefab(GameObject prefab) { playerPrefab = prefab; } /// /// Spawn New Player at a specific point /// /// Point to spawn player public virtual void SpawnAtPoint(Transform targetPoint) { if (playerPrefab != null) { if (oldPlayer != null && destroyBodyAfterDead) { if (displayInfoInFadeText && vHUDController.instance) { vHUDController.instance.ShowText("Player destroyed: " + oldPlayer.name.Replace("(Clone)", "").Replace("Instance", "")); } Destroy(oldPlayer); } else if (oldPlayer != null) { if (displayInfoInFadeText && vHUDController.instance) { vHUDController.instance.ShowText("Remove Player Components: " + oldPlayer.name.Replace("(Clone)", "").Replace("Instance", "")); } DestroyPlayerComponents(oldPlayer); } currentPlayer = Instantiate(playerPrefab, targetPoint.position, targetPoint.rotation); currentController = currentPlayer.GetComponent(); currentController.onDead.AddListener(OnCharacterDead); OnReloadGame.Invoke(); if (displayInfoInFadeText && vHUDController.instance) { vHUDController.instance.ShowText("Spawn player: " + currentPlayer.name.Replace("(Clone)", "")); } } } /// /// Spawn player /// /// public virtual void SpawnPlayer(GameObject prefab) { if (prefab != null && spawnPoint != null) { var targetPoint = spawnPoint; if (oldPlayer != null && destroyBodyAfterDead) { if (displayInfoInFadeText && vHUDController.instance) { vHUDController.instance.ShowText("Player destroyed: " + oldPlayer.name.Replace("(Clone)", "").Replace("Instance", "")); } Destroy(oldPlayer); } else if (oldPlayer != null) { if (displayInfoInFadeText && vHUDController.instance) { vHUDController.instance.ShowText("Remove Player Components: " + oldPlayer.name.Replace("(Clone)", "").Replace("Instance", "")); } DestroyPlayerComponents(oldPlayer); } currentPlayer = Instantiate(prefab, targetPoint.position, targetPoint.rotation); currentController = currentPlayer.GetComponent(); currentController.onDead.AddListener(OnCharacterDead); OnReloadGame.Invoke(); if (displayInfoInFadeText && vHUDController.instance) { vHUDController.instance.ShowText("Spawn player: " + currentPlayer.name.Replace("(Clone)", "")); } } } /// /// Reload current Scene and current Player /// public virtual void ResetScene() { if (oldPlayer) { DestroyPlayerComponents(oldPlayer); } var scene = SceneManager.GetActiveScene(); SceneManager.LoadScene(scene.name); if (oldPlayer && destroyBodyAfterDead) { Destroy(oldPlayer); } } } }