From 25f36d38c9a427dac5b875cf14d38e29416ea4ce Mon Sep 17 00:00:00 2001 From: basil <2428390463@qq.com> Date: Thu, 18 Jun 2026 23:09:44 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B0=86=20Enemy=20=E6=94=BB=E5=87=BB=E5=86=B7?= =?UTF-8?q?=E5=8D=B4=E8=AE=A1=E6=97=B6=E5=99=A8=20=E7=94=B1=20Enemy=20?= =?UTF-8?q?=E5=86=85=E9=83=A8=E7=BB=B4=E6=8A=A4=E8=BF=81=E7=A7=BB=E5=88=B0?= =?UTF-8?q?=20TimerModule=20=E7=BB=B4=E6=8A=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Entity/EntityLogic/Enemy/MeleeEnemy.cs | 24 +++++++++---------- .../Entity/EntityLogic/Enemy/RemoteEnemy.cs | 22 ++++++++++------- .../TimerModule/Runtime/TimerComponent.cs | 2 +- 3 files changed, 25 insertions(+), 23 deletions(-) diff --git a/Assets/GameMain/Scripts/Runtime/Entity/EntityLogic/Enemy/MeleeEnemy.cs b/Assets/GameMain/Scripts/Runtime/Entity/EntityLogic/Enemy/MeleeEnemy.cs index cf20541..19c77c1 100644 --- a/Assets/GameMain/Scripts/Runtime/Entity/EntityLogic/Enemy/MeleeEnemy.cs +++ b/Assets/GameMain/Scripts/Runtime/Entity/EntityLogic/Enemy/MeleeEnemy.cs @@ -1,6 +1,7 @@ using SepCore.Components; using SepCore.CustomUtility; using SepCore.Definition; +using SepCore.Timer; using UnityEngine; using UnityGameFramework.Runtime; @@ -23,7 +24,8 @@ namespace SepCore.Entity private int _attackDamage = 1; private float _sqrAttackRange; - private float _currAttackTimer; + private TimerHandle _attackTimerHandle; + private bool _canAttack; private AttackStateType _attackState = AttackStateType.Idle; @@ -69,7 +71,8 @@ namespace SepCore.Entity _attackDamage = Mathf.Max(1, _meleeEnemyData.AttackDamage); _sqrAttackRange = _attackRange * _attackRange; - _currAttackTimer = 0f; + _canAttack = false; + _attackTimerHandle = GameEntry.Timer.ScheduleOnce(_attackCooldown, () => _canAttack = true, this); _attackState = AttackStateType.Idle; _targetableTarget = null; @@ -93,7 +96,8 @@ namespace SepCore.Entity _movementComponent.OnReset(); _healthComponent.OnReset(); _targetableTarget = null; - _currAttackTimer = 0f; + GameEntry.Timer.Cancel(_attackTimerHandle); + _attackTimerHandle = TimerHandle.Invalid; _attackState = AttackStateType.Idle; base.OnHide(isShutdown, userData); @@ -109,8 +113,6 @@ namespace SepCore.Entity private void UpdateAttackState(float elapseSeconds) { - _currAttackTimer += elapseSeconds; - switch (_attackState) { case AttackStateType.Idle: @@ -151,8 +153,10 @@ namespace SepCore.Entity return; } - if (_currAttackTimer >= _attackCooldown) + if (_canAttack) { + _canAttack = false; + _attackTimerHandle = GameEntry.Timer.ScheduleOnce(_attackCooldown, () => _canAttack = true, this); TransitionTo(AttackStateType.Attack); } @@ -169,21 +173,15 @@ namespace SepCore.Entity SetMove(false); } - if (_attackState == AttackStateType.Check_InRange && _currAttackTimer >= _attackCooldown) - { - TransitionTo(AttackStateType.Attack); - return; - } - if (_attackState == AttackStateType.Attack) { SetMove(false); ExecuteAttack(); - _currAttackTimer = 0f; TransitionTo(AttackStateType.Check_InRange); } } + private bool HasValidTarget() { if (_target == null) return false; diff --git a/Assets/GameMain/Scripts/Runtime/Entity/EntityLogic/Enemy/RemoteEnemy.cs b/Assets/GameMain/Scripts/Runtime/Entity/EntityLogic/Enemy/RemoteEnemy.cs index 25c3af0..5efdb52 100644 --- a/Assets/GameMain/Scripts/Runtime/Entity/EntityLogic/Enemy/RemoteEnemy.cs +++ b/Assets/GameMain/Scripts/Runtime/Entity/EntityLogic/Enemy/RemoteEnemy.cs @@ -3,6 +3,7 @@ using SepCore.AsyncTask; using SepCore.CustomUtility; using SepCore.Definition; using Cysharp.Threading.Tasks; +using SepCore.Timer; using UnityEngine; using UnityGameFramework.Runtime; @@ -27,7 +28,8 @@ namespace SepCore.Entity private float _attackRangeSquared; private float _attackCooldown = 1f; private int _attackDamage = 1; - private float _currAttackTimer; + private TimerHandle _attackTimerHandle; + private bool _canAttack; [SerializeField] private float _projectileSpeed = 12f; [SerializeField] private float _projectileLifeTime = 3f; @@ -81,7 +83,8 @@ namespace SepCore.Entity _projectileSpawnHeightOffset); _projectileAssetName = ReadStringParam(ProjectileAssetNameParamKey, _projectileAssetName); - _currAttackTimer = 0f; + _canAttack = false; + _attackTimerHandle = GameEntry.Timer.ScheduleOnce(_attackCooldown, () => _canAttack = true, this); this.CachedTransform.position = enemyData.Position; } else @@ -94,8 +97,6 @@ namespace SepCore.Entity { base.OnUpdate(elapseSeconds, realElapseSeconds); - _currAttackTimer += elapseSeconds; - if (_target == null) { _movementComponent.SetMove(false); @@ -108,7 +109,12 @@ namespace SepCore.Entity if (distanceSquared <= _attackRangeSquared) { _movementComponent.SetMove(false); - TryFireProjectile(); + if (_canAttack) + { + TryFireProjectile(); + _canAttack = false; + _attackTimerHandle = GameEntry.Timer.ScheduleOnce(_attackCooldown, () => _canAttack = true, this); + } } else { @@ -121,14 +127,14 @@ namespace SepCore.Entity { _movementComponent.OnReset(); _healthComponent.OnReset(); - _currAttackTimer = 0f; + GameEntry.Timer.Cancel(_attackTimerHandle); + _attackTimerHandle = TimerHandle.Invalid; base.OnHide(isShutdown, userData); } private void TryFireProjectile() { - if (_currAttackTimer < _attackCooldown || _target == null) return; if (!EnsureEnemyProjectileGroup()) return; Vector3 spawnPosition = this.CachedTransform.position + @@ -165,8 +171,6 @@ namespace SepCore.Entity EnemyProjectileGroupName, Constant.AssetPriority.BulletAsset, projectileData).Forget(); - - _currAttackTimer = 0f; } private static bool EnsureEnemyProjectileGroup() diff --git a/Assets/Plugins/TimerModule/Runtime/TimerComponent.cs b/Assets/Plugins/TimerModule/Runtime/TimerComponent.cs index e70a4e9..234ad4c 100644 --- a/Assets/Plugins/TimerModule/Runtime/TimerComponent.cs +++ b/Assets/Plugins/TimerModule/Runtime/TimerComponent.cs @@ -81,7 +81,7 @@ namespace SepCore.Timer public TimerHandle ScheduleOnce(float delay, Action callback, object owner = null, TimerTimeMode timeMode = TimerTimeMode.Scaled) { - return Schedule(new TimerTask(delay, 0f, 1, callback, owner, timeMode)); + return Schedule(new TimerTask(delay, delay, 1, callback, owner, timeMode)); } ///