139 lines
2.6 KiB
C#
139 lines
2.6 KiB
C#
using System;
|
|
using TMPro;
|
|
using UnityEngine;
|
|
|
|
public class GameManager : MonoBehaviour
|
|
{
|
|
#region Statication
|
|
|
|
public static GameManager instance;
|
|
|
|
private void Awake()
|
|
{
|
|
if (instance != null && instance != this)
|
|
{
|
|
Destroy(gameObject);
|
|
return;
|
|
}
|
|
instance = this;
|
|
DontDestroyOnLoad(this);
|
|
}
|
|
|
|
#endregion
|
|
|
|
[Header("Game Stats")]
|
|
public int continues;
|
|
public string time;
|
|
public int wave;
|
|
public int deflections;
|
|
public int projectilesShot;
|
|
public int projectilesHit;
|
|
public string accuracy;
|
|
public int livesLost;
|
|
[Header("Pause UI")]
|
|
public bool canPause = true;
|
|
public bool isEndless = false;
|
|
[Header("Game Over")]
|
|
[SerializeField] private DialogueScript winCutscene;
|
|
public bool gameOver;
|
|
|
|
public void EndGame()
|
|
{
|
|
SetPause(true);
|
|
gameOver = true;
|
|
canPause = false;
|
|
time = TimeSpan.FromSeconds(Time.timeSinceLevelLoad).ToString(@"mm\:ss");
|
|
wave = WaveManager.instance.wave-1;
|
|
WaveManager.instance.musicSource.Stop();
|
|
accuracy = ((float)projectilesHit / projectilesShot).ToString(@"P");
|
|
}
|
|
public void LoseGame()
|
|
{
|
|
WaveManager.instance.loseUI.SetActive(true);
|
|
EndGame();
|
|
WaveManager.instance.statsUI.SetUI();
|
|
}
|
|
|
|
public void WinGame()
|
|
{
|
|
EndGame();
|
|
LevelSwitcher.instance.SwitchLevel(2, winCutscene);
|
|
SetPause(false);
|
|
}
|
|
|
|
public void ExitGame()
|
|
{
|
|
LevelSwitcher.instance.SwitchLevel(0);
|
|
SetPause(false);
|
|
canPause = true;
|
|
ClearStats();
|
|
}
|
|
|
|
public void RestartGame()
|
|
{
|
|
LevelSwitcher.instance.SwitchLevel(1);
|
|
canPause = true;
|
|
SetPause(false);
|
|
ClearStats();
|
|
}
|
|
|
|
public void ContinueGame()
|
|
{
|
|
gameOver = false;
|
|
canPause = true;
|
|
WaveManager.instance.loseUI.SetActive(false);
|
|
SetPause(false);
|
|
WaveManager.instance.musicSource.Play();
|
|
WaveManager.instance.player.health = WaveManager.instance.player.maxHealth;
|
|
continues++;
|
|
}
|
|
|
|
private void ClearStats()
|
|
{
|
|
continues = 0;
|
|
wave = 0;
|
|
projectilesHit = 0;
|
|
projectilesShot = 0;
|
|
accuracy = "";
|
|
deflections = 0;
|
|
livesLost = 0;
|
|
time = "";
|
|
}
|
|
|
|
public void SetPause(bool state)
|
|
{
|
|
if (state)
|
|
{
|
|
Time.timeScale = 0f;
|
|
}
|
|
else
|
|
{
|
|
Time.timeScale = 1f;
|
|
}
|
|
}
|
|
|
|
public void SetPauseMenu()
|
|
{
|
|
if (!gameOver && canPause && WaveManager.instance)
|
|
{
|
|
if (!WaveManager.instance.pauseMenu.activeSelf)
|
|
{
|
|
WaveManager.instance.pauseMenu.SetActive(true);
|
|
SetPause(true);
|
|
}
|
|
else
|
|
{
|
|
WaveManager.instance.pauseMenu.SetActive(false);
|
|
SetPause(false);
|
|
}
|
|
}
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
if (Input.GetKeyDown(KeyCode.Escape) && !DialogueManager.instance.dialogueActive) //we need to convert this to a better input system
|
|
{
|
|
SetPauseMenu();
|
|
}
|
|
}
|
|
}
|