|
| 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 | +} |
0 commit comments