Files
blackhole-escape/Assets/Scripts/Scripts/GameEngine.cs
iDunnoDev 3ab4b78a79 Added Accessors for the GameManager to get the Score, Cores and Level values
Added a Score to the Game
Added an actual blackhole material and shader to the black hole
Added a new Main menu and How to Play menu system
Added Beam swords to all ships (replacing the light saber things the player had)
Added a pause menu
Overhauled the UI for the game scene
Added List to hold the amount of core energy needed for each level
Added the URP package for better rendering (apparently)
Added a GameState enum to set which part of the game the player is in
Added Magnet powerup to game
Changed the way powerups are spawned, enemies now have a loot table
Changed cores to core energy which is a percentage needed to pass a level
Changed all the Ints to Hidden Value ints to maybe stop cheat engine users finding the important values
Changed all level loads to use sceneloadasync
Updated all of the materials to use URP shaders
Removed Junk Files from external sources
Rearranged the folders inside the unity project to try and reduce some name length issues
2022-06-30 01:09:48 +01:00

320 lines
10 KiB
C#

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<BlackHoleManager>();
}
private void Start()
{
// Start the game, make sure the play GO is active
currentPlayer.SetActive(true);
if (currentBGMManager)
{
currentBGMManager.StartAudioQueueAndPlay(0);
}
_playerHoverMovement = currentPlayer.GetComponent<PlayerHoverMovement>();
versionText.text = versionText.text.Replace("{versionNo}", GameManager.Instance.CurrentVersion);
mainPlayer = currentPlayer;
}
/// <summary>
/// Gets the current blackhole radius
/// </summary>
/// <returns>Current Blackhole size as a float</returns>
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<TMPro.TextMeshProUGUI>().text = "(+" + (GameManager.Instance.CurrentLives - 5).ToString() + ")";
}
else
{
lifeOverflow.GetComponent<TMPro.TextMeshProUGUI>().text = "(+0)";
lifeOverflow.gameObject.SetActive(false);
}
}
}
}
private void UpdateCoresBar()
{
float corePowerPercent = (float)GameManager.Instance.CurrentCores / (float)GameManager.Instance.CurrentNeededCores;
if (coresText != null)
{
coresText.GetComponent<TMPro.TextMeshProUGUI>().text = "Core Power: " + Mathf.RoundToInt(corePowerPercent * 100.0f) + "%";
}
if (coresBar != null)
{
if (coresFillBar != null)
{
RectTransform fillBarImage = coresFillBar.GetComponent<RectTransform>();
RectTransform parentBar = fillBarImage.parent.GetComponent<RectTransform>();
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<TMPro.TextMeshProUGUI>().text = "(+" + (GameManager.Instance.CurrentCores - 5).ToString() + ")";
// }
// else
// {
// coreOverflow.GetComponent<TMPro.TextMeshProUGUI>().text = "(+0)";
// coreOverflow.gameObject.SetActive(false);
// }
//}
}
}
private void UpdateScore()
{
if (scoreText != null)
{
scoreText.GetComponent<TMPro.TextMeshProUGUI>().text = GameManager.Instance.CurrentScore.ToString().PadLeft(10, '0');
}
}
private void UpdateLevel()
{
if (levelText != null)
{
levelText.GetComponent<TMPro.TextMeshProUGUI>().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<Slider>().value = currentMasterVol;
musicVolSliderGo.GetComponent<Slider>().value = currentMusicVol;
SFXVolSliderGo.GetComponent<Slider>().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);
}
}