将 Weapon 攻击冷却计时器 由 Weapon 内部维护迁移到 TimerModule 维护

This commit is contained in:
SepComet 2026-06-18 23:23:29 +08:00
parent 25f36d38c9
commit 846e4d6f44
26 changed files with 48 additions and 36 deletions

View File

@ -4,6 +4,7 @@ using SepCore.Definition;
using SepCore.Entity; using SepCore.Entity;
using GameFramework; using GameFramework;
using SepCore.CustomUtility; using SepCore.CustomUtility;
using SepCore.Timer;
using UnityEngine; using UnityEngine;
using UnityGameFramework.Runtime; using UnityGameFramework.Runtime;
@ -41,7 +42,8 @@ namespace SepCore.Entity.Weapon
protected EntityBase _target; protected EntityBase _target;
protected float _currAttackTimer; protected bool _canAttack;
protected TimerHandle _attackTimerHandle;
protected float _sqrRange; protected float _sqrRange;
@ -306,6 +308,26 @@ namespace SepCore.Entity.Weapon
_attackStatCallback = null; _attackStatCallback = null;
AttackStat = new StatProperty(); AttackStat = new StatProperty();
} }
protected void StartAttackCooldown(float cooldown)
{
_canAttack = false;
_attackTimerHandle = GameEntry.Timer.ScheduleOnce(cooldown, () => _canAttack = true, this);
}
protected void ResetAttackCooldown(float cooldown)
{
GameEntry.Timer.Cancel(_attackTimerHandle);
_canAttack = false;
_attackTimerHandle = GameEntry.Timer.ScheduleOnce(cooldown, () => _canAttack = true, this);
}
protected void CancelAttackCooldown()
{
GameEntry.Timer.Cancel(_attackTimerHandle);
_attackTimerHandle = TimerHandle.Invalid;
_canAttack = false;
}
} }
public abstract class WeaponStateBase public abstract class WeaponStateBase

View File

@ -10,7 +10,7 @@ namespace SepCore.Entity.Weapon
public override void OnEnter() public override void OnEnter()
{ {
_weapon._currAttackTimer = 0f; _weapon.ResetAttackCooldown(_weapon._weaponData.Cooldown);
_weapon.Attack(); _weapon.Attack();
} }

View File

