1
1
mirror of https://github.com/Rundll86/Dog-Lynx-And-HCN.git synced 2026-06-29 23:12:28 +08:00

refactor(武器系统): 优化武器攻击逻辑和能量检查

- 将武器攻击前的条件检查提取为独立方法 canAttackBy
- 重构能量检查逻辑,增加 isEnergyEnough 方法
- 简化公鸡角色的输入处理,合并重复代码为 tryLaunch 方法
- 调整钢管武器的冷却时间为 3000.0
This commit is contained in:
2026-02-05 20:52:04 +08:00
parent d1bf911c79
commit e1ac836497
4 changed files with 34 additions and 42 deletions
+1 -1
View File
@@ -21,7 +21,7 @@ storeType = {
descriptionTemplate = "按住蓄力扔出钢管,蓄力越久伤害越高。 descriptionTemplate = "按住蓄力扔出钢管,蓄力越久伤害越高。
基础伤害:$atk,蓄力倍率:$charge" 基础伤害:$atk,蓄力倍率:$charge"
needEnergy = 2.0 needEnergy = 2.0
cooldown = 1000.0 cooldown = 3000.0
[node name="avatar" parent="container/info" index="0"] [node name="avatar" parent="container/info" index="0"]
texture = ExtResource("2_mmtf8") texture = ExtResource("2_mmtf8")
+25 -36
View File
@@ -25,27 +25,12 @@ func ai():
move(direction) move(direction)
if direction.length() == 0: if direction.length() == 0:
texture.play("idle") texture.play("idle")
if Input.is_action_just_pressed("attack"): tryLaunch("attack", 0)
startCharge(0) tryLaunch("attack2", 1)
if Input.is_action_just_released("attack"): tryLaunch("smallSkill", 2)
endCharge(0) tryLaunch("superSkill", 3)
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)
for i in range(3): for i in range(3):
if Input.is_action_just_pressed("cardSkill" + str(i)): tryLaunch("cardSkill%d" % i, 4 + i)
startCharge(4 + i)
if Input.is_action_just_released("cardSkill" + str(i)):
endCharge(4 + i)
if Input.is_action_just_pressed("sprint"): if Input.is_action_just_pressed("sprint"):
trySprint() trySprint()
if Input.is_action_just_pressed("heal"): if Input.is_action_just_pressed("heal"):
@@ -60,21 +45,25 @@ func sprint():
Input.get_axis("m_up", "m_down") Input.get_axis("m_up", "m_down")
) * sprintMultiplier, true) ) * sprintMultiplier, true)
func startCharge(weaponIndex: int): func tryLaunch(action: String, weaponIndex: int):
if len(weapons) > weaponIndex: if Input.is_action_just_pressed(action):
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)
if len(weapons) > weaponIndex: if len(weapons) > weaponIndex:
var weapon = weapons[weaponIndex] var weapon = weapons[weaponIndex]
if weapon.chargable: if weapon.chargable and weapon.canAttackBy(self):
weapon.chargedTime = chargedTime chargeStartTime[weaponIndex] = Time.get_ticks_msec()
tryAttack(weaponIndex) chargeParticle.emitting = true
chargeParticle.emitting = false 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
+4 -3
View File
@@ -292,11 +292,12 @@ func finalEnergy(base: float):
return base / fields.get(FieldStore.Entity.SAVE_ENERGY) return base / fields.get(FieldStore.Entity.SAVE_ENERGY)
func fillingProgress(base: float): func fillingProgress(base: float):
return energy / finalEnergy(base) return energy / finalEnergy(base)
func isEnergyEnough(base: float):
return energy >= finalEnergy(base)
func useEnergy(value: float): func useEnergy(value: float):
value = finalEnergy(value) var state = isEnergyEnough(value)
var state = energy >= value
if state: if state:
energy -= value energy -= finalEnergy(value)
energyChanged.emit(energy, false) energyChanged.emit(energy, false)
return state return state
func tryAttack(type: int, needChargeUp: bool = false): func tryAttack(type: int, needChargeUp: bool = false):
+4 -2
View File
@@ -166,9 +166,11 @@ func playSound(sound: String):
cloned.play() cloned.play()
await cloned.finished await cloned.finished
cloned.queue_free() cloned.queue_free()
func tryAttack(entity: EntityBase): func canAttackBy(entity: EntityBase):
cooldownTimer.speedScale = entity.fields.get(FieldStore.Entity.ATTACK_SPEED) 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) var result = await attack(entity)
if result: if result:
cooldownTimer.start() cooldownTimer.start()