sk-client/Assets/ManagedResources/~Lua/Modules/EnergyBase/EnergyBaseMainPanel.lua

418 lines
17 KiB
Lua

require("Base/BasePanel")
EnergyBaseMainPanel = Inherit(BasePanel)
local this = EnergyBaseMainPanel
local BaseAttachmentConfig = ConfigManager.GetConfig(ConfigName.BaseAttachmentConfig)
local PropertyConfig = ConfigManager.GetConfig(ConfigName.PropertyConfig)
local BaseResourceConfig = ConfigManager.GetConfig(ConfigName.BaseResourceConfig)
local SpecialConfig = ConfigManager.GetConfig(ConfigName.SpecialConfig)
local ItemConfig = ConfigManager.GetConfig(ConfigName.ItemConfig)
--滑动相关
local trigger = nil
local moveY = 0
local moveSpeed = 50
local moveMax = 0
local moveMin = 0
local builds = {}--设施
local items = {}--资源
local buffList = {}--buff
local timer = nil
local timeList = {}
--显示
local showType = {
My = 1,
Other = 2,
}
local curType, curPlayerId, curPlayerName
function EnergyBaseMainPanel:InitComponent()
this.btnBack = Util.GetGameObject(this.gameObject, "btnBack")
this.dragBg = Util.GetGameObject(this.gameObject, "dragBg")
this.bg = Util.GetGameObject(this.gameObject, "bg")
this.top = Util.GetGameObject(this.gameObject, "Top")
this.btnAdd = Util.GetGameObject(this.gameObject, "Top/btnAdd")
this.btnQuery = Util.GetGameObject(this.gameObject, "Top/btnQuery")
this.btnQueryRp = Util.GetGameObject(this.gameObject, "Top/btnQuery/redpoint")
this.btnUpLv = Util.GetGameObject(this.gameObject, "Top/btnUpLv")
this.btnUpLvRp = Util.GetGameObject(this.gameObject, "Top/btnUpLv/redpoint")
this.btnGift = Util.GetGameObject(this.gameObject, "Top/btnGift")
this.btnLog = Util.GetGameObject(this.gameObject, "Top/btnLog")
this.btnHelp = Util.GetGameObject(this.gameObject, "Top/btnHelp")
this.helpPosition = this.btnHelp:GetComponent("RectTransform").localPosition
this.baseLevel = Util.GetGameObject(this.gameObject, "Top/baseLevel"):GetComponent("Text")
this.fatigue = Util.GetGameObject(this.gameObject, "Top/fatigue"):GetComponent("Text")
this.coolieNum = Util.GetGameObject(this.gameObject, "Top/coolieNum"):GetComponent("Text")
this.buffGrid = Util.GetGameObject(this.gameObject, "Top/buff")
this.buff = Util.GetGameObject(this.gameObject, "Top/buff/pre")
this.btnPlunder = Util.GetGameObject(this.gameObject, "tab/btnPlunder")
this.btnRefresh = Util.GetGameObject(this.gameObject, "tab/btnRefresh")
this.builds = Util.GetGameObject(this.gameObject, "bg/builds")
for i = 1, 4 do
local build = Util.GetGameObject(this.builds, "build"..i)
builds[i] = {
name = Util.GetGameObject(build, "name"):GetComponent("Text"),
redpoint = Util.GetGameObject(build, "redpoint"),
btnUpLv = Util.GetGameObject(build, "btnUpLv"),
}
end
for i = 1, 6 do
local item = Util.GetGameObject(this.gameObject, "bg/items/item"..i)
items[i] = {
left = Util.GetGameObject(item, "left"),
right = Util.GetGameObject(item, "right"),
resources = Util.GetGameObject(item, "resources")
}
end
this.baseName = Util.GetGameObject(this.gameObject, "name/Text"):GetComponent("Text")
this.OnInitTrigger()
-- local posY = this.bg:GetComponent("RectTransform").localPosition.y
local height = this.bg:GetComponent("RectTransform").rect.height
--2700为背景高度
if height < 2700 then
moveMax = math.abs(height - 2700)
-- moveMax = math.abs(UIManager.realHeigt - 2700)/2+posY
else
moveMax = 0
end
end
function EnergyBaseMainPanel:BindEvent()
Util.AddClick(this.btnBack, function()
if curType == showType.My then
self:ClosePanel()
elseif curType == showType.Other then
UIManager.OpenPanel(UIName.EnergyBaseMainPanel, showType.My)
end
end)
Util.AddClick(this.btnUpLv, function()
local args = EnergyBaseManager.GetBaseInfo()
if args.baseLevel >= EnergyBaseManager.baseMaxLv then
return
end
UIManager.OpenPanel(UIName.EnergyBaseUpLevel)
end)
Util.AddClick(this.btnGift, function()
UIManager.OpenPanel(UIName.EnergyGiftBag)
end)
Util.AddClick(this.btnHelp, function()
UIManager.OpenPanel(UIName.HelpPopup, HELP_TYPE.EnergyBase, this.helpPosition.x-50, this.helpPosition.y+900)
end)
Util.AddClick(this.btnLog, function()
UIManager.OpenPanel(UIName.EnergyBaseLog)
end)
Util.AddClick(this.btnAdd, function()
UIManager.OpenPanel(UIName.RobotDetails)
end)
Util.AddClick(this.btnQuery, function()
UIManager.OpenPanel(UIName.RobotDetails)
end)
Util.AddClick(this.btnPlunder, function()
UIManager.OpenPanel(UIName.EnergyBaseLoot)
end)
Util.AddClick(this.btnRefresh, function()
UIManager.OpenPanel(UIName.GeneralPopup, GENERAL_POPUP_TYPE.EnergyBaseRefresh, function ()
EnergyBaseManager.RefreshResourceDetails(function ()
-- this.EventRefreshResource()
end)
end)
end)
end
function EnergyBaseMainPanel:AddListener()
Game.GlobalEvent:AddEvent(GameEvent.Energy.RefreshResource, this.EventRefreshResource)
Game.GlobalEvent:AddEvent(GameEvent.Energy.RefreshBaseInfo, this.EventRefreshBaseInfo)
Game.GlobalEvent:AddEvent(GameEvent.Energy.RefreshResourceForOtherBase, this.EventRefreshResourceForOtherBase)
end
function EnergyBaseMainPanel:RemoveListener()
Game.GlobalEvent:RemoveEvent(GameEvent.Energy.RefreshResource, this.EventRefreshResource)
Game.GlobalEvent:RemoveEvent(GameEvent.Energy.RefreshBaseInfo, this.EventRefreshBaseInfo)
Game.GlobalEvent:RemoveEvent(GameEvent.Energy.RefreshResourceForOtherBase, this.EventRefreshResourceForOtherBase)
end
function EnergyBaseMainPanel:OnSortingOrderChange()
-- local sortingOrder = self.sortingOrder
-- for i = 1, #items do
-- local effect = Util.GetGameObject(items[i].resources, "GameObject/RobotEffect"):GetComponent("SortingGroup")
-- effect.sortingOrder = sortingOrder + 10
-- end
-- Util.GetGameObject(this.gameObject, "tab"):GetComponent("Canvas").sortingOrder = sortingOrder + 11
-- this.btnPlunder:GetComponent("Canvas").sortingOrder = sortingOrder + 12
-- this.btnRefresh:GetComponent("Canvas").sortingOrder = sortingOrder + 12
-- this.btnBack:GetComponent("Canvas").sortingOrder = sortingOrder + 12
end
function EnergyBaseMainPanel:OnOpen(type, args)
curType = type and type or showType.My
if not args then
args = {id = PlayerManager.uid, name = PlayerManager.nickName}
end
curPlayerId = args.id
curPlayerName = args.name
EnergyBaseManager.curPlayerId = curPlayerId
moveY = 0
if moveMax > 0 then
if curType == showType.Other then
moveY = moveMax
end
end
this.bg.gameObject:GetComponent("RectTransform").anchoredPosition = Vector3.New(0, moveY, 0)
end
function EnergyBaseMainPanel:OnShow()
this.top:SetActive(curType == showType.My)
this.builds:SetActive(curType == showType.My)
this.btnRefresh:SetActive(curType == showType.My)
this.baseName.text = curPlayerName..GetLanguageStrById(50464)
timeList = {}
if curType == showType.My then
EnergyBaseManager.SetBaseEnergyInfoData(function ()
this.SetBuff()
this.SetBaseInfo()
this.SetBaseResourceDetails()
this.TimeDown()
end)
elseif curType == showType.Other then
EnergyBaseManager.AccessOtherBase(curPlayerId, function (msg)
this.SetBaseResourceDetails(msg)
this.TimeDown()
end)
end
end
function EnergyBaseMainPanel:OnClose()
if timer then
timer:Stop()
timer = nil
end
end
function EnergyBaseMainPanel:OnDestroy()
moveY = 0
trigger.onBeginDrag = trigger.onBeginDrag - this.OnBeginDrag
trigger.onDrag = trigger.onDrag - this.OnDrag
trigger.onEndDrag = trigger.onEndDrag - this.OnEndDrag
trigger = nil
builds = {}
items = {}
buffList = {}
timeList = {}
if timer then
timer:Stop()
timer = nil
end
end
function this.OnDrag(p,d)
if d.delta.y > 0 then--向上划
if moveY >= moveMax then
return
end
moveY = moveY + moveSpeed
this.bg.gameObject:GetComponent("RectTransform").anchoredPosition = Vector3.New(0, moveY, 0)
elseif d.delta.y < 0 then--向下划
if moveY <= moveMin then
this.bg.gameObject:GetComponent("RectTransform").anchoredPosition = Vector3.New(0, 0, 0)
return
end
moveY = moveY - moveSpeed
this.bg.gameObject:GetComponent("RectTransform").anchoredPosition = Vector3.New(0, moveY, 0)
end
end
function this.OnInitTrigger()
if not trigger then
trigger = Util.GetEventTriggerListener(this.dragBg)
trigger.onBeginDrag = trigger.onBeginDrag + this.OnBeginDrag
trigger.onDrag = trigger.onDrag + this.OnDrag
trigger.onEndDrag = trigger.onEndDrag + this.OnEndDrag
end
end
function this.SetBuff()
for i = 1, #buffList do
buffList[i].gameObject:SetActive(false)
end
local infos = EnergyBaseManager.GetFacilityInfos()
for i = 1, 4 do
local config = EnergyBaseManager.GetBuildInfo(infos[i].type, infos[i].level)
if not buffList[i] then
buffList[i] = newObjToParent(this.buff, this.buffGrid)
end
Util.GetGameObject(buffList[i], "icon"):GetComponent("Image").sprite = Util.LoadSprite(PropertyConfig[config.PropertyAdd[1][1]].Icon)
Util.GetGameObject(buffList[i], "name"):GetComponent("Text").text = GetLanguageStrById(PropertyConfig[config.PropertyAdd[1][1]].Info)
Util.GetGameObject(buffList[i], "value"):GetComponent("Text").text = "+"..GetPropertyFormatStr(PropertyConfig[config.PropertyAdd[1][1]].Style, config.PropertyAdd[1][2])
buffList[i].gameObject:SetActive(true)
end
end
function this.SetBaseInfo()
local args = EnergyBaseManager.GetBaseInfo()
this.btnUpLv:SetActive(not (args.baseLevel >= EnergyBaseManager.baseMaxLv))
this.btnUpLvRp:SetActive(EnergyBaseManager.BaseUpLvRedpoint())
this.btnQueryRp:SetActive(EnergyBaseManager.RobotBuyRedpoint())
this.baseLevel.text = args.baseLevel
this.coolieNum.text = args.freeCoolieCount.."/"..args.coolieCount
this.fatigue.text = args.totalSnatchCount
local infos = EnergyBaseManager.GetFacilityInfos()
for i = 1, #builds do
local config, next = EnergyBaseManager.GetBuildInfo(infos[i].type, infos[i].level)
builds[i].name.text = GetLanguageStrById(config.Name).." ".."Lv"..infos[i].level
builds[i].btnUpLv:SetActive(not not next)
builds[i].redpoint:SetActive(EnergyBaseManager.BuildUpLvRedpointForType(infos[i].type))
Util.AddClick(builds[i].btnUpLv, function ()
local cur, next = EnergyBaseManager.GetBuildInfo(infos[i].type, infos[i].level)
if next then
UIManager.OpenPanel(UIName.SheShiUpLevel, infos[i].type)
end
end)
end
end
--[[
资源方 资源 争夺方
进度: 1 0 -1
|————————————————————|————————————————————|
]]
local minPosX = 160
local maxPosX = 500
function this.SetRobotMove(data)
local p = EnergyBaseManager.ComputingProgress(data.resource)
local posX = 0
local distance = (maxPosX - minPosX) * math.abs(p)
local posY = data.item.left:GetComponent("RectTransform").anchoredPosition.y
local rposY = data.item.resources:GetComponent("RectTransform").anchoredPosition.y
if p < 0 then posX = 0 + distance end
if p > 0 then posX = 0 - distance end
-- LogError("进度:"..p.." 资源:"..posX)
if posX < -maxPosX+160 then posX = -maxPosX+160 end
if posX > maxPosX-160 then posX = maxPosX-160 end
data.item.resources:GetComponent("RectTransform").anchoredPosition = Vector2.New(posX, rposY)
data.item.left:GetComponent("RectTransform").anchoredPosition = Vector2.New(posX-160, posY)
data.item.right:GetComponent("RectTransform").anchoredPosition = Vector2.New(posX+160, posY)
end
function this.SetBaseResourceDetails(msg)
for i = 1, #items do
local data = EnergyBaseManager.ResourceDetailsForId(curPlayerId, msg, i)
local time = Util.GetGameObject(items[i].resources, "time")
local timeTxt = Util.GetGameObject(items[i].resources, "time/Text"):GetComponent("Text")
local icon = Util.GetGameObject(items[i].resources, "icon"):GetComponent("Image")
local lv = Util.GetGameObject(items[i].resources, "lv")
local lvTxt = Util.GetGameObject(items[i].resources, "lv/Text"):GetComponent("Text")
if data and data.resourceId > 0 then
local resourceData = BaseResourceConfig[data.resourceId]
icon.sprite = Util.LoadSprite(GetResourcePath(ItemConfig[resourceData.Reward[1]].ResourceID))
lvTxt.text = "Lv.<size=30>"..resourceData.Level.."</size>"
local finalTime = EnergyBaseManager.ComputingTime(data)
if data.collieCount > 0 or data.snatchCollieCount > 0 then
timeTxt.text = TimeToHMS(finalTime-GetTimeStamp())
if not timeList[i] then
local isJoin = (data.snatchId == PlayerManager.uid) or (curPlayerId == PlayerManager.uid)
local list = {pos = i, resource = data, txt = timeTxt, time = finalTime, item = items[i], state = true, isJoin = isJoin}
timeList[i] = list
this.SetRobotMove(list)
end
else
local posX = 0
local posY = items[i].resources:GetComponent("RectTransform").anchoredPosition.y
local distance = (maxPosX - minPosX) * math.abs(data.progress)
if data.progress < 0 then posX = 0 + distance end
if data.progress > 0 then posX = 0 - distance end
items[i].resources:GetComponent("RectTransform").anchoredPosition = Vector2.New(posX, posY)
end
Util.AddOnceClick(items[i].resources, function ()
if curPlayerId == PlayerManager.uid or data.snatchId == PlayerManager.uid or data.snatchId == 0 then
if curPlayerId ~= PlayerManager.uid and data.snatchCollieCount == 0 and EnergyBaseManager.GetBaseInfo().freeCoolieCount <= 0 then
PopupTipPanel.ShowTip(GetLanguageStrById(50475))
return
end
UIManager.OpenPanel(UIName.EnergyDetails, {pos = i, resource = data, playerId = curPlayerId})
end
end)
Util.GetGameObject(items[i].left, "num"):GetComponent("Text").text = data.collieCount
Util.GetGameObject(items[i].right, "num"):GetComponent("Text").text = data.snatchCollieCount
Util.GetGameObject(items[i].left, "Image"):SetActive(data.collieCount >= data.snatchCollieCount)
Util.GetGameObject(items[i].right, "Image"):SetActive(data.collieCount < data.snatchCollieCount)
Util.GetGameObject(items[i].right, "name"):GetComponent("Text").text = data.snatchName
end
lv:SetActive(not not data and data.resourceId > 0)
time:SetActive(not not data and (data.collieCount > 0 or data.snatchCollieCount > 0))
icon.gameObject:SetActive(not not data and data.resourceId > 0)
items[i].left:SetActive(not not data and data.collieCount > 0)
items[i].right:SetActive(not not data and data.snatchCollieCount > 0)
items[i].resources:SetActive(not not data and data.resourceId > 0)
end
end
function this.TimeDown()
if timer then
timer:Stop()
timer = nil
end
timer = Timer.New(function()
for index, value in pairs(timeList) do
if value then
value.txt.text = TimeToHMS(value.time-GetTimeStamp())
this.SetRobotMove(value)
end
if value.time-GetTimeStamp() <= 0 then
value.state = false
if not value.isJoin then
value.item.resources:SetActive(false)
value.item.left:SetActive(false)
value.item.right:SetActive(false)
end
end
local isOn = false
for k, v in pairs(timeList) do
if v.state then
isOn = true
end
end
if not isOn then
if timer then
timer:Stop()
timer = nil
end
end
end
end, 1, -1, true)
timer:Start()
end
function this.EventRefreshBaseInfo()
-- LogError("EventRefreshBaseInfo")
this.SetBuff()
this.SetBaseInfo()
end
function this.EventRefreshResource()
-- LogError("EventRefreshResource")
timeList = {}
this.SetBaseResourceDetails()
this.SetBaseInfo()
this.TimeDown()
end
function this.EventRefreshResourceForOtherBase(msg)
-- LogError("EventRefreshResourceForOtherBase")
timeList = {}
this.SetBaseResourceDetails(msg)
this.TimeDown()
end
return EnergyBaseMainPanel