寻找一种更干净、更高效的方式

function GetDamage(spell, unit)
    if spell == _Q and (isReady(_Q) or qActive) and not HasItem(3025) and not HasItem(3100) and not HasItem(3057) and not HasItem(3078) then
        return myHero:CalcDamage(unit, ((((myHero:GetSpellData(_Q).level * 20) + 10) + myHero.totalDamage) + qStacks))
    elseif spell == _Q and (isReady(_Q) or qActive) and (HasItem(3057) or sheenActive) then
        return myHero:CalcDamage(unit, myHero.damage) + myHero:CalcDamage(unit, ((((myHero:GetSpellData(_Q).level * 20) + 10) + myHero.totalDamage) + qStacks))
    elseif spell == _Q and (isReady(_Q) or qActive) and (HasItem(3025) or fActive) then
        return myHero:CalcDamage(unit, myHero.damage) + myHero:CalcDamage(unit, ((((myHero:GetSpellData(_Q).level * 20) + 10) + myHero.totalDamage) + qStacks))
    elseif spell == _Q and (isReady(_Q) or qActive) and (HasItem(3100) or lActive) then
        return myHero:CalcMagicDamage(unit, ((myHero.damage * 0.75) + (myHero.ap * 0.5))) + myHero:CalcDamage(unit, ((((myHero:GetSpellData(_Q).level * 20) + 10) + myHero.totalDamage) + qStacks))
    elseif spell == _Q and (isReady(_Q) or qActive) and (HasItem(3078) or tActive) then
        return myHero:CalcDamage(unit, (myHero.damage * 2)) + myHero:CalcDamage(unit, ((((myHero:GetSpellData(_Q).level * 20) + 10) + myHero.totalDamage) + qStacks))
    elseif spell == _E and isReady(_E) then
        return myHero:CalcMagicDamage(unit, (((myHero:GetSpellData(_E).level * 40) + 15) + (myHero.ap * 0.6)))
    else
        return 0
    end
end

这是我用来返回对特定敌人造成伤害的代码,虽然它能按预期工作,但它看起来过于冗长而且难看,而且我希望它能稍微有些不同的工作方式,我不确定如何编写它。

我希望它能做到的是,如果我输入了例如GetDamage(all)或类似的东西,我希望它返回我可以造成的总伤害,只要isReady(spell)为true,即如果技能qe准备就绪,它将只返回qe的总伤害,或者如果所有技能都准备就绪,它将返回所有技能的总伤害,此外,如果我只需要知道r的伤害,我仍然只需要输入GetDamage(_R)

有没有一种更干净的方法使用一个表或者一种更有效的方式来得到我所需要的结果?因为当前使用GetDamage(spell) + GetDamage(spell2) + GetDamage(spell3)等方式看起来像一个非常糟糕的编码。

点赞
用户1847592
用户1847592

介绍新技能“ALL”

function GetDamage(spell, unit)
   local result = 0
   if (spell == "ALL" or spell == _Q) and (isReady(_Q) or qActive) then
      local bonus = 0
      if (HasItem(3057) or sheenActive) then
         bonus = myHero:CalcDamage(unit, myHero.damage)
      elseif (HasItem(3025) or fActive) then
         bonus = myHero:CalcDamage(unit, myHero.damage)
      elseif (HasItem(3100) or lActive) then
         bonus = myHero:CalcMagicDamage(unit, ((myHero.damage * 0.75) + (myHero.ap * 0.5)))
      elseif (HasItem(3078) or tActive) then
         bonus = myHero:CalcDamage(unit, (myHero.damage * 2))
      end
      result = result + bonus + myHero:CalcDamage(unit, ((((myHero:GetSpellData(_Q).level * 20) + 10) + myHero.totalDamage) + qStacks))
   end
   if (spell == "ALL" or spell == _E) and isReady(_E) then
      result = result + myHero:CalcMagicDamage(unit, (((myHero:GetSpellData(_E).level * 40) + 15) + (myHero.ap * 0.6)))
   end
   return result
end

使用示例:

dmg = GetDamage("ALL", unit)
dmg = GetDamage(_Q, unit)
dmg = GetDamage(_E, unit)

编辑:

还有一种实现方式是使用以技能为键、以函数为值的表:

spell_dmg_func = {
   [_Q] =
      function(unit)
         if (isReady(_Q) or qActive) then
            local bonus = 0
            if (HasItem(3057) or sheenActive) then
               bonus = myHero:CalcDamage(unit, myHero.damage)
            elseif (HasItem(3025) or fActive) then
               bonus = myHero:CalcDamage(unit, myHero.damage)
            elseif (HasItem(3100) or lActive) then
               bonus = myHero:CalcMagicDamage(unit, ((myHero.damage * 0.75) + (myHero.ap * 0.5)))
            elseif (HasItem(3078) or tActive) then
               bonus = myHero:CalcDamage(unit, (myHero.damage * 2))
            end
            return bonus + myHero:CalcDamage(unit, ((((myHero:GetSpellData(_Q).level * 20) + 10) + myHero.totalDamage) + qStacks))
         end
      end,
   [_E] =
      function(unit)
         if isReady(_E) then
            return myHero:CalcMagicDamage(unit, (((myHero:GetSpellData(_E).level * 40) + 15) + (myHero.ap * 0.6)))
         end
      end,
}

function GetDamage(spell, unit)
   if spells == "ALL" then
      local sum = 0
      for spell, func in pairs(spell_dmg_func) do
         sum = sum + (func(unit) or 0)
      end
      return sum
   else
      return spell_dmg_func[spell](unit) or 0
   end
end
2017-01-05 11:13:50