private void SaveCharactersInInitalState(GameCharacter[] characters) { GameCharacter[] storedCharacters = new GameCharacter[characters.Length]; for (int i = 0; i < characters.Length; ++i) { GameCharacter character = characters[i]; bool isActive = character.gameObject.activeSelf; character.gameObject.SetActive(false); GameCharacter stored = Instantiate(character, character.transform.position, character.transform.rotation); stored.name = character.name; character.gameObject.SetActive(isActive); ExposeToEditor[] exposeToEditor = stored.GetComponentsInChildren <ExposeToEditor>(); foreach (ExposeToEditor obj in exposeToEditor) { obj.MarkAsDestroyed = true; } stored.transform.SetParent(transform); storedCharacters[i] = stored; } m_storedCharacters = storedCharacters; }
public void OnPlayerDie(GameCharacter gameCharacter) { m_gameOver = true; m_activeCharacters.Remove(gameCharacter); TxtTip.gameObject.SetActive(false); StartCoroutine(ShowText("Game Over!")); for (int i = 0; i < m_activeCharacters.Count; ++i) { m_activeCharacters[i].HandleInput = false; } }
private void DestroyStoredCharacters() { if (m_storedCharacters != null) { for (int i = 0; i < m_storedCharacters.Length; ++i) { GameCharacter storedCharacter = m_storedCharacters[i]; if (storedCharacter != null) { Destroy(storedCharacter.gameObject); } } } }
public void OnPlayerFinish(GameCharacter gameCharacter) { m_score++; UpdateScore(); SwitchPlayer(gameCharacter, 1.0f, true); m_activeCharacters.Remove(gameCharacter); if (IsGameCompleted()) { m_gameOver = true; TxtTip.gameObject.SetActive(false); StartCoroutine(ShowText("Congratulation! \n You have completed a great game ")); } }
private void RestartGame() { if (m_activeCharacters != null) { for (int i = 0; i < m_activeCharacters.Count; ++i) { GameCharacter activeCharacter = m_activeCharacters[i]; Destroy(activeCharacter.gameObject); } } GameCharacter[] characters = m_storedCharacters; SaveCharactersInInitalState(characters); InitializeGame(characters, -1); }
public void SwitchPlayer(GameCharacter current, float delay, bool next) { if (m_gameOver) { return; } if (current != null) { current.HandleInput = false; m_currentIndex = m_activeCharacters.IndexOf(current); if (next) { m_currentIndex++; if (m_currentIndex >= m_activeCharacters.Count) { m_currentIndex = 0; } } else { m_currentIndex--; if (m_currentIndex < 0) { m_currentIndex = m_activeCharacters.Count - 1; } } } if (m_currentIndex < 0) { m_currentIndex = 0; } m_current = m_activeCharacters[m_currentIndex]; if (current == null) { ActivatePlayer(); } else { StartCoroutine(ActivateNextPlayer(delay)); } }
private void StopGame() { RestartGame(); for (int i = 0; i < m_activeCharacters.Count; ++i) { GameCharacter character = m_activeCharacters[i]; character.IsActive = false; } if (m_playerCamera != null) { m_playerCamera.target = null; } if (GameUI != null) { GameUI.SetActive(false); } }
private void InitializeGame(GameCharacter[] characters, int activeCharacterIndex) { m_gameOver = false; if (m_cameraFollow) { m_playerCamera = FindObjectOfType <GameCameraFollow>(); if (m_playerCamera == null) { if (Camera.main != null) { m_playerCamera = Camera.main.gameObject.AddComponent <GameCameraFollow>(); } } if (m_playerCamera != null) { Canvas canvas = GetComponentInChildren <Canvas>(); canvas.renderMode = RenderMode.ScreenSpaceCamera; Camera cam = m_playerCamera.GetComponent <Camera>(); canvas.worldCamera = cam; canvas.planeDistance = cam.nearClipPlane + 0.05f; } } m_activeCharacters = new List <GameCharacter>(); for (int i = 0; i < characters.Length; ++i) { GameCharacter character = characters[i]; character.transform.SetParent(null); character.gameObject.SetActive(true); character.HandleInput = false; ExposeToEditor exposeToEditor = character.GetComponent <ExposeToEditor>(); if (!exposeToEditor) { character.gameObject.AddComponent <ExposeToEditor>(); } else { ExposeToEditor[] children = character.GetComponentsInChildren <ExposeToEditor>(true); for (int j = 0; j < children.Length; ++j) { ExposeToEditor child = children[j]; child.MarkAsDestroyed = false; } } m_activeCharacters.Add(character); } m_total = m_activeCharacters.Count; m_score = 0; if (m_total == 0) { TxtCompleted.gameObject.SetActive(true); TxtScore.gameObject.SetActive(false); TxtTip.gameObject.SetActive(false); TxtCompleted.text = "Game Over!"; m_gameOver = true; } else { TxtCompleted.gameObject.SetActive(false); TxtScore.gameObject.SetActive(true); TxtTip.gameObject.SetActive(true); UpdateScore(); if (activeCharacterIndex >= 0) { m_current = m_activeCharacters[activeCharacterIndex]; if (m_current != null) { ActivatePlayer(); } else { SwitchPlayer(null, 0.0f, true); } } else { SwitchPlayer(null, 0.0f, true); } } }