146 lines
4.2 KiB
C#
146 lines
4.2 KiB
C#
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
public class BattleMenu : MonoBehaviour
|
|
{
|
|
private EnemyAi _enemyLogic;
|
|
private readonly List<Player> _players = new List<Player>();
|
|
private int _currentPlayerIndex;
|
|
|
|
private Player CurrentPlayer => _players[_currentPlayerIndex];
|
|
|
|
public Button attackButton;
|
|
public Button bloodCounterButton;
|
|
public Button counterButton;
|
|
public Button defendButton;
|
|
public Text userHealth;
|
|
public Text enemyHealth;
|
|
|
|
private void Awake()
|
|
{
|
|
_players.Add(new Player {health = 100, damage = 5, updateHealth = UpdateUserHealth, isHumanControlled = true});
|
|
_players.Add(new Player {health = 100, damage = 5, updateHealth = UpdateEnemyHealth, isHumanControlled = false});
|
|
|
|
_enemyLogic = new EnemyAi();
|
|
_enemyLogic.attackCallback += Attack;
|
|
_enemyLogic.defendCallback += Defend;
|
|
_enemyLogic.counterCallback += Counter;
|
|
}
|
|
|
|
private void Start()
|
|
{
|
|
attackButton.onClick.AddListener(Attack);
|
|
bloodCounterButton.onClick.AddListener(BloodCounter);
|
|
defendButton.onClick.AddListener(Defend);
|
|
counterButton.onClick.AddListener(Counter);
|
|
|
|
foreach (var player in _players)
|
|
{
|
|
player.updateHealth(player.health);
|
|
}
|
|
|
|
Debug.Log(CurrentPlayer.isHumanControlled ? "Start user turn" : "Start enemy turn");
|
|
StartTurn();
|
|
}
|
|
|
|
private void UpdateUserHealth(int health)
|
|
{
|
|
userHealth.text = $"Health: {health}";
|
|
}
|
|
|
|
private void UpdateEnemyHealth(int health)
|
|
{
|
|
enemyHealth.text = $"Health: {health}";
|
|
}
|
|
|
|
private void FinishTurn()
|
|
{
|
|
// TODO: disable hud
|
|
attackButton.interactable = !CurrentPlayer.isHumanControlled;
|
|
bloodCounterButton.interactable = !CurrentPlayer.isHumanControlled;
|
|
defendButton.interactable = !CurrentPlayer.isHumanControlled;
|
|
counterButton.interactable = !CurrentPlayer.isHumanControlled;
|
|
|
|
_currentPlayerIndex = ++_currentPlayerIndex % _players.Count;
|
|
StartTurn();
|
|
}
|
|
|
|
private void Attack()
|
|
{
|
|
// TODO: when target selection is a thing this will need to be changed.
|
|
var victimIndex = (_currentPlayerIndex + 1) % _players.Count;
|
|
Attack(CurrentPlayer, _players[victimIndex]);
|
|
FinishTurn();
|
|
}
|
|
|
|
private void BloodCounter()
|
|
{
|
|
CurrentPlayer.isBloodCounter = true;
|
|
FinishTurn();
|
|
}
|
|
|
|
private void Defend()
|
|
{
|
|
CurrentPlayer.damageReduction = 0.4f;
|
|
CurrentPlayer.startTurnEffects = ResetDamageReduction;
|
|
FinishTurn();
|
|
}
|
|
|
|
private void Counter()
|
|
{
|
|
CurrentPlayer.isCounter = true;
|
|
FinishTurn();
|
|
}
|
|
|
|
private void StartTurn()
|
|
{
|
|
CurrentPlayer.isCounter = false;
|
|
if (CurrentPlayer.isStunned)
|
|
{
|
|
Debug.Log("stunned, skipping turn");
|
|
CurrentPlayer.isStunned = false;
|
|
FinishTurn();
|
|
return;
|
|
}
|
|
|
|
// TODO: enable hud.
|
|
attackButton.interactable = CurrentPlayer.isHumanControlled;
|
|
bloodCounterButton.interactable = CurrentPlayer.isHumanControlled;
|
|
defendButton.interactable = CurrentPlayer.isHumanControlled;
|
|
counterButton.interactable = CurrentPlayer.isHumanControlled;
|
|
|
|
CurrentPlayer.startTurnEffects?.Invoke(CurrentPlayer);
|
|
CurrentPlayer.startTurnEffects = null;
|
|
|
|
// TODO: controller.
|
|
if (!CurrentPlayer.isHumanControlled)
|
|
{
|
|
_enemyLogic.TakeTurn();
|
|
}
|
|
}
|
|
|
|
private void ResetDamageReduction(Player p)
|
|
{
|
|
p.damageReduction = 1f;
|
|
}
|
|
|
|
private void Attack(Player attacker, Player victim)
|
|
{
|
|
if (victim.isCounter)
|
|
{
|
|
attacker.isStunned = true;
|
|
Debug.Log("COUNTER");
|
|
return;
|
|
}
|
|
victim.TakeDamage((int)(attacker.damage * attacker.damageModification));
|
|
// damage mod is spent whenever they attack.
|
|
attacker.damageModification = 1;
|
|
attacker.isBloodCounter = false;
|
|
if (victim.health <= 0)
|
|
{
|
|
Debug.Log("The victim died!");
|
|
}
|
|
}
|
|
}
|