using UnityEngine;
|
using TowerDefense.Towers.Projectiles;
|
using TowerDefense.Agents;
|
using ActionGameFramework.Projectiles;
|
|
namespace ActionGameFramework.Health
|
{
|
/// <summary>
|
/// Damage collider - a collider based implementation of DamageZone
|
/// </summary>
|
[RequireComponent(typeof(Collider))]
|
public class DamageCollider : DamageZone
|
{
|
/// <summary>
|
/// On collision enter, see if the colliding object has a Damager and then make the damageableBehaviour take damage
|
/// </summary>
|
/// <param name="c">The collider</param>
|
protected void OnCollisionEnter(Collision c)
|
{
|
var damager = c.gameObject.GetComponent<Damager>();
|
if (damager == null)
|
{
|
return;
|
}
|
|
// 非Agent不参与碰撞:
|
var agent = this.gameObject.GetComponent<Agent>();
|
if (agent == null)
|
return;
|
|
LazyLoad();
|
|
BallisticAttack ballisticAttack = damager.GetComponent<BallisticAttack>();
|
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<Damager>();
|
if (damager == null)
|
{
|
return;
|
}
|
|
// 非Projectile不参与此次的碰撞
|
var projec = c.gameObject.GetComponent<IProjectile>();
|
if (projec == null)
|
return;
|
|
// 非Agent不参与碰撞:
|
var agent = this.gameObject.GetComponent<Agent>();
|
if (agent == null || agent.isDead)
|
return;
|
|
BallisticProjectile ballistic = c.gameObject.GetComponent<BallisticProjectile>();
|
|
LazyLoad();
|
|
BallisticAttack ballisticAttack = damager.GetComponent<BallisticAttack>();
|
ballisticAttack.DealDamage((Targetable)damageableBehaviour, damager);
|
|
damager.HasDamaged(c.transform.position, damageableBehaviour.configuration.alignmentProvider);
|
}
|
|
/// <summary>
|
/// Averages the contacts to get the position.
|
/// </summary>
|
/// <returns>The average position.</returns>
|
/// <param name="contacts">Contacts.</param>
|
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;
|
}
|
}
|
}
|