c1_lua/lua/app/ui/battle/battle_ui.lua
2023-04-12 15:16:30 +08:00

267 lines
9.0 KiB
Lua

local BattleUI = class("BattleUI", BaseUI)
local GRID_CELL = "app/ui/battle/cell/grid_cell"
local GRID_CELL_PATH = "assets/prefabs/ui/battle/cell/grid_cell.prefab"
local DEFAULT_X = 10000
local SKILL_POS = {
[GConst.BattleConst.ELEMENT_TYPE.RED] = BF.Vector2(-183, 454),
[GConst.BattleConst.ELEMENT_TYPE.YELLOW] = BF.Vector2(-81.61, 454),
[GConst.BattleConst.ELEMENT_TYPE.GREEN] = BF.Vector2(17.1, 454),
[GConst.BattleConst.ELEMENT_TYPE.BLUE] = BF.Vector2(124.7, 454),
[GConst.BattleConst.ELEMENT_TYPE.PURPLE] = BF.Vector2(229.4, 454),
}
function BattleUI:getPrefabPath()
return "assets/prefabs/ui/battle/battle_ui.prefab"
end
function BattleUI:onLoadRootComplete()
self:_display()
self:_addListeners()
end
function BattleUI:_display()
local uiMap = self.root:genAllChildren()
self.boardMask2D = uiMap["battle_ui.bg_2.board_node"]:getComponent(GConst.TYPEOF_UNITY_CLASS.UI_RECT_MASK_2D)
self.boardMask = uiMap["battle_ui.bg_2.board_mask"]
self.boardMask:setVisible(false)
self:initSkill()
self:initBattlefield()
end
function BattleUI:_addListeners()
local uiMap = self.root:genAllChildren()
uiMap["battle_ui.close_btn"]:addClickListener(function()
local params = {
content = I18N:getGlobalText(I18N.GlobalConst.BATTLE_DESC_1),
boxType = GConst.MESSAGE_BOX_TYPE.MB_OK_CANCEL,
okText = I18N:getGlobalText(I18N.GlobalConst.BTN_TEXT_OK),
cancelText = I18N:getGlobalText(I18N.GlobalConst.BTN_TEXT_CANCEL),
okFunc = function()
ModuleManager.BattleManager:endBattleAndExit()
end,
}
GFunc.showMessageBox(params)
end)
end
function BattleUI:initSkill()
if self.skillObjs then
return
end
self.skillObjs = {}
local uiMap = self.root:genAllChildren()
for elementType, skillEntity in pairs(DataManager.BattleData:getSkillEntities()) do
if not self.skillObjs[elementType] then
local prefix = "battle_ui.bg_2.skill_node_" .. elementType
self.skillObjs[elementType] = {
icon = uiMap[prefix],
mask = uiMap[prefix .. ".mask"],
}
self.skillObjs[elementType].imgComp = self.skillObjs[elementType].mask:getComponent(GConst.TYPEOF_UNITY_CLASS.UI_IMAGE)
self.skillObjs[elementType].imgComp.fillAmount = 1
self.skillObjs[elementType].icon:setSprite(GConst.ATLAS_PATH.ICON_SKILL, skillEntity:getBattleIcon())
self.skillObjs[elementType].mask:setSprite(GConst.ATLAS_PATH.ICON_SKILL, skillEntity:getBattleIcon())
end
end
end
function BattleUI:initBattlefield()
local uiMap = self.root:genAllChildren()
self.battleNode = uiMap["battle_ui.battle_node"]
end
function BattleUI:getBattleNode()
return self.battleNode
end
function BattleUI:refreshSkill(elementMap)
if not self.skillObjs then
return
end
for elementType, skillEntity in pairs(DataManager.BattleData:getSkillEntities()) do
if not self.skillObjs[elementType] then
return
end
local add = 0
if elementMap and elementMap[elementType] then
add = elementMap[elementType]
end
local curEnergy = skillEntity:getEnergy() + add
local needEnergy = skillEntity:getNeedEnergy()
self.skillObjs[elementType].imgComp.fillAmount = (needEnergy - curEnergy) / needEnergy
end
end
function BattleUI:initGridCell()
if self.root.gridCells then
self.gridCells = self.root.gridCells
self.gridInitOver = true
end
if self.gridCells and self.gridInitOver then
self:onInitGridCellOver()
return
end
local uiMap = self.root:genAllChildren()
local parent = uiMap["battle_ui.bg_2.board_node"]
self.gridCells = {}
self.cellLoadRemianCount = GConst.BattleConst.ROW_COUNT * GConst.BattleConst.COLUMN_COUNT
for r = 1, GConst.BattleConst.ROW_COUNT do
for c = 1, GConst.BattleConst.COLUMN_COUNT do
CellManager:loadCellAsync(GRID_CELL_PATH, GRID_CELL, parent, function(cell)
local posId = ModuleManager.BattleManager:getPosId(r, c)
self.gridCells[posId] = cell
local gameObject = cell:getGameObject()
gameObject.name = "grid_cell_" .. posId
cell:getBaseObject():setAnchoredPositionX(DEFAULT_X) -- 初始化时放到屏幕外
self.cellLoadRemianCount = self.cellLoadRemianCount - 1
if self.cellLoadRemianCount <= 0 then
self.gridInitOver = true
self:onInitGridCellOver()
self.root.gridCells = self.gridCells
end
end)
end
end
end
function BattleUI:onInitGridCellOver()
for posId, cell in pairs(self.gridCells) do
local entity = DataManager.BattleData:getGridEntity(posId)
if entity then
cell:refresh(entity)
cell:addTouchListener(function(eventType)
if self.battleController then
self.battleController:onTouchEvent(eventType, entity:getPosId())
end
end)
cell:unBindAll()
cell:bind(entity, "isDirty", function()
cell:refresh(entity, self.curElementType)
end)
local pos = entity:getPos()
cell:getBaseObject():setAnchoredPosition(pos.x, pos.y)
entity:setCell(cell)
end
end
end
function BattleUI:setController(controller)
self.battleController = controller
end
function BattleUI:showBoardMask(elementType)
if not self.gridCells then
return
end
if self.curElementType == elementType then
return
end
self.curElementType = elementType
local entities = DataManager.BattleData:getGridEnties()
for posId, entity in pairs(entities) do
if entity and entity:getCell() then
entity:getCell():refresh(entity, self.curElementType)
end
end
end
function BattleUI:eliminationAni(sequence, callback)
if not sequence then
if callback then
callback()
end
return
end
self.boardMask:setVisible(true)
self.boardMask:getTransform():SetAsLastSibling()
if self.eliminationAniSeq then
self.eliminationAniSeq:Kill()
self.eliminationAniSeq = nil
end
self.eliminationAniSeq = self.root:createBindTweenSequence()
self.eliminationAniSeq:AppendCallback(function()
if self.boardMask2D then
self.boardMask2D.enabled = false
end
end)
if not self.posIdMap then
self.posIdMap = {}
end
for index, info in ipairs(sequence) do
if not self.posIdMap[info.posId] then
self.posIdMap[info.posId] = true
local entity = DataManager.BattleData:getGridEntity(info.posId)
if entity and entity:getCell() then
local baseObject = entity:getCell():getBaseObject()
baseObject:getTransform():SetAsLastSibling()
if entity:getSkillId() or info.noAni then
baseObject:setAnchoredPositionX(DEFAULT_X)
else
self.eliminationAniSeq:Insert(index * 0.01, baseObject:getTransform():DOScale(1.3, 0.1))
self.eliminationAniSeq:Insert(index * 0.01 + 0.2, baseObject:getTransform():DOAnchorPos(SKILL_POS[entity:getElementType()], 0.3))
self.eliminationAniSeq:Insert(index * 0.01 + 0.2, baseObject:getTransform():DOScale(1, 0.3))
end
end
end
end
self.eliminationAniSeq:AppendCallback(function()
self.posIdMap = {}
if self.boardMask2D then
self.boardMask2D.enabled = true
end
if callback then
callback()
end
self.boardMask:setVisible(false)
self:refreshSkill()
end)
end
function BattleUI:fallGrid(listInfo, callback)
self.fallAniCount = 0
for posId, info in pairs(listInfo) do
self.fallAniCount = self.fallAniCount + 1
local entity = DataManager.BattleData:getGridEntity(posId)
local cell = entity:getCell()
local posId = entity:getPosId()
if cell then
local gameObject = cell:getGameObject()
gameObject.name = "grid_cell_" .. posId
if cell.fallSeq then
cell.fallSeq:Kill()
cell.fallSeq = nil
end
local baseObject = cell:getBaseObject()
cell.fallSeq = baseObject:createBindTweenSequence()
baseObject:setAnchoredPosition(info[1].x, info[1].y)
local count = #info
cell.fallSeq:Append(baseObject:getTransform():DOLocalPath(info, GConst.BattleConst.ONE_STEP_TIME * count))
cell.fallSeq:AppendCallback(function()
self.fallAniCount = self.fallAniCount - 1
if self.fallAniCount == 0 then
if callback then
callback()
end
end
end)
end
end
end
return BattleUI