chip-game/Assets/Scripts/GameFSM/ClearFieldState.cs

97 lines
3.4 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 ClearFieldState : IState
{
const int FIELD_HEIGHT = 6;
const int FIELD_WIDTH = 7;
const string SORTLAYER_BACKGROUND = "Background";
const float EXPLOSION_FORCE = 30;
readonly Vector3 EXPLOSION_ORIGIN = new Vector3(0, -2, -5);
const float EXPLOSION_RADIUS = 50;
const float TIME_TO_DESTROY = 5;
Transform[,] field;
bool toMenu;
CellState firstPlayer;
//передаем переходим мы в меню или нет. Если нет, то переход отсюда в новый раунд
//Надо еще сделать в конструкторе чтобы nextState указывать а то ж непонятно вообще
public ClearFieldState(bool toMenu, CellState firstPlayer)
{
this.toMenu = toMenu;
this.firstPlayer = firstPlayer;
}
public void enter()
{
field = GameManager.instance.chipTransforms;
SoundManager.instance.playExplosion();
Explode();
//переход
if (toMenu)
GameManager.instance.stateMachine.ChangeState(new MenuState(fromGameplay: true));
else
{
//Фишка ходит первой если ее не побеждали сегодня еще
bool goingToLastBot = DifficultyManager.instance.currentOpponent == GameManager.instance.botsAmount - 1;
if (goingToLastBot)
{
//bool bossUpgraded = GameManager.instance.BossUpgraded;
bool bossFirst = SettingsManager.GetString("BaseBotDifficulty", "Фиксированная") != "Обычная";
CellState firstRoundPlayer = bossFirst ? CellState.Player2 : CellState.Player1;
GameManager.instance.stateMachine.ChangeState(new StartRoundState(firstPlayer: firstRoundPlayer));
return;
}
GameManager.instance.stateMachine.ChangeState(new StartRoundState(firstPlayer: firstPlayer));
}
return;
}
private void Explode()
{
for (int i = 0; i < GameManager.instance.Rows; i++)
{
for (int j = 0; j < GameManager.instance.Cols; j++)
{
Transform chipTransform = field[i, j];
if (chipTransform != null)
{
Rigidbody rb = chipTransform.GetComponent<Rigidbody>();
rb.isKinematic = false;
rb.AddExplosionForce(EXPLOSION_FORCE, EXPLOSION_ORIGIN, EXPLOSION_RADIUS, upwardsModifier: 1, ForceMode.Impulse);
chipTransform.GetComponent<TrailRenderer>().sortingLayerName = SORTLAYER_BACKGROUND;
chipTransform.GetChild(0).GetComponent<SpriteRenderer>().sortingLayerName = SORTLAYER_BACKGROUND;
Object.Destroy(chipTransform.gameObject, TIME_TO_DESTROY+Random.Range(-1f,1f));//чтобы уничтожались не одновременно
}
}
}
}
public void exit()
{
if (GameManager.instance.currentChip != null)
{
Object.Destroy(GameManager.instance.currentChip);
}
GameManager.fieldModel.makeEmptyField(width: GameManager.instance.Cols, height: GameManager.instance.Rows);
GameManager.instance.chipTransforms = new Transform[GameManager.instance.Rows, GameManager.instance.Cols];
}
public void update(){ }
}