154 lines
3.1 KiB
C#
154 lines
3.1 KiB
C#
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
public class GameManager : MonoBehaviour
|
|
{
|
|
public CaptureZone[] zones;
|
|
public TerritoryUI territoryUI;
|
|
public GameObject winPanel;
|
|
public Text winText;
|
|
public int scoreLimit = 100;
|
|
|
|
private bool gameOver;
|
|
|
|
private void Start()
|
|
{
|
|
Time.timeScale = 1f;
|
|
|
|
if (territoryUI != null)
|
|
{
|
|
territoryUI.HideGameOver();
|
|
}
|
|
|
|
SubscribeToZones();
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
if (gameOver)
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (territoryUI != null)
|
|
{
|
|
territoryUI.UpdateZoneStatus(zones);
|
|
}
|
|
|
|
CheckVictoryConditions();
|
|
}
|
|
|
|
private void SubscribeToZones()
|
|
{
|
|
if (zones == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
for (int i = 0; i < zones.Length; i++)
|
|
{
|
|
if (zones[i] == null)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
zones[i].onCapturedByPlayer += OnZoneCaptured;
|
|
zones[i].onCapturedByEnemy += OnZoneCaptured;
|
|
}
|
|
}
|
|
|
|
private void OnDestroy()
|
|
{
|
|
if (zones == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
for (int i = 0; i < zones.Length; i++)
|
|
{
|
|
if (zones[i] == null)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
zones[i].onCapturedByPlayer -= OnZoneCaptured;
|
|
zones[i].onCapturedByEnemy -= OnZoneCaptured;
|
|
}
|
|
}
|
|
|
|
private void OnZoneCaptured(CaptureZone zone)
|
|
{
|
|
CheckVictoryConditions();
|
|
}
|
|
|
|
private void CheckVictoryConditions()
|
|
{
|
|
if (zones == null || zones.Length == 0)
|
|
{
|
|
return;
|
|
}
|
|
|
|
bool allPlayer = true;
|
|
bool allEnemy = true;
|
|
|
|
for (int i = 0; i < zones.Length; i++)
|
|
{
|
|
if (zones[i] == null)
|
|
{
|
|
allPlayer = false;
|
|
allEnemy = false;
|
|
continue;
|
|
}
|
|
|
|
allPlayer &= zones[i].GetOwner() == CaptureZone.Owner.Player;
|
|
allEnemy &= zones[i].GetOwner() == CaptureZone.Owner.Enemy;
|
|
}
|
|
|
|
if (allPlayer)
|
|
{
|
|
GameOver("Победа игрока: все зоны захвачены!");
|
|
return;
|
|
}
|
|
|
|
if (allEnemy)
|
|
{
|
|
GameOver("Победа врага: все зоны потеряны!");
|
|
return;
|
|
}
|
|
|
|
if (ResourceManager.Instance != null && ResourceManager.Instance.playerScore >= scoreLimit)
|
|
{
|
|
GameOver("Победа игрока: достигнут лимит очков!");
|
|
}
|
|
}
|
|
|
|
private void GameOver(string message)
|
|
{
|
|
if (gameOver)
|
|
{
|
|
return;
|
|
}
|
|
|
|
gameOver = true;
|
|
|
|
if (territoryUI != null)
|
|
{
|
|
territoryUI.ShowGameOver(message);
|
|
}
|
|
else
|
|
{
|
|
if (winPanel != null)
|
|
{
|
|
winPanel.SetActive(true);
|
|
}
|
|
|
|
if (winText != null)
|
|
{
|
|
winText.text = message;
|
|
}
|
|
}
|
|
|
|
Time.timeScale = 0f;
|
|
}
|
|
}
|