Skip to content

Commit 3dc40cb

Browse files
committed
Add high scores screen
1 parent e5ee84f commit 3dc40cb

File tree

5 files changed

+78
-5
lines changed

5 files changed

+78
-5
lines changed

src/components/Game.tsx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { GameScene } from "../game/scenes/GameScene";
66
import { MenuScene } from "../game/scenes/MenuScene";
77
import { GameOverScene } from "../game/scenes/GameOverScene";
88
import { NameEntryScene } from "../game/scenes/NameEntryScene";
9+
import { HighScoresScene } from "../game/scenes/HighScoresScene";
910

1011
const Game = () => {
1112
const [device, setDevice] = useState<HIDDevice | null>(null);
@@ -27,7 +28,13 @@ const Game = () => {
2728
const initTimer = setTimeout(() => {
2829
gameRef.current = new Phaser.Game({
2930
...config,
30-
scene: [MenuScene, GameScene, GameOverScene, NameEntryScene],
31+
scene: [
32+
MenuScene,
33+
GameScene,
34+
GameOverScene,
35+
NameEntryScene,
36+
HighScoresScene,
37+
],
3138
});
3239
}, 50);
3340

src/game/config.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { GameScene } from "./scenes/GameScene";
33
import { GameOverScene } from "./scenes/GameOverScene";
44
import { MenuScene } from "./scenes/MenuScene";
55
import { NameEntryScene } from "./scenes/NameEntryScene";
6+
import { HighScoresScene } from "./scenes/HighScoresScene";
67

78
export const gameConfig: Phaser.Types.Core.GameConfig = {
89
type: Phaser.AUTO,
@@ -17,7 +18,7 @@ export const gameConfig: Phaser.Types.Core.GameConfig = {
1718
debug: false,
1819
},
1920
},
20-
scene: [MenuScene, GameScene, GameOverScene, NameEntryScene],
21+
scene: [MenuScene, GameScene, GameOverScene, NameEntryScene, HighScoresScene],
2122
scale: {
2223
parent: "game-container",
2324
},

src/game/scenes/HighScoresScene.ts

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import Phaser from "phaser";
2+
3+
export class HighScoresScene extends Phaser.Scene {
4+
constructor() {
5+
super("HighScoresScene");
6+
}
7+
8+
create() {
9+
const { width, height } = this.cameras.main;
10+
const stored = localStorage.getItem("switchy-hiscores");
11+
const scores = stored ? JSON.parse(stored) : [];
12+
13+
document.fonts.load('20px "Monogram"').then(() => {
14+
this.add
15+
.text(width / 2, 20, "HIGH SCORES", {
16+
fontFamily: "Monogram",
17+
fontSize: "20px",
18+
color: "#0f380f",
19+
})
20+
.setOrigin(0.5);
21+
22+
if (scores.length === 0) {
23+
this.add
24+
.text(width / 2, height / 2, "None yet...\nBe the first!", {
25+
fontFamily: "Monogram",
26+
fontSize: "20px",
27+
color: "#0f380f",
28+
})
29+
.setOrigin(0.5);
30+
} else {
31+
scores.forEach((entry: { name: string; score: number }, i: number) => {
32+
const y = 40 + i * 12;
33+
this.add
34+
.text(width / 2 - 40, y, `${i + 1}. ${entry.name}`, {
35+
fontFamily: "Monogram",
36+
fontSize: "20px",
37+
color: "#0f380f",
38+
})
39+
.setOrigin(0, 0);
40+
this.add
41+
.text(width / 2 + 40, y, `${entry.score}`, {
42+
fontFamily: "Monogram",
43+
fontSize: "20px",
44+
color: "#0f380f",
45+
})
46+
.setOrigin(1, 0);
47+
});
48+
}
49+
this.add
50+
.text(width / 2, height - 20, "Space to go back!", {
51+
fontFamily: "Monogram",
52+
fontSize: "20px",
53+
color: "#0f380f",
54+
})
55+
.setOrigin(0.5);
56+
});
57+
58+
this.input.keyboard?.once("keydown-SPACE", () => {
59+
this.scene.start("MenuScene");
60+
});
61+
}
62+
}

src/game/scenes/MenuScene.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,11 @@ export class MenuScene extends Phaser.Scene {
9595

9696
selectOption() {
9797
const selected = this.options[this.selectedIndex];
98+
this.selectedIndex = 0;
9899
if (selected === "START GAME") {
99100
this.scene.start("GameScene");
100101
} else if (selected === "HI SCORES") {
101-
console.log("TODO: Show high scores");
102+
this.scene.start("HighScoresScene");
102103
}
103104
}
104105
}

src/game/scenes/NameEntryScene.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export class NameEntryScene extends Phaser.Scene {
1111

1212
init(data: { score: number }) {
1313
this.score = data.score;
14+
this.enteredName = "";
1415
}
1516

1617
create() {
@@ -34,7 +35,7 @@ export class NameEntryScene extends Phaser.Scene {
3435
.setOrigin(0.5);
3536

3637
this.nameText = this.add
37-
.text(width / 2, height / 2, `${this.nameText ?? ""}`, {
38+
.text(width / 2, height / 2, `___`, {
3839
fontFamily: "Monogram",
3940
fontSize: "20px",
4041
color: "#0f380f",
@@ -58,7 +59,8 @@ export class NameEntryScene extends Phaser.Scene {
5859
}
5960

6061
updateNameDisplay() {
61-
this.nameText.setText(this.enteredName);
62+
const padded = this.enteredName.padEnd(3, "_");
63+
this.nameText.setText(padded);
6264
}
6365

6466
saveScore() {

0 commit comments

Comments
 (0)