c1_lua/lua/app/ui/hero/hero_comp.lua
2023-04-15 16:47:18 +08:00

167 lines
5.4 KiB
Lua

local HeroComp = class("HeroComp", LuaComponent)
local HERO_LIST_CELL = "app/ui/hero/cell/hero_list_cell"
local OUT_SCREEN_X = 10000
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, function(cell, heroId)
self:onClickHero(cell, heroId)
end)
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 = {}
self.largeHeroCell = CellManager:addCellComp(self.uiMap["hero_ui.scrollrect.viewport.content.large_hero_cell"], GConst.TYPEOF_LUA_CLASS.LARGE_HERO_CELL)
self.content = self.uiMap["hero_ui.scrollrect.viewport.content"]
self.largeHeroCell:getBaseObject():setAnchoredPositionX(OUT_SCREEN_X)
self.content:addClickListener(function()
self.largeHeroCell:getBaseObject():setAnchoredPositionX(OUT_SCREEN_X)
end)
for index, obj in ipairs(self.heroNodeList) do
obj:addClickListener(function()
local heroId = self.stageFormation[index]
if heroId then
local hero = DataManager.HeroData:getHeroById(heroId)
if hero then
ModuleManager.HeroManager:showHeroDetailUI(heroId)
end
end
end)
end
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:isActived() and heroB:isActived() 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:isActived() and not heroB:isActived() then
return true
elseif heroB:isActived() and not heroA:isActived() then
return false
else
return a > b
end
end)
end
function HeroComp:onClickHero(cell, heroId)
if not cell or not heroId then
self.largeHeroCell:getBaseObject():setAnchoredPositionX(OUT_SCREEN_X)
return
end
local entity = DataManager.HeroData:getHeroById(heroId)
if entity then
if entity:isActived() then
local targetPos = cell:getBaseObject():getTransform().position
local sPoint = UIManager:getUICameraComponent():WorldToScreenPoint(targetPos)
targetPos = CS.BF.Utils.RectTransformScreenPointToLocalPointInRectangle(self.content:getTransform(), sPoint.x, sPoint.y, UIManager:getUICameraComponent())
self.largeHeroCell:getBaseObject():setAnchoredPosition(targetPos.x, targetPos.y)
self.largeHeroCell:getBaseObject():getTransform():SetAsLastSibling()
self.largeHeroCell:refresh(entity, self.stageFormation[entity:getMatchType()] == heroId)
else
ModuleManager.HeroManager:showHeroDetailUI(heroId)
self.largeHeroCell:getBaseObject():setAnchoredPositionX(OUT_SCREEN_X)
end
else
self.largeHeroCell:getBaseObject():setAnchoredPositionX(OUT_SCREEN_X)
end
end
return HeroComp