117 lines
2.9 KiB
C#
117 lines
2.9 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using UnityEngine;
|
|
|
|
public class TurnHandler : MonoBehaviour
|
|
{
|
|
#region Statication
|
|
|
|
public static TurnHandler instance;
|
|
|
|
private void Awake()
|
|
{
|
|
if (instance != null && instance != this)
|
|
{
|
|
Destroy(gameObject);
|
|
return;
|
|
}
|
|
instance = this;
|
|
}
|
|
|
|
#endregion
|
|
|
|
[Header("Entity Lists")]
|
|
public List<PlayerEntity> playerEntities = new();
|
|
public List<Enemy> enemyEntities = new();
|
|
public enum GameState {PlayerTurn, EnemyTurn}
|
|
public GameState currentGameState = GameState.PlayerTurn;
|
|
|
|
private int currentEnemy = 0;
|
|
|
|
[Header("Spawning")]
|
|
[SerializeField] private int enemySpawnAmount;
|
|
|
|
[Header("Endless Mechanics")]
|
|
[SerializeField] private bool isEndless = false;
|
|
[SerializeField] private int baseSpawnAmount;
|
|
[SerializeField] private int amountToSpawn;
|
|
[SerializeField] private float exponentIncrease;
|
|
private int currentWave = 1;
|
|
public static event Action waveUpdate;
|
|
|
|
private void SortEnemies()
|
|
{
|
|
enemyEntities = enemyEntities.OrderBy(o=>o.turnSpeed).ToList();
|
|
}
|
|
|
|
private void Start()
|
|
{
|
|
SortEnemies();
|
|
}
|
|
|
|
public void UpdateTurns()
|
|
{
|
|
if (currentGameState == GameState.PlayerTurn)
|
|
{
|
|
foreach (PlayerEntity player in playerEntities)
|
|
{
|
|
if (player.actions > 0)
|
|
{
|
|
return;
|
|
}
|
|
}
|
|
if (enemyEntities.Count == 0)
|
|
{
|
|
EndRound();
|
|
}
|
|
else
|
|
{
|
|
currentGameState = GameState.EnemyTurn;
|
|
enemyEntities[0].StartTurn();
|
|
}
|
|
}
|
|
else if (currentGameState == GameState.EnemyTurn)
|
|
{
|
|
currentEnemy++;
|
|
if (currentEnemy > enemyEntities.Count - 1)
|
|
{
|
|
EndRound();
|
|
return;
|
|
}
|
|
enemyEntities[currentEnemy].StartTurn();
|
|
}
|
|
}
|
|
|
|
private void EndRound()
|
|
{
|
|
EnvironmentTurn();
|
|
waveUpdate?.Invoke();
|
|
currentEnemy = 0;
|
|
currentGameState = GameState.PlayerTurn;
|
|
foreach (PlayerEntity player in playerEntities)
|
|
{
|
|
player.actions = player.maxActions;
|
|
player.debugDoneObject.SetActive(false);
|
|
}
|
|
}
|
|
|
|
public void EnvironmentTurn()
|
|
{
|
|
if (isEndless && enemyEntities.Count == 0)
|
|
{
|
|
amountToSpawn = Mathf.FloorToInt(baseSpawnAmount * Mathf.Pow(currentWave, exponentIncrease));
|
|
EnemySpawner.instance.SpawnEnemy(enemySpawnAmount);
|
|
currentWave++;
|
|
}
|
|
}
|
|
|
|
public void SkipAll()
|
|
{
|
|
foreach (PlayerEntity player in playerEntities)
|
|
{
|
|
player.actions = 0;
|
|
}
|
|
UpdateTurns();
|
|
}
|
|
}
|