Skip to content

Commit 6002196

Browse files
committed
Both players can input high scores in two player mode.
1 parent 5cfe8d0 commit 6002196

File tree

3 files changed

+33
-46
lines changed

3 files changed

+33
-46
lines changed

src/nodes/Multiplayer.ts

Lines changed: 27 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { HTMLText, Point, type Ticker } from "pixi.js";
2-
import { HEIGHT, TEXT_FONT, theme, WIDTH } from "../constants";
1+
import { Point, type Ticker } from "pixi.js";
2+
import { HEIGHT, WIDTH } from "../constants";
33
import GameNode from "./GameNode";
44
import Player from "./Player";
55
import { campaign } from "../levels";
@@ -10,6 +10,7 @@ import type { IMediaInstance } from "@pixi/sound";
1010
import { playSound } from "../audio";
1111
import { LEFT, RIGHT } from "../points";
1212
import { slideIn } from "../animations";
13+
import ScoreScreen from "./ScoreScreen";
1314

1415
type State = "game" | "end";
1516
export default class Multiplayer extends GameNode {
@@ -18,6 +19,8 @@ export default class Multiplayer extends GameNode {
1819
onFinish?: () => void;
1920
music?: IMediaInstance;
2021
state: State = "game";
22+
outPlayers: number[] = [];
23+
scoreFinishPlayers: number[] = [];
2124

2225
constructor(background: Background, startLevel: number) {
2326
super();
@@ -37,42 +40,37 @@ export default class Multiplayer extends GameNode {
3740
];
3841
for (let [index, player] of this.players.entries()) {
3942
player.onTopOut = () => {
40-
this.music?.stop();
41-
for (let p2 of this.players) {
42-
p2.currentState = "pause";
43+
this.outPlayers.push(index);
44+
if (this.outPlayers.length === 2) {
45+
this.music?.stop();
4346
}
4447
};
45-
player.onGameOver = () => {
46-
playSound("ending");
47-
const text = new HTMLText({
48-
text: `Player ${2 - index} Wins!`,
49-
style: {
50-
fontSize: 72,
51-
fontFamily: TEXT_FONT,
52-
fill: theme.colors.primary,
53-
stroke: { color: theme.colors.background, width: 10 },
54-
fontWeight: "bold",
55-
},
56-
});
57-
text.anchor = { x: 0.5, y: 0.5 };
58-
text.position = {
59-
x: (index === 0 ? 1 / 4 : 3 / 4) * WIDTH,
48+
player.onGameOver = (score) => {
49+
this.view.removeChild(player.view);
50+
player.destroy();
51+
const scores = new ScoreScreen(
52+
score * 100,
53+
inputs[index === 0 ? "player1" : "player2"]
54+
);
55+
scores.view.position = {
56+
x: WIDTH * (index === 0 ? 0.25 : 0.75),
6057
y: HEIGHT / 2,
6158
};
62-
this.view.addChild(text);
63-
this.state = "end";
64-
// for (let p2 of this.players) {
65-
// this.view.removeChild(p2.view);
66-
// p2.destroy();
67-
// }
68-
// // TODO show win screen
69-
// this.onFinish?.();
59+
this.view.addChild(scores.view);
60+
scores.onFinish = () => {
61+
this.view.removeChild(scores.view);
62+
scores.destroy();
63+
this.scoreFinishPlayers.push(index);
64+
if (this.scoreFinishPlayers.length === 2) {
65+
this.onFinish?.();
66+
}
67+
};
68+
scores.start();
7069
};
7170
}
7271
}
7372

7473
async start() {
75-
document.addEventListener("keydown", this.handleKeyDown);
7674
for (let player of this.players) {
7775
this.view.addChild(player.view);
7876
}
@@ -97,19 +95,6 @@ export default class Multiplayer extends GameNode {
9795
this.music = await playSound("bgMusic", { loop: true });
9896
}
9997

100-
handleKeyDown = () => {
101-
if (this.state !== "end") {
102-
return;
103-
}
104-
for (let p2 of this.players) {
105-
this.view.removeChild(p2.view);
106-
p2.destroy();
107-
}
108-
document.removeEventListener("keydown", this.handleKeyDown);
109-
this.onFinish?.();
110-
// End the game
111-
};
112-
11398
tick = (time: Ticker) => {
11499
for (let player of this.players) {
115100
player.tick(time);

src/nodes/ScoreScreen.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Container, Graphics, HTMLText } from "pixi.js";
2-
import { HEIGHT, TEXT_FONT, theme, WIDTH } from "../constants";
2+
import { HEIGHT, TEXT_FONT, theme } from "../constants";
33
import GameNode from "./GameNode";
44
import { type PlayerInput } from "../inputs";
55
import { getScores, setScores, type Score } from "../storage";
@@ -25,7 +25,6 @@ export default class ScoreScreen extends GameNode {
2525
super();
2626
this.score = score;
2727
this.inputMap = inputMap;
28-
this.view.position = { x: WIDTH / 2, y: HEIGHT / 2 };
2928

3029
const containerSize = HEIGHT * 0.75;
3130
this.view.addChild(
@@ -204,7 +203,9 @@ export default class ScoreScreen extends GameNode {
204203
}
205204
} else {
206205
// Press any key to go back to start
207-
this.onFinish?.();
206+
if (Object.values(this.inputMap).includes(e.key)) {
207+
this.onFinish?.();
208+
}
208209
}
209210
};
210211

src/nodes/SinglePlayer.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Point, type Ticker } from "pixi.js";
2-
import { WIDTH } from "../constants";
2+
import { HEIGHT, WIDTH } from "../constants";
33
import GameNode from "./GameNode";
44
import Player from "./Player";
55
import { campaign } from "../levels";
@@ -40,6 +40,7 @@ export default class SinglePlayer extends GameNode {
4040
this.player.destroy();
4141
this.mode = "score";
4242
const scores = new ScoreScreen(score * 100, inputs.player1);
43+
scores.view.position = { x: WIDTH / 2, y: HEIGHT / 2 };
4344
this.view.addChild(scores.view);
4445
scores.onFinish = () => {
4546
scores.destroy();

0 commit comments

Comments
 (0)