using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
using TMPro;
public class GameManager : MonoBehaviour
{
//Defines instance for the game manager
public static GameManager Instance { get; private set; }
//Defines UI objects
public GameObject gameOverUI;
public GameObject winScreenUI;
public Timer timer;
public TMP_Text coinsCollectedTMPText;
//Defines save manager and player object
public SaveManager saveManager;
public GameObject player;
//Define constants
private const float gameOverTime = 500f;
private const int winCoinCount = 100;
const string TOWN_TEXT = "Core Nexus";
//Define other variables
public int coinsCollected = 0;
public int totalEnemies = 0;
public int totalDestructibles = 0;
public int enemiesDestroyed = 0;
public int destructiblesDestroyed = 0;
///
/// This method runs when the game is first awake and it
/// basically finds the player object and sets the camera
/// to follow the player.
///
private void Awake()
{
//Don't destory the player object if instance is null
if (Instance == null)
{
Instance = this;
DontDestroyOnLoad(gameObject);
}
//Destory the player object if instance is not null
else
{
Destroy(gameObject);
}
//Set camera to follow the player
CameraController.Instance.SetPlayerCameraFollow();
}
///
/// This method runs when the game first starts and
/// it set the UI menus to false so they don't show yet.
///
private void Start(){
gameOverUI.SetActive(false);
winScreenUI.SetActive(false);
}
///
/// This method is called whenever an enemy is destroyed
///
public void EnemyDestroyed()
{
enemiesDestroyed++;
CheckWinCondition();
}
///
/// This method is called whenever a destructible object is destroyed
///
public void DestructibleDestroyed()
{
destructiblesDestroyed++;
CheckWinCondition();
}
///
/// This method checks if all enemies and destructibles are destroyed
///
public void CheckWinCondition()
{
if (enemiesDestroyed >= totalEnemies && destructiblesDestroyed >= totalDestructibles)
{
Win();
}
}
///
/// This method constantly updates throughout the game and
/// it calls on different method within this script.
///
private void Update()
{
//Check if the timer has reached the game over time
if (timer.timeRemaining >= gameOverTime)
{
gameOver();
}
//Check for key presses to save and load the game
if (Input.GetKeyDown(KeyCode.K))
{
SaveGame();
}
if (Input.GetKeyDown(KeyCode.L))
{
LoadGame();
}
//Check for key press to quit the game
if (Input.GetKeyDown(KeyCode.Escape))
{
mainMenu();
}
}
///
/// This method is for when the player collects the coins.
///
public void CollectCoin()
{
//Add collected coins to coin count of the player
coinsCollected++;
//Player wins if coins collected are more than the win coin count
if (coinsCollected >= winCoinCount)
{
Win();
}
}
///
/// This method is for when the player loses and gets
/// the game over screen meaning player needs to start
/// the game again.
///
public void gameOver(){
//Show game over screen
gameOverUI.SetActive(true);
//Stop timer
timer.timeIsRunning = false;
}
///
/// This method is for when the player wins and the
/// win screen shows to the player.
///
public void Win()
{
//Show win screen
winScreenUI.SetActive(true);
//Stop timer
timer.timeIsRunning = false;
float timeTaken = timer.timeRemaining;
// Set the text to display only the number of coins collected using TextMeshPro
coinsCollectedTMPText.text = "Coins Collected: " + coinsCollected;
}
///
/// This method restarts the game back to the start.
///
public void restart(){
//Deactivate UI Screens
gameOverUI.SetActive(false);
winScreenUI.SetActive(false);
//Reset timer, coin count
timer.timeRemaining = 0f;
timer.timeIsRunning = true;
coinsCollected = 0;
//Load Scene 1/Level 1 of the game
SceneManager.LoadScene("Core Nexus");
Debug.Log("Restart!!!");
}
///
/// This method takes the player back to the main menu
/// also known as Command Centre.
///
public void mainMenu(){
//Set game over and win screen to false
gameOverUI.SetActive(false);
winScreenUI.SetActive(false);
//Load Main Menu scene
SceneManager.LoadScene("Command Centre");
Debug.Log("Command Centre!!!");
}
///
/// This method Saves the game.
///
public void SaveGame()
{
// ave the game state before going to the main menu
PlayerHealth playerHealth = player.GetComponent();
if (playerHealth != null)
{
//Save these game components/variables
int playerCoins = coinsCollected;
int playerCurrentHealth = (int)playerHealth.currentHealth;
int playerStaminaLevel = 0;
//Use Save Game method of the save manager to save game
saveManager.SaveGame(playerCoins, playerCurrentHealth, playerStaminaLevel, player.transform.position);
}
}
///
/// This method Loads the game.
///
public void LoadGame()
{
//Load the game data
SaveData saveData = saveManager.LoadGame();
if (saveData != null)
{
//Update GameManager or PlayerHealth with loaded data
PlayerHealth playerHealth = player.GetComponent();
if (playerHealth != null)
{
playerHealth.currentHealth = saveData.currentHealth;
playerHealth.SetPlayerPosition(saveData.playerPosition.ToVector3());
}
//Load the coins collected by the player
coinsCollected = saveData.coins;
Debug.Log("Game Loaded");
}
else
{
//Show error if the game could not be loaded because save data wasn't found
Debug.LogWarning("No save data found.");
}
}
///
/// This method quits the game.
///
public void quit()
{
Application.Quit();
Debug.Log("Quit!!!");
}
}