81 lines
1.9 KiB
C#
81 lines
1.9 KiB
C#
using System.Collections;
|
|
using UnityEngine;
|
|
public class ChipFallingState : IState
|
|
{
|
|
int targetRow;
|
|
int targetColumn;
|
|
Vector2 targetPosition;
|
|
|
|
Chip chip;
|
|
|
|
float startDelay = 0;
|
|
bool delayCompleted = false;
|
|
private GameManager gm;
|
|
|
|
public ChipFallingState(float startDelay, int column)
|
|
{
|
|
gm = GameManager.instance;
|
|
chip = gm.currentChip;
|
|
this.startDelay = startDelay;
|
|
this.targetColumn = column;
|
|
this.targetRow = Board.Instance.GetLowestEmptyRow(column);
|
|
|
|
}
|
|
|
|
public void enter()
|
|
{
|
|
gm.StartCoroutine(DelayedEnter(startDelay));
|
|
chip.SetRandomRotationSpeed();
|
|
targetPosition = Board.Instance.GetTargetPosition(targetColumn);
|
|
}
|
|
|
|
private void PrepareForFalling()
|
|
{
|
|
chip.GetComponent<TrailRenderer>().enabled = true;
|
|
}
|
|
private void InitialSound() => SoundManager.instance.playDropDown();
|
|
|
|
public void update()
|
|
{
|
|
if (!delayCompleted) return;
|
|
|
|
bool reachedTarget = chip.MoveToAccelerated(targetPosition);
|
|
chip.Rotate();
|
|
|
|
if (reachedTarget)
|
|
{
|
|
chip.GetComponent<TrailRenderer>().emitting = false;
|
|
SoundManager.instance.playChipBounce();
|
|
CompleteChipLanding();
|
|
}
|
|
}
|
|
|
|
private void CompleteChipLanding()
|
|
{
|
|
//Board.Instance.AttachChipToGrid(chip, targetColumn, targetRow);
|
|
StateRouter.RouteAfterTurn();
|
|
}
|
|
|
|
public void exit()
|
|
{
|
|
AnnouncePlayerTurn();
|
|
}
|
|
|
|
private void AnnouncePlayerTurn()
|
|
{
|
|
if (GameManager.instance.currentPlayer == CellState.Player1)
|
|
{
|
|
CharacterEventBus.Instance.OnPlayerTurn?.Invoke();
|
|
}
|
|
}
|
|
|
|
IEnumerator DelayedEnter(float startDelay)
|
|
{
|
|
yield return new WaitForSeconds(startDelay);
|
|
|
|
PrepareForFalling();
|
|
InitialSound();
|
|
|
|
delayCompleted = true;
|
|
}
|
|
} |