Space-Smash-Out/Assets/Scripts/GameManager.cs

55 lines
1.0 KiB
C#

using UnityEngine;
using UnityEngine.SceneManagement;
public class GameManager : MonoBehaviour
{
[SerializeField] ZoneRules zone;
[SerializeField] Announcments announcements;
private bool gameLost = false;
private float restartMatchTime = 0;
void Update()
{
if (restartMatchTime > 0)
{
restartMatchTime -= Time.deltaTime;
}
else if (gameLost)
{
restartMatchTime = 0;
StartNewMatch();
}
}
// Start is called before the first frame update
void Start()
{
if (zone != null)
{
zone.onPlayZoneExited += CheckLosingCondition;
}
if (announcements != null)
{
announcements.AnnounceText("Match Start", 1.6f);
}
}
private void CheckLosingCondition(GameObject go)
{
if (!go.CompareTag("Player") || gameLost)
return;
gameLost = true;
Destroy(go);
announcements.AnnounceText(go.name + " has lost the match", 2f);
restartMatchTime = 2.2f;
}
private void StartNewMatch()
{
string currentSceneName = SceneManager.GetActiveScene().name;
SceneManager.LoadScene(currentSceneName);
}
}