182 lines
5.6 KiB
C#
182 lines
5.6 KiB
C#
using PrizeSystem;
|
||
using UnityEngine;
|
||
|
||
public static class EndOfTurnHandler
|
||
{
|
||
private static GameManager gm;
|
||
private static CheckResult result;
|
||
public static void NormalHandling()
|
||
{
|
||
result = GameManager.fieldModel.Check(GameManager.instance.currentPlayer);
|
||
gm = GameManager.instance;
|
||
gm.lastResult = result;
|
||
|
||
bool wrapToPrizeState = false;
|
||
|
||
IState nextState = new ShowWinnerState(result);//временно
|
||
|
||
if (result.End)
|
||
{
|
||
if (!result.Draw)
|
||
{
|
||
if (gm.gameMode == GameMode.pvp)
|
||
{
|
||
PvpEndroundHandler();
|
||
}
|
||
else if (gm.gameMode == GameMode.pve) //конец раунда с ботом
|
||
{
|
||
PveEndroundHandler();
|
||
if (gm.currentPlayer == CellState.Player1)
|
||
{//автопереход на некст бота.
|
||
wrapToPrizeState = true;
|
||
nextState = CreateAutoTransitionState(result, 4);
|
||
}
|
||
}
|
||
|
||
if (TotalWin())
|
||
{//конец игры - призовое состояние
|
||
nextState = new VictoryState();
|
||
ClearAllScales();
|
||
}
|
||
}
|
||
else if (gm.gameMode == GameMode.pve && result.Draw)
|
||
{
|
||
CharacterEventBus.Instance.OnDraw?.Invoke();
|
||
}
|
||
}
|
||
else
|
||
{
|
||
gm.SwitchPlayer();
|
||
nextState = new StartTurnState();
|
||
}
|
||
|
||
if(wrapToPrizeState)
|
||
nextState = new PrizeState(nextState: nextState);
|
||
|
||
gm.stateMachine.ChangeState(nextState);
|
||
}
|
||
|
||
|
||
private static void PveEndroundHandler()
|
||
{
|
||
if (gm.currentPlayer == CellState.Player1)
|
||
{
|
||
PlayerWinHandler();
|
||
}
|
||
else//Бот победил
|
||
{
|
||
BotWinHandler();
|
||
}
|
||
gm.botScale.SetMedalsCount(gm.vsBotScore);
|
||
|
||
//если фишку победили, то повышаем.
|
||
if (gm.vsBotScore == gm.botsAmount) gm.upgradeFeeshkaDifficulty();
|
||
}
|
||
|
||
private static void PvpEndroundHandler()
|
||
{
|
||
CellState winner = gm.currentPlayer;
|
||
gm.IncreaseScoreAndScaleOfPlayer(winner);
|
||
|
||
if (TotalWin())
|
||
AvatarEventBus.Instance.OnEndGame?.Invoke(gm.currentPlayer);
|
||
else
|
||
AvatarEventBus.Instance.OnEndRound?.Invoke(gm.currentPlayer);
|
||
}
|
||
private static void BotWinHandler()
|
||
{
|
||
LightUtility.Lose();
|
||
CharacterController.CurrentCharacter.GetComponent<Animator>().SetTrigger("Win");
|
||
DifficultyManager.Instance.DecreaseDifficulty();
|
||
if (gm.vsBotScore != 0)
|
||
gm.vsBotScore -= 1;
|
||
|
||
CharacterEventBus.Instance.OnBotWin?.Invoke();
|
||
}
|
||
private static void PlayerWinHandler()
|
||
{
|
||
if (CharacterController.CurrentCharacter.HasPrize)
|
||
{
|
||
PrizeSystemManager.GivePrize(CharacterController.CurrentCharacter.prize);
|
||
CharacterController.CurrentCharacter.HasPrize = false;
|
||
}
|
||
|
||
DifficultyManager.Instance.IncreaseDifficulty();
|
||
gm.vsBotScore += 1;
|
||
CharacterEventBus.Instance.OnPlayerWin?.Invoke();
|
||
|
||
LightUtility.Win();
|
||
CharacterController.CurrentCharacter.Animator.SetTrigger("Lose");
|
||
}
|
||
|
||
private static bool TotalWin() =>
|
||
gm.player1score == 5 || gm.player2score == 5 || gm.vsBotScore == gm.botsAmount;
|
||
private static void ClearAllScales()
|
||
{
|
||
if (gm.botScale.isActiveAndEnabled) gm.botScale.ClearScale();
|
||
if (gm.Player1Scale.isActiveAndEnabled) gm.Player1Scale.ClearScale();
|
||
if (gm.Player2Scale.isActiveAndEnabled) gm.Player2Scale.ClearScale();
|
||
}
|
||
|
||
private static ShowWinnerState CreateAutoTransitionState(CheckResult result, float time)
|
||
{
|
||
var state = new ShowWinnerState(result);
|
||
state.subState = ShowWinnerState.ShowWinnerSubstates.ExitByTime;
|
||
state.timeRest = time;
|
||
return state;
|
||
}
|
||
|
||
public static void WinByTimerHandling()
|
||
{
|
||
//При победе по таймеру:
|
||
//PVP проиграл тот, кто не успел кинуть фишку
|
||
//PVE - прогрывает человек
|
||
|
||
|
||
result = new CheckResult(true, false, null);
|
||
IState nextState;
|
||
|
||
if (gm.gameMode == GameMode.pvp)
|
||
{
|
||
//p2 не успел
|
||
if (gm.currentPlayer == CellState.Player2)
|
||
{
|
||
gm.player1score += 1;
|
||
gm.Player1Scale.SetMedalsCount(gm.player1score);
|
||
}
|
||
else//p1 не успел
|
||
{
|
||
gm.player2score += 1;
|
||
gm.Player2Scale.SetMedalsCount(gm.player2score);
|
||
}
|
||
}
|
||
else if (gm.gameMode == GameMode.pve)//время вышло при игре с ботом - бот победил
|
||
{
|
||
LightUtility.Lose();
|
||
CharacterController.CurrentCharacter.GetComponent<Animator>().SetTrigger("Win");
|
||
|
||
//НЕ НИЧЬЯ
|
||
VictoryScale vsBotScale = gm.botScale;
|
||
DifficultyManager.Instance.DecreaseDifficulty();
|
||
CharacterEventBus.Instance.OnBotWin?.Invoke();
|
||
if (gm.vsBotScore != 0) gm.vsBotScore -= 1;
|
||
|
||
vsBotScale.SetMedalsCount(gm.vsBotScore);
|
||
}
|
||
|
||
nextState = new ShowWinnerState(result);
|
||
|
||
//конец игры - призовое состояние
|
||
if (TotalWin())
|
||
{
|
||
nextState = new VictoryState();
|
||
ClearAllScales();
|
||
}
|
||
|
||
gm.stateMachine.ChangeState(nextState);
|
||
}
|
||
|
||
|
||
|
||
}
|