解决换阵营子弹碰撞问题

This commit is contained in:
小李xl 2024-04-04 11:58:13 +08:00
parent 84a0b1d608
commit ff7712db8f
10 changed files with 24 additions and 21 deletions

View File

@ -21,19 +21,15 @@ public partial class HurtArea : Area2D, IHurt
Monitoring = false; Monitoring = false;
} }
public bool CanHurt(ActivityObject target) public bool CanHurt(CampEnum targetCamp)
{ {
//无敌状态 //无敌状态
if (Master.Invincible) if (Master.Invincible)
{ {
return true; return true;
} }
if (target is Role role) return Master.IsEnemy(targetCamp);
{
return Master.IsEnemy(role);
}
return true;
} }
public void Hurt(ActivityObject target, int damage, float angle) public void Hurt(ActivityObject target, int damage, float angle)

View File

@ -6,8 +6,8 @@ public interface IHurt
/// <summary> /// <summary>
/// 返回是否可以造成伤害 /// 返回是否可以造成伤害
/// </summary> /// </summary>
/// <param name="target">触发伤害的对象, 为 null 表示不存在对象或者对象已经被销毁</param> /// <param name="targetCamp">攻击目标所属层级</param>
bool CanHurt(ActivityObject target); bool CanHurt(CampEnum targetCamp);
/// <summary> /// <summary>
/// 受到伤害 /// 受到伤害

View File

