Start AI COntroller

This commit is contained in:
Caleb Sandford deQuincey
2025-10-28 17:26:07 +00:00
parent bc21e94bc1
commit 1972134c81
724 changed files with 579740 additions and 2918 deletions

View File

@@ -1,8 +1,11 @@
using UnityEngine;
using RPG.Combat;
public class CombatTarget : MonoBehaviour
namespace RPG.Combat
{
// This class is intentionally left empty.
// It serves as a marker to identify combat targets in the game.
[RequireComponent(typeof(Health))]
public class CombatTarget : MonoBehaviour
{
// This class is intentionally left empty.
// It serves as a marker to identify combat targets in the game.
}
}

View File

@@ -1,39 +1,85 @@
using UnityEngine;
using RPG.Movement;
using UnityEngine.Rendering;
using RPG.Core;
namespace RPG.Combat
{
public class Fighter : MonoBehaviour
public class Fighter : MonoBehaviour, IAction
{
[SerializeField]
private float weaponRange = 2f;
Transform target;
private void Update()
[SerializeField] private float timeBetweenAttacks = 1f;
[SerializeField] private float weaponRange = 2f;
[SerializeField] private float weaponDamage = 5f;
Health target;
private float timeSinceLastAttack = 0;
void Update()
{
timeSinceLastAttack += Time.deltaTime;
if (target == null) return;
if (target.IsDead()) return;
if (!GetIsInRange())
{
GetComponent<Mover>().MoveTo(target.position);
GetComponent<Mover>().MoveTo(target.transform.position);
}
else
{
GetComponent<Mover>().Stop();
GetComponent<Mover>().Cancel();
AttackBehaviour();
}
}
private void AttackBehaviour()
{
transform.LookAt(target.transform);
if (timeSinceLastAttack > timeBetweenAttacks)
{
//This will trigger the Hit() event.
TriggerAttack();
timeSinceLastAttack = 0;
}
else return;
}
private void TriggerAttack()
{
GetComponent<Animator>().ResetTrigger("stopAttack");
GetComponent<Animator>().SetTrigger("attack");
}
//Animation event
void Hit()
{
if (target == null) return;
target.TakeDamage(weaponDamage);
}
private bool GetIsInRange()
{
return Vector3.Distance(transform.position, target.position) <= weaponRange;
return Vector3.Distance(transform.position, target.transform.position) <= weaponRange;
}
public void Attack(CombatTarget combatTarget)
public bool CanAttack(GameObject combatTarget)
{
target = combatTarget.transform;
if (combatTarget == null) return false;
Health targetToTest = combatTarget.GetComponent<Health>();
return targetToTest != null && !targetToTest.IsDead();
}
public void Attack(GameObject combatTarget)
{
GetComponent<ActionScheduler>().StartAction(this);
target = combatTarget.GetComponent<Health>();
}
public void Cancel()
{
target = null;
StopAttack();
target = null;
}
private void StopAttack()
{
GetComponent<Animator>().ResetTrigger("attack");
GetComponent<Animator>().SetTrigger("stopAttack");
}
}
}

View File

@@ -0,0 +1,32 @@
using Unity.VisualScripting;
using UnityEngine;
namespace RPG.Combat
{
public class Health : MonoBehaviour
{
[SerializeField] float health = 100f;
bool isDead = false;
public bool IsDead()
{
return isDead;
}
public void TakeDamage(float damage)
{
health = Mathf.Max(health - damage, 0);
if(health == 0)
{
Die();
}
}
private void Die()
{
if (isDead) return;
isDead = true;
GetComponent<Animator>().SetTrigger("die");
}
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 8fc6fa8368af1d9b8a0ecc2745f48ebe