Extremely basic turn based combat.
Need to add multiple characters. need a way to select a target. need a team concept so you can't target friendlies. need an ability system. need Kenny art
This commit is contained in:
@@ -0,0 +1,92 @@
|
||||
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!");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 32b02efcd32730c47a01bb1ad6d469d1
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,11 @@
|
||||
using System;
|
||||
|
||||
public class EnemyAi
|
||||
{
|
||||
public Action AttackCallback;
|
||||
|
||||
public void TakeTurn()
|
||||
{
|
||||
AttackCallback();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 0bfd8c7d2aecc6a4aa28055221bf8e32
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user