From ad64fdf087487e65752f9bf986cc92a9cd6265eb Mon Sep 17 00:00:00 2001 From: Panzer Date: Mon, 26 Aug 2024 07:44:01 -0600 Subject: [PATCH] refactor: New timer status --- scripts/common/timer.js | 13 ++++++ scripts/hud/status.js | 94 ++++++++++++++++++++++++++++++++--------- scripts/hud/timer.js | 84 +++++++++++++++++++++++++++++++----- 3 files changed, 159 insertions(+), 32 deletions(-) diff --git a/scripts/common/timer.js b/scripts/common/timer.js index a14088dc..d4ec1337 100644 --- a/scripts/common/timer.js +++ b/scripts/common/timer.js @@ -10,3 +10,16 @@ const TimerState = { RUNNING: 1, PRACTICE: 2 }; + +const TimerStateNEW = { + DISABLED: 0, + PRIMED: 1, + RUNNING: 2, + FINISHED: 3 +}; + +const TrackType = { + MAIN: 0, + STAGE: 1, + BONUS: 2 +}; diff --git a/scripts/hud/status.js b/scripts/hud/status.js index 76aa1c9d..c11fc116 100644 --- a/scripts/hud/status.js +++ b/scripts/hud/status.js @@ -38,28 +38,76 @@ class HudStatus { this.updateLabel(); } + static onTimerStatusChanged() { + this.updateLabel(); + } + static updateLabel() { - const enteredStartZone = this.enter && this.curZone === 1; - const enteredEndZone = this.enter && this.curZone === 0; - - let text = $.Localize('#HudStatus_Spawn'); - - if (this.saveStateUsing && this.timerState !== TimerState.RUNNING) { - text = `${$.Localize('#HudStatus_SaveState')} ${this.saveStateCurrent}/${this.saveStateCount}`; - } else { - if (enteredStartZone) { - text = $.Localize('#HudStatus_StartZone'); - } else if (enteredEndZone) { - text = $.Localize('#HudStatus_EndZone'); - } else if (this.curZone >= 0) { - text = `${$.Localize(this.linear ? '#HudStatus_Checkpoint' : '#HudStatus_Stage')} ${ - this.curZone - }/${ZonesAPI.GetZoneCount(this.curTrack)}`; - } - - if (this.curTrack > 0) { - text = `${$.Localize('#HudStatus_Bonus')} ${this.curTrack} | ${text}`; - } + const timerStatus = MomentumTimerAPI.GetObservedTimerStatus(); + + let text = ''; + + switch (timerStatus.state) { + case TimerStateNEW.DISABLED: + text = $.Localize('#HudStatus_TimerDisabled'); + break; + case TimerStateNEW.PRIMED: + text = $.Localize('#HudStatus_TimerPrimed'); + break; + case TimerStateNEW.RUNNING: + switch (timerStatus.trackId.type) { + case TrackType.MAIN: + if (timerStatus.segmentsCount === 1) { + if (timerStatus.segmentCheckpointsCount > 1) { + text = `${$.Localize('#HudStatus_Checkpoint')} ${timerStatus.minorNum}/${ + timerStatus.segmentCheckpointsCount + }`; + } else { + // no state text in this case + } + } else { + text = + timerStatus.segmentCheckpointsCount > 1 + ? `${$.Localize('#HudStatus_Stage')} ${timerStatus.majorNum}/${ + timerStatus.segmentsCount + } | ${$.Localize('#HudStatus_Checkpoint')} ${timerStatus.minorNum}/${ + timerStatus.segmentCheckpointsCount + }` + : `${$.Localize('#HudStatus_Stage')} ${timerStatus.majorNum}/${ + timerStatus.segmentsCount + }`; + } + break; + case TrackType.STAGE: + text = + timerStatus.segmentCheckpointsCount > 1 + ? `${$.Localize('#HudStatus_Stage')} ${timerStatus.trackId.number} | ${$.Localize( + '#HudStatus_Checkpoint' + )} ${timerStatus.minorNum}/${timerStatus.segmentCheckpointsCount}` + : `${$.Localize('#HudStatus_Stage')} ${timerStatus.trackId.number}`; + break; + case TrackType.BONUS: + text = + timerStatus.segmentCheckpointsCount > 1 + ? `${$.Localize('#HudStatus_Bonus')} ${timerStatus.trackId.number} | ${$.Localize( + '#HudStatus_Checkpoint' + )} ${timerStatus.minorNum}/${timerStatus.segmentCheckpointsCount}` + : `${$.Localize('#HudStatus_Bonus')} ${timerStatus.trackId.number}`; + break; + } + break; + case TimerStateNEW.FINISHED: + text = $.Localize('#HudStatus_TimerFinished'); + break; + default: + $.Warning('Unknown timer state'); + text = '???'; + break; + } + + // TODO: maybe show these somewhere else instead of prepending tons of stuff + if (this.saveStateUsing) { + text = `${$.Localize('#HudStatus_SaveState')} ${this.saveStateCurrent}/${this.saveStateCount} | ${text}`; } if (this.inPracticeMode) { @@ -78,6 +126,10 @@ class HudStatus { $.RegisterForUnhandledEvent('OnMomentumPlayerPracticeModeStateChange', this.onPracticeModeChange.bind(this)); $.RegisterForUnhandledEvent('OnSaveStateUpdate', this.onSaveStateChange.bind(this)); + $.RegisterForUnhandledEvent('OnObservedTimerStateChange', this.onTimerStatusChanged.bind(this)); + $.RegisterForUnhandledEvent('OnObservedTimerCheckpointProgressed', this.onTimerStatusChanged.bind(this)); + $.RegisterForUnhandledEvent('OnObservedTimerReplaced', this.onTimerStatusChanged.bind(this)); + this.label.text = $.Localize('#HudStatus_Spawn'); } } diff --git a/scripts/hud/timer.js b/scripts/hud/timer.js index c324c977..b2e39f5f 100644 --- a/scripts/hud/timer.js +++ b/scripts/hud/timer.js @@ -12,6 +12,8 @@ class HudTimer { static timeLabel = $('#HudTimerTime'); /** @type {Label} @static */ static compLabel = $('#HudTimerComparison'); + /** @type {Panel} @static */ + static panel = $.GetContextPanel(); static prevZone = 0; @@ -24,7 +26,7 @@ class HudTimer { static onTimerFinished() { this.timeLabel.AddClass(FINISHED_CLASS); - $.GetContextPanel().SetDialogVariableFloat('runtime', MomentumTimerAPI.GetCurrentRunTime()); + this.panel.SetDialogVariableFloat('runtime', MomentumTimerAPI.GetCurrentRunTime()); if (MomentumTimerAPI.IsFinishSoundEnabled()) $.PlaySoundEvent('Momentum.FinishTimer'); } @@ -45,10 +47,7 @@ class HudTimer { } static onUpdate() { - const timerState = MomentumTimerAPI.GetTimerState(); - if (timerState === TimerState.NOTRUNNING) return; - - $.GetContextPanel().SetDialogVariableFloat('runtime', MomentumTimerAPI.GetCurrentRunTime()); + this.panel.SetDialogVariableFloat('runtime', MomentumTimerAPI.GetObservedTimerStatus().runTime); } static onZoneChange(enter, linear, curZone, _curTrack, timerState) { @@ -77,8 +76,8 @@ class HudTimer { diffSymbol = ''; } - $.GetContextPanel().SetDialogVariableFloat('runtimediff', Math.abs(diff)); - $.GetContextPanel().SetDialogVariable('diffSymbol', diffSymbol); + this.panel.SetDialogVariableFloat('runtimediff', Math.abs(diff)); + this.panel.SetDialogVariable('diffSymbol', diffSymbol); this.compLabel.AddClass(FADEOUT_START_CLASS); this.compLabel.TriggerClass(FADEOUT_CLASS); @@ -93,7 +92,7 @@ class HudTimer { } static resetTimer() { - $.GetContextPanel().SetDialogVariableFloat('runtime', 0); + this.panel.SetDialogVariableFloat('runtime', 0); this.timeLabel.AddClass(INACTIVE_CLASS); this.forceHideComparison(); this.prevZone = 0; @@ -141,17 +140,80 @@ class HudTimer { this.prevZone = ZonesAPI.GetCurrentZone(); // if curZone === 0 and the timer is running we have big problems } + static onTimerStateChange() { + this.updateFullState(); + this.playStateSound(); + } + + static onTimerReplaced() { + this.updateFullState(); + } + + static updateFullState() { + const timerStatus = MomentumTimerAPI.GetObservedTimerStatus(); + + switch (timerStatus.state) { + case TimerStateNEW.DISABLED: + this.timeLabel.AddClass(INACTIVE_CLASS); + this.timeLabel.RemoveClass(FINISHED_CLASS); + break; + case TimerStateNEW.RUNNING: + this.timeLabel.RemoveClass(INACTIVE_CLASS); + this.timeLabel.RemoveClass(FINISHED_CLASS); + break; + case TimerStateNEW.FINISHED: + this.timeLabel.AddClass(FINISHED_CLASS); + this.timeLabel.RemoveClass(INACTIVE_CLASS); + break; + case TimerStateNEW.PRIMED: + this.timeLabel.AddClass(INACTIVE_CLASS); + this.timeLabel.RemoveClass(FINISHED_CLASS); + break; + default: + $.Warning('Unknown timer state'); + break; + } + + this.panel.SetDialogVariableFloat('runtime', MomentumTimerAPI.GetObservedTimerStatus().runTime); + } + + static playStateSound() { + const timerStatus = MomentumTimerAPI.GetObservedTimerStatus(); + + switch (timerStatus.state) { + case TimerStateNEW.DISABLED: + if (MomentumTimerAPI.IsStopSoundEnabled()) $.PlaySoundEvent('Momentum.StopTimer'); + break; + case TimerStateNEW.RUNNING: + if (MomentumTimerAPI.IsStartSoundEnabled()) $.PlaySoundEvent('Momentum.StartTimer'); + break; + case TimerStateNEW.FINISHED: + if (MomentumTimerAPI.IsFinishSoundEnabled()) $.PlaySoundEvent('Momentum.FinishTimer'); + break; + case TimerStateNEW.PRIMED: + // no sound + break; + default: + $.Warning('Unknown timer state'); + break; + } + } + static onLoad() { - $.GetContextPanel().hiddenHUDBits = HideHud.TABMENU; + this.panel.hiddenHUDBits = HideHud.TABMENU; } static { - $.RegisterEventHandler('HudProcessInput', $.GetContextPanel(), this.onUpdate.bind(this)); + $.RegisterEventHandler('HudProcessInput', this.panel, this.onUpdate.bind(this)); $.RegisterForUnhandledEvent('OnMomentumTimerStateChange', this.onTimerEvent.bind(this)); $.RegisterForUnhandledEvent('OnMomentumZoneChange', this.onZoneChange.bind(this)); $.RegisterForUnhandledEvent('OnSaveStateUpdate', this.onSaveStateChange.bind(this)); $.RegisterForUnhandledEvent('OnMomentumReplayStopped', this.onReplayStopped.bind(this)); - $.GetContextPanel().SetDialogVariableFloat('runtime', 0); + this.panel.SetDialogVariableFloat('runtime', 0); + + // NEW + $.RegisterForUnhandledEvent('OnObservedTimerStateChange', this.onTimerStateChange.bind(this)); + $.RegisterForUnhandledEvent('OnObservedTimerReplaced', this.onTimerReplaced.bind(this)); } }