Skip to content
Snippets Groups Projects
Commit fd28d244 authored by Paolo.Brasolin's avatar Paolo.Brasolin
Browse files

refactor: #fe extract HUD

parent 63f6034a
No related branches found
No related tags found
No related merge requests found
...@@ -9,6 +9,7 @@ import levenshtein from "damerau-levenshtein"; ...@@ -9,6 +9,7 @@ import levenshtein from "damerau-levenshtein";
import * as Types from "../../../backend/src/types"; import * as Types from "../../../backend/src/types";
import Foe from "./foe"; import Foe from "./foe";
import Typewriter from "./typewriter"; import Typewriter from "./typewriter";
import HUD from "./hud";
const DEVICE_KEY = "OETZI/DEVICE_ID"; const DEVICE_KEY = "OETZI/DEVICE_ID";
...@@ -19,12 +20,6 @@ interface InputStatus { ...@@ -19,12 +20,6 @@ interface InputStatus {
final: string; final: string;
} }
interface HUD {
score: Phaser.GameObjects.Text;
input: Phaser.GameObjects.Text;
health: Phaser.GameObjects.Text;
}
export default class FightScene extends Phaser.Scene { export default class FightScene extends Phaser.Scene {
foes: Array<Foe>; foes: Array<Foe>;
player: Phaser.Types.Physics.Arcade.SpriteWithDynamicBody; player: Phaser.Types.Physics.Arcade.SpriteWithDynamicBody;
...@@ -39,7 +34,6 @@ export default class FightScene extends Phaser.Scene { ...@@ -39,7 +34,6 @@ export default class FightScene extends Phaser.Scene {
constructor() { constructor() {
super("fight"); super("fight");
this.foes = []; this.foes = [];
this.hud = {};
} }
preload() { preload() {
...@@ -80,6 +74,9 @@ export default class FightScene extends Phaser.Scene { ...@@ -80,6 +74,9 @@ export default class FightScene extends Phaser.Scene {
init() { init() {
this.score = 0; this.score = 0;
this.health = 100; this.health = 100;
this.hud = new HUD(this);
this.hud.setHealth(this.health);
this.hud.setScore(this.score);
this.events.on("pause", this.concealClues.bind(this)); this.events.on("pause", this.concealClues.bind(this));
this.events.on("resume", this.uncoverClues.bind(this)); this.events.on("resume", this.uncoverClues.bind(this));
} }
...@@ -120,7 +117,6 @@ export default class FightScene extends Phaser.Scene { ...@@ -120,7 +117,6 @@ export default class FightScene extends Phaser.Scene {
// ); // );
// this.scale.refresh(); // this.scale.refresh();
this.createHUD();
this.createAndBindTypewriter(); this.createAndBindTypewriter();
await this.initBeDevice(); await this.initBeDevice();
...@@ -202,42 +198,15 @@ export default class FightScene extends Phaser.Scene { ...@@ -202,42 +198,15 @@ export default class FightScene extends Phaser.Scene {
}); });
} }
createHUD() {
this.hud.input = this.add.text(
this.cameras.main.width / 2,
this.cameras.main.height / 2,
"",
{
font: "bold 64px Courier",
color: "#ffffff",
},
);
this.hud.input.setOrigin(0.5, 0.5);
this.hud.score = this.add.text(10, 10, "", {
font: "bold 48px Courier",
color: "lightgreen",
});
this.hud.score.setOrigin(0, 0);
this.updateScore(0);
this.hud.health = this.add.text(this.cameras.main.width - 10, 10, "", {
font: "bold 48px Courier",
color: "orange",
});
this.hud.health.setOrigin(1, 0);
this.updateHealth(0);
}
updateScore(delta: number) { updateScore(delta: number) {
this.score += delta; this.score += delta;
this.hud.score.text = "" + this.score.toString(); this.hud.setScore(this.score);
} }
updateHealth(delta: number) { updateHealth(delta: number) {
this.health += delta; this.health += delta;
this.health = Math.max(this.health, 0); this.health = Math.max(this.health, 0);
this.hud.health.text = this.health.toString() + ""; this.hud.setHealth(this.health);
this.checkAlive(); this.checkAlive();
} }
...@@ -360,7 +329,7 @@ export default class FightScene extends Phaser.Scene { ...@@ -360,7 +329,7 @@ export default class FightScene extends Phaser.Scene {
if (inputStatus.began_at === null) return; if (inputStatus.began_at === null) return;
if (inputStatus.ended_at === null) return; if (inputStatus.ended_at === null) return;
if (inputStatus.final === "") return; if (inputStatus.final === "") return;
this.hud.input.text = ""; this.hud.setInput("");
this.submitTranscription({ this.submitTranscription({
began_at: inputStatus.began_at.toISOString(), began_at: inputStatus.began_at.toISOString(),
ended_at: inputStatus.ended_at.toISOString(), ended_at: inputStatus.ended_at.toISOString(),
...@@ -369,7 +338,7 @@ export default class FightScene extends Phaser.Scene { ...@@ -369,7 +338,7 @@ export default class FightScene extends Phaser.Scene {
}); });
}; };
this.typewriter.onChange = (inputStatus) => { this.typewriter.onChange = (inputStatus) => {
this.hud.input.text = inputStatus.final; this.hud.setInput(inputStatus.final);
}; };
} }
......
const ICONS = {
SCORE: "️⭐️",
HEALTH: "❤️️",
};
export default class HUD {
scene: Phaser.Scene;
input: Phaser.GameObjects.Text;
score: Phaser.GameObjects.Text;
health: Phaser.GameObjects.Text;
constructor(scene: Phaser.Scene) {
this.scene = scene;
this.input = this.initInput(scene);
this.score = this.initScore(scene);
this.health = this.initHealth(scene);
}
initInput(scene: Phaser.Scene) {
return scene.add
.text(scene.cameras.main.width / 2, scene.cameras.main.height / 2, "", {
font: "bold 64px Courier",
color: "#ffffff",
})
.setOrigin(0.5, 0.5);
}
initScore(scene: Phaser.Scene) {
return scene.add
.text(10, 10, "", {
font: "bold 32px Courier",
color: "lightgreen",
testString: `${ICONS.SCORE}100000`,
})
.setOrigin(0, 0);
}
initHealth(scene: Phaser.Scene) {
return scene.add
.text(scene.cameras.main.width - 10, 10, "", {
font: "bold 32px Courier",
color: "orange",
testString: `100 ${ICONS.HEALTH}`,
})
.setOrigin(1, 0);
}
setInput(input: string) {
this.input.text = input;
}
setScore(score: number) {
this.score.text = `${ICONS.SCORE}${score}`;
}
setHealth(health: number) {
this.health.text = `${health}${ICONS.HEALTH}`;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment