206 lines
6.2 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using UnityEngine;
public class CheckState : IState
{
GameManager gm;
bool timerWin;
CheckResult result;
public CheckState() : this(false)
{ }
public CheckState(bool timerWin)
{
this.timerWin = timerWin;
result = timerWin ? new CheckResult(true, false, null) : GameManager.fieldModel.Check(GameManager.instance.currentPlayer);
gm = GameManager.instance;
gm.lastResult = result;
}
public void enter()
{
gm = GameManager.instance;
if (timerWin)
{
WinByTimerHandling();
}
else
{
NormalHandling();
}
}
void NormalHandling()
{
bool swipeToNextRightNow = false;
IState nextState;
//pvp решение победа в раунде или в игре
bool endround = result.End && !result.Draw;
bool endgame = false;
// подсчет очков
if (result.End)//Раунд окончен. Считаем очки и ставим стейт показ победителя
{
if (gm.gameMode == GameMode.pvp && !result.Draw)
{
//победил p1
if (gm.currentPlayer == CellState.Player1)
{
gm.player1score += 1;
gm.Player1Scale.SetMedalsCount(gm.player1score);
}
else//победил p2
{
gm.player2score += 1;
gm.Player2Scale.SetMedalsCount(gm.player2score);
}
if (gm.player1score == 5 || gm.player2score == 5)
endgame = true;
if (endgame)
AvatarEventBus.Instance.OnEndGame?.Invoke(gm.currentPlayer);
else if (endround)
AvatarEventBus.Instance.OnEndRound?.Invoke(gm.currentPlayer);
}
else if (gm.gameMode == GameMode.pve && !result.Draw) //конец раунда с ботом
{
//НЕ НИЧЬЯ
VictoryScale botScale = gm.botScale;
if (gm.currentPlayer == CellState.Player1)//[ПОБЕДА ИГРОКА]
{
if (CharacterController.CurrentCharacter.HasPrize)
{
PrizeSystem.GivePrize(CharacterController.CurrentCharacter.prize);
CharacterController.CurrentCharacter.HasPrize = false;
}
DifficultyManager.instance.IncreaseDifficulty();
gm.vsBotScore += 1;
CharacterEventBus.Instance.OnPlayerWin?.Invoke();
LightUtility.Win();
CharacterController.CurrentCharacter.GetComponent<Animator>().SetTrigger("Lose");
swipeToNextRightNow = true;
}
else//Бот победил
{
LightUtility.Lose();
CharacterController.CurrentCharacter.GetComponent<Animator>().SetTrigger("Win");
DifficultyManager.instance.DecreaseDifficulty();
if (gm.vsBotScore != 0)
gm.vsBotScore -= 1;
CharacterEventBus.Instance.OnBotWin?.Invoke();
}
botScale.SetMedalsCount(gm.vsBotScore);
}
else if (gm.gameMode == GameMode.pve && result.Draw)
{
CharacterEventBus.Instance.OnDraw?.Invoke();
}
nextState = new ShowWinnerState(result);
}
else
{
gm.SwitchPlayer();
nextState = new StartTurnState();
}
if (gm.gameMode == GameMode.pve && gm.vsBotScore == gm.botsAmount) gm.upgradeFeeshkaDifficulty();
//конец игры - призовое состояние
if (IsEndGame())
{
nextState = new PrizeState();
ClearAllScales();
}
else if (swipeToNextRightNow)
{
ShowWinnerState showWinner = new ShowWinnerState(result);
showWinner.subState = ShowWinnerState.ShowWinnerSubstates.ExitByTime;
showWinner.timeRest = 4;
nextState = showWinner;
}
gm.stateMachine.ChangeState(nextState);
}
private bool IsEndGame() =>
gm.player1score == 5 || gm.player2score == 5 || gm.vsBotScore == gm.botsAmount;
void WinByTimerHandling()
{
//запустить проверку по полю
IState nextState;
if (gm.gameMode == GameMode.pvp && !result.Draw)
{
//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 && !result.Draw)//время вышло при игре с ботом - бот победил
{
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 (IsEndGame())
{
nextState = new PrizeState();
ClearAllScales();
}
gm.stateMachine.ChangeState(nextState);
}
private void ClearAllScales()
{
if (gm.botScale.isActiveAndEnabled) gm.botScale.ClearScale();
if (gm.Player1Scale.isActiveAndEnabled) gm.Player1Scale.ClearScale();
if (gm.Player2Scale.isActiveAndEnabled) gm.Player2Scale.ClearScale();
}
public void exit()
{
}
public void update()
{
}
}