2023-04-26 19:43:12 +08:00

200 lines
7.0 KiB
Lua

local MainComp = class("MainComp", LuaComponent)
local CHAPTER_PATH = "assets/arts/textures/background/chapter/%s.png"
local BOX_ICON = {
"common_chest_1",
"common_chest_4"
}
function MainComp:init()
self.uiMap = self:getBaseObject():genAllChildren()
self:initChapter()
self:initStageFormation()
self:initGM()
end
function MainComp:initChapter()
self.chapterImg = self.uiMap["main_comp.chapter.img"]
self.chapterNameTx = self.uiMap["main_comp.chapter.name_tx"]
self.chapterWavetx = self.uiMap["main_comp.chapter.record_tx"]
self.leftArrow = self.uiMap["main_comp.chapter.left_arrow"]
local leftArrowBtn = self.uiMap["main_comp.chapter.left_arrow.btn"]
leftArrowBtn:addClickListener(function()
if DataManager.ChapterData:goLastChapter() then
self:refresh()
end
end)
self.rightArrow = self.uiMap["main_comp.chapter.right_arrow"]
local rightArrowBtn = self.uiMap["main_comp.chapter.right_arrow.btn"]
rightArrowBtn:addClickListener(function()
if DataManager.ChapterData:goNextChapter() then
self:refresh()
end
end)
local fightBtn = self.uiMap["main_comp.fight_btn"]
fightBtn:addClickListener(function()
self:onFightBtnClick()
end)
self.uiMap["main_comp.chapter.effect_node.ui_spine_obj"]:playAnim("idle", true, false)
end
function MainComp:initStageFormation()
self.heroCells = {
self.uiMap["main_comp.hero_bg.hero_cell_1"]:addLuaComponent(GConst.TYPEOF_LUA_CLASS.HERO_CELL),
self.uiMap["main_comp.hero_bg.hero_cell_2"]:addLuaComponent(GConst.TYPEOF_LUA_CLASS.HERO_CELL),
self.uiMap["main_comp.hero_bg.hero_cell_3"]:addLuaComponent(GConst.TYPEOF_LUA_CLASS.HERO_CELL),
self.uiMap["main_comp.hero_bg.hero_cell_4"]:addLuaComponent(GConst.TYPEOF_LUA_CLASS.HERO_CELL),
self.uiMap["main_comp.hero_bg.hero_cell_5"]:addLuaComponent(GConst.TYPEOF_LUA_CLASS.HERO_CELL),
}
end
function MainComp:initGM()
self.btnGM = self.uiMap["main_comp.gm_btn"]
self.btnGM:setVisible(Platform:getIsDevChannel(), 2)
self.btnGM:addClickListener(function()
ModuleManager.DevToolManager:showOrHideDevListUI()
end)
end
function MainComp:refresh()
self:refreshChapter()
self:refreshStageFormaion()
end
function MainComp:onFightBtnClick()
ModuleManager.ChapterManager:startFight()
end
function MainComp:refreshChapter(force)
local chapterId = DataManager.ChapterData:getChapterId()
if self.currChapterId ~= chapterId or force then
if DataManager.ChapterData:getIsFirstChapter(chapterId) then -- 第一章不需要左箭头
self.leftArrow:setVisible(false)
self.rightArrow:setVisible(DataManager.ChapterData:getMaxChapterId() >= 1)
elseif chapterId == DataManager.ChapterData:getMaxChapterId() + 1 then -- 只能看打的最远的关卡
self.leftArrow:setVisible(true)
self.rightArrow:setVisible(false)
else
self.leftArrow:setVisible(true)
self.rightArrow:setVisible(not DataManager.ChapterData:isFinalChapter(chapterId))
end
self.currChapterId = chapterId
local chapterInfo = ConfigManager:getConfig("chapter")[chapterId]
local chapterI18NInfo = I18N:getConfig("chapter")[chapterId]
if chapterInfo then
self.chapterImg:setTexture(string.format(CHAPTER_PATH, chapterInfo.icon))
end
if chapterI18NInfo then
self.chapterNameTx:setText(chapterI18NInfo.name)
end
self.chapterWavetx:setText(I18N:getGlobalText(I18N.GlobalConst.CHAPTER_DESC_1, DataManager.ChapterData:getChapterMaxWave()))
local slider = self.uiMap["main_comp.chapter.progress_bg.slider"]
if not self.boxObjs then
self.boxObjs = {}
for i = 1, 3 do
self.boxObjs[i] = {
box = self.uiMap["main_comp.chapter.progress_bg.box_" .. i],
desc = self.uiMap["main_comp.chapter.progress_bg.box_desc_" .. i],
spineObj = self.uiMap["main_comp.chapter.progress_bg.spine_node.ui_spine_obj_" .. i],
boxIcon = self.uiMap["main_comp.chapter.progress_bg.box_" .. i .. ".box_icon"]
}
end
end
local rewardChapterId = DataManager.ChapterData:getIsHaveRewardsMinId()
local curMaxWave = DataManager.ChapterData:getChapterMaxWave(rewardChapterId)
local boxCount = DataManager.ChapterData:getChapterBoxCount(rewardChapterId)
local unitValue = 1 / boxCount
local sliderValue = 0
local lastValue = 0
for i = 1, boxCount do
if curMaxWave < lastValue then
break
end
local maxWave = DataManager.ChapterData:getChapterBoxNum(rewardChapterId, i)
local wave = math.min(curMaxWave, maxWave)
sliderValue = sliderValue + unitValue * (wave - lastValue) / (maxWave - lastValue)
lastValue = maxWave
end
slider:getComponent(GConst.TYPEOF_UNITY_CLASS.BF_SLIDER).value = sliderValue
for index, objs in ipairs(self.boxObjs) do
local show = boxCount >= index
objs.box:setActive(show)
objs.desc:setActive(show)
objs.spineObj:setVisible(false)
if show then
local needWave = DataManager.ChapterData:getChapterBoxNum(rewardChapterId, index)
local x = 370 * (index / boxCount)
local rewards = DataManager.ChapterData:getChapterBoxRewards(rewardChapterId, index)
local num = DataManager.ChapterData:getChapterBoxNum(rewardChapterId, index)
local rewardGot = DataManager.ChapterData:getChapterBoxRewardGot(rewardChapterId, index)
local icon = BOX_ICON[1]
if rewardGot then
icon = BOX_ICON[2]
end
objs.box:addClickListener(function()
if needWave <= curMaxWave and not rewardGot then
objs.spineObj:setVisible(true)
objs.spineObj:playAnimComplete("open", true, false, function()
objs.spineObj:setVisible(false)
ModuleManager.ChapterManager:openBox(rewardChapterId, index)
end)
else
ModuleManager.TipsManager:showRewardsTips(rewards, nil, objs.box)
end
end)
objs.boxIcon:setSprite(GConst.ATLAS_PATH.COMMON, icon)
objs.box:setAnchoredPositionX(x)
if needWave <= curMaxWave and not rewardGot then
objs.spineObj:setVisible(true)
objs.spineObj:playAnim("idle", true, false)
objs.spineObj:setAnchoredPositionX(x)
objs.boxIcon:setVisible(false)
else
objs.spineObj:setVisible(false)
objs.boxIcon:setVisible(true)
end
objs.desc:setAnchoredPositionX(x)
if boxCount == index then
objs.desc:setText(I18N:getGlobalText(I18N.GlobalConst.MAIN_DESC_1, rewardChapterId))
else
objs.desc:setText(num)
end
end
end
self.uiMap["main_comp.fight_btn.desc"]:setText(I18N:getGlobalText(I18N.GlobalConst.START_DESC))
local cost = DataManager.ChapterData:getFightCost()
if cost then
self.uiMap["main_comp.fight_btn.desc_2"]:setText(GFunc.getRewardNum(cost))
else
self.uiMap["main_comp.fight_btn.desc_2"]:setText("0")
end
end
end
function MainComp:refreshStageFormaion()
local formation = DataManager.FormationData:getStageFormation()
for i, heroCell in ipairs(self.heroCells) do
if formation[i] then
local heroEntity = DataManager.HeroData:getHeroById(formation[i])
if heroEntity then
heroCell:setVisible(true, 0.84)
heroCell:refresh(heroEntity)
heroCell:addClickListener(function()
ModuleManager.HeroManager:showHeroDetailUI(heroEntity:getCfgId())
end)
else
heroCell:setVisible(false)
end
else
heroCell:setVisible(false)
end
end
end
return MainComp