@ -10,7 +10,7 @@ namespace SepCore.Entity.Weapon
public override void OnEnter() public override void OnEnter()
{ {
if (_weapon._currAttackTimer >= _weapon._weaponData.Cooldown) if (_weapon._canAttack)
{ {
_weapon.TransitionTo(WeaponStateType.Attack); _weapon.TransitionTo(WeaponStateType.Attack);
} }
@ -20,7 +20,6 @@ namespace SepCore.Entity.Weapon
{ {
_weapon.Check(); _weapon.Check();
_weapon.RotateToTarget(elapseSeconds); _weapon.RotateToTarget(elapseSeconds);
_weapon._currAttackTimer += elapseSeconds;
if (_weapon._target == null || !_weapon._target.Available) if (_weapon._target == null || !_weapon._target.Available)
{ {
@ -34,7 +33,7 @@ namespace SepCore.Entity.Weapon
return; return;
} }
if (_weapon._currAttackTimer >= _weapon._weaponData.Cooldown) if (_weapon._canAttack)
{ {
_weapon.TransitionTo(WeaponStateType.Attack); _weapon.TransitionTo(WeaponStateType.Attack);
} }

View File

@ -13,7 +13,6 @@ namespace SepCore.Entity.Weapon
{ {
_weapon.Check(); _weapon.Check();
_weapon.RotateToTarget(elapseSeconds); _weapon.RotateToTarget(elapseSeconds);
_weapon._currAttackTimer += elapseSeconds;
if (_weapon._target == null || !_weapon._target.Available) if (_weapon._target == null || !_weapon._target.Available)
{ {

View File

@ -13,7 +13,6 @@ namespace SepCore.Entity.Weapon
{ {
_weapon.Check(); _weapon.Check();
_weapon.RotateToOrigin(elapseSeconds); _weapon.RotateToOrigin(elapseSeconds);
_weapon._currAttackTimer += elapseSeconds;
if (_weapon._target != null && _weapon._target.Available) if (_weapon._target != null && _weapon._target.Available)
{ {

View File

@ -129,7 +129,7 @@ namespace SepCore.Entity.Weapon
if (_weaponData == null) return false; if (_weaponData == null) return false;
WeaponData = _weaponData; WeaponData = _weaponData;
_currAttackTimer = 0f; StartAttackCooldown(_weaponData.Cooldown);
_sqrRange = _weaponData.AttackRange * _weaponData.AttackRange; _sqrRange = _weaponData.AttackRange * _weaponData.AttackRange;
_cachedRotation = CachedTransform.rotation; _cachedRotation = CachedTransform.rotation;
_attackEffect = new HandgunHitMarkerAttackEffect(_hitMarkerSize, _hitMarkerYOffset, _hitMarkerDuration, _attackEffect = new HandgunHitMarkerAttackEffect(_hitMarkerSize, _hitMarkerYOffset, _hitMarkerDuration,
@ -151,6 +151,7 @@ namespace SepCore.Entity.Weapon
protected override void OnWeaponHide(object userData) protected override void OnWeaponHide(object userData)
{ {
CancelAttackCooldown();
_attackEffect = null; _attackEffect = null;
} }

View File

@ -11,7 +11,7 @@ namespace SepCore.Entity.Weapon
public override void OnEnter() public override void OnEnter()
{ {
_weapon._currAttackTimer = 0f; _weapon.ResetAttackCooldown(_weapon._weaponData.Cooldown);
_weapon.Attack(); _weapon.Attack();
} }

View File

@ -11,7 +11,7 @@ namespace SepCore.Entity.Weapon
public override void OnEnter() public override void OnEnter()
{ {
if (_weapon._currAttackTimer >= _weapon._weaponData.Cooldown) if (_weapon._canAttack)
{ {
_weapon.TransitionTo(WeaponStateType.Attack); _weapon.TransitionTo(WeaponStateType.Attack);
} }
@ -21,7 +21,6 @@ namespace SepCore.Entity.Weapon
{ {
_weapon.Check(); _weapon.Check();
_weapon.RotateToTarget(elapseSeconds); _weapon.RotateToTarget(elapseSeconds);
_weapon._currAttackTimer += elapseSeconds;
if (_weapon._target == null || !_weapon._target.Available) if (_weapon._target == null || !_weapon._target.Available)
{ {
@ -35,7 +34,7 @@ namespace SepCore.Entity.Weapon
return; return;
} }
if (_weapon._currAttackTimer >= _weapon._weaponData.Cooldown) if (_weapon._canAttack)
{ {
_weapon.TransitionTo(WeaponStateType.Attack); _weapon.TransitionTo(WeaponStateType.Attack);
} }

View File

@ -17,7 +17,6 @@ namespace SepCore.Entity.Weapon
{ {
_weapon.Check(); _weapon.Check();
_weapon.RotateToTarget(elapseSeconds); _weapon.RotateToTarget(elapseSeconds);
_weapon._currAttackTimer += elapseSeconds;
if (_weapon._target == null || !_weapon._target.Available) if (_weapon._target == null || !_weapon._target.Available)
{ {

View File

@ -17,7 +17,6 @@ namespace SepCore.Entity.Weapon
{ {
_weapon.Check(); _weapon.Check();
_weapon.RotateToOrigin(elapseSeconds); _weapon.RotateToOrigin(elapseSeconds);
_weapon._currAttackTimer += elapseSeconds;
if (_weapon._target != null && _weapon._target.Available) if (_weapon._target != null && _weapon._target.Available)
{ {

View File

@ -150,7 +150,7 @@ namespace SepCore.Entity.Weapon
if (_weaponData == null) return false; if (_weaponData == null) return false;
WeaponData = _weaponData; WeaponData = _weaponData;
_currAttackTimer = 0f; StartAttackCooldown(_weaponData.Cooldown);
_sqrRange = _weaponData.AttackRange * _weaponData.AttackRange; _sqrRange = _weaponData.AttackRange * _weaponData.AttackRange;
_cachedRotation = CachedTransform.rotation; _cachedRotation = CachedTransform.rotation;
@ -183,6 +183,7 @@ namespace SepCore.Entity.Weapon
protected override void OnWeaponHide(object userData) protected override void OnWeaponHide(object userData)
{ {
StopAttackTween(true); StopAttackTween(true);
CancelAttackCooldown();
_attackEffect = null; _attackEffect = null;
} }

View File

@ -11,7 +11,7 @@ namespace SepCore.Entity.Weapon
public override void OnEnter() public override void OnEnter()
{ {
_weapon._currAttackTimer = 0f; _weapon.ResetAttackCooldown(_weapon._weaponData.Cooldown);
_weapon.Attack(); _weapon.Attack();
} }

View File

@ -11,7 +11,7 @@ namespace SepCore.Entity.Weapon
public override void OnEnter() public override void OnEnter()
{ {
if (_weapon._currAttackTimer >= _weapon._weaponData.Cooldown) if (_weapon._canAttack)
{ {
_weapon.TransitionTo(WeaponStateType.Attack); _weapon.TransitionTo(WeaponStateType.Attack);
} }
@ -21,7 +21,6 @@ namespace SepCore.Entity.Weapon
{ {
_weapon.Check(); _weapon.Check();
_weapon.RotateToTarget(elapseSeconds); _weapon.RotateToTarget(elapseSeconds);
_weapon._currAttackTimer += elapseSeconds;
if (_weapon._target == null || !_weapon._target.Available) if (_weapon._target == null || !_weapon._target.Available)
{ {
@ -35,7 +34,7 @@ namespace SepCore.Entity.Weapon
return; return;
} }
if (_weapon._currAttackTimer >= _weapon._weaponData.Cooldown) if (_weapon._canAttack)
{ {
_weapon.TransitionTo(WeaponStateType.Attack); _weapon.TransitionTo(WeaponStateType.Attack);
} }

View File

@ -17,7 +17,6 @@ namespace SepCore.Entity.Weapon
{ {
_weapon.Check(); _weapon.Check();
_weapon.RotateToTarget(elapseSeconds); _weapon.RotateToTarget(elapseSeconds);
_weapon._currAttackTimer += elapseSeconds;
if (_weapon._target == null || !_weapon._target.Available) if (_weapon._target == null || !_weapon._target.Available)
{ {

View File

@ -17,7 +17,6 @@ namespace SepCore.Entity.Weapon
{ {
_weapon.Check(); _weapon.Check();
_weapon.RotateToOrigin(elapseSeconds); _weapon.RotateToOrigin(elapseSeconds);
_weapon._currAttackTimer += elapseSeconds;
if (_weapon._target != null && _weapon._target.Available) if (_weapon._target != null && _weapon._target.Available)
{ {

View File

@ -244,7 +244,7 @@ namespace SepCore.Entity.Weapon
if (_weaponData == null) return false; if (_weaponData == null) return false;
WeaponData = _weaponData; WeaponData = _weaponData;
_currAttackTimer = 0f; StartAttackCooldown(_weaponData.Cooldown);
_sqrRange = _weaponData.AttackRange * _weaponData.AttackRange; _sqrRange = _weaponData.AttackRange * _weaponData.AttackRange;
_cachedRotation = CachedTransform.rotation; _cachedRotation = CachedTransform.rotation;
@ -308,6 +308,7 @@ namespace SepCore.Entity.Weapon
protected override void OnWeaponHide(object userData) protected override void OnWeaponHide(object userData)
{ {
StopAttackTween(true); StopAttackTween(true);
CancelAttackCooldown();
_attackEffect = null; _attackEffect = null;
} }

View File

@ -11,7 +11,7 @@ namespace SepCore.Entity.Weapon
public override void OnEnter() public override void OnEnter()
{ {
_weapon._currAttackTimer = 0f; _weapon.ResetAttackCooldown(_weapon._weaponData.Cooldown);
_weapon.Attack(); _weapon.Attack();
} }

View File

@ -11,7 +11,7 @@ namespace SepCore.Entity.Weapon
public override void OnEnter() public override void OnEnter()
{ {
if (_weapon._currAttackTimer >= _weapon._weaponData.Cooldown) if (_weapon._canAttack)
{ {
_weapon.TransitionTo(WeaponStateType.Attack); _weapon.TransitionTo(WeaponStateType.Attack);
} }
@ -21,7 +21,6 @@ namespace SepCore.Entity.Weapon
{ {
_weapon.Check(); _weapon.Check();
_weapon.RotateToTarget(elapseSeconds); _weapon.RotateToTarget(elapseSeconds);
_weapon._currAttackTimer += elapseSeconds;
if (_weapon._target == null || !_weapon._target.Available) if (_weapon._target == null || !_weapon._target.Available)
{ {
@ -35,7 +34,7 @@ namespace SepCore.Entity.Weapon
return; return;
} }
if (_weapon._currAttackTimer >= _weapon._weaponData.Cooldown) if (_weapon._canAttack)
{ {
_weapon.TransitionTo(WeaponStateType.Attack); _weapon.TransitionTo(WeaponStateType.Attack);
} }

View File

@ -17,7 +17,6 @@ namespace SepCore.Entity.Weapon
{ {
_weapon.Check(); _weapon.Check();
_weapon.RotateToTarget(elapseSeconds); _weapon.RotateToTarget(elapseSeconds);
_weapon._currAttackTimer += elapseSeconds;
if (_weapon._target == null || !_weapon._target.Available) if (_weapon._target == null || !_weapon._target.Available)
{ {

View File

@ -17,7 +17,6 @@ namespace SepCore.Entity.Weapon
{ {
_weapon.Check(); _weapon.Check();
_weapon.RotateToOrigin(elapseSeconds); _weapon.RotateToOrigin(elapseSeconds);
_weapon._currAttackTimer += elapseSeconds;
if (_weapon._target != null && _weapon._target.Available) if (_weapon._target != null && _weapon._target.Available)
{ {

View File

@ -171,7 +171,7 @@ namespace SepCore.Entity.Weapon
if (_weaponData == null) return false; if (_weaponData == null) return false;
WeaponData = _weaponData; WeaponData = _weaponData;
_currAttackTimer = 0f; StartAttackCooldown(_weaponData.Cooldown);
_sqrRange = _weaponData.AttackRange * _weaponData.AttackRange; _sqrRange = _weaponData.AttackRange * _weaponData.AttackRange;
_cachedRotation = CachedTransform.rotation; _cachedRotation = CachedTransform.rotation;
@ -209,6 +209,7 @@ namespace SepCore.Entity.Weapon
protected override void OnWeaponHide(object userData) protected override void OnWeaponHide(object userData)
{ {
StopAttackTween(true); StopAttackTween(true);
CancelAttackCooldown();
_attackEffect = null; _attackEffect = null;
} }

View File

@ -10,7 +10,7 @@ namespace SepCore.Entity.Weapon
public override void OnEnter() public override void OnEnter()
{ {
_weapon._currAttackTimer = 0f; _weapon.ResetAttackCooldown(_weapon._weaponData.Cooldown);
_weapon.Attack(); _weapon.Attack();
} }

View File

@ -10,7 +10,7 @@ namespace SepCore.Entity.Weapon
public override void OnEnter() public override void OnEnter()
{ {
if (_weapon._currAttackTimer >= _weapon._weaponData.Cooldown) if (_weapon._canAttack)
{ {
_weapon.TransitionTo(WeaponStateType.Attack); _weapon.TransitionTo(WeaponStateType.Attack);
} }
@ -20,7 +20,6 @@ namespace SepCore.Entity.Weapon
{ {
_weapon.Check(); _weapon.Check();
_weapon.RotateToTarget(elapseSeconds); _weapon.RotateToTarget(elapseSeconds);
_weapon._currAttackTimer += elapseSeconds;
if (_weapon._target == null || !_weapon._target.Available) if (_weapon._target == null || !_weapon._target.Available)
{ {
@ -34,7 +33,7 @@ namespace SepCore.Entity.Weapon
return; return;
} }
if (_weapon._currAttackTimer >= _weapon._weaponData.Cooldown) if (_weapon._canAttack)
{ {
_weapon.TransitionTo(WeaponStateType.Attack); _weapon.TransitionTo(WeaponStateType.Attack);
} }

View File

@ -16,7 +16,6 @@ namespace SepCore.Entity.Weapon
{ {
_weapon.Check(); _weapon.Check();
_weapon.RotateToTarget(elapseSeconds); _weapon.RotateToTarget(elapseSeconds);
_weapon._currAttackTimer += elapseSeconds;
if (_weapon._target == null || !_weapon._target.Available) if (_weapon._target == null || !_weapon._target.Available)
{ {

View File

@ -16,7 +16,6 @@ namespace SepCore.Entity.Weapon
{ {
_weapon.Check(); _weapon.Check();
_weapon.RotateToOrigin(elapseSeconds); _weapon.RotateToOrigin(elapseSeconds);
_weapon._currAttackTimer += elapseSeconds;
if (_weapon._target != null && _weapon._target.Available) if (_weapon._target != null && _weapon._target.Available)
{ {

View File

@ -174,7 +174,7 @@ namespace SepCore.Entity.Weapon
if (_weaponData == null) return false; if (_weaponData == null) return false;
WeaponData = _weaponData; WeaponData = _weaponData;
_currAttackTimer = 0f; StartAttackCooldown(_weaponData.Cooldown);
_sqrRange = _weaponData.AttackRange * _weaponData.AttackRange; _sqrRange = _weaponData.AttackRange * _weaponData.AttackRange;
_cachedRotation = CachedTransform.rotation; _cachedRotation = CachedTransform.rotation;
@ -209,6 +209,7 @@ namespace SepCore.Entity.Weapon
protected override void OnWeaponHide(object userData) protected override void OnWeaponHide(object userData)
{ {
StopAttackTween(true); StopAttackTween(true);
CancelAttackCooldown();
_attackEffect = null; _attackEffect = null;
} }