176 lines
5.3 KiB
C#
176 lines
5.3 KiB
C#
using PrizeSystem;
|
|
using UnityEngine;
|
|
|
|
public static class StateRouter
|
|
{
|
|
private static GameManager gm => GameManager.instance;
|
|
public static void RouteAfterTurn()
|
|
{
|
|
var checkResult = GameManager.fieldModel.Check(GameManager.instance.currentPlayer);
|
|
gm.lastResult = checkResult;//это неправильно. Нужно при проверке записывать в поле.
|
|
TurnEndingResult turnResult = EndOfTurnHandler.Evaluate(checkResult, gm.gameMode, gm.currentPlayer);
|
|
|
|
if(turnResult == TurnEndingResult.GameContinues)
|
|
{
|
|
gm.DetermineNextTurn();
|
|
return;
|
|
}
|
|
|
|
IState nextState = turnResult switch
|
|
{
|
|
TurnEndingResult.BotWin => BotWinHandler(),
|
|
TurnEndingResult.PlayerWinPve => PlayerWinPveHandler(),
|
|
TurnEndingResult.PlayerWinPvp => PlayerWinPvpHandler(gm.currentPlayer),
|
|
TurnEndingResult.Draw => DrawHandler(),
|
|
_ => ToMenu()
|
|
};
|
|
|
|
gm.stateMachine.ChangeState(nextState);
|
|
}
|
|
|
|
|
|
private static IState BotWinHandler()
|
|
{
|
|
LightUtility.Lose();
|
|
CharacterController.CurrentCharacter.GetComponent<Animator>().SetTrigger("Win");
|
|
DifficultyManager.Instance.DecreaseDifficulty();
|
|
if (gm.vsBotScore != 0)
|
|
gm.vsBotScore -= 1;
|
|
|
|
CharacterEventBus.Instance.OnBotWin?.Invoke();
|
|
gm.botScale.SetMedalsCount(gm.vsBotScore);
|
|
|
|
return new ShowWinnerState();
|
|
}
|
|
|
|
private static IState PlayerWinPveHandler() //тут нужна обертка в приз + обертка в приз/супер приз
|
|
{
|
|
bool prizing = SettingsManager.GetBool("Prizing", false);
|
|
if (!prizing) return NextBot();
|
|
|
|
bool takeOrRisk = SettingsManager.GetBool("TakeOrRisk", false);
|
|
if (!takeOrRisk)
|
|
{
|
|
TryGivePrize();
|
|
return NextBot();
|
|
}
|
|
|
|
var currentPrize = CharacterController.CurrentCharacter.prize;
|
|
bool currentBotHasNonEmptyPrize = CharacterController.CurrentCharacter.HasPrize && currentPrize is not EmptyPrize;
|
|
|
|
if(currentPrize is VitrinaPrize vitrinaPrize)
|
|
{
|
|
int[] readyBoxes = PrizeStandController.GetReadyBoxes();
|
|
if (VitrinaPanel.FindIntersection(readyBoxes, vitrinaPrize.Boxes).Length == 0)
|
|
{
|
|
currentBotHasNonEmptyPrize = false;
|
|
}
|
|
}
|
|
|
|
if (!currentBotHasNonEmptyPrize) return NextBot();
|
|
|
|
|
|
var nextPrize = PrizeSystemManager.GetFirstNonEmptyPrizeAfter(currentPrize);
|
|
if (nextPrize != null)
|
|
{
|
|
gm.takeOrRiskPanel.SetPrizes(currentPrize, nextPrize);
|
|
gm.timerManager.StopAllTimers();
|
|
return new TakeOrRiskState(NextBot(), gm.takeOrRiskPanel);
|
|
}
|
|
else
|
|
{
|
|
return NextBot();
|
|
}
|
|
|
|
void TryGivePrize()
|
|
{
|
|
if (CharacterController.CurrentCharacter.HasPrize)
|
|
{
|
|
PrizeSystemManager.GivePrize(CharacterController.CurrentCharacter.prize);
|
|
CharacterController.CurrentCharacter.HasPrize = false;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
}
|
|
public static IState NextBot()
|
|
{
|
|
DifficultyManager.Instance.IncreaseDifficulty();
|
|
gm.vsBotScore += 1;
|
|
CharacterEventBus.Instance.OnPlayerWin?.Invoke();
|
|
|
|
LightUtility.Win();
|
|
CharacterController.CurrentCharacter.Animator.SetTrigger("Lose");
|
|
|
|
gm.botScale.SetMedalsCount(gm.vsBotScore);
|
|
|
|
//если фишку победили, то повышаем.
|
|
if (gm.vsBotScore == gm.botsAmount)
|
|
DifficultyManager.Instance.upgradeFeeshkaDifficulty();
|
|
|
|
if (TotalWin())
|
|
{
|
|
ClearAllScales();
|
|
return new VictoryState();
|
|
}
|
|
else
|
|
{
|
|
return new PrizeState(CreateAutoTransitionState(2f));
|
|
}
|
|
}
|
|
|
|
|
|
private static IState PlayerWinPvpHandler(CellState winner)
|
|
{
|
|
gm.IncreaseScoreAndScaleOfPlayer(winner);
|
|
|
|
if (TotalWin())
|
|
AvatarEventBus.Instance.OnEndGame?.Invoke(gm.currentPlayer);
|
|
else
|
|
AvatarEventBus.Instance.OnEndRound?.Invoke(gm.currentPlayer);
|
|
|
|
if (TotalWin())
|
|
{
|
|
ClearAllScales();
|
|
return new VictoryState();
|
|
}
|
|
else
|
|
{
|
|
return new ShowWinnerState();
|
|
}
|
|
}
|
|
|
|
private static IState DrawHandler()
|
|
{
|
|
CharacterEventBus.Instance.OnDraw?.Invoke();
|
|
return new ShowWinnerState();
|
|
}
|
|
|
|
public static IState ToMenu()
|
|
{
|
|
FieldCleaner.Clear();
|
|
return new MenuState();
|
|
}
|
|
|
|
|
|
// Helpers
|
|
private static ShowWinnerState CreateAutoTransitionState(float time)
|
|
{
|
|
var state = new ShowWinnerState();
|
|
state.subState = ShowWinnerState.ShowWinnerSubstates.ExitByTime;
|
|
state.timeToExit = time;
|
|
return state;
|
|
}
|
|
|
|
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();
|
|
}
|
|
}
|