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.allHeroCount, self.activeCount) end) self.heroList = {} local heroCfg = ConfigManager:getConfig("hero") for id, v in pairs(heroCfg) do table.insert(self.heroList, id) end end function HeroComp:refresh() self:refreshScrollRect() 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