整理武器配置和重构

This commit is contained in:
2026-09-15 10:13:58 +08:00
parent 17ee268416
commit 421f8bf619
115 changed files with 1431 additions and 1905 deletions
@@ -1,31 +0,0 @@
namespace SepCore.Entity
{
public partial class WeaponLightning
{
private class AttackState : WeaponStateBase
{
private WeaponLightning _weapon;
public override WeaponStateType State => WeaponStateType.Attack;
public override void OnInit(WeaponBase weapon) => _weapon = weapon as WeaponLightning;
public override void OnEnter()
{
_weapon.ResetAttackCooldown(_weapon._weaponData.Cooldown);
_weapon.Attack();
}
public override void OnUpdate(float elapseSeconds, float realElapseSeconds)
{
if (!_weapon._isAttacking)
{
_weapon.TransitionTo(WeaponStateType.Check_InRange);
}
}
public override void OnLeave()
{
}
}
}
}
@@ -1,48 +0,0 @@
namespace SepCore.Entity
{
public partial class WeaponLightning
{
private class CheckInRangeState : WeaponStateBase
{
private WeaponLightning _weapon;
public override WeaponStateType State => WeaponStateType.Check_InRange;
public override void OnInit(WeaponBase weapon) => _weapon = weapon as WeaponLightning;
public override void OnEnter()
{
if (_weapon._canAttack)
{
_weapon.TransitionTo(WeaponStateType.Attack);
}
}
public override void OnUpdate(float elapseSeconds, float realElapseSeconds)
{
_weapon.Check();
_weapon.RotateToTarget(elapseSeconds);
if (_weapon._target == null || !_weapon._target.Available)
{
_weapon.TransitionTo(WeaponStateType.Idle);
return;
}
if (!_weapon.IsInRange(_weapon._target, _weapon._sqrRange))
{
_weapon.TransitionTo(WeaponStateType.Check_OutRange);
return;
}
if (_weapon._canAttack)
{
_weapon.TransitionTo(WeaponStateType.Attack);
}
}
public override void OnLeave()
{
}
}
}
}
@@ -1,38 +0,0 @@
namespace SepCore.Entity
{
public partial class WeaponLightning
{
private class CheckOutRangeState : WeaponStateBase
{
private WeaponLightning _weapon;
public override WeaponStateType State => WeaponStateType.Check_OutRange;
public override void OnInit(WeaponBase weapon) => _weapon = weapon as WeaponLightning;
public override void OnEnter()
{
}
public override void OnUpdate(float elapseSeconds, float realElapseSeconds)
{
_weapon.Check();
_weapon.RotateToTarget(elapseSeconds);
if (_weapon._target == null || !_weapon._target.Available)
{
_weapon.TransitionTo(WeaponStateType.Idle);
return;
}
if (_weapon.IsInRange(_weapon._target, _weapon._sqrRange))
{
_weapon.TransitionTo(WeaponStateType.Check_InRange);
}
}
public override void OnLeave()
{
}
}
}
}
@@ -1,32 +0,0 @@
namespace SepCore.Entity
{
public partial class WeaponLightning
{
private class IdleState : WeaponStateBase
{
private WeaponLightning _weapon;
public override WeaponStateType State => WeaponStateType.Idle;
public override void OnInit(WeaponBase weapon) => _weapon = weapon as WeaponLightning;
public override void OnEnter()
{
}
public override void OnUpdate(float elapseSeconds, float realElapseSeconds)
{
_weapon.Check();
_weapon.RotateToOrigin(elapseSeconds);
if (_weapon._target != null && _weapon._target.Available)
{
_weapon.TransitionTo(WeaponStateType.Check_OutRange);
}
}
public override void OnLeave()
{
}
}
}
}
@@ -8,13 +8,8 @@ using UnityGameFramework.Runtime;
namespace SepCore.Entity
{
public partial class WeaponLightning : WeaponBase
public class WeaponLightning : WeaponBase
{
private WeaponLightningData _weaponData;
private Quaternion _cachedRotation;
[SerializeField] private float _rotateSpeed = 5f;
[SerializeField] private float _takeOffDuration = 0.3f;
[SerializeField] private float _flyToTargetDuration = 0.2f;
[SerializeField] private float _strikeDuration = 0.1f;
@@ -22,7 +17,6 @@ namespace SepCore.Entity
[SerializeField] private float _hoverHeight = 15f;
[SerializeField] private LayerMask _hitMask = ~0;
[SerializeField] private int _maxHitColliders = 32;
private Sequence _attackSequence;
@@ -32,25 +26,19 @@ namespace SepCore.Entity
private Collider[] _hitResults;
private readonly HashSet<int> _hitEntityIds = new();
private float _hitRadius;
private float _hitRadiusSqr;
public float HitRadius => WeaponData.GetParamFloat(ParamType.HitRadius);
private IWeaponAttackEffect _attackEffect;
public override ImpactData GetImpactData()
{
return new ImpactData(_weaponData.OwnerCamp, _weaponData.Attack, AttackStat);
}
protected override void BuildStates()
{
RegisterState(new IdleState());
RegisterState(new CheckOutRangeState());
RegisterState(new CheckInRangeState());
RegisterState(new AttackState());
RegisterState(new WeaponLightningIdleState());
RegisterState(new WeaponLightningSeekState());
RegisterState(new WeaponLightningAimState());
RegisterState(new WeaponLightningAttackState());
}
protected override void Attack()
public override void Attack()
{
StopAttackTween(false);
FaceTargetImmediately();
@@ -70,7 +58,7 @@ namespace SepCore.Entity
_attackSequence.Append(CachedTransform.DOMove(_lockedStrikePoint, _strikeDuration).SetEase(Ease.InQuad));
_attackSequence.AppendCallback(() =>
{
_attackEffect?.Play(this, _lockedStrikePoint, _target, _hitRadius);
_attackEffect.Play(this, _lockedStrikePoint, _target, HitRadius);
ApplyGroundAreaDamage();
if (_attackParent != null)
@@ -87,11 +75,6 @@ namespace SepCore.Entity
});
}
protected override void Check()
{
_target = SelectTarget(_sqrRange);
}
private Vector3 ResolveStrikePoint()
{
if (_target != null && _target.Available)
@@ -104,19 +87,20 @@ namespace SepCore.Entity
private void ApplyGroundAreaDamage()
{
if (_hitRadius <= 0f) return;
float radius = HitRadius;
if (radius <= 0f) return;
if (TryQueueAreaCollisionQuery(_lockedStrikePoint, _hitRadius, Mathf.Max(1, _maxHitColliders)))
if (TryQueueAreaCollisionQuery(_lockedStrikePoint, radius, Mathf.Max(1, _maxHitColliders)))
{
_hitEntityIds.Clear();
return;
}
if (_hitResults == null || _hitResults.Length == 0) return;
int hitCount = Physics.OverlapSphereNonAlloc(_lockedStrikePoint, _hitRadius, _hitResults, _hitMask,
int hitCount = Physics.OverlapSphereNonAlloc(_lockedStrikePoint, radius, _hitResults, _hitMask,
QueryTriggerInteraction.Collide);
float radiusSqr = radius * radius;
_hitEntityIds.Clear();
for (int i = 0; i < hitCount; i++)
{
@@ -129,61 +113,14 @@ namespace SepCore.Entity
Vector3 delta = targetable.CachedTransform.position - _lockedStrikePoint;
delta.y = 0f;
if (delta.sqrMagnitude > _hitRadiusSqr) continue;
if (delta.sqrMagnitude > radiusSqr) continue;
AIUtility.PerformCollision(targetable, this);
}
}
private void RotateToTarget(float elapseSeconds)
{
if (_target == null || !_target.Available) return;
Vector3 directionToTarget = _target.CachedTransform.position - CachedTransform.position;
directionToTarget.y = 0f;
if (directionToTarget.sqrMagnitude <= Mathf.Epsilon) return;
Quaternion targetRotation = Quaternion.LookRotation(directionToTarget.normalized, Vector3.up);
CachedTransform.rotation =
Quaternion.Slerp(CachedTransform.rotation, targetRotation, _rotateSpeed * elapseSeconds);
}
private void RotateToOrigin(float elapseSeconds)
{
CachedTransform.rotation =
Quaternion.Slerp(CachedTransform.rotation, _cachedRotation, _rotateSpeed * elapseSeconds);
}
private void FaceTargetImmediately()
{
if (_target == null || !_target.Available) return;
Vector3 directionToTarget = _target.CachedTransform.position - CachedTransform.position;
directionToTarget.y = 0f;
if (directionToTarget.sqrMagnitude <= Mathf.Epsilon) return;
CachedTransform.rotation = Quaternion.LookRotation(directionToTarget.normalized, Vector3.up);
}
protected override bool OnWeaponShow(object userData)
{
_weaponData = RequireWeaponData<WeaponLightningData>(userData);
if (_weaponData == null) return false;
WeaponData = _weaponData;
StartAttackCooldown(_weaponData.Cooldown);
_sqrRange = _weaponData.AttackRange * _weaponData.AttackRange;
_cachedRotation = CachedTransform.rotation;
float configuredHitRadius = _weaponData.ParamsData != null ? _weaponData.ParamsData.HitRadius : 0f;
_hitRadius = configuredHitRadius > 0f ? Mathf.Max(0.1f, configuredHitRadius) : _weaponData.AttackRange;
_hitRadiusSqr = _hitRadius * _hitRadius;
float configuredHoverHeight = _weaponData.ParamsData != null ? _weaponData.ParamsData.HoverHeight : 0f;
if (configuredHoverHeight > 0f)
{
_hoverHeight = Mathf.Max(0.1f, configuredHoverHeight);
}
int colliderCapacity = Mathf.Max(1, _maxHitColliders);
if (_hitResults == null || _hitResults.Length != colliderCapacity)
{
@@ -191,37 +128,19 @@ namespace SepCore.Entity
}
_attackEffect = new LightningStrikeAttackEffect();
if (_weaponData.OwnerCamp == CampType.Player)
{
gameObject.layer = LayerMask.NameToLayer("PlayerWeapon");
_hitMask = LayerMask.GetMask("Enemy");
}
else if (_weaponData.OwnerCamp == CampType.Enemy)
{
gameObject.layer = LayerMask.NameToLayer("EnemyWeapon");
_hitMask = LayerMask.GetMask("Player");
}
return true;
}
protected override void OnWeaponHide(object userData)
{
StopAttackTween(true);
CancelAttackCooldown();
_attackEffect = null;
}
protected override void OnWeaponAttach(EntityLogic parentEntity, Transform parentTransform, object userData)
{
BindAttackStatFromOwner(parentEntity);
}
protected override void OnWeaponDetach(EntityLogic parentEntity, object userData)
{
StopAttackTween(true);
ReleaseAttackStatSubscription();
base.OnWeaponDetach(parentEntity, userData);
}
protected override void OnEnabledChanged(bool enabled)
@@ -0,0 +1,51 @@
using SepCore.Definition;
namespace SepCore.Entity
{
/// <summary>
/// 闪电的瞄准状态。
/// </summary>
public sealed class WeaponLightningAimState : WeaponStateBase
{
private WeaponLightning _weapon;
public override WeaponStateType State => WeaponStateType.Aim;
public override void OnInit(WeaponBase weapon) => _weapon = (WeaponLightning)weapon;
public override void OnEnter()
{
if (_weapon.CanAttack)
{
_weapon.TransitionTo(WeaponStateType.Attack);
}
}
public override void OnUpdate(float elapseSeconds, float realElapseSeconds)
{
_weapon.Check();
_weapon.RotateToTarget(elapseSeconds);
if (_weapon.Target == null || !_weapon.Target.Available)
{
_weapon.TransitionTo(WeaponStateType.Idle);
return;
}
if (!_weapon.IsInRange(_weapon.Target, _weapon.SqrRange))
{
_weapon.TransitionTo(WeaponStateType.Seek);
return;
}
if (_weapon.CanAttack)
{
_weapon.TransitionTo(WeaponStateType.Attack);
}
}
public override void OnLeave()
{
}
}
}
@@ -1,5 +1,5 @@
fileFormatVersion: 2
guid: f3aa479abb79491d83e8de76806faf7a
guid: 472efc8d8802b4941892df6907b98d03
MonoImporter:
externalObjects: {}
serializedVersion: 2
@@ -8,4 +8,4 @@ MonoImporter:
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
assetBundleVariant:
@@ -0,0 +1,34 @@
using SepCore.Definition;
namespace SepCore.Entity
{
/// <summary>
/// 闪电的攻击状态。
/// </summary>
public sealed class WeaponLightningAttackState : WeaponStateBase
{
private WeaponLightning _weapon;
public override WeaponStateType State => WeaponStateType.Attack;
public override void OnInit(WeaponBase weapon) => _weapon = (WeaponLightning)weapon;
public override void OnEnter()
{
_weapon.ResetAttackCooldown(_weapon.AttackCooldown);
_weapon.Attack();
}
public override void OnUpdate(float elapseSeconds, float realElapseSeconds)
{
if (!_weapon.IsAttacking)
{
_weapon.TransitionTo(WeaponStateType.Aim);
}
}
public override void OnLeave()
{
}
}
}
@@ -1,5 +1,5 @@
fileFormatVersion: 2
guid: 25854832a7d3416dacab67bcfef2a2fa
guid: 9d4b2a83cb82e1442826effabf6c9351
MonoImporter:
externalObjects: {}
serializedVersion: 2
@@ -8,4 +8,4 @@ MonoImporter:
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
assetBundleVariant:
@@ -0,0 +1,35 @@
using SepCore.Definition;
namespace SepCore.Entity
{
/// <summary>
/// 闪电的待机状态。
/// </summary>
public sealed class WeaponLightningIdleState : WeaponStateBase
{
private WeaponLightning _weapon;
public override WeaponStateType State => WeaponStateType.Idle;
public override void OnInit(WeaponBase weapon) => _weapon = (WeaponLightning)weapon;
public override void OnEnter()
{
}
public override void OnUpdate(float elapseSeconds, float realElapseSeconds)
{
_weapon.Check();
_weapon.RotateToOrigin(elapseSeconds);
if (_weapon.Target != null && _weapon.Target.Available)
{
_weapon.TransitionTo(WeaponStateType.Seek);
}
}
public override void OnLeave()
{
}
}
}
@@ -1,5 +1,5 @@
fileFormatVersion: 2
guid: 3464037c29774c3ea326dc93fe33551e
guid: 1f6fccbd2de0c494fa2f33e0fba04d4f
MonoImporter:
externalObjects: {}
serializedVersion: 2
@@ -8,4 +8,4 @@ MonoImporter:
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
assetBundleVariant:
@@ -0,0 +1,41 @@
using SepCore.Definition;
namespace SepCore.Entity
{
/// <summary>
/// 闪电的搜寻状态。
/// </summary>
public sealed class WeaponLightningSeekState : WeaponStateBase
{
private WeaponLightning _weapon;
public override WeaponStateType State => WeaponStateType.Seek;
public override void OnInit(WeaponBase weapon) => _weapon = (WeaponLightning)weapon;
public override void OnEnter()
{
}
public override void OnUpdate(float elapseSeconds, float realElapseSeconds)
{
_weapon.Check();
_weapon.RotateToTarget(elapseSeconds);
if (_weapon.Target == null || !_weapon.Target.Available)
{
_weapon.TransitionTo(WeaponStateType.Idle);
return;
}
if (_weapon.IsInRange(_weapon.Target, _weapon.SqrRange))
{
_weapon.TransitionTo(WeaponStateType.Aim);
}
}
public override void OnLeave()
{
}
}
}
@@ -1,5 +1,5 @@
fileFormatVersion: 2
guid: 25404ad172434ba38609e797b6d96919
guid: 9fe264206ae0d0d4492b94b952d6d698
MonoImporter:
externalObjects: {}
serializedVersion: 2
@@ -8,4 +8,4 @@ MonoImporter:
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
assetBundleVariant: