Files
Endfield-Data/LuaScripts/UI/Panels/CommonTaskTrackCountdown/CommonTaskTrackCountdownCtrl.lua
2025-12-02 20:37:18 +07:00

275 lines
7.7 KiB
Lua

local uiCtrl = require_ex('UI/Panels/Base/UICtrl')
local PANEL_ID = PanelId.CommonTaskTrackCountdown
local Component = {
Countdown = 1,
Counting = 2,
}
CommonTaskTrackCountdownCtrl = HL.Class('CommonTaskTrackCountdownCtrl', uiCtrl.UICtrl)
CommonTaskTrackCountdownCtrl.m_countDownTickId = HL.Field(HL.Number) << -1
CommonTaskTrackCountdownCtrl.m_isCountdownPause = HL.Field(HL.Boolean) << false
CommonTaskTrackCountdownCtrl.m_countingTickId = HL.Field(HL.Number) << -1
CommonTaskTrackCountdownCtrl.m_isCountingPause = HL.Field(HL.Boolean) << false
CommonTaskTrackCountdownCtrl.m_originalAnchoredPos = HL.Field(Vector2)
CommonTaskTrackCountdownCtrl.s_messages = HL.StaticField(HL.Table) << {
[MessageConst.ON_COMMON_TASK_COUNTDOWN_SWITCH_ON] = "OnCommonTaskCountdownSwitchOn",
[MessageConst.ON_CLOSE_COMMON_TASK_COUNTDOWN] = "OnCloseCommonTaskCountdown",
[MessageConst.ON_COMMON_TASK_COUNTING_SWITCH_ON] = "OnCommonTaskCountingSwitchOn",
[MessageConst.ON_FINISH_COMMON_TASK_COUNTING] = "OnFinishCommonTaskCounting",
}
CommonTaskTrackCountdownCtrl.OnShowCommonTaskCountdown = HL.StaticMethod(HL.Any) << function(args)
local ctrl = CommonTaskTrackCountdownCtrl.AutoOpen(PANEL_ID, args, true)
ctrl:ShowCountdown(args)
end
CommonTaskTrackCountdownCtrl.OnStartCommonTaskCounting = HL.StaticMethod() << function(args)
local ctrl = CommonTaskTrackCountdownCtrl.AutoOpen(PANEL_ID, args, true)
ctrl:StartCounting(args)
end
CommonTaskTrackCountdownCtrl.OnCreate = HL.Override(HL.Any) << function(self, arg)
self.m_originalAnchoredPos = self.view.main.anchoredPosition
end
CommonTaskTrackCountdownCtrl.OnClose = HL.Override() << function(self)
if self.m_countDownTickId > 0 then
self.m_countDownTickId = LuaUpdate:Remove(self.m_countDownTickId)
end
if self.m_countingTickId > 0 then
self.m_countingTickId = LuaUpdate:Remove(self.m_countingTickId)
end
end
CommonTaskTrackCountdownCtrl._ToggleComponentOn = HL.Method(HL.Number) << function(self, component)
self.view.countdown.gameObject:SetActiveIfNecessary(component == Component.Countdown)
self.view.counting.gameObject:SetActiveIfNecessary(component == Component.Counting)
end
CommonTaskTrackCountdownCtrl._IsWorldFreeze = HL.Method().Return(HL.Boolean) << function(self)
local isOpen, ctrl = UIManager:IsOpen(PanelId.CommonPopUp)
return UIWorldFreezeManager:IsUIWorldFreeze() or isOpen and ctrl.m_timeScaleHandler > 0
end
CommonTaskTrackCountdownCtrl.ShowCountdown = HL.Method(HL.Any) << function(self, arg)
self:_ToggleComponentOn(Component.Countdown)
local countdownDurationMilli, expireTimestampMilli, cb = unpack(arg)
local countdownDuration = countdownDurationMilli / 1000
local curTimestamp = DateTimeUtils.GetCurrentTimestampBySeconds()
local expireTimestamp = expireTimestampMilli and expireTimestampMilli / 1000 or curTimestamp + countdownDuration
local curLeftTime = expireTimestamp - curTimestamp
self.view.countdown.countDownTxt.text = UIUtils.getLeftTimeToSecond(curLeftTime)
self.view.countdown.fill.fillAmount = curLeftTime / countdownDuration
local isInAlert = false
self.view.countdownStateCtrl:SetState("Normal")
self.m_countDownTickId = LuaUpdate:Add("Tick", function(deltaTime)
if self.m_isCountdownPause then
expireTimestamp = expireTimestamp + deltaTime
return
end
if self:_IsWorldFreeze() then
expireTimestamp = expireTimestamp + deltaTime
return
end
local curTs = DateTimeUtils.GetCurrentTimestampBySeconds()
local leftTime = math.max(expireTimestamp - curTs, 0)
local countdownNode = self.view.countdown
countdownNode.countDownTxt.text = UIUtils.getLeftTimeToSecond(leftTime)
countdownNode.fill.fillAmount = leftTime / countdownDuration
if not isInAlert and leftTime <= self.view.config.COUNTDOWN_ALERT_TIME_THRESHOLD then
isInAlert = true
self.view.countdownStateCtrl:SetState("Alert")
self.view.animationWrapper:Play("tasktrackcountdown_loop")
AudioAdapter.PostEvent("Au_UI_Toast_DungeonNormalTick")
end
if expireTimestamp - curTs <= 0 then
if cb then
cb()
end
self.m_countDownTickId = LuaUpdate:Remove(self.m_countDownTickId)
self:PlayAnimationOut(UIConst.PANEL_PLAY_ANIMATION_OUT_COMPLETE_ACTION_TYPE.Close)
end
end)
end
CommonTaskTrackCountdownCtrl.StartCounting = HL.Method(HL.Any) << function(self, arg)
self:_ToggleComponentOn(Component.Counting)
arg = arg or { DateTimeUtils.GetCurrentTimestampByMilliseconds() }
local startCountingTimestampMilli = unpack(arg)
local curCounting = (DateTimeUtils.GetCurrentTimestampByMilliseconds() - startCountingTimestampMilli) / 1000
self.view.counting.countingTxt.text = UIUtils.getLeftTimeToSecond(curCounting)
local tickInterval = 0
self.m_countDownTickId = LuaUpdate:Add("Tick", function(deltaTime)
if self.m_isCountingPause then
return
end
if self:_IsWorldFreeze() then
return
end
tickInterval = tickInterval + deltaTime
if tickInterval < UIConst.COMMON_UI_TIME_UPDATE_INTERVAL then
return
end
tickInterval = 0
curCounting = curCounting + UIConst.COMMON_UI_TIME_UPDATE_INTERVAL
self.view.counting.countingTxt.text = UIUtils.getLeftTimeToSecond(curCounting)
end)
end
CommonTaskTrackCountdownCtrl.OnCloseCommonTaskCountdown = HL.Method() << function(self)
self:Close()
end
CommonTaskTrackCountdownCtrl.OnFinishCommonTaskCounting = HL.Method() << function(self)
self:Close()
end
CommonTaskTrackCountdownCtrl.OnCommonTaskCountdownSwitchOn = HL.Method(HL.Any) << function(self, arg)
local isOn = unpack(arg)
self.m_isCountdownPause = not isOn
end
CommonTaskTrackCountdownCtrl.OnCommonTaskCountingSwitchOn = HL.Method(HL.Any) << function(self, arg)
local isOn = unpack(arg)
self.m_isCountingPause = not isOn
end
CommonTaskTrackCountdownCtrl.OnAddHeadBar = HL.Method(HL.Table) << function(self, args)
local succ, ctrl = UIManager:IsOpen(PanelId.BattleBossInfo)
if not succ then
return
end
local targetAbilitySystem = unpack(args)
if targetAbilitySystem and targetAbilitySystem.showBigHeadBar then
if targetAbilitySystem.alive then
self:_StartCoroutine(function()
coroutine.step()
local active, pos = ctrl:GetFollowPointPosition()
if active then
DOTween.To(function()
return self.view.main.position
end, function(value)
self.view.main.position = value
end, pos, self.view.config.COUNTDOWN_TWEEN_TO_TARGET_DURATION)
end
end)
else
DOTween.To(function()
return self.view.main.anchoredPosition
end, function(value)
self.view.main.anchoredPosition = value
end, self.m_originalAnchoredPos, self.view.config.COUNTDOWN_TWEEN_TO_TARGET_DURATION)
end
end
end
CommonTaskTrackCountdownCtrl.OnRemoveHeadBar = HL.Method(HL.Table) << function(self, args)
local targetAbilitySystem = unpack(args)
if targetAbilitySystem and targetAbilitySystem.showBigHeadBar then
DOTween.To(function()
return self.view.main.anchoredPosition
end, function(value)
self.view.main.anchoredPosition = value
end, self.m_originalAnchoredPos, self.view.config.COUNTDOWN_TWEEN_TO_TARGET_DURATION)
end
end
HL.Commit(CommonTaskTrackCountdownCtrl)