I’m making a turn-based game where the player moves, then the enemy moves, then there’s a short pause before the player can move again. I created a state switch function that runs from Update(). The code sample below works, but i don’t think this is the proper approach. I want to place the TurnWait() pause in the “EndTurn” state, but in doing so i just spawn dozens of coroutines to run and this causes the player to move far too quickly across the screen.
I am able to get around this by adding the delay at the end of TurnState.Enemy (which presumes Enemy) is last to move, and the actual TurnState.EndTurn doesn’t actually have any code in it.
Like i said, this works but i feel like there’s a better approach. I also need to build something similar for pausing for other state machines - e.g. overall Game State (Game in progress, game paused, game over, etc) so before i do this i thought i’d check if anyone had any suggestions at a better approach while using States to control turn cycle?
private void ManageTurnCycle()
{
if (turnState == TurnState.EndTurn)
return;
switch (turnState)
{
case TurnState.Player:
if (player.GetInput())
turnState = TurnState.Enemy;
break;
case TurnState.Enemy:
for (int i = 0; i < units.Count; i++)
{
if (units[i].myUnitType == UnitType.Enemy)
{
((Enemy)units[i]).ProcessMove();
}
}
turnState = TurnState.EndTurn;
// For now it's the enemy that moves last in the turn.
// Apply the wait between turns delay
StartCoroutine(TurnWait(.2f));
break;
case TurnState.EndTurn:
break;
}
}
IEnumerator TurnWait(float time)
{
yield return new WaitForSeconds(time);
turnState = TurnState.Player;
}