c1_lua/lua/app/ui/hero/hero_comp.lua
2023-04-11 22:45:17 +08:00

122 lines
3.5 KiB
Lua

local HeroComp = class("HeroComp", LuaComponent)
local HERO_LIST_CELL = "app/ui/hero/cell/hero_list_cell"
function HeroComp:init()
self.uiMap = self:getBaseObject():genAllChildren()
self.scrollRect = self.uiMap["hero_ui.scrollrect"]:addLuaComponent(GConst.TYPEOF_LUA_CLASS.SCROLL_RECT_BASE)
self.scrollRect:addInitCallback(function()
return HERO_LIST_CELL
end)
self.scrollRect:addRefreshCallback(function(index, cell)
cell:refresh(index, self.heroList, self.stageFormation, self.allHeroCount, self.activeCount)
end)
self.heroList = {}
local heroCfg = ConfigManager:getConfig("hero")
for id, v in pairs(heroCfg) do
table.insert(self.heroList, id)
end
self.heroNodeList = {
self.uiMap["hero_ui.formation.hero_1"],
self.uiMap["hero_ui.formation.hero_2"],
self.uiMap["hero_ui.formation.hero_3"],
self.uiMap["hero_ui.formation.hero_4"],
self.uiMap["hero_ui.formation.hero_5"],
}
self.heroAddImgList = {
self.uiMap["hero_ui.formation.hero_1.add"],
self.uiMap["hero_ui.formation.hero_2.add"],
self.uiMap["hero_ui.formation.hero_3.add"],
self.uiMap["hero_ui.formation.hero_4.add"],
self.uiMap["hero_ui.formation.hero_5.add"],
}
self.heroSpineList = {}
end
function HeroComp:refresh()
self.stageFormation = DataManager.FormationData:getStageFormation()
self:refreshStageFormation()
self:refreshScrollRect()
end
function HeroComp:refreshStageFormation()
for i = 1, 5 do
local heroId = self.stageFormation[i]
local hero = DataManager.HeroData:getHeroById(heroId)
if hero and hero:getLv() > 0 then
self.heroAddImgList[i]:setVisible(false)
if self.heroSpineList[i] and self.heroSpineList[i]:getModelId() == hero:getModelId() then
self.heroSpineList[i]:setActive(true)
self.heroSpineList[i]:playAnimation("idle", true)
else
SpineManager:loadHeroAsync(hero:getModelId(), self.heroNodeList[i], function(spineObject)
if heroId ~= self.stageFormation[i] then
return
end
if self.heroSpineList[i] then
self.heroSpineList[i]:destroy()
end
spineObject:playAnimation("idle", true)
self.heroSpineList[i] = spineObject
self.heroSpineList[i]:setLocalPosition(0, -52, 0)
end)
end
else
self.heroAddImgList[i]:setVisible(true)
if self.heroSpineList[i] then
self.heroSpineList[i]:setActive(false)
end
end
end
end
function HeroComp:refreshScrollRect()
self:sortHeroList()
self.allHeroCount = #self.heroList
self.activeCount = DataManager.HeroData:getActiveHeroCount()
local lockCount = self.allHeroCount - self.activeCount
local cellCount = 0
if self.activeCount > 0 then
cellCount = cellCount + math.ceil(self.activeCount / 4)
end
if lockCount > 0 then
cellCount = cellCount + math.ceil(lockCount / 4)
end
local currCount = self.scrollRect:getTotalCount()
if cellCount == currCount then
self.scrollRect:updateAllCell()
else
self.scrollRect:clearCells()
self.scrollRect:refillCells(cellCount)
end
end
-- 等级>品质>id
function HeroComp:sortHeroList()
local heroA
local heroB
local HeroData = DataManager.HeroData
table.sort(self.heroList, function(a, b)
heroA = HeroData:getHeroById(a)
heroB = HeroData:getHeroById(b)
if heroA and heroB then
if heroA:getLv() == heroB:getLv() then
if heroA:getQlt() == heroB:getQlt() then
return a > b
else
return heroA:getQlt() > heroB:getQlt()
end
else
return heroA:getLv() > heroB:getLv()
end
elseif heroA and heroB == nil then
return true
elseif heroB and heroA == nil then
return false
else
return a > b
end
end)
end
return HeroComp