using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using UnityEngine.SceneManagement; using UnityEngine.EventSystems; public class GameEngine : MonoBehaviour { [Header("Game Attributes")] public GameObject currentBlackHole; public GameObject currentPlayer; public static GameObject mainPlayer; private PlayerHoverMovement _playerHoverMovement; private BlackHoleManager _currentBlackHoleManager; public float arenaSize; [Header("Sound Elements")] public BackgroundMusicManager currentBGMManager; public MenuAudioManager currentMenuAudioManager; //public TMPro.TextMeshProUGUI coreCountText; [Header("Gameplay UI Elements")] public GameObject livesBar; public GameObject coresText; public GameObject coresBar; public GameObject coresFillBar; public GameObject boostStatus; public GameObject scoreText; public GameObject levelText; [Header("Pause Menu Elements")] public TMPro.TextMeshProUGUI versionText; public GameObject pauseMenu; public GameObject mainPausePanel; public GameObject optionsPanel; public GameObject confirmPanel; public static bool isPaused = false; public GameObject pauseMenuFirstItem; private GameObject _lastControlSelected; // Start is called before the first frame update void Awake() { // Make sure godmode is off when starting. GameManager.Instance.currentGameState = GameState.GAME; GameManager.Instance.godMode = false; GameManager.Instance.SetupLevel(); // Get the current blackhole so we have easy access to its variables _currentBlackHoleManager = currentBlackHole.GetComponent(); } private void Start() { // Start the game, make sure the play GO is active currentPlayer.SetActive(true); if (currentBGMManager) { currentBGMManager.StartAudioQueueAndPlay(0); } _playerHoverMovement = currentPlayer.GetComponent(); versionText.text = versionText.text.Replace("{versionNo}", GameManager.Instance.CurrentVersion); mainPlayer = currentPlayer; } /// /// Gets the current blackhole radius /// /// Current Blackhole size as a float public float GetCurrentBlackHoleRadius() { return _currentBlackHoleManager.currentBlackHoleRadius; } private void UpdateLifeBar() { if (livesBar != null) { for (int i = 0; i < 6; i++) { Transform currentLife = livesBar.transform.Find("Life" + i.ToString()); if (currentLife != null) { if (i <= GameManager.Instance.CurrentLives) { currentLife.gameObject.SetActive(true); } else { currentLife.gameObject.SetActive(false); } } } Transform lifeOverflow = livesBar.transform.Find("LifeOverflow"); if (lifeOverflow != null) { if (GameManager.Instance.CurrentLives > 5) { lifeOverflow.gameObject.SetActive(true); lifeOverflow.GetComponent().text = "(+" + (GameManager.Instance.CurrentLives - 5).ToString() + ")"; } else { lifeOverflow.GetComponent().text = "(+0)"; lifeOverflow.gameObject.SetActive(false); } } } } private void UpdateCoresBar() { float corePowerPercent = (float)GameManager.Instance.CurrentCores / (float)GameManager.Instance.CurrentNeededCores; if (coresText != null) { coresText.GetComponent().text = "Core Power: " + Mathf.RoundToInt(corePowerPercent * 100.0f) + "%"; } if (coresBar != null) { if (coresFillBar != null) { RectTransform fillBarImage = coresFillBar.GetComponent(); RectTransform parentBar = fillBarImage.parent.GetComponent(); float leftOffset = parentBar.rect.size.x - (parentBar.rect.size.x * corePowerPercent); fillBarImage.offsetMin = new Vector2(leftOffset, 0); //fillBarImage.SetInsetAndSizeFromParentEdge(RectTransform.Edge.Left, 0, leftOffset); } //for (int i = 0; i < 6; i++) //{ // Transform currentCore = coresBar.transform.Find("Core" + i.ToString()); // if (currentCore != null) // { // if (i <= GameManager.Instance.CurrentCores) // { // currentCore.gameObject.SetActive(true); // } // else // { // currentCore.gameObject.SetActive(false); // } // } //} //Transform coreOverflow = coresBar.transform.Find("CoreOverflow"); //if (coreOverflow != null) //{ // if (GameManager.Instance.CurrentCores > 5) // { // coreOverflow.gameObject.SetActive(true); // coreOverflow.GetComponent().text = "(+" + (GameManager.Instance.CurrentCores - 5).ToString() + ")"; // } // else // { // coreOverflow.GetComponent().text = "(+0)"; // coreOverflow.gameObject.SetActive(false); // } //} } } private void UpdateScore() { if (scoreText != null) { scoreText.GetComponent().text = GameManager.Instance.CurrentScore.ToString().PadLeft(10, '0'); } } private void UpdateLevel() { if (levelText != null) { levelText.GetComponent().text = (GameManager.Instance.CurrentLevel + 1).ToString().PadLeft(2, '0'); } } private void UpdateBoostStatus() { if (boostStatus != null) { if (_playerHoverMovement != null) { if (_playerHoverMovement.BoostReady) { boostStatus.SetActive(true); } else { boostStatus.SetActive(false); } } } } // Update is called once per frame void LateUpdate() { UpdateLifeBar(); UpdateCoresBar(); UpdateScore(); UpdateLevel(); UpdateBoostStatus(); if (Input.GetButtonDown("Cancel")) { if (!isPaused) { isPaused = true; pauseMenu.SetActive(true); EventSystem.current.SetSelectedGameObject(pauseMenuFirstItem); _lastControlSelected = pauseMenuFirstItem; Time.timeScale = 0; } else { UnpauseGame(); } } if (isPaused) { float horzLRot = -Input.GetAxis("Horizontal"); if (horzLRot != 0) { if (EventSystem.current.currentSelectedGameObject == null) { EventSystem.current.SetSelectedGameObject(_lastControlSelected); } else { _lastControlSelected = EventSystem.current.currentSelectedGameObject; } } } } public void UnpauseGame() { isPaused = false; mainPausePanel.SetActive(true); optionsPanel.SetActive(false); confirmPanel.SetActive(false); pauseMenu.SetActive(false); Time.timeScale = 1.0f; } public void SetupVolumeOptions() { // Gets the volume settings from the player profile and sets it in the game float currentMasterVol = PlayerPrefs.GetFloat("currentMasterVol"); GameObject masterVolSliderGo = GameObject.Find("MainVolSlider"); float currentMusicVol = PlayerPrefs.GetFloat("currentMusicVol"); GameObject musicVolSliderGo = GameObject.Find("MusicVolSlider"); float currentSFXVol = PlayerPrefs.GetFloat("currentSFXVol"); GameObject SFXVolSliderGo = GameObject.Find("SfxVolSlider"); masterVolSliderGo.GetComponent().value = currentMasterVol; musicVolSliderGo.GetComponent().value = currentMusicVol; SFXVolSliderGo.GetComponent().value = currentSFXVol; } public void PlayMenuClick() { if (currentMenuAudioManager) { currentMenuAudioManager.PlayMenuClick(); } } public void ResumeGame() { // Play the menu click sound if the audio manager is present if (currentMenuAudioManager) { currentMenuAudioManager.PlayMenuClick(); } UnpauseGame(); } public void Quit() { // Play the menu click sound if the audio manager is present if (currentMenuAudioManager) { currentMenuAudioManager.PlayMenuClick(); } Time.timeScale = 1.0f; SceneManager.LoadSceneAsync("MainMenu"); } public void SetMasterVolume(float value) { // Converts the master volume into decibels and stores it in the player profile GameManager.Instance.currentAudioMixer.SetFloat("masterVol", SharedMethods.ConvertToDecibels(value)); PlayerPrefs.SetFloat("currentMasterVol", value); } public void SetMusicVolume(float value) { // Converts the music volume into decibels and stores it in the player profile GameManager.Instance.currentAudioMixer.SetFloat("musicVol", SharedMethods.ConvertToDecibels(value)); PlayerPrefs.SetFloat("currentMusicVol", value); } public void SetSFXVolume(float value) { // Converts the sfx volume into decibels and stores it in the player profile GameManager.Instance.currentAudioMixer.SetFloat("sfxVol", SharedMethods.ConvertToDecibels(value)); PlayerPrefs.SetFloat("currentSFXVol", value); } }