WXMC/.svn/pristine/53/53ee62df3cfda531533c6ad311c8e4d72c0be4ed.svn-base

82 lines
2.7 KiB
Plaintext
Raw Normal View History

2024-12-04 16:18:46 +08:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ProjectileCollideHurtHandler : PHomeProjectileCollideEventHandler
{
public float Damage;
public float PushForce;
public bool DestorySelf;
public bool OutForce;
private Dictionary<PHomeProjectile,PHomeUnit> m_preEnemyDic = new Dictionary<PHomeProjectile, PHomeUnit>();
protected Vector2 m_point;
protected Collider2D[] colliders;
public override void HandleEvent(ProjectileCollideContext context)
{
base.HandleEvent(context);
if(!DestorySelf)
{
if(!m_preEnemyDic.ContainsKey(context.Projectile))
{
m_preEnemyDic.Add(context.Projectile,context.OtherUnit);
}
}
if (!context.Projectile.HasCollision || context.OtherUnit == null || !PHomeGameMode.IsEnemy(context.Projectile.Instigator, context.OtherUnit))
{
if(!DestorySelf)
{
m_preEnemyDic[context.Projectile] = null;
}
return;
}
if (!DestorySelf && m_preEnemyDic[context.Projectile] == context.OtherUnit)
{
return;
}
PHomeGameMode.main.DoProjectileAttack(context.Projectile.Instigator, context.OtherUnit, context.Projectile, Damage + context.Projectile.GetWeaponDamage());
AddEnemyForce(context.Projectile, context.OtherUnit.GetComponentInChildren<Collider2D>());
if(!DestorySelf)
{
m_preEnemyDic[context.Projectile] = context.OtherUnit;
}
if (DestorySelf)
{
if(m_preEnemyDic.ContainsKey(context.Projectile))
{
m_preEnemyDic.Remove(context.Projectile);
}
Destroy(context.Projectile.gameObject);
}
}
protected virtual void AddEnemyForce(PHomeProjectile projectile, Collider2D result)
{
if (PushForce <= 0) return;
Vector3 dir = Vector3.zero;
if (projectile.GetComponent<PhysicsMovement2D>() != null)
{
dir = projectile.GetComponent<PhysicsMovement2D>().DestVelocity;
}
if (projectile.GetComponent<PHomeProjectileMover>() != null)
{
dir = projectile.GetComponent<PHomeProjectileMover>().DestVelocity;
}
if (OutForce)
{
dir = projectile.GetOutDir();
}
result.attachedRigidbody.AddForce(dir.normalized * PushForce);
}
public override void HandleDestroyEvent(ProjectileDestroyContext context)
{
if(m_preEnemyDic.ContainsKey(context.Projectile))
{
m_preEnemyDic.Remove(context.Projectile);
}
}
}