Skip to content

Commit

Permalink
wip: timer
Browse files Browse the repository at this point in the history
  • Loading branch information
tsa96 committed Aug 28, 2024
1 parent 639d31b commit 7cbf1e9
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 108 deletions.
192 changes: 94 additions & 98 deletions scripts/hud/timer.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { PanelHandler } from 'util/module-helpers';
import { HideHud } from 'common/state';
import { TimerEvent_OLD, TimerState_OLD } from 'common/timer';

const INACTIVE_CLASS = 'hudtimer__time--inactive';
const FINISHED_CLASS = 'hudtimer__time--finished';
Expand All @@ -12,7 +11,7 @@ const FADEOUT_CLASS = 'hudtimer__comparison--fadeout';
const FADEOUT_START_CLASS = 'hudtimer__comparison--fade-start';

@PanelHandler()
class HudTimerHandler {
class HudTimer {
readonly panels = {
cp: $.GetContextPanel<MomHudTimer>(),
time: $('#HudTimerTime'),
Expand All @@ -22,139 +21,73 @@ class HudTimerHandler {
prevZone = 0;

constructor() {
$.RegisterEventHandler('HudProcessInput', $.GetContextPanel(), () => this.onUpdate());
$.RegisterForUnhandledEvent('OnMomentumTimerStateChange', (arg1, arg2) => this.onTimerEvent(arg1, arg2));
$.RegisterForUnhandledEvent(
'OnMomentumZoneChange',
(entering, isLinear, currentZone, currentTrack, timerState) =>
this.onZoneChange(entering, isLinear, currentZone, currentTrack, timerState)
);
$.RegisterForUnhandledEvent('OnSaveStateUpdate', (count, current, usingMenu) =>
this.onSaveStateChange(count, current, usingMenu)
);
$.RegisterForUnhandledEvent('OnMomentumReplayStopped', () => this.onReplayStopped());

$.GetContextPanel().SetDialogVariableFloat('runtime', 0);
this.panels.cp.SetDialogVariableFloat('runtime', 0);
this.panels.cp.hiddenHUDBits = HideHud.TABMENU;
}

onTimerStarted() {
this.panels.time.RemoveClass(FAILED_CLASS); // fail animation could be happening, so force stop
this.panels.time.RemoveClass(INACTIVE_CLASS);

if (this.isStartSoundEnabled()) {
$.PlaySoundEvent('Momentum.StartTimer');
}
}

onTimerFinished() {
this.panels.time.AddClass(FINISHED_CLASS);

$.GetContextPanel().SetDialogVariableFloat('runtime', MomentumTimerAPI.GetCurrentRunTime());
$.RegisterEventHandler('HudProcessInput', this.panels.cp, () => this.onUpdate());

if (this.isFinishSoundEnabled()) {
$.PlaySoundEvent('Momentum.FinishTimer');
}
}

onTimerStopped() {
this.resetTimer();

// if we want special styling for timer artificially running (via savestate), do it here like so
// if (MomentumTimerAPI.GetTimerState() === Globals.Timer.State.PRACTICE) HudTimerthis.panels.time.AddClass(PRACTICE_CLASS);
$.RegisterForUnhandledEvent('OnObservedTimerStateChange', this.onTimerStateChange.bind(this));
$.RegisterForUnhandledEvent('OnObservedTimerReplaced', this.onTimerReplaced.bind(this));

if (this.isStopSoundEnabled()) {
$.PlaySoundEvent('Momentum.StopTimer');
}
}
onTimerFailed() {
// failed to start timer, so resetting is not needed

this.panels.time.TriggerClass(FAILED_CLASS);

if (this.isFailSoundEnabled()) {
$.PlaySoundEvent('Momentum.FailedStartTimer');
}
$.RegisterForUnhandledEvent('OnSaveStateUpdate', this.onSaveStateChange.bind(this));
$.RegisterForUnhandledEvent('OnMomentumReplayStopped', this.onReplayStopped.bind(this));
}

onUpdate() {
const timerState = MomentumTimerAPI.GetObservedTimerStatus().runTime;
if (timerState === TimerState_OLD.NOT_RUNNING) return;

$.GetContextPanel().SetDialogVariableFloat('runtime', MomentumTimerAPI.GetCurrentRunTime());
this.panels.cp.SetDialogVariableFloat('runtime', MomentumTimerAPI.GetObservedTimerStatus().runTime);
}

onZoneChange(enter: any, linear: any, curZone: any, _curTrack: any, timerState: any) {
if (timerState === TimerState_OLD.NOT_RUNNING && enter && curZone === 1) {
onZoneChange(enter, linear, curZone, _curTrack, timerState) {
if (timerState === TimerState.NOTRUNNING && enter && curZone === 1) {
// timer state is not reset on map finished until entering the start zone again (on reset)
this.resetTimer();
this.panels.time.RemoveClass(FINISHED_CLASS);
return;
}

if (timerState === TimerState_OLD.RUNNING && curZone > 1 && enter === linear && this.prevZone !== curZone) {
if (timerState === TimerState.RUNNING && curZone > 1 && enter === linear && HudTimer.prevZone !== curZone) {
const diff = RunComparisonsAPI.GetLoadedComparisonOverallDiff(curZone);

let diffSymbol;
if (diff > 0) {
this.panels.comparison.AddClass(DECREASE_CLASS);
this.panels.comparison.RemoveClass(INCREASE_CLASS);
this.compLabel.AddClass(DECREASE_CLASS);
this.compLabel.RemoveClass(INCREASE_CLASS);
diffSymbol = '+';
} else if (diff < 0) {
this.panels.comparison.AddClass(INCREASE_CLASS);
this.panels.comparison.RemoveClass(DECREASE_CLASS);
this.compLabel.AddClass(INCREASE_CLASS);
this.compLabel.RemoveClass(DECREASE_CLASS);
diffSymbol = '-';
} else {
this.panels.comparison.RemoveClass(INCREASE_CLASS);
this.panels.comparison.RemoveClass(DECREASE_CLASS);
this.compLabel.RemoveClass(INCREASE_CLASS);
this.compLabel.RemoveClass(DECREASE_CLASS);
diffSymbol = '';
}

$.GetContextPanel().SetDialogVariableFloat('runtimediff', Math.abs(diff));
$.GetContextPanel().SetDialogVariable('diffSymbol', diffSymbol);
this.panels.cp.SetDialogVariableFloat('runtimediff', Math.abs(diff));
this.panels.cp.SetDialogVariable('diffSymbol', diffSymbol);

this.panels.comparison.AddClass(FADEOUT_START_CLASS);
this.panels.comparison.TriggerClass(FADEOUT_CLASS);
this.compLabel.AddClass(FADEOUT_START_CLASS);
this.compLabel.TriggerClass(FADEOUT_CLASS);
}

this.prevZone = curZone;
}

forceHideComparison() {
this.panels.comparison.RemoveClass(FADEOUT_START_CLASS);
this.panels.comparison.TriggerClass(FADEOUT_CLASS);
this.compLabel.RemoveClass(FADEOUT_START_CLASS);
this.compLabel.TriggerClass(FADEOUT_CLASS);
}

resetTimer() {
$.GetContextPanel().SetDialogVariableFloat('runtime', 0);
this.panels.cp.SetDialogVariableFloat('runtime', 0);
this.panels.time.AddClass(INACTIVE_CLASS);
this.forceHideComparison();
this.prevZone = 0;
}

onTimerEvent(_ent: any, type: any) {
switch (type) {
case TimerEvent_OLD.STARTED:
this.onTimerStarted();
break;
case TimerEvent_OLD.FINISHED:
this.onTimerFinished();
break;
case TimerEvent_OLD.STOPPED:
this.onTimerStopped();
break;
case TimerEvent_OLD.FAILED:
this.onTimerFailed();
break;
default:
$.Warning('Unknown timer event given to Momentum hud timer!');
break;
}
}

onSaveStateChange(_count: any, _current: any, _usingmenu: any) {
onSaveStateChange(_count, _current, _usingmenu) {
const timerState = MomentumTimerAPI.GetTimerState();
if (timerState !== TimerState_OLD.RUNNING) {
if (timerState !== TimerState.RUNNING) {
this.resetTimer();
this.panels.time.RemoveClass(FINISHED_CLASS);
}
Expand All @@ -164,7 +97,7 @@ class HudTimerHandler {
this.panels.time.RemoveClass(FINISHED_CLASS);

const timerState = MomentumTimerAPI.GetTimerState();
if (timerState !== TimerState_OLD.RUNNING) {
if (timerState !== TimerState.RUNNING) {
this.resetTimer();
return;
}
Expand All @@ -174,19 +107,82 @@ class HudTimerHandler {
this.prevZone = ZonesAPI.GetCurrentZone(); // if curZone === 0 and the timer is running we have big problems
}

isStartSoundEnabled(): boolean {
onTimerStateChange() {
this.updateFullState();
this.playStateSound();
}

onTimerReplaced() {
this.updateFullState();
}

updateFullState() {
const timerStatus = MomentumTimerAPI.GetObservedTimerStatus();

switch (timerStatus.state) {
case TimerStateNEW.DISABLED:
this.panels.time.AddClass(INACTIVE_CLASS);
this.panels.time.RemoveClass(FINISHED_CLASS);
break;
case TimerStateNEW.RUNNING:
this.panels.time.RemoveClass(INACTIVE_CLASS);
this.panels.time.RemoveClass(FINISHED_CLASS);
break;
case TimerStateNEW.FINISHED:
this.panels.time.AddClass(FINISHED_CLASS);
this.panels.time.RemoveClass(INACTIVE_CLASS);
break;
case TimerStateNEW.PRIMED:
this.panels.time.AddClass(INACTIVE_CLASS);
this.panels.time.RemoveClass(FINISHED_CLASS);
break;
default:
$.Warning('Unknown timer state');
break;
}

this.panels.cp.SetDialogVariableFloat('runtime', MomentumTimerAPI.GetObservedTimerStatus().runTime);
}

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;
}
}

onLoad() {
this.panels.hiddenHUDBits = HideHud.TABMENU;
}

get isStartSoundEnabled(): boolean {
return GameInterfaceAPI.GetSettingBool('mom_hud_timer_sound_start_enable');
}

isFinishSoundEnabled(): boolean {
get isFinishSoundEnabled(): boolean {
return GameInterfaceAPI.GetSettingBool('mom_hud_timer_sound_finish_enable');
}

isStopSoundEnabled(): boolean {
get isStopSoundEnabled(): boolean {
return GameInterfaceAPI.GetSettingBool('mom_hud_timer_sound_stop_enable');
}

isFailSoundEnabled(): boolean {
get isFailSoundEnabled(): boolean {
return GameInterfaceAPI.GetSettingBool('mom_hud_timer_sound_fail_enable');
}
}
7 changes: 0 additions & 7 deletions scripts/types-mom/apis.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,13 +265,6 @@ declare namespace RunComparisonsAPI {
function GetLoadedComparisonOverallDiff(arg: any): any;
}

declare namespace ZonesAPI {
function GetZoneCount(...args: any[]): any; // TODO: Old API (I think)
function GetZoneSpeed(...args: any[]): any;
function GetZoneSpeed(...args: any[]): any;
function GetCurrentZone(...args: any[]): any;
}

declare namespace NewsAPI {
interface RSSFeedItem {
title: string;
Expand Down
3 changes: 0 additions & 3 deletions scripts/types-mom/events.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,6 @@ interface GlobalEventNameMap {
/** Fired when the JS panel should update what it's showing */
HudCompare_Update: () => void;

// TODO: Old, remove after rio stuff is in
OnMomentumTimerStateChange: (arg1: any, arg2: any) => any;

OnCookUpdate: (time: float, percentage: float) => void; // Conc thing!

OnConcEntityPanelThink: () => void;
Expand Down

0 comments on commit 7cbf1e9

Please sign in to comment.