ProjetAMJV_CR/Assets/Scripts/AttackHandler.cs
Crizomb 0c0b9287ce Fix physics with navmesh agent
- desactivate navmesh temporarly when knockback (MovementHandler TakeImpulse)

- Delete unit when HealthHandler Death called

- Made 150 units fight (for performance test)
2025-01-09 01:03:04 +01:00

85 lines
2.6 KiB
C#

using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Serialization;
using Random = UnityEngine.Random;
[RequireComponent(typeof(Unit))]
public class AttackHandler : MonoBehaviour
{
[SerializeField] private float damage;
[SerializeField] private float cooldown;
[SerializeField] private Collider attackShape;
[SerializeField] private float knockbackHorizontalForce;
[SerializeField] private float knockbackVerticalForce;
private float _timer;
private Unit _unit;
void Awake()
{
_unit = GetComponent<Unit>();
}
void Start()
{
// Random to avoid too much synchronicity
_timer = cooldown + Random.Range(-cooldown*0.2f, cooldown*0.2f);
}
void Update()
{
_timer = _timer - Time.deltaTime;
Attack();
}
/// <summary>
/// Launch an Attack, and return true if it's possible to attack
/// see what Units are in the attackShape, apply damage and knockback to those unit if they're ennemies
/// </summary>
public bool Attack()
{
if (_timer > 0) return false;
Collider[] targets = DetectTargets();
foreach (Collider target in targets)
{
if (!target.CompareTag("Unit")) continue;
// GetComponent is expensive in performance, optimize here if it's slow
Unit targetUnit = target.GetComponent<Unit>();
// No friendly fire
if (targetUnit.IsTeamA == _unit.IsTeamA) continue;
targetUnit.Health.TakeDamage(damage);
Vector3 knockbackVector = knockbackHorizontalForce * (target.transform.position - transform.position).normalized
+ knockbackVerticalForce * Vector3.up;
targetUnit.StartCoroutine(targetUnit.Move.TakeImpulse(knockbackVector));
}
_timer = cooldown + Random.Range(-cooldown*0.2f, cooldown*0.2f);
return true;
}
private Collider[] DetectTargets()
{
Collider[] hitColliders;
switch (attackShape)
{
case SphereCollider sphere:
hitColliders = Physics.OverlapSphere(sphere.transform.position, sphere.radius, sphere.includeLayers);
break;
case BoxCollider box:
hitColliders = Physics.OverlapBox(box.bounds.center, box.bounds.extents, box.transform.rotation, box.includeLayers);
break;
default:
throw new ArgumentException("Only sphere or box are supported");
}
return hitColliders;
}
}