402 lines
12 KiB
C#
402 lines
12 KiB
C#
using System.Collections;
|
||
using UnityEngine;
|
||
|
||
enum MenuSubStates {OpeningMenu, Choosing, Payment, ClosingMenu, SwitchState}
|
||
public class MenuState : IState
|
||
{
|
||
GameManager gm;
|
||
|
||
float SecondsToPromo = 180;
|
||
float ElapsedPromoTimer;
|
||
float timeToFastcheck = 2;
|
||
|
||
float GotoSettingsPressedTime = 3;
|
||
float ElapsedSettinsPressedTime;
|
||
|
||
bool animationEnded = true;
|
||
|
||
public Transform
|
||
label,
|
||
buttonLeft,
|
||
buttonRight,
|
||
coinPanel,
|
||
label2,
|
||
board,
|
||
noPrizeLabel;
|
||
RectTransform SmallPrizesOut;
|
||
|
||
MenuSubStates subState;
|
||
|
||
#region начальные и конечные позиции для элементов меню
|
||
//s - startPosition, e - endPosition
|
||
Vector2
|
||
//надпись "ФИШКА"
|
||
sLabel = new Vector2(0, 320),
|
||
eLabel = new Vector2(0, 1000),
|
||
|
||
//Иконки режимов
|
||
sButton = new Vector2(400, -15),
|
||
eButton = new Vector2(2000, -15),
|
||
|
||
//подсказка + лого + техподдержка
|
||
sLabel2 = new Vector2(0, -380),
|
||
eLabel2 = new Vector2(0, -1000),
|
||
|
||
//ИГРОВОЕ ПОЛЕ
|
||
sBoard = new Vector2(-40, -0.5f),
|
||
eBoard = new Vector2(0, -0.5f),
|
||
|
||
sNoPrize = new Vector2(-0, -70f),
|
||
eNoPrize = new Vector2(15, -409);
|
||
Vector3
|
||
sCoin = new Vector3(0, -1.93f, 0),
|
||
eCoin = new Vector3(-8.964f, 3.6f, 0);
|
||
|
||
Vector2 sSmallPrizesOutSize = new Vector2(560, 100);
|
||
Vector2 eSmallPrizesOutSize = new Vector2(660, 100);
|
||
#endregion
|
||
|
||
|
||
public MenuState()
|
||
{
|
||
subState = MenuSubStates.Choosing;//субстейт и так ставится в энтере
|
||
}
|
||
|
||
//если пришли из игры, то нужно начать с открытия меню
|
||
public MenuState(bool fromGameplay) : this()
|
||
{
|
||
subState = MenuSubStates.OpeningMenu;//субстейт и так ставится в энтере
|
||
}
|
||
|
||
public void enter()
|
||
{
|
||
PullRefs();
|
||
ResetGameSession();
|
||
|
||
ElapsedPromoTimer = 0;
|
||
|
||
subState = gm.menuOpened ? MenuSubStates.Choosing : MenuSubStates.OpeningMenu;
|
||
LightUtility.Idle();
|
||
}
|
||
|
||
private void PullRefs()
|
||
{
|
||
gm = GameManager.instance;
|
||
label = gm.label;
|
||
buttonLeft = gm.buttonLeft;
|
||
buttonRight = gm.buttonRight;
|
||
coinPanel = gm.coinPanel;
|
||
label2 = gm.label2;
|
||
board = gm.board;
|
||
noPrizeLabel = gm.noPrizeLabel;
|
||
SmallPrizesOut = gm.smallPrizesOut;
|
||
}
|
||
private void ResetGameSession()
|
||
{
|
||
//ресет
|
||
gm.vsBotThinkTime = 0;
|
||
gm.vsBotMoves = 0;
|
||
gm.movesAndTimeCounter.SetVisibility(false);
|
||
|
||
CharacterController.HideCharacter();
|
||
CharacterController.I.SetAllCharactersPrizeAvailable(true);
|
||
DualAvatarController.Instance.HideCharacters();
|
||
|
||
DifficultyManager.instance.ResetDifficulty();
|
||
|
||
gm.player1score = 0;
|
||
gm.player2score = 0;
|
||
gm.vsBotScore = 0;
|
||
|
||
gm.moveMade = false;
|
||
}
|
||
|
||
public void exit() { }
|
||
|
||
public void update()
|
||
{
|
||
switch (subState)
|
||
{
|
||
case MenuSubStates.OpeningMenu:
|
||
OpenMenu();
|
||
break;
|
||
case MenuSubStates.Choosing:
|
||
ChoosingMode();
|
||
break;
|
||
case MenuSubStates.Payment:
|
||
Payment();
|
||
break;
|
||
case MenuSubStates.ClosingMenu:
|
||
CloseMenu();
|
||
break;
|
||
case MenuSubStates.SwitchState:
|
||
SwitchState();
|
||
break;
|
||
|
||
}
|
||
}
|
||
|
||
private void ChoosingMode()//основное состояние(выбор)
|
||
{
|
||
if (!animationEnded)
|
||
return;
|
||
|
||
//Заход в настройки
|
||
if (ShouldGotosettings())
|
||
{
|
||
SoundManager.instance.playChoose();
|
||
//gm.stateMachine.ChangeState(new SettingsState());
|
||
gm.stateMachine.ChangeState(new CheckSettingsPasswordState());
|
||
return;
|
||
}
|
||
|
||
//Определяемся с режимом игры
|
||
if (GamemodeChosen()) return;
|
||
|
||
//Правила
|
||
if (In.GetButtonDownAnyPlayer(PlayerAction.down))
|
||
{
|
||
gm.stateMachine.ChangeState(new RulesState());
|
||
return;
|
||
}
|
||
|
||
|
||
//Промовидос
|
||
ElapsedPromoTimer = Mathf.Clamp(ElapsedPromoTimer +Time.deltaTime, 0, SecondsToPromo);
|
||
if (ElapsedPromoTimer == SecondsToPromo)
|
||
{
|
||
gm.stateMachine.ChangeState(new PromoState());
|
||
return;
|
||
}
|
||
|
||
//Заход в лидерборд
|
||
bool gotoLeaderboard = In.GetButtonDownAnyPlayer(PlayerAction.left) && In.GetButtonDownAnyPlayer(PlayerAction.right);
|
||
if (gotoLeaderboard)
|
||
{
|
||
ElapsedPromoTimer = 0;
|
||
|
||
gm.stateMachine.ChangeState(new CheckLeadersFromMenuState());
|
||
return;
|
||
}
|
||
|
||
//проверка счетчиков
|
||
bool checkCountersCombination = In.GetButton(ButtonPinMapping.startOnePlayer) && In.GetButton(ButtonPinMapping.startTwoPlayers);
|
||
if (checkCountersCombination)
|
||
{
|
||
timeToFastcheck -= Time.deltaTime;
|
||
if (timeToFastcheck <= 0)
|
||
{
|
||
bool fastCheckEnabled = SettingsManager.GetBool("FastCheck", false);
|
||
if (fastCheckEnabled) gm.fastCheckCounters.SetActive(true);
|
||
|
||
timeToFastcheck = 2;
|
||
}
|
||
}
|
||
else
|
||
timeToFastcheck = 2;
|
||
}
|
||
private bool ShouldGotosettings()
|
||
{
|
||
//читаем кнопки
|
||
bool settingButtonPressed = In.GetButton(ButtonPinMapping.secret) || Input.GetKey(KeyCode.F5);
|
||
|
||
//считаем таймер
|
||
float sign = settingButtonPressed ? 1 : -1;
|
||
ElapsedSettinsPressedTime += Time.deltaTime * sign;
|
||
ElapsedSettinsPressedTime = Mathf.Clamp(ElapsedSettinsPressedTime, 0, GotoSettingsPressedTime);
|
||
|
||
//Жали столько сколько надо для захода в меню
|
||
if (ElapsedSettinsPressedTime == GotoSettingsPressedTime)
|
||
{
|
||
ElapsedSettinsPressedTime = 0;
|
||
return true;
|
||
}
|
||
|
||
return false;
|
||
}
|
||
private bool GamemodeChosen()
|
||
{
|
||
bool onePlayer = In.GetButtonDown(ButtonPinMapping.startOnePlayer);
|
||
bool twoPlayer = In.GetButtonDown(ButtonPinMapping.startTwoPlayers);
|
||
|
||
if (onePlayer) gm.SetMode(GameMode.pve);
|
||
if (twoPlayer) gm.SetMode(GameMode.pvp);
|
||
|
||
if (onePlayer || twoPlayer)
|
||
{
|
||
ElapsedPromoTimer = 0;
|
||
|
||
gm.InitVictoryScales();
|
||
SoundManager.instance.playChoose();
|
||
subState = MenuSubStates.Payment;
|
||
return true;
|
||
}
|
||
|
||
return false;
|
||
}
|
||
|
||
|
||
private void Payment()
|
||
{
|
||
|
||
int creditsPrice = gm.gameMode == GameMode.pve ? PlayerPrefs.GetInt("PricePVE", 10) : PlayerPrefs.GetInt("PricePVP", 10);
|
||
if (ComWrapper.Pay(creditsPrice))
|
||
{//оплата успешно - закрываем меню
|
||
subState = MenuSubStates.ClosingMenu;
|
||
}
|
||
else
|
||
{//не хватило монет - возвращаемся к выбору
|
||
gm.OnNoMoney?.Invoke();
|
||
subState = MenuSubStates.Choosing;
|
||
}
|
||
}
|
||
|
||
void CloseMenu()
|
||
{
|
||
gm.timerManager.SetTimersActivity(gm.gameMode);
|
||
|
||
gm.menuOpened = true;
|
||
animationEnded = false;
|
||
|
||
gm.StartCoroutine(closeMenuAnimation());
|
||
subState = MenuSubStates.SwitchState;
|
||
}
|
||
void OpenMenu()
|
||
{
|
||
animationEnded = false;
|
||
gm.menuOpened = true;
|
||
|
||
gm.StartCoroutine(openMenuAnimation());
|
||
|
||
subState = MenuSubStates.Choosing;
|
||
}
|
||
|
||
void SwitchState()
|
||
{
|
||
if (animationEnded)
|
||
{
|
||
if (subState == MenuSubStates.OpeningMenu)
|
||
{
|
||
subState = MenuSubStates.Choosing;
|
||
return;
|
||
}
|
||
else
|
||
{//заход в игру
|
||
gm.menuOpened = false;
|
||
CharacterController.ResetState();
|
||
|
||
//при переходе из меню первым ходит игрок1
|
||
gm.stateMachine.ChangeState(new StartRoundState(CellState.Player1));
|
||
|
||
if(gm.gameMode==GameMode.pve) gm.movesAndTimeCounter.SetVisibility(true);
|
||
}
|
||
}
|
||
}
|
||
|
||
IEnumerator closeMenuAnimation()
|
||
{
|
||
animationEnded = false;
|
||
float parametr;
|
||
|
||
//Мув кнопок, надписей, монетника
|
||
parametr = 0;
|
||
while (parametr < 1)
|
||
{
|
||
parametr += Time.deltaTime*2;
|
||
|
||
label.localPosition = Vector3.Lerp(sLabel, eLabel, parametr);
|
||
|
||
buttonLeft.localPosition = Vector3.Lerp(new Vector3(-sButton.x, sButton.y,0), new Vector3(-eButton.x, eButton.y, 0), parametr);
|
||
buttonRight.localPosition = Vector3.Lerp(sButton, eButton, parametr);
|
||
|
||
|
||
label2.localPosition = Vector3.Lerp(sLabel2, eLabel2, parametr);
|
||
noPrizeLabel.localPosition = Vector3.Lerp(sNoPrize, eNoPrize+Vector2.left*3500, parametr);
|
||
coinPanel.localPosition = Vector3.Lerp(sCoin, eCoin, parametr);
|
||
|
||
SmallPrizesOut.sizeDelta = Vector2.Lerp(sSmallPrizesOutSize, eSmallPrizesOutSize, parametr);
|
||
|
||
yield return null;
|
||
}
|
||
buttonLeft.localPosition = -eButton;
|
||
buttonRight.localPosition = eButton;
|
||
|
||
//мув доски
|
||
parametr = 0;
|
||
while (parametr < 1)
|
||
{
|
||
parametr += Time.deltaTime * 2;
|
||
board.localPosition = Vector3.Lerp(sBoard, eBoard, parametr);
|
||
noPrizeLabel.localPosition = Vector3.Lerp(sNoPrize + Vector2.left * 3500, eNoPrize, parametr);
|
||
|
||
yield return null;
|
||
}
|
||
|
||
animationEnded = true;
|
||
}
|
||
IEnumerator openMenuAnimation()
|
||
{
|
||
|
||
Vector3 coinStart = coinPanel.localPosition;
|
||
|
||
animationEnded = false;
|
||
float parametr = 1;
|
||
while (parametr >= 0)
|
||
{
|
||
board.localPosition = Vector3.Lerp(sBoard, eBoard, parametr);
|
||
noPrizeLabel.localPosition = Vector3.Lerp(eNoPrize + Vector2.left * 3500, eNoPrize , parametr);
|
||
parametr -= Time.deltaTime * 2;
|
||
yield return null;
|
||
}
|
||
|
||
|
||
parametr = 1;
|
||
while (parametr >= 0)
|
||
{
|
||
parametr -= Time.deltaTime * 2;
|
||
|
||
label.localPosition = Vector3.Lerp(sLabel, eLabel, parametr);
|
||
|
||
//перемещение кнопок
|
||
buttonLeft.localPosition = Vector3.Lerp(
|
||
-sButton + Vector2.up *sButton.y*2, //к точке старта (Y инвертируем)
|
||
-eButton, //от спрятанной позиции
|
||
parametr);
|
||
|
||
buttonRight.localPosition = Vector3.Lerp(
|
||
sButton /*+ Vector2.up * 10f*/,
|
||
eButton ,
|
||
parametr);
|
||
|
||
noPrizeLabel.localPosition = Vector3.Lerp(sNoPrize, eNoPrize + Vector2.left * 3500, parametr);
|
||
|
||
|
||
|
||
//монетка
|
||
float startDistance = Vector3.Distance(coinPanel.position, sCoin);
|
||
while (Vector3.Distance(coinPanel.position, sCoin) > 0.01f)
|
||
{
|
||
float coinspeed = startDistance * Time.deltaTime * 2;
|
||
coinPanel.localPosition = Vector3.MoveTowards(coinPanel.localPosition, sCoin, coinspeed);
|
||
yield return null;
|
||
}
|
||
coinPanel.localPosition = sCoin;
|
||
label2.localPosition = Vector3.Lerp(sLabel2, eLabel2, parametr);
|
||
|
||
SmallPrizesOut.sizeDelta = Vector2.Lerp(sSmallPrizesOutSize, eSmallPrizesOutSize, parametr);
|
||
|
||
yield return null;
|
||
}
|
||
|
||
animationEnded = true;
|
||
|
||
|
||
}
|
||
|
||
//это было в конце корутины открытия меню
|
||
//gm.botScale.gameObject.SetActive(false);
|
||
//gm.Player1Scale.gameObject.SetActive(false);
|
||
//gm.Player2Scale.gameObject.SetActive(false);// это не через шкала менеджер делается?
|
||
//СДЕЛАЛ метод для выключения таймеров в менеджере
|
||
}
|