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" ;
33import GameNode from "./GameNode" ;
44import Player from "./Player" ;
55import { campaign } from "../levels" ;
@@ -10,6 +10,7 @@ import type { IMediaInstance } from "@pixi/sound";
1010import { playSound } from "../audio" ;
1111import { LEFT , RIGHT } from "../points" ;
1212import { slideIn } from "../animations" ;
13+ import ScoreScreen from "./ScoreScreen" ;
1314
1415type State = "game" | "end" ;
1516export 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 ) ;
0 commit comments