proto_turn_based_rpg/Assets/Scripts/BattleMenu.cs

93 lines
2.1 KiB
C#

using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class BattleMenu : MonoBehaviour
{
private class Player
{
public int Health;
public int Damage;
public Action UpdateHealth;
}
private Player _user;
private Player _enemy;
private EnemyAi _enemyLogic;
private readonly List<Action> _turnOrder = new List<Action>();
private int _currentTurnIndex = 0;
public Button attackButton;
public Text userHealth;
public Text enemyHealth;
private void Awake()
{
_user = new Player {Health = 10, Damage = 1, UpdateHealth = UpdateUserHealth};
_enemy = new Player {Health = 10, Damage = 1, UpdateHealth = UpdateEnemyHealth};
_enemyLogic = new EnemyAi();
_enemyLogic.AttackCallback += EnemyAttack;
}
private void Start()
{
attackButton.onClick.AddListener(PlayerAttack);
_turnOrder.Add(UserTakeTurn);
_turnOrder.Add(_enemyLogic.TakeTurn);
_turnOrder[_currentTurnIndex]();
UpdateUserHealth();
UpdateEnemyHealth();
}
private void UpdateUserHealth()
{
userHealth.text = $"Health: {_user.Health}";
}
private void UpdateEnemyHealth()
{
enemyHealth.text = $"Health: {_enemy.Health}";
}
private void FinishTurn()
{
_currentTurnIndex = ++_currentTurnIndex % _turnOrder.Count;
_turnOrder[_currentTurnIndex]();
}
private void PlayerAttack()
{
Attack(_user, _enemy);
// TODO: disable hud
attackButton.interactable = false;
FinishTurn();
}
private void EnemyAttack()
{
Attack(_enemy, _user);
FinishTurn();
}
private void UserTakeTurn()
{
// TODO: enable hud.
attackButton.interactable = true;
}
private void Attack(Player attacker, Player victim)
{
victim.Health -= attacker.Damage;
victim.UpdateHealth();
if (victim.Health <= 0)
{
Debug.Log("The victim died!");
}
}
}