2020-05-17 00:14:00 +00:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
using UnityEngine.Events;
|
|
|
|
|
|
|
|
|
|
namespace SanAndreasUnity.Utilities
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
public class DamageInfo
|
|
|
|
|
{
|
|
|
|
|
public float amount = 0f;
|
|
|
|
|
public Transform raycastHitTransform = null;
|
2020-05-18 23:00:24 +00:00
|
|
|
|
public object attacker = null;
|
2020-05-17 00:14:00 +00:00
|
|
|
|
public object data = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class Damageable : MonoBehaviour
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
[SerializeField] private float m_health = 0f;
|
|
|
|
|
public float Health { get { return m_health; } set { m_health = value; } }
|
|
|
|
|
|
|
|
|
|
[SerializeField] private UnityEvent m_onDamage = new UnityEvent ();
|
2020-06-09 16:34:15 +00:00
|
|
|
|
public UnityEvent OnDamageEvent => m_onDamage;
|
2020-05-17 00:14:00 +00:00
|
|
|
|
|
|
|
|
|
public DamageInfo LastDamageInfo { get; private set; }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void Damage (DamageInfo info)
|
|
|
|
|
{
|
|
|
|
|
this.LastDamageInfo = info;
|
2020-05-17 00:17:52 +00:00
|
|
|
|
|
|
|
|
|
F.RunExceptionSafe(() => m_onDamage.Invoke());
|
2020-05-17 00:14:00 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void HandleDamageByDefault ()
|
|
|
|
|
{
|
|
|
|
|
DamageInfo info = this.LastDamageInfo;
|
|
|
|
|
|
|
|
|
|
this.Health -= info.amount;
|
|
|
|
|
|
|
|
|
|
if (this.Health <= 0f) {
|
|
|
|
|
Destroy (this.gameObject);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-28 14:35:35 +00:00
|
|
|
|
public static void InflictDamageToObjectsInArea(
|
|
|
|
|
Vector3 center, float radius, float damageAmount, AnimationCurve damageOverDistanceCurve)
|
2020-06-21 16:21:15 +00:00
|
|
|
|
{
|
2020-06-27 19:55:04 +00:00
|
|
|
|
Collider[] overlappingColliders = Physics.OverlapSphere(center, radius);
|
2020-06-21 16:21:15 +00:00
|
|
|
|
|
2020-06-27 19:55:04 +00:00
|
|
|
|
var damagables = new Dictionary<Damageable, List<Collider>>();
|
2020-06-21 16:21:15 +00:00
|
|
|
|
|
2020-06-27 19:55:04 +00:00
|
|
|
|
foreach (var collider in overlappingColliders)
|
2020-06-21 16:21:15 +00:00
|
|
|
|
{
|
|
|
|
|
var damagable = collider.GetComponentInParent<Damageable>();
|
2020-06-27 19:55:04 +00:00
|
|
|
|
if (damagable != null)
|
2020-06-21 16:21:15 +00:00
|
|
|
|
{
|
2020-06-27 19:55:04 +00:00
|
|
|
|
if (damagables.ContainsKey(damagable))
|
|
|
|
|
{
|
|
|
|
|
damagables[damagable].Add(collider);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
damagables.Add(damagable, new List<Collider>() { collider });
|
|
|
|
|
}
|
2020-06-21 16:21:15 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-27 19:55:04 +00:00
|
|
|
|
foreach (var pair in damagables)
|
2020-06-21 16:21:15 +00:00
|
|
|
|
{
|
2020-06-27 19:55:04 +00:00
|
|
|
|
Damageable damageable = pair.Key;
|
|
|
|
|
List<Collider> colliders = pair.Value;
|
2020-06-21 16:21:15 +00:00
|
|
|
|
|
2020-06-27 19:55:04 +00:00
|
|
|
|
// find closest point from all colliders
|
2020-06-21 16:21:15 +00:00
|
|
|
|
|
2020-06-27 19:55:04 +00:00
|
|
|
|
float closestPointDistance = float.MaxValue;
|
|
|
|
|
Collider closestPointCollider = null;
|
|
|
|
|
|
|
|
|
|
foreach (var collider in pair.Value)
|
|
|
|
|
{
|
2020-06-28 18:38:23 +00:00
|
|
|
|
Vector3 closestPointOnCollider = collider.ClosestPointOrBoundsCenter(center);
|
2020-06-27 19:55:04 +00:00
|
|
|
|
float distanceToPointOnCollider = Vector3.Distance(center, closestPointOnCollider);
|
|
|
|
|
float distanceToColliderTransform = Vector3.Distance(center, collider.transform.position);
|
|
|
|
|
|
|
|
|
|
float minDistance = Mathf.Min(distanceToPointOnCollider, distanceToColliderTransform);
|
|
|
|
|
|
|
|
|
|
if (minDistance < closestPointDistance)
|
|
|
|
|
{
|
|
|
|
|
closestPointDistance = minDistance;
|
|
|
|
|
closestPointCollider = collider;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// apply damage based on closest distance
|
|
|
|
|
|
|
|
|
|
float distance = closestPointDistance;
|
2020-06-28 14:35:35 +00:00
|
|
|
|
float distanceFactor = damageOverDistanceCurve.Evaluate(Mathf.Clamp01(distance / radius));
|
2020-06-21 16:21:15 +00:00
|
|
|
|
float damageAmountBasedOnDistance = damageAmount * distanceFactor;
|
|
|
|
|
|
|
|
|
|
F.RunExceptionSafe(() => damageable.Damage(new DamageInfo() { amount = damageAmountBasedOnDistance }));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-17 00:14:00 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|