From e597b9f7be088cdc6a7f351148660f4bee35b0e3 Mon Sep 17 00:00:00 2001 From: xiekaidong Date: Wed, 5 Jul 2023 17:05:17 +0800 Subject: [PATCH] =?UTF-8?q?=E6=88=98=E6=96=97=E4=B8=AD=E5=B1=9E=E6=80=A7?= =?UTF-8?q?=E5=8A=A0=E5=AF=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lua/app/global/global_const.lua | 2 +- lua/app/global/global_func.lua | 8 + lua/app/module/battle/battle_const.lua | 2 + .../battle/team/battle_team_entity.lua | 153 ++++++++++-------- .../battle/team/battle_unit_entity.lua | 2 +- 5 files changed, 101 insertions(+), 66 deletions(-) diff --git a/lua/app/global/global_const.lua b/lua/app/global/global_const.lua index 206fadad..3b810651 100644 --- a/lua/app/global/global_const.lua +++ b/lua/app/global/global_const.lua @@ -55,7 +55,7 @@ else end GConst.UI_SCREEN_WIDTH = width GConst.UI_SCREEN_HEIGHT = height - +GConst.NUMBER_ENCRYPTION_CONSTANT = 163 GConst.WAIT_NET_RSP_TIME = 8 -- 与服务器交互的等待回复时间 GConst.INVALID = -1 GConst.DEPTH_BUFFER = 24 diff --git a/lua/app/global/global_func.lua b/lua/app/global/global_func.lua index 53658360..ef37d898 100644 --- a/lua/app/global/global_func.lua +++ b/lua/app/global/global_func.lua @@ -1742,4 +1742,12 @@ function GFunc.readOnlyTab(inputTable) return _read_only(inputTable) end +function GFunc.encryptNumber(value) + return value + GConst.NUMBER_ENCRYPTION_CONSTANT +end + +function GFunc.decryptNumber(value) + return value - GConst.NUMBER_ENCRYPTION_CONSTANT +end + return GFunc \ No newline at end of file diff --git a/lua/app/module/battle/battle_const.lua b/lua/app/module/battle/battle_const.lua index ec887c3b..65e9856a 100644 --- a/lua/app/module/battle/battle_const.lua +++ b/lua/app/module/battle/battle_const.lua @@ -335,6 +335,8 @@ local BUFF_NAME = { BattleConst.BUFF_NAME = BUFF_NAME local ATTR_NAME = { + HP = "hp", + MAX_HP = "max_hp", ATK = "atk", ATK_RED = "atk_red", ATK_YELLOW = "atk_yellow", diff --git a/lua/app/userdata/battle/team/battle_team_entity.lua b/lua/app/userdata/battle/team/battle_team_entity.lua index 9f05cf30..183439b9 100644 --- a/lua/app/userdata/battle/team/battle_team_entity.lua +++ b/lua/app/userdata/battle/team/battle_team_entity.lua @@ -23,14 +23,14 @@ function BattleTeamEntity:init(side, data) self.side = side if self.baseAttr then for k, v in pairs(self.baseAttr) do - self.baseAttr[k] = 0 + self:setBaseAttrValue(k, 0) end else self.baseAttr = {} end if self.attr then for k, v in pairs(self.attr) do - self.attr[k] = 0 + self:setAttrValue(k, 0) end else self.attr = {} @@ -83,50 +83,75 @@ function BattleTeamEntity:addUnit(unitData) return unit end +function BattleTeamEntity:getBaseAttrValue(name) + if not self.baseAttr[name] then + return + end + return GFunc.decryptNumber(self.baseAttr[name]) +end + +function BattleTeamEntity:setBaseAttrValue(name, value) + value = value or 0 + self.baseAttr[name] = GFunc.encryptNumber(value) +end + function BattleTeamEntity:addBaseAttr(unitAttr) for k, v in pairs(unitAttr) do - self.baseAttr[k] = (self.baseAttr[k] or 0) + v - self.attr[k] = (self.attr[k] or 0) + v + self:setBaseAttrValue(k, (self:getBaseAttrValue(k) or 0) + v) + self:setAttrValue(k, (self:getAttrValue(k) or 0) + v) end end +function BattleTeamEntity:getAttrValue(name, noDefault) + if not self.attr[name] then + if noDefault then + return + else + return 0 + end + end + return GFunc.decryptNumber(self.attr[name]) +end + +function BattleTeamEntity:setAttrValue(name, value) + value = value or 0 + self.attr[name] = GFunc.encryptNumber(value) +end + function BattleTeamEntity:addAttr(name, num, isPercent) local addNum = 0 if isPercent then - if self.attr[name] then + if self:getAttrValue(name, true) then -- 百分比加减属性的时候如果是减少属性,则先取正数做计算,在减去此结果, 否则在做整除运算的时候,正数和负数整除后的绝对值会不一致 -- 例如150 // 100 = 1, -150 // 100 = -2, 导致添加和移除的时候计算的属性值不一样 + local attrValue = self:getAttrValue(name) if num < 0 then - if self.baseAttr[name] then - addNum = self.baseAttr[name] * -num // DEFAULT_FACTOR - self.attr[name] = self.attr[name] - addNum + if self:getBaseAttrValue(name) then + addNum = self:getBaseAttrValue(name) * -num // DEFAULT_FACTOR + self:setAttrValue(name, attrValue - addNum) else - addNum = self.attr[name] * -num // DEFAULT_FACTOR - self.attr[name] = self.attr[name] - addNum + addNum = attrValue * -num // DEFAULT_FACTOR + self:setAttrValue(name, attrValue - addNum) end addNum = -addNum else - if self.baseAttr[name] then - addNum = self.baseAttr[name] * num // DEFAULT_FACTOR - self.attr[name] = self.attr[name] + addNum + if self:getBaseAttrValue(name) then + addNum = self:getBaseAttrValue(name) * num // DEFAULT_FACTOR + self:setAttrValue(name, attrValue + addNum) else - addNum = self.attr[name] * num // DEFAULT_FACTOR - self.attr[name] = self.attr[name] + addNum + addNum = attrValue * num // DEFAULT_FACTOR + self:setAttrValue(name, attrValue + addNum) end end end else addNum = num - local currNum = self.attr[name] or 0 - self.attr[name] = currNum + num + local currNum = self:getAttrValue(name) + self:setAttrValue(name, currNum + num) end return addNum end -function BattleTeamEntity:setAttr(name, num) - self.attr[name] = num -end - function BattleTeamEntity:getAllMembers() return self.members end @@ -136,60 +161,61 @@ function BattleTeamEntity:getMembersCount() end function BattleTeamEntity:getHp() - return self.attr.hp + return self:getAttrValue(ATTR_NAME.HP) end function BattleTeamEntity:getMaxHp() - return self.attr.max_hp + return self:getAttrValue(ATTR_NAME.MAX_HP) end function BattleTeamEntity:getHpPercent(hp) - hp = hp or self.attr.hp - return hp / self.attr.max_hp + local maxHp = self:getMaxHp() + hp = hp or self:getHp() + return hp / maxHp end function BattleTeamEntity:getAtk(matchType) - return self.attr[MATCH_ATTACK_NAME[matchType]] or 0 + return self:getAttrValue(MATCH_ATTACK_NAME[matchType]) end function BattleTeamEntity:getDmgAddition(matchType) - return (self.attr.dmg_addition_all or 0) + (self.attr[MATCH_DMG_ADDITION_NAME[matchType]] or 0) + return self:getAttrValue(ATTR_NAME.DMG_ADDITION_ALL) + self:getAttrValue(MATCH_DMG_ADDITION_NAME[matchType]) end function BattleTeamEntity:getDmgDec(matchType) - return (self.attr.dmg_dec_all or 0) + (self.attr[MATCH_DMG_DEC_NAME[matchType]] or 0) + return self:getAttrValue(ATTR_NAME.DMG_DEC_ALL) + self:getAttrValue(MATCH_DMG_DEC_NAME[matchType]) end function BattleTeamEntity:getDecDmg(matchType) - return (self.attr.dec_dmg_all or 0) + (self.attr[MATCH_DEC_DMG_NAME[matchType]] or 0) + return self:getAttrValue(ATTR_NAME.DEC_DMG_ALL) + self:getAttrValue(MATCH_DEC_DMG_NAME[matchType]) end function BattleTeamEntity:getWeakness(matchType) - return (self.attr.weakness_all or 0) + (self.attr[MATCH_WEAKNESS_NAME[matchType]] or 0) + return self:getAttrValue(ATTR_NAME.WEAKNESS_ALL) + self:getAttrValue(MATCH_WEAKNESS_NAME[matchType]) end function BattleTeamEntity:getCrittime() - return self.attr.crit_time or 0 + return self:getAttrValue(ATTR_NAME.CRIT_TIME) end function BattleTeamEntity:getCrit() - return self.attr.crit or 0 + return self:getAttrValue(ATTR_NAME.CRIT) end function BattleTeamEntity:getCureAddition() - return self.attr.cure_addition or 0 + return self:getAttrValue(ATTR_NAME.CURE_ADDITION) end function BattleTeamEntity:getCureDec() - return self.attr.cure_dec or 0 + return self:getAttrValue(ATTR_NAME.CURE_DEC) end function BattleTeamEntity:getBeDmgToHeal() - return self.attr.be_dmg_to_heal or 0 + return self:getAttrValue(ATTR_NAME.BE_DMG_TO_HEAL) end function BattleTeamEntity:getBeSucked() - return self.attr.be_sucked or 0 + return self:getAttrValue(ATTR_NAME.BE_SUCKED) end function BattleTeamEntity:getUndeadHp() @@ -201,32 +227,32 @@ function BattleTeamEntity:setUndeadHp(hp) end function BattleTeamEntity:getNormalAttackAddCount() - return self.attr[ATTR_NAME.NORMAL_ATTACK_COUNT] or 0 + return self:getAttrValue(ATTR_NAME.NORMAL_ATTACK_COUNT) end function BattleTeamEntity:getThorns() - return self.attr[ATTR_NAME.THORNS] or 0 + return self:getAttrValue(ATTR_NAME.THORNS) end function BattleTeamEntity:getExpTime() - return self.attr.exp_time or 0 + return self:getAttrValue(ATTR_NAME.EXP_TIME) end function BattleTeamEntity:getFirstHand() - return self.attr.first_hand or 0 + return self:getAttrValue(ATTR_NAME.FIRST_HAND) end function BattleTeamEntity:getDeathSummon() - return self.attr.death_summon or 0 + return self:getAttrValue(ATTR_NAME.DEATH_SUMMON) end function BattleTeamEntity:addMaxHp(num) - local hpBefore = self.attr.hp - local currPercent = hpBefore * DEFAULT_FACTOR // self.attr.max_hp - self.attr.max_hp = self.attr.max_hp + self.baseAttr.max_hp * num // DEFAULT_FACTOR - self.attr.hp = currPercent*self.attr.max_hp//DEFAULT_FACTOR - if self.attr.hp <= 0 and hpBefore > 0 then - self.attr.hp = 1 + local hpBefore = self:getHp() + local currPercent = hpBefore * DEFAULT_FACTOR // self:getMaxHp() + self:setAttrValue(ATTR_NAME.MAX_HP, self:getMaxHp() + (self:getBaseAttrValue(ATTR_NAME.MAX_HP) or 0) * num // DEFAULT_FACTOR) + self:setAttrValue(ATTR_NAME.HP, currPercent * self:getMaxHp() // DEFAULT_FACTOR) + if self:getHp() <= 0 and hpBefore > 0 then + self:setAttrValue(ATTR_NAME.HP, 1) end end @@ -295,23 +321,19 @@ function BattleTeamEntity:getShieldHp() end function BattleTeamEntity:getShieldRebound() - return self.attr.shield_rebound or 0 + return self:getAttrValue(ATTR_NAME.SHIELD_REBOUND) end function BattleTeamEntity:getBlock() - return self.attr.block or 0 + return self:getAttrValue(ATTR_NAME.BLOCK) end function BattleTeamEntity:getCounterAttack() - return self.attr.counterattack or 0 + return self:getAttrValue(ATTR_NAME.COUNTER_ATTACK) end function BattleTeamEntity:getSkillHurt() - return self.attr.skill_hurt or 0 -end - -function BattleTeamEntity:getAttrValue(attrName) - return self.attr[attrName] or 0 + return self:getAttrValue(ATTR_NAME.SKILL_HURT) end function BattleTeamEntity:takeDamageOrCure(num) @@ -324,23 +346,26 @@ function BattleTeamEntity:takeDamageOrCure(num) return 0 end end - local hpBefore = self.attr.hp - self.attr.hp = self.attr.hp + num + local hpBefore = self:getHp() + self:setAttrValue(ATTR_NAME.HP, self:getHp() + num) local hurtEventNum = 0 - if self:getUndeadHp() > 0 and self.attr.hp <= 0 then - self.attr.hp = self:getUndeadHp() + if self:getUndeadHp() > 0 and self:getHp() <= 0 then + self:setAttrValue(ATTR_NAME.HP, self:getUndeadHp()) num = self:getUndeadHp() - hpBefore end - if self.attr.hp <= 0 then -- 死了 + + local hp = self:getHp() + local maxhp = self:getMaxHp() + if hp <= 0 then -- 死了 hurtEventNum = -hpBefore - self.attr.hp = 0 + self:setAttrValue(ATTR_NAME.HP, 0) self:die() - elseif self.attr.hp < self.attr.max_hp then + elseif hp < maxhp then hurtEventNum = num else -- 满血了 -- 这是加血 - hurtEventNum = self.attr.max_hp - hpBefore - self.attr.hp = self.attr.max_hp + hurtEventNum = maxhp - hpBefore + self:setAttrValue(ATTR_NAME.HP, maxhp) end return hurtEventNum end diff --git a/lua/app/userdata/battle/team/battle_unit_entity.lua b/lua/app/userdata/battle/team/battle_unit_entity.lua index 04ab878d..e8db0e48 100644 --- a/lua/app/userdata/battle/team/battle_unit_entity.lua +++ b/lua/app/userdata/battle/team/battle_unit_entity.lua @@ -104,7 +104,7 @@ function BattleUnitEntity:addAttr(name, num, isPercent) end function BattleUnitEntity:setAttr(name, num) - return self.team:setAttr(name, num) + return self.team:setAttrValue(name, num) end function BattleUnitEntity:getModelId()