@ -29,6 +29,11 @@ public partial class Explode : Area2D, IPoolItem
/// 产生爆炸的子弹数据 /// 产生爆炸的子弹数据
/// </summary> /// </summary>
public BulletData BulletData { get; private set; } public BulletData BulletData { get; private set; }
/// <summary>
/// 所属阵营
/// </summary>
public CampEnum Camp { get; private set; }
private bool _init = false; private bool _init = false;
private float _hitRadius; private float _hitRadius;
@ -56,11 +61,12 @@ public partial class Explode : Area2D, IPoolItem
/// 初始化爆炸数据 /// 初始化爆炸数据
/// </summary> /// </summary>
/// <param name="bulletData">产生爆炸的子弹数据</param> /// <param name="bulletData">产生爆炸的子弹数据</param>
/// <param name="camp">所属阵营</param>
/// <param name="hitRadius">伤害半径</param> /// <param name="hitRadius">伤害半径</param>
/// <param name="harm">造成的伤害</param> /// <param name="harm">造成的伤害</param>
/// <param name="repelledRadius">击退半径</param> /// <param name="repelledRadius">击退半径</param>
/// <param name="maxRepelled">最大击退速度</param> /// <param name="maxRepelled">最大击退速度</param>
public void Init(BulletData bulletData, float hitRadius, int harm, float repelledRadius, float maxRepelled) public void Init(BulletData bulletData, CampEnum camp, float hitRadius, int harm, float repelledRadius, float maxRepelled)
{ {
if (!_init) if (!_init)
{ {
@ -73,6 +79,7 @@ public partial class Explode : Area2D, IPoolItem
BodyEntered += OnBodyEntered; BodyEntered += OnBodyEntered;
} }
Camp = camp;
BulletData = bulletData; BulletData = bulletData;
_hitRadius = hitRadius; _hitRadius = hitRadius;
_harm = harm; _harm = harm;
@ -159,9 +166,9 @@ public partial class Explode : Area2D, IPoolItem
var len = temp.Length(); var len = temp.Length();
var angle = temp.Angle(); var angle = temp.Angle();
var target = BulletData.TriggerRole.IsDestroyed ? null : BulletData.TriggerRole; if (hurt.CanHurt(Camp))
if (hurt.CanHurt(target))
{ {
var target = BulletData.TriggerRole.IsDestroyed ? null : BulletData.TriggerRole;
if (len <= _hitRadius) //在伤害半径内 if (len <= _hitRadius) //在伤害半径内
{ {
hurt.Hurt(target, _harm, angle); hurt.Hurt(target, _harm, angle);

View File

@ -215,8 +215,7 @@ public partial class Laser : Area2D, IBullet
private void HandlerCollision(IHurt hurt) private void HandlerCollision(IHurt hurt)
{ {
var target = BulletData.TriggerRole.IsDestroyed ? null : BulletData.TriggerRole; if (hurt.CanHurt(Camp))
if (hurt.CanHurt(target))
{ {
if (BulletData.Repel != 0) if (BulletData.Repel != 0)
{ {
@ -228,6 +227,7 @@ public partial class Laser : Area2D, IBullet
} }
//造成伤害 //造成伤害
var target = BulletData.TriggerRole.IsDestroyed ? null : BulletData.TriggerRole;
hurt.Hurt(target, BulletData.Harm, Rotation); hurt.Hurt(target, BulletData.Harm, Rotation);
} }
} }

View File

@ -59,7 +59,7 @@ public partial class BoomBullet : Bullet
explode.Position = pos; explode.Position = pos;
explode.RotationDegrees = Utils.Random.RandomRangeInt(0, 360); explode.RotationDegrees = Utils.Random.RandomRangeInt(0, 360);
explode.AddToActivityRootDeferred(RoomLayerEnum.YSortLayer); explode.AddToActivityRootDeferred(RoomLayerEnum.YSortLayer);
explode.Init(BulletData, 25, BulletData.Harm, 50, BulletData.Repel); explode.Init(BulletData, Camp, 25, BulletData.Harm, 50, BulletData.Repel);
explode.RunPlay(BulletData.TriggerRole); explode.RunPlay(BulletData.TriggerRole);
if (AffiliationArea != null) if (AffiliationArea != null)
{ {

View File

@ -189,8 +189,7 @@ public partial class Bullet : ActivityObject, IBullet
/// </summary> /// </summary>
public virtual void OnCollisionTarget(IHurt hurt) public virtual void OnCollisionTarget(IHurt hurt)
{ {
var target = BulletData.TriggerRole.IsDestroyed ? null : BulletData.TriggerRole; if (hurt.CanHurt(Camp))
if (hurt.CanHurt(target))
{ {
OnPlayDisappearEffect(); OnPlayDisappearEffect();
if (BulletData.Repel != 0) if (BulletData.Repel != 0)
@ -203,6 +202,7 @@ public partial class Bullet : ActivityObject, IBullet
} }
//造成伤害 //造成伤害
var target = BulletData.TriggerRole.IsDestroyed ? null : BulletData.TriggerRole;
hurt.Hurt(target, BulletData.Harm, Rotation); hurt.Hurt(target, BulletData.Harm, Rotation);
//穿透次数 //穿透次数

View File

@ -7,7 +7,7 @@ using Godot;
[Tool] [Tool]
public partial class ObstacleObject : ActivityObject, IHurt public partial class ObstacleObject : ActivityObject, IHurt
{ {
public virtual bool CanHurt(ActivityObject target) public virtual bool CanHurt(CampEnum targetCamp)
{ {
return true; return true;
} }

View File

@ -1422,7 +1422,7 @@ public abstract partial class Role : ActivityObject
private void HandlerCollision(IHurt hurt, Weapon activeWeapon) private void HandlerCollision(IHurt hurt, Weapon activeWeapon)
{ {
if (hurt.CanHurt(this)) if (hurt.CanHurt(Camp))
{ {
var damage = Utils.Random.RandomConfigRange(activeWeapon.Attribute.MeleeAttackHarmRange); var damage = Utils.Random.RandomConfigRange(activeWeapon.Attribute.MeleeAttackHarmRange);
damage = RoleState.CalcDamage(damage); damage = RoleState.CalcDamage(damage);

View File

@ -225,7 +225,7 @@ public partial class Player : Role
foreach (var enemy in enemyList) foreach (var enemy in enemyList)
{ {
var hurt = ((Enemy)enemy).HurtArea; var hurt = ((Enemy)enemy).HurtArea;
if (hurt.CanHurt(this)) if (hurt.CanHurt(Camp))
{ {
hurt.Hurt(this, 1000, 0); hurt.Hurt(this, 1000, 0);
} }

View File

@ -159,7 +159,7 @@ public partial class Knife : Weapon
private void HandlerCollision(IHurt hurt) private void HandlerCollision(IHurt hurt)
{ {
if (hurt.CanHurt(TriggerRole)) if (TriggerRole == null || hurt.CanHurt(TriggerRole.Camp))
{ {
var damage = Utils.Random.RandomConfigRange(Attribute.Bullet.HarmRange); var damage = Utils.Random.RandomConfigRange(Attribute.Bullet.HarmRange);
//计算子弹造成的伤害 //计算子弹造成的伤害