using UnityEngine;
using TowerDefense.Towers.Projectiles;
using TowerDefense.Agents;
using ActionGameFramework.Projectiles;
namespace ActionGameFramework.Health
{
///
/// Damage collider - a collider based implementation of DamageZone
///
[RequireComponent(typeof(Collider))]
public class DamageCollider : DamageZone
{
///
/// On collision enter, see if the colliding object has a Damager and then make the damageableBehaviour take damage
///
/// The collider
protected void OnCollisionEnter(Collision c)
{
var damager = c.gameObject.GetComponent();
if (damager == null)
{
return;
}
// 非Agent不参与碰撞:
var agent = this.gameObject.GetComponent();
if (agent == null)
return;
LazyLoad();
BallisticAttack ballisticAttack = damager.GetComponent();
Vector3 collisionPosition = ConvertContactsToPosition(c.contacts);
ballisticAttack.DealDamage((Targetable)damageableBehaviour);
damager.HasDamaged(collisionPosition, damageableBehaviour.configuration.alignmentProvider);
}
protected void OnTriggerEnter(Collider c)
{
var damager = c.gameObject.GetComponent();
if (damager == null)
{
return;
}
// 非Projectile不参与此次的碰撞
var projec = c.gameObject.GetComponent();
if (projec == null)
return;
// 非Agent不参与碰撞:
var agent = this.gameObject.GetComponent();
if (agent == null)
return;
BallisticProjectile ballistic = c.gameObject.GetComponent();
if (ballistic.IsHitEnemy)
{
Debug.LogError("--------------------- 已发生过碰撞 ---------------------");
return;
}
ballistic.IsHitEnemy = true;
LazyLoad();
BallisticAttack ballisticAttack = damager.GetComponent();
ballisticAttack.DealDamage((Targetable)damageableBehaviour);
damager.HasDamaged(c.transform.position, damageableBehaviour.configuration.alignmentProvider);
}
///
/// Averages the contacts to get the position.
///
/// The average position.
/// Contacts.
protected Vector3 ConvertContactsToPosition(ContactPoint[] contacts)
{
Vector3 output = Vector3.zero;
int length = contacts.Length;
if (length == 0)
{
return output;
}
for (int i = 0; i < length; i++)
{
output += contacts[i].point;
}
output = output / length;
return output;
}
}
}