Skip to content

Commit

Permalink
refactor: New timer status
Browse files Browse the repository at this point in the history
  • Loading branch information
Panzerhandschuh authored and tsa96 committed Aug 26, 2024
1 parent 9130893 commit 31b127e
Show file tree
Hide file tree
Showing 3 changed files with 159 additions and 32 deletions.
13 changes: 13 additions & 0 deletions scripts/common/timer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
};
94 changes: 73 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,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');
}
}
84 changes: 73 additions & 11 deletions scripts/hud/timer.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ class HudTimer {
static timeLabel = $('#HudTimerTime');
/** @type {Label} @static */
static compLabel = $('#HudTimerComparison');
/** @type {Panel} @static */
static panel = $.GetContextPanel();

static prevZone = 0;

Expand All @@ -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');
}
Expand All @@ -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) {
Expand Down Expand Up @@ -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);
Expand All @@ -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;
Expand Down Expand Up @@ -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));
}
}

0 comments on commit 31b127e

Please sign in to comment.