HitBox 暴露 TargetLayers 运行时读写。Projectile 缓存预制体初始目标层并在 Initialize 还原(对象池复用不被上一发弹反污染);ReflectAsPlayerProjectile 时目标层随阵营翻转(PlayerHurtBox→EnemyHurtBox,保留可破坏物等其他位)。 ParryableProjectile 绕过 HitBox 自行判定,补上同样的目标层过滤;并修复其弹反分支不切 PlayerProjectile 层的问题——原先反射后仍留在 EnemyProjectile 层,碰撞矩阵 EnemyProjectile↔EnemyHurtBox 不碰撞,反射弹永远打不中敌人。 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
68 lines
2.7 KiB
C#
68 lines
2.7 KiB
C#
using UnityEngine;
|
|
using BaseGames.Parry;
|
|
|
|
namespace BaseGames.Combat
|
|
{
|
|
/// <summary>
|
|
/// 可被玩家弹反的抛射物。
|
|
/// 触发时优先检测弹反窗口;若成功弹反则反向飞行并可切换伤害源;
|
|
/// 否则走正常伤害流水线。
|
|
/// </summary>
|
|
public class ParryableProjectile : LinearProjectile
|
|
{
|
|
[SerializeField] private DamageSourceSO _reflectSource;
|
|
|
|
private bool _reflected;
|
|
|
|
protected override void OnInitialized()
|
|
{
|
|
// 禁用子 HitBox 的自动检测,改由本组件的 OnTriggerEnter2D 手动处理,
|
|
// 以便在命中前插入弹反判断。
|
|
_hitBox.Deactivate();
|
|
_rb.velocity = Direction * _config.Speed;
|
|
}
|
|
|
|
private void OnTriggerEnter2D(Collider2D other)
|
|
{
|
|
// ── 弹反判断 ────────────────────────────────────────────────
|
|
if (!_reflected)
|
|
{
|
|
var parrySystem = other.GetComponentInParent<ParrySystem>();
|
|
if (parrySystem != null && parrySystem.IsParrying && parrySystem.ConsumeParry())
|
|
{
|
|
_reflected = true;
|
|
Direction = -Direction;
|
|
_rb.velocity = Direction * _config.Speed * _config.ParrySpeedMultiplier;
|
|
|
|
// 阵营随弹反翻转:切到 PlayerProjectile 层(否则碰撞矩阵
|
|
// EnemyProjectile↔EnemyHurtBox 不碰撞,反射后永远打不中敌人),
|
|
// 伤害目标层同步从玩家侧切到敌人侧。
|
|
int playerProjLayer = LayerMask.NameToLayer("PlayerProjectile");
|
|
if (playerProjLayer >= 0) gameObject.layer = playerProjLayer;
|
|
RetargetToEnemyFaction();
|
|
|
|
if (_reflectSource != null)
|
|
DamageInfo = DamageInfo.From(_reflectSource);
|
|
return;
|
|
}
|
|
}
|
|
|
|
// ── 正常命中 ─────────────────────────────────────────────────
|
|
// 本类绕过 HitBox 自行判定,需自行套用其伤害目标层过滤
|
|
if ((_hitBox.TargetLayers.value & (1 << other.gameObject.layer)) == 0) return;
|
|
var hurtBox = other.GetComponent<HurtBox>();
|
|
if (hurtBox != null)
|
|
{
|
|
hurtBox.ReceiveDamage(DamageInfo);
|
|
ReturnToPool();
|
|
}
|
|
}
|
|
|
|
protected override void OnDisable()
|
|
{
|
|
base.OnDisable();
|
|
_reflected = false;
|
|
}
|
|
}
|
|
}
|