Skip to content

Commit

Permalink
refactor: New timer status
Browse files Browse the repository at this point in the history
  • Loading branch information
Panzerhandschuh committed Aug 26, 2024
1 parent 9130893 commit aa0d0cc
Show file tree
Hide file tree
Showing 2 changed files with 138 additions and 25 deletions.
95 changes: 74 additions & 21 deletions scripts/hud/status.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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');
}
}
68 changes: 64 additions & 4 deletions scripts/hud/timer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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.TABMENU;
}
Expand All @@ -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));
}
}

0 comments on commit aa0d0cc

Please sign in to comment.