推进计时器逻辑的集中管理

- 将敌人的攻击计时任务放到基类中进行创建
- 将 EnemySpawnScheduler 敌人生成调度器的刷怪计时任务迁移到 TimerComponent
This commit is contained in:
2026-06-23 20:42:24 +08:00
parent 846e4d6f44
commit cc8982b131
6 changed files with 69 additions and 93 deletions
@@ -2,6 +2,7 @@
using SepCore.AsyncTask;
using SepCore.Definition;
using SepCore.Entity;
using SepCore.Timer;
using UnityEngine;
public abstract class EnemyBase : TargetableObject
@@ -15,6 +16,29 @@ public abstract class EnemyBase : TargetableObject
protected EnemyData _enemyData;
protected bool _canAttack;
protected TimerHandle _attackTimerHandle;
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;
}
protected override void OnShow(object userData)
{
base.OnShow(userData);
@@ -1,7 +1,6 @@
using SepCore.Components;
using SepCore.CustomUtility;
using SepCore.Definition;
using SepCore.Timer;
using UnityEngine;
using UnityGameFramework.Runtime;
@@ -24,8 +23,6 @@ namespace SepCore.Entity
private int _attackDamage = 1;
private float _sqrAttackRange;
private TimerHandle _attackTimerHandle;
private bool _canAttack;
private AttackStateType _attackState = AttackStateType.Idle;
@@ -71,8 +68,7 @@ namespace SepCore.Entity
_attackDamage = Mathf.Max(1, _meleeEnemyData.AttackDamage);
_sqrAttackRange = _attackRange * _attackRange;
_canAttack = false;
_attackTimerHandle = GameEntry.Timer.ScheduleOnce(_attackCooldown, () => _canAttack = true, this);
StartAttackCooldown(_attackCooldown);
_attackState = AttackStateType.Idle;
_targetableTarget = null;
@@ -96,8 +92,7 @@ namespace SepCore.Entity
_movementComponent.OnReset();
_healthComponent.OnReset();
_targetableTarget = null;
GameEntry.Timer.Cancel(_attackTimerHandle);
_attackTimerHandle = TimerHandle.Invalid;
CancelAttackCooldown();
_attackState = AttackStateType.Idle;
base.OnHide(isShutdown, userData);
@@ -155,8 +150,7 @@ namespace SepCore.Entity
if (_canAttack)
{
_canAttack = false;
_attackTimerHandle = GameEntry.Timer.ScheduleOnce(_attackCooldown, () => _canAttack = true, this);
ResetAttackCooldown(_attackCooldown);
TransitionTo(AttackStateType.Attack);
}
@@ -3,7 +3,6 @@ using SepCore.AsyncTask;
using SepCore.CustomUtility;
using SepCore.Definition;
using Cysharp.Threading.Tasks;
using SepCore.Timer;
using UnityEngine;
using UnityGameFramework.Runtime;
@@ -28,8 +27,6 @@ namespace SepCore.Entity
private float _attackRangeSquared;
private float _attackCooldown = 1f;
private int _attackDamage = 1;
private TimerHandle _attackTimerHandle;
private bool _canAttack;
[SerializeField] private float _projectileSpeed = 12f;
[SerializeField] private float _projectileLifeTime = 3f;
@@ -83,8 +80,7 @@ namespace SepCore.Entity
_projectileSpawnHeightOffset);
_projectileAssetName = ReadStringParam(ProjectileAssetNameParamKey, _projectileAssetName);
_canAttack = false;
_attackTimerHandle = GameEntry.Timer.ScheduleOnce(_attackCooldown, () => _canAttack = true, this);
StartAttackCooldown(_attackCooldown);
this.CachedTransform.position = enemyData.Position;
}
else
@@ -112,8 +108,7 @@ namespace SepCore.Entity
if (_canAttack)
{
TryFireProjectile();
_canAttack = false;
_attackTimerHandle = GameEntry.Timer.ScheduleOnce(_attackCooldown, () => _canAttack = true, this);
ResetAttackCooldown(_attackCooldown);
}
}
else
@@ -127,8 +122,7 @@ namespace SepCore.Entity
{
_movementComponent.OnReset();
_healthComponent.OnReset();
GameEntry.Timer.Cancel(_attackTimerHandle);
_attackTimerHandle = TimerHandle.Invalid;
CancelAttackCooldown();
base.OnHide(isShutdown, userData);
}