From 9194130cbf92ce2f5cacf427643eddbb4af62ab4 Mon Sep 17 00:00:00 2001
From: Paolo Brasolin <paolo.brasolin@eurac.edu>
Date: Wed, 13 Apr 2022 17:13:16 +0200
Subject: [PATCH] refactor: #fe extract useful stuff from HUD

---
 frontend/src/js/hud.ts | 24 +++++++++++++-----------
 1 file changed, 13 insertions(+), 11 deletions(-)

diff --git a/frontend/src/js/hud.ts b/frontend/src/js/hud.ts
index 7b7aee6..935aa40 100644
--- a/frontend/src/js/hud.ts
+++ b/frontend/src/js/hud.ts
@@ -1,4 +1,4 @@
-const ICONS = {
+export const ICONS = {
   SCORE: "️⭐️",
   CLOCK: "⏲️",
   HEALTH: "❤️️",
@@ -28,6 +28,17 @@ interface HudOptions {
   inputPosition: number;
 }
 
+export const formatTime = (milliseconds: number) => {
+  const minutes = Math.floor(milliseconds / 60000);
+  const seconds = Math.floor((milliseconds % 60000) / 1000)
+    .toString()
+    .padStart(2, "0");
+  const hundredths = Math.floor((milliseconds % 1000) / 10)
+    .toString()
+    .padStart(2, "0");
+  return `${minutes}:${seconds}.${hundredths}`;
+};
+
 export default class HUD {
   scene: Phaser.Scene;
   options: HudOptions;
@@ -118,16 +129,7 @@ export default class HUD {
   }
 
   setClock(milliseconds: number) {
-    const minutes = Math.floor(milliseconds / 60000);
-    const seconds = Math.floor((milliseconds % 60000) / 1000)
-      .toString()
-      .padStart(2, "0");
-    const hundredths = Math.floor((milliseconds % 1000) / 10)
-      .toString()
-      .padStart(2, "0");
-    const formatted = `${minutes}:${seconds}.${hundredths}`;
-    // TODO: we can probably do away with the clock icon
-    this.clock.text = `${formatted}\u2009${ICONS.CLOCK}`;
+    this.clock.text = `${this.formatTime(milliseconds)}\u2009${ICONS.CLOCK}`;
   }
 
   showSubmitFeedback(color: string, input: string) {
-- 
GitLab