diff --git a/scripts/hud/status.js b/scripts/hud/status.js index bde14ade..8d96689e 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,11 @@ class HudStatus { $.RegisterForUnhandledEvent('OnMomentumPlayerPracticeModeStateChange', this.onPracticeModeChange.bind(this)); $.RegisterForUnhandledEvent('OnSaveStateUpdate', this.onSaveStateChange.bind(this)); + $.RegisterForUnhandledEvent('LevelInitPostEntity', this.onLevelInit.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 63c7c9e7..dae6f574 100644 --- a/scripts/hud/timer.js +++ b/scripts/hud/timer.js @@ -45,10 +45,7 @@ class HudTimer { } static onUpdate() { - const timerState = MomentumTimerAPI.GetTimerState(); - if (timerState === TimerState.NOTRUNNING) return; - - $.GetContextPanel().SetDialogVariableFloat('runtime', MomentumTimerAPI.GetCurrentRunTime()); + $.GetContextPanel().SetDialogVariableFloat('runtime', MomentumTimerAPI.GetObservedTimerStatus().runTime); } static onZoneChange(enter, linear, curZone, _curTrack, timerState) { @@ -141,6 +138,65 @@ 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; + } + + $.GetContextPanel().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.LEADERBOARDS; } @@ -153,5 +209,9 @@ class HudTimer { $.RegisterForUnhandledEvent('OnMomentumReplayStopped', this.onReplayStopped.bind(this)); $.GetContextPanel().SetDialogVariableFloat('runtime', 0); + + // NEW + $.RegisterForUnhandledEvent('OnObservedTimerStateChange', this.onTimerStateChange.bind(this)); + $.RegisterForUnhandledEvent('OnObservedTimerReplaced', this.onTimerReplaced.bind(this)); } }