From 2f0e60fb5a84b1bf137c00f417e0060823422c00 Mon Sep 17 00:00:00 2001 From: chenxi Date: Mon, 17 Apr 2023 00:09:48 +0800 Subject: [PATCH] battle team --- .../battle/component/battle_unit_comp.lua | 4 ++ .../battle/controller/battle_controller.lua | 53 +++++++------- .../controller/battle_controller_stage.lua | 23 ++---- lua/app/module/battle/team.meta | 8 +++ lua/app/module/battle/team/battle_team.lua | 71 +++++++++++++++++++ .../module/battle/team/battle_team.lua.meta | 10 +++ 6 files changed, 122 insertions(+), 47 deletions(-) create mode 100644 lua/app/module/battle/team.meta create mode 100644 lua/app/module/battle/team/battle_team.lua create mode 100644 lua/app/module/battle/team/battle_team.lua.meta diff --git a/lua/app/module/battle/component/battle_unit_comp.lua b/lua/app/module/battle/component/battle_unit_comp.lua index 90bc4ec6..53e56a1b 100644 --- a/lua/app/module/battle/component/battle_unit_comp.lua +++ b/lua/app/module/battle/component/battle_unit_comp.lua @@ -32,6 +32,10 @@ function BattleUnitComp:getModelId() return self.modelId end +function BattleUnitComp:getMatchType() + return self.unitEntity:getMatchType() +end + function BattleUnitComp:_initBase() self.isClear = false self.isMove = false diff --git a/lua/app/module/battle/controller/battle_controller.lua b/lua/app/module/battle/controller/battle_controller.lua index 520b6718..5a779e56 100644 --- a/lua/app/module/battle/controller/battle_controller.lua +++ b/lua/app/module/battle/controller/battle_controller.lua @@ -1,5 +1,6 @@ local BattleHelper = require "app/module/battle/helper/battle_helper" local BattleScheduler = require "app/module/battle/helper/battle_scheduler" +local BattleTeam = require "app/module/battle/team/battle_team" local BattleController = class("BattleController") @@ -130,19 +131,24 @@ function BattleController:init(params) self.maxWaveIndex = self:getMaxWave() self.victory = false self.roundStep = BattleConst.BATTLE_ROUND_STEP.WAIT_BEGIN - self.atkUnits = {} - self.defUnits = {} - self.allUnits = {} self.effectTexts = {} self.instructions = {} self.time = 0 self.battleData:init() BattleScheduler:init() BattleHelper:init() + self:initBattleTeam() self:initOther() self:prepareFight() end +function BattleController:initBattleTeam() + self.atkTeam = BattleTeam:create() + self.atkTeam:init(BattleConst.SIDE_ATK) + self.defTeam = BattleTeam:create() + self.defTeam:init(BattleConst.SIDE_DEF) +end + function BattleController:prepareFight() local count = 0 local totalCount = 3 @@ -164,25 +170,26 @@ function BattleController:prepareFight() end function BattleController:initAtkUnits(callback) - local atkTeam = self.battleData:getAtkTeam() + local atkTeamEntity = self.battleData:getAtkTeam() local count = 0 - local totalCount = atkTeam:getMembersCount() + local totalCount = atkTeamEntity:getMembersCount() local function onloadFinished() count = count + 1 if count == totalCount then - self.battleUI:refreshAtkHp(atkTeam:getHp(), atkTeam:getHpPercent()) + self.battleUI:refreshAtkHp(atkTeamEntity:getHp(), atkTeamEntity:getHpPercent()) callback() end end - local members = atkTeam:getAllMembers() + local members = atkTeamEntity:getAllMembers() for k, v in pairs(members) do local modelId = v:getModelId() BattleHelper:loadBattleHeroModel(modelId, self.battleUI:getBattleNode(), function(spineObject) local heroComp = spineObject:addLuaComponent(BattleConst.TYPEOF_LUA_COMP.BATTLE_HERO_COMPONENT) heroComp:initWithEntity(modelId, v, self) - self.atkUnits[v:getMatchType()] = heroComp - table.insert(self.allUnits, heroComp) - if not v:getIsMainUnit() then + if v:getIsMainUnit() then + self.atkTeam:addUnit(heroComp, true) + else + self.atkTeam:addUnit(heroComp) heroComp:hideOutsideScreen() end onloadFinished() @@ -203,9 +210,9 @@ end function BattleController:getOtherSideMainUnit(side) if side == BattleConst.SIDE_ATK then - return self.defMainUnit + return self.defTeam:getMainUnit() else - return self.atkMainUnit + return self.atkTeam:getMainUnit() end end @@ -305,7 +312,7 @@ end function BattleController:enterDefStep() self.roundStep = BattleConst.BATTLE_ROUND_STEP.ON_DEF_STEP - self.defMainUnit:useAllSkills(function() + self.defTeam:mainUnitUseAllSkills(function() self:enterDefStepOver() end) end @@ -1168,9 +1175,8 @@ function BattleController:_tick(dt) return end self.time = self.time + dt - for k, v in ipairs(self.allUnits) do - v:tick(dt) - end + self.atkTeam:tick(dt) + self.defTeam:tick(dt) local count = #self.effectTexts for i = count, 1, -1 do self.effectTexts[i]:tick(dt) @@ -1218,24 +1224,13 @@ local function _assisting(self, instruction, callback) end local function _generalAttack(self, instruction, callback) - local hero = self.atkUnits[instruction.skillMatch] - if hero == nil then - return callback() - end - self.atkMainUnit = hero - hero:useNormalSkill(instruction.count, callback) + self.atkTeam:useNormalSkill(instruction.skillMatch, instruction.count, callback) end local function _playSkill(self, instruction, callback) - local hero = self.atkUnits[instruction.skillMatch] - if hero == nil then - return callback() - end - self.atkMainUnit = hero - hero:useSkill(1, callback) + self.atkTeam:useSkill(instruction.skillMatch, callback) end - BattleController._doInstruction = { [BattleConst.INSTRUCTION_NAME.ADD_CUR_ROUND_ATTR] = _addCurRoundAttr, [BattleConst.INSTRUCTION_NAME.ASSISTING] = _assisting, diff --git a/lua/app/module/battle/controller/battle_controller_stage.lua b/lua/app/module/battle/controller/battle_controller_stage.lua index c650938c..61f6bc1b 100644 --- a/lua/app/module/battle/controller/battle_controller_stage.lua +++ b/lua/app/module/battle/controller/battle_controller_stage.lua @@ -24,9 +24,7 @@ function BattleControllerStage:initDefUnits(callback) BattleHelper:loadBattleHeroModel(modelId, self.battleUI:getBattleNode(), function(spineObject) local monsterComp = spineObject:addLuaComponent(GConst.BattleConst.TYPEOF_LUA_COMP.BATTLE_MONSTER_COMPONENT) monsterComp:initWithEntity(modelId, unitEntity, self) - self.defUnits[unitEntity:getMatchType()] = monsterComp - table.insert(self.allUnits, monsterComp) - self.defMainUnit = monsterComp + self.defTeam:addUnit(monsterComp, true) self.battleUI:refreshDefHp(unitEntity:getHp(), unitEntity:getHpPercent()) callback() end) @@ -37,22 +35,11 @@ function BattleControllerStage:_stageGenerateNextMonster() local unitEntity = DataManager.BattleData:addMonster(config.monster[self.waveIndex], true) local modelId = unitEntity:getModelId() BattleHelper:loadBattleHeroModel(modelId, self.battleUI:getBattleNode(), function(spineObject) - if self.defMainUnit then - for k, v in ipairs(self.allUnits) do - if v == self.defMainUnit then - table.remove(self.allUnits, k) - break - end - end - self.defMainUnit:recycle() - end - + self.defTeam:removeAllUnits() local monsterComp = spineObject:addLuaComponent(GConst.BattleConst.TYPEOF_LUA_COMP.BATTLE_MONSTER_COMPONENT) monsterComp:initWithEntity(modelId, unitEntity, self) - self.defUnits[unitEntity:getMatchType()] = monsterComp - table.insert(self.allUnits, monsterComp) - self.defMainUnit = monsterComp - self.defMainUnit:playEnterBattlefield(function() + self.defTeam:addUnit(monsterComp, true) + monsterComp:playEnterBattlefield(function() self:enterNextWave() self:enterRefreshBoard() end) @@ -107,7 +94,7 @@ function BattleControllerStage:getNotInvolvedSkills() end function BattleControllerStage:findNextDefUnit() - self.defMainUnit:playDead(function() + self.defTeam:getMainUnit():playDead(function() self:_stageGenerateNextMonster() end) end diff --git a/lua/app/module/battle/team.meta b/lua/app/module/battle/team.meta new file mode 100644 index 00000000..387908f7 --- /dev/null +++ b/lua/app/module/battle/team.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 3401612032d992f43a88efbb82ff9ca7 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/lua/app/module/battle/team/battle_team.lua b/lua/app/module/battle/team/battle_team.lua new file mode 100644 index 00000000..5b9ebbe3 --- /dev/null +++ b/lua/app/module/battle/team/battle_team.lua @@ -0,0 +1,71 @@ +local BattleTeam = class("BattleTeam") + +function BattleTeam:init(side) + self.side = side + self.unitList = {} + self.unitMap = {} +end + +function BattleTeam:addUnit(unit, isMainUnit) + self.unitMap[unit:getMatchType()] = unit + table.insert(self.unitList, unit) + if isMainUnit then + self.mainUnit = unit + end +end + +function BattleTeam:getMainUnit() + return self.mainUnit +end + +function BattleTeam:removeAllUnits() + for k, v in pairs(self.unitMap) do + self.unitMap[k] = nil + end + local count = #self.unitList + for i = 1, count do + self.unitList[i]:recycle() + table.remove(self.unitList) + end + self.mainUnit = nil +end + +function BattleTeam:useNormalSkill(matchType, count, callback) + local unit = nil + if matchType == nil then + unit = self.unitList[1] + else + unit = self.unitMap[matchType] + end + if unit == nil then + return callback() + end + self.mainUnit = unit + unit:useNormalSkill(count, callback) +end + +function BattleTeam:useSkill(matchType, callback) + local unit = nil + if matchType == nil then + unit = self.unitList[1] + else + unit = self.unitMap[matchType] + end + if unit == nil then + return callback() + end + self.mainUnit = unit + unit:useSkill(1, callback) +end + +function BattleTeam:mainUnitUseAllSkills() + self.mainUnit:useAllSkills() +end + +function BattleTeam:tick(dt) + for k, v in ipairs(self.unitList) do + v:tick(dt) + end +end + +return BattleTeam \ No newline at end of file diff --git a/lua/app/module/battle/team/battle_team.lua.meta b/lua/app/module/battle/team/battle_team.lua.meta new file mode 100644 index 00000000..3c7fa1d7 --- /dev/null +++ b/lua/app/module/battle/team/battle_team.lua.meta @@ -0,0 +1,10 @@ +fileFormatVersion: 2 +guid: 39135bd7b51505c4d873a1f99917dc5d +ScriptedImporter: + internalIDToNameTable: [] + externalObjects: {} + serializedVersion: 2 + userData: + assetBundleName: + assetBundleVariant: + script: {fileID: 11500000, guid: 3b8b241bab4a4ac9a22fcce9c64f1242, type: 3}