77 lines
2.5 KiB
C#
77 lines
2.5 KiB
C#
using System;
|
|
using System.Linq;
|
|
using Actions;
|
|
using Grid;
|
|
using UnityEngine;
|
|
using UnityEngine.Serialization;
|
|
|
|
public class EnemyAI : MonoBehaviour {
|
|
public enum State {
|
|
WaitingForEnemyTurn,
|
|
TakingTurn,
|
|
Busy
|
|
}
|
|
|
|
public static EnemyAI Instance { get; private set; }
|
|
public State CurrentState { get; private set; }
|
|
private float timer;
|
|
|
|
private void Awake() {
|
|
if (Instance is not null) {
|
|
Debug.LogError($"There is more than one TurnSystem! {transform} - {Instance}");
|
|
Destroy(gameObject);
|
|
return;
|
|
}
|
|
Instance = this;
|
|
CurrentState = State.WaitingForEnemyTurn;
|
|
}
|
|
private void Start() => TurnSystem.Instance.OnTurnChanged += TurnSystem_OnTurnChanged;
|
|
|
|
private void Update() {
|
|
if (TurnSystem.Instance.IsPlayerTurn) return;
|
|
|
|
switch (CurrentState) {
|
|
case State.WaitingForEnemyTurn:
|
|
break;
|
|
case State.TakingTurn:
|
|
timer -= Time.deltaTime;
|
|
if (timer <= 0f) {
|
|
if (TryTakeEnemyAIAction(SetStateTakingTurn)) {
|
|
CurrentState = State.Busy;
|
|
}
|
|
else {
|
|
TurnSystem.Instance.NextTurn(); // No more enemies have actions they can take, end enemy turn
|
|
}
|
|
}
|
|
break;
|
|
case State.Busy:
|
|
break;
|
|
}
|
|
}
|
|
|
|
private void SetStateTakingTurn() {
|
|
timer = .5f;
|
|
CurrentState = State.TakingTurn;
|
|
}
|
|
|
|
private void TurnSystem_OnTurnChanged(object sender, EventArgs e) {
|
|
if (TurnSystem.Instance.IsPlayerTurn) return;
|
|
CurrentState = State.TakingTurn;
|
|
timer = 2f;
|
|
}
|
|
|
|
private static bool TryTakeEnemyAIAction(Action onEnemyAIActionComplete) {
|
|
Debug.Log($"Take Enemy AI Action");
|
|
return UnitManager.Instance.EnemyUnitList.Any(enemyUnit => TryTakeEnemyAIAction(enemyUnit, onEnemyAIActionComplete));
|
|
}
|
|
|
|
private static bool TryTakeEnemyAIAction(Unit enemyUnit, Action onEnemyAIActionComplete) {
|
|
SpinAction spinAction = enemyUnit.SpinAction;
|
|
GridPosition actionGridPosition = enemyUnit.GridPosition;
|
|
if (!spinAction.IsValidActionGridPosition(actionGridPosition)) return false;
|
|
if (!enemyUnit.TrySpendActionPointsToTakeAction(spinAction)) return false;
|
|
Debug.Log($"Spin Action");
|
|
spinAction.TakeAction(actionGridPosition, onEnemyAIActionComplete);
|
|
return true;
|
|
}
|
|
} |