130 lines
4.2 KiB
C#
130 lines
4.2 KiB
C#
using CharacterSystem;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.Serialization;
|
|
|
|
public class EndGameHandler : MonoBehaviour
|
|
{
|
|
[SerializeField, FormerlySerializedAs("winPanel")] private PveWinPanel _winPanel;
|
|
[SerializeField, FormerlySerializedAs("lossPanel")] private PveLossPanel _lossPanel;
|
|
[SerializeField, FormerlySerializedAs("pvpWinPanel")] private PvpWinPanel _pvpWinPanel;
|
|
[SerializeField, FormerlySerializedAs("drawPanel")] private PveLossPanel _drawPanel;
|
|
|
|
private const float WinAnimationFrameRate = 40f;
|
|
|
|
private Coroutine _winAnimationCoroutine;
|
|
|
|
private void OnDisable()
|
|
{
|
|
if (_winAnimationCoroutine != null)
|
|
{
|
|
StopCoroutine(_winAnimationCoroutine);
|
|
_winAnimationCoroutine = null;
|
|
}
|
|
}
|
|
|
|
public void HandleEndGame(Team lastPlayer, GameMode gameMode, CheckResult lastResult)
|
|
{
|
|
Debug.Log("Конец игры");
|
|
GameManager.Instance.turnTracker.EnableTimer(false);
|
|
|
|
if (lastResult.Draw)
|
|
DrawHandler(gameMode);
|
|
else
|
|
WinHandler(lastPlayer, gameMode, lastResult.WinningIndexes);
|
|
}
|
|
|
|
private void DrawHandler(GameMode gameMode)
|
|
{
|
|
if (gameMode == GameMode.pve)
|
|
{
|
|
EventBus.TriggerEvent(GameEvent.Draw);
|
|
_drawPanel.Open(GameManager.Instance.ContinueAfterPveDraw);
|
|
}
|
|
else
|
|
{
|
|
GameManager gameManager = GameManager.Instance;
|
|
_pvpWinPanel.Open(
|
|
Team.None,
|
|
false,
|
|
gameManager.player1score,
|
|
gameManager.player2score,
|
|
gameManager.ContinueAfterPvpRound);
|
|
}
|
|
}
|
|
|
|
private void WinHandler(Team winner, GameMode gameMode, HashSet<(int row, int col)> winChipsCoords)
|
|
{
|
|
Debug.Log($"Победа {winner}; Режим {gameMode}");
|
|
PlayWinChipsAnimation(winChipsCoords);
|
|
|
|
if (gameMode == GameMode.pve)
|
|
{
|
|
if (winner == Team.Player1)
|
|
{
|
|
MachineCountersService.RecordBotResult(true);
|
|
EventBus.TriggerEvent(GameEvent.PlayerWin);
|
|
_winPanel.Open();
|
|
}
|
|
else
|
|
{
|
|
MachineCountersService.RecordBotResult(false);
|
|
EventBus.TriggerEvent(GameEvent.BotWin);
|
|
_lossPanel.Open(GameManager.Instance.ContinueAfterPveLoss);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
GameManager gameManager = GameManager.Instance;
|
|
DualAvatarController.Instance?.PlayWin(winner);
|
|
gameManager.IncreaseScore(winner);
|
|
bool isMatchWin = gameManager.HasPvpMatchWinner(winner);
|
|
_pvpWinPanel.Open(
|
|
winner,
|
|
isMatchWin,
|
|
gameManager.player1score,
|
|
gameManager.player2score,
|
|
isMatchWin ? gameManager.RestartPvpMatch : gameManager.ContinueAfterPvpRound);
|
|
}
|
|
}
|
|
|
|
private void PlayWinChipsAnimation(HashSet<(int row, int col)> winChipsCoords)
|
|
{
|
|
Chip[,] grid = Board.Instance.Grid;
|
|
Chip[] winChips = new Chip[winChipsCoords.Count];
|
|
int chipIndex = 0;
|
|
|
|
foreach ((int row, int col) winCoord in winChipsCoords)
|
|
{
|
|
Chip chip = grid[winCoord.col, winCoord.row];
|
|
winChips[chipIndex++] = chip;
|
|
chip.WinAnimation();
|
|
}
|
|
|
|
if (winChips.Length == 0)
|
|
return;
|
|
|
|
if (_winAnimationCoroutine != null)
|
|
StopCoroutine(_winAnimationCoroutine);
|
|
|
|
_winAnimationCoroutine = StartCoroutine(PlayWinDotsAnimation(winChips));
|
|
}
|
|
|
|
private IEnumerator PlayWinDotsAnimation(Chip[] winChips)
|
|
{
|
|
int frameCount = winChips[0].WinAnimationFrameCount;
|
|
WaitForSeconds frameDelay = new WaitForSeconds(1f / WinAnimationFrameRate);
|
|
|
|
for (int frameIndex = 0; frameIndex < frameCount; frameIndex++)
|
|
{
|
|
for (int chipIndex = 0; chipIndex < winChips.Length; chipIndex++)
|
|
winChips[chipIndex].SetWinAnimationFrame(frameIndex);
|
|
|
|
yield return frameDelay;
|
|
}
|
|
|
|
_winAnimationCoroutine = null;
|
|
}
|
|
}
|