From e1ac8364971e3fddd7b9e9e20a5ccbb2d777ad07 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=A8=E8=90=BD=E5=9F=BA=E5=9B=B4=E8=99=BE?= <3161880837@qq.com> Date: Thu, 5 Feb 2026 20:52:04 +0800 Subject: [PATCH] =?UTF-8?q?refactor(=E6=AD=A6=E5=99=A8=E7=B3=BB=E7=BB=9F):?= =?UTF-8?q?=20=E4=BC=98=E5=8C=96=E6=AD=A6=E5=99=A8=E6=94=BB=E5=87=BB?= =?UTF-8?q?=E9=80=BB=E8=BE=91=E5=92=8C=E8=83=BD=E9=87=8F=E6=A3=80=E6=9F=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 将武器攻击前的条件检查提取为独立方法 canAttackBy - 重构能量检查逻辑,增加 isEnergyEnough 方法 - 简化公鸡角色的输入处理,合并重复代码为 tryLaunch 方法 - 调整钢管武器的冷却时间为 3000.0 --- components/Weapons/Pipe.tscn | 2 +- scripts/Contents/Characters/Rooster.gd | 61 +++++++++++--------------- scripts/Statemachine/EntityBase.gd | 7 +-- scripts/Structs/Weapon.gd | 6 ++- 4 files changed, 34 insertions(+), 42 deletions(-) diff --git a/components/Weapons/Pipe.tscn b/components/Weapons/Pipe.tscn index f6c6bde..f630c8f 100644 --- a/components/Weapons/Pipe.tscn +++ b/components/Weapons/Pipe.tscn @@ -21,7 +21,7 @@ storeType = { descriptionTemplate = "按住蓄力扔出钢管,蓄力越久伤害越高。 基础伤害:$atk,蓄力倍率:$charge" needEnergy = 2.0 -cooldown = 1000.0 +cooldown = 3000.0 [node name="avatar" parent="container/info" index="0"] texture = ExtResource("2_mmtf8") diff --git a/scripts/Contents/Characters/Rooster.gd b/scripts/Contents/Characters/Rooster.gd index 3fdeb66..080397c 100644 --- a/scripts/Contents/Characters/Rooster.gd +++ b/scripts/Contents/Characters/Rooster.gd @@ -25,27 +25,12 @@ func ai(): move(direction) if direction.length() == 0: texture.play("idle") - if Input.is_action_just_pressed("attack"): - startCharge(0) - if Input.is_action_just_released("attack"): - endCharge(0) - if Input.is_action_just_pressed("attack2"): - startCharge(1) - if Input.is_action_just_released("attack2"): - endCharge(1) - if Input.is_action_just_pressed("smallSkill"): - startCharge(2) - if Input.is_action_just_released("smallSkill"): - endCharge(2) - if Input.is_action_just_pressed("superSkill"): - startCharge(3) - if Input.is_action_just_released("superSkill"): - endCharge(3) + tryLaunch("attack", 0) + tryLaunch("attack2", 1) + tryLaunch("smallSkill", 2) + tryLaunch("superSkill", 3) for i in range(3): - if Input.is_action_just_pressed("cardSkill" + str(i)): - startCharge(4 + i) - if Input.is_action_just_released("cardSkill" + str(i)): - endCharge(4 + i) + tryLaunch("cardSkill%d" % i, 4 + i) if Input.is_action_just_pressed("sprint"): trySprint() if Input.is_action_just_pressed("heal"): @@ -60,21 +45,25 @@ func sprint(): Input.get_axis("m_up", "m_down") ) * sprintMultiplier, true) -func startCharge(weaponIndex: int): - if len(weapons) > weaponIndex: - var weapon = weapons[weaponIndex] - if weapon.chargable: - chargeStartTime[weaponIndex] = Time.get_ticks_msec() - chargeParticle.emitting = true -func endCharge(weaponIndex: int): - if chargeStartTime.has(weaponIndex): - var startTime = chargeStartTime[weaponIndex] - var endTime = Time.get_ticks_msec() - var chargedTime = endTime - startTime - chargeStartTime.erase(weaponIndex) +func tryLaunch(action: String, weaponIndex: int): + if Input.is_action_just_pressed(action): if len(weapons) > weaponIndex: var weapon = weapons[weaponIndex] - if weapon.chargable: - weapon.chargedTime = chargedTime - tryAttack(weaponIndex) - chargeParticle.emitting = false + if weapon.chargable and weapon.canAttackBy(self): + chargeStartTime[weaponIndex] = Time.get_ticks_msec() + chargeParticle.emitting = true + if Input.is_action_pressed(action): + if !chargeStartTime.has(weaponIndex): + tryAttack(weaponIndex) + if Input.is_action_just_released(action): + if chargeStartTime.has(weaponIndex): + var startTime = chargeStartTime[weaponIndex] + var endTime = Time.get_ticks_msec() + var chargedTime = endTime - startTime + chargeStartTime.erase(weaponIndex) + if len(weapons) > weaponIndex: + var weapon = weapons[weaponIndex] + if weapon.chargable: + weapon.chargedTime = chargedTime + tryAttack(weaponIndex) + chargeParticle.emitting = false diff --git a/scripts/Statemachine/EntityBase.gd b/scripts/Statemachine/EntityBase.gd index f600f1e..dd32e21 100644 --- a/scripts/Statemachine/EntityBase.gd +++ b/scripts/Statemachine/EntityBase.gd @@ -292,11 +292,12 @@ func finalEnergy(base: float): return base / fields.get(FieldStore.Entity.SAVE_ENERGY) func fillingProgress(base: float): return energy / finalEnergy(base) +func isEnergyEnough(base: float): + return energy >= finalEnergy(base) func useEnergy(value: float): - value = finalEnergy(value) - var state = energy >= value + var state = isEnergyEnough(value) if state: - energy -= value + energy -= finalEnergy(value) energyChanged.emit(energy, false) return state func tryAttack(type: int, needChargeUp: bool = false): diff --git a/scripts/Structs/Weapon.gd b/scripts/Structs/Weapon.gd index 2522fbf..10838e0 100644 --- a/scripts/Structs/Weapon.gd +++ b/scripts/Structs/Weapon.gd @@ -166,9 +166,11 @@ func playSound(sound: String): cloned.play() await cloned.finished cloned.queue_free() -func tryAttack(entity: EntityBase): +func canAttackBy(entity: EntityBase): cooldownTimer.speedScale = entity.fields.get(FieldStore.Entity.ATTACK_SPEED) - if cooldownTimer.isCooldowned(): + return cooldownTimer.isCooldowned() and entity.isEnergyEnough(needEnergy) +func tryAttack(entity: EntityBase): + if canAttackBy(entity): var result = await attack(entity) if result: cooldownTimer.start()