You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Swift运行时修改攻击函数逻辑:伤害药水功能实现求助

解决方案:实现伤害药水的攻击加成效果

1. 给Character类添加状态属性

先在Character类里加一个布尔属性,用来跟踪伤害药水的激活状态:

class Character {
    // 你的其他属性(比如health)
    var hasHarmingPotionActive = false // 标记是否激活伤害加成
    
    // 已有的updateHealth方法
    func updateHealth(amount: Int, target: Character) {
        // 你的生命值更新逻辑
    }
}

2. 修改attack函数的伤害计算逻辑

直接根据角色的药水状态计算50%伤害加成,不用再依赖调用时传入的extra参数(如果需要保留extra参数,也可以结合状态灵活调整):

func attack(enemy: Character) {
    // 基础伤害范围保持0-30
    var dmgDealt = Int.random(in: 0...30)
    
    // 激活药水时提升50%伤害
    if hasHarmingPotionActive {
        dmgDealt = Int(Double(dmgDealt) * 1.5)
        // 要是想让加成后的伤害范围更固定,也可以直接改成随机15-45,但乘以1.5更贴合"50%提升"的描述
    }
    
    updateHealth(amount: -dmgDealt, target: enemy)
    
    // 如果是单次生效的药水,攻击后关闭效果:
    // hasHarmingPotionActive = false
}

如果需要保留原有的extra参数(比如用于其他额外伤害场景),可以调整为:

func attack(enemy: Character, extra: Bool = false) {
    let baseRange = extra ? (15...45) : (0...30)
    var dmgDealt = Int.random(in: baseRange)
    
    if hasHarmingPotionActive {
        dmgDealt = Int(Double(dmgDealt) * 1.5)
    }
    
    updateHealth(amount: -dmgDealt, target: enemy)
}

3. 完善shop函数的药水生效逻辑

修改购买伤害药水的分支,激活角色的伤害加成状态,同时要先检查货币是否充足,另外currency要改成inout参数才能修改外部的货币值:

func shop(target: Character, currency: inout myCoins) {
    print("Hello! Welcome to the shop! Our stock includes: \nHarming Potion: You do 50% more damage. 200 Coins.\nHealth Potion: Gives a one-time grant of 75 health. 150 Coins.\nTurtle Potion: You heal for more health but are more susceptible to attacks from the enemy. 300 coins.\nDamage Potion: Throw this at the enemy to deal 50 Damage! 200 coins.")
    
    guard let potionChoice = readLine()?.lowercased() else {
        print("Invalid input!")
        return
    }
    
    print("Good Choice!")
    switch potionChoice {
        case "harming":
            if currency >= 200 {
                currency -= 200
                target.hasHarmingPotionActive = true
                print("Harming Potion activated! Your attacks now deal 50% more damage.")
            } else {
                print("Not enough coins to buy Harming Potion!")
            }
        
        case "health":
            if currency >= 150 {
                currency -= 150
                target.updateHealth(amount: 75, target: target) // 给自己加75血
                print("You gained 75 health!")
            } else {
                print("Not enough coins to buy Health Potion!")
            }
        
        case "turtle":
            if currency >= 300 {
                currency -= 300
                print("Turtle Potion activated! You heal more but take more damage.")
                // 这里可以给Character加对应属性(比如turtlePotionActive),后续修改治疗/受击逻辑
                // target.turtlePotionActive = true
            } else {
                print("Not enough coins to buy Turtle Potion!")
            }
        
        case "damage":
            if currency >= 200 {
                currency -= 200
                // 直接对敌人造成50伤害,需要你在调用shop时传入敌人对象,或者调整函数参数
                // enemy.updateHealth(amount: -50, target: enemy)
                print("You threw a Damage Potion! Enemy takes 50 damage!")
            } else {
                print("Not enough coins to buy Damage Potion!")
            }
        
        default:
            print("That's not a valid potion choice!")
    }
}

关键注意点

  • currency参数必须用inout,否则修改后外部的货币值不会更新,调用时要传&currency。
  • 根据需求决定药水是永久生效还是单次生效:如果是单次,在attack函数攻击后把hasHarmingPotionActive设回false即可。
  • 其他药水(比如乌龟药水)可以参考这个模式,给Character添加对应状态属性来管理效果。

内容的提问来源于stack exchange,提问作者ELIAS

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.10 02:15:43