2023-04-13 21:13:43 +08:00

137 lines
4.4 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_2"
}
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.leftArrow = self.uiMap["main_comp.chapter.left_arrow"]
self.leftArrow:addClickListener(function()
if DataManager.ChapterData:goLastChapter() then
self:refresh()
end
end)
self.rightArrow = self.uiMap["main_comp.chapter.right_arrow"]
self.rightArrow: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)
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(not Platform:getIsPublishChannel(), 2)
self.btnGM:addClickListener(function()
ModuleManager.DevToolManager:showOrHideDevListUI()
end)
end
function MainComp:refresh()
self:refreshChapter()
self:refreshStageFormaion()
end
function MainComp:onFightBtnClick()
ModuleManager.BattleManager:playBattle(GConst.BattleConst.BATTLE_TYPE.STAGE)
end
function MainComp:refreshChapter(force)
local chapterId = DataManager.ChapterData:getChapterId()
if self.currChapterId ~= chapterId or force then
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
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]
}
end
end
local boxCount = DataManager.ChapterData:getChapterBoxCount()
slider:getComponent(GConst.TYPEOF_UNITY_CLASS.BF_SLIDER).value = DataManager.ChapterData:getChapterMaxWave() / boxCount
for index, objs in ipairs(self.boxObjs) do
local show = boxCount >= index
objs.box:setActive(show)
objs.desc:setActive(show)
if show then
local x = 370 * (index / boxCount)
local rewards = DataManager.ChapterData:getChapterBoxByIndex(nil, index)
local num = DataManager.ChapterData:getChapterBoxNum(nil, index)
local rewardGot = DataManager.ChapterData:getChapterBoxRewardGot(nil, index)
local icon = BOX_ICON[1]
if rewardGot then
icon = BOX_ICON[2]
end
objs.box:addClickListener(function()
ModuleManager.TipsManager:showRewardsTips(rewards, nil, objs.box)
end)
objs.box:setSprite(GConst.ATLAS_PATH.COMMON, icon)
objs.box:setAnchoredPositionX(x)
objs.desc:setAnchoredPositionX(x)
objs.desc:setText(num)
end
end
self.uiMap["main_comp.fight_btn.desc"]:setText(I18N:getGlobalText(I18N.GlobalConst.START_DESC))
self.uiMap["main_comp.fight_btn.desc_2"]:setText(DataManager.ChapterData:getFightCost())
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: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