Start AI COntroller
This commit is contained in:
@@ -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.
|
||||
}
|
||||
}
|
||||
@@ -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");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
32
Assets/Scripts/Combat/Health.cs
Normal file
32
Assets/Scripts/Combat/Health.cs
Normal 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");
|
||||
}
|
||||
}
|
||||
}
|
||||
2
Assets/Scripts/Combat/Health.cs.meta
Normal file
2
Assets/Scripts/Combat/Health.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 8fc6fa8368af1d9b8a0ecc2745f48ebe
|
||||
Reference in New Issue
Block a user