11import { Scene } from 'phaser' ;
22import { displayPlayer , globalConsts } from '../main.ts' ;
3- import { formatTime } from '../thatFolder/ThatPlayer.ts' ;
3+ import { formatTime , get1 , get3 } from '../thatFolder/ThatPlayer.ts' ;
44import { Button } from '../custom_classes/Button.ts' ;
55import { ButtonManager } from '../custom_classes/ButtonManager.ts' ;
66import Text = Phaser . GameObjects . Text ;
7- import Rectangle = Phaser . GameObjects . Rectangle ;
87import { fetchLeaderboard , removeEntry , sortedLeaderboard , sortLeaderboard } from './Leaderboard.ts' ;
8+ import Gamepad = Phaser . Input . Gamepad . Gamepad ;
99
1010// config
1111const range : number = 2 ;
@@ -26,7 +26,9 @@ let leaderboardText: Phaser.GameObjects.Text;
2626let gameOverImage : Phaser . GameObjects . Image ;
2727let saveButton : Button ;
2828let leaderboardIsLoaded : boolean = false ;
29+ let savedScore : boolean = false ;
2930let buttonManager : ButtonManager ;
31+ let gamePad : Gamepad ;
3032
3133// Scene class
3234export class GameOver extends Scene {
@@ -48,12 +50,6 @@ export class GameOver extends Scene {
4850 // Creates the button manager
4951 buttonManager = new ButtonManager ( scene ) ;
5052
51- // Adds the restart button (invisible but functional for keyboard/gamepad)
52- new Button ( globalConsts . gameWidth / 2 , globalConsts . gameHeight * 0.85 , 1 , "button_play" , scene , ( ) => {
53- window . location . reload ( ) ;
54- } , 'ENTER' , 2 , buttonManager ) . button . setVisible ( false ) ;
55-
56-
5753 // GameOver Image
5854 gameOverImage = this . add . image ( 512 , 250 , 'gameOverTitle' ) ;
5955 gameOverImage . setScale ( 0.2 ) ;
@@ -62,45 +58,70 @@ export class GameOver extends Scene {
6258 leaderboardText = scene . add . text ( 500 , 290 , "" , style ) . setOrigin ( 0 , 0 ) ;
6359
6460 // gets score
65- const item : string | null = localStorage . getItem ( "score" ) ;
61+ const item : string | null = localStorage . getItem ( "last. score" ) ;
6662 score = parseInt ( item ? item : "0" , 10 ) ;
6763
6864 // Game infos
69- new Button ( 130 , 298 , 4 , "button_yourScore" , this . scene . scene , ( ) => {
70- } ) ;
71- this . add . text ( 245 , 290 , formatTime ( score ) , style ) . setOrigin ( 0 , 0 ) ;
65+ new Button ( 70 , 298 , 4 , "button_yourScore" , this . scene . scene ) . button . setOrigin ( 0 , 0.5 ) ;
66+ this . add . text ( 265 , 290 , formatTime ( score ) , style ) . setOrigin ( 0 , 0 ) ;
67+
68+ new Button ( 70 , 338 , 4 , "button_jumpsLeft" , this . scene . scene ) . button . setOrigin ( 0 , 0.5 ) ;
69+ this . add . text ( 265 , 330 , localStorage . getItem ( "last.jumpsLeft" ) ?? "0" , style ) . setOrigin ( 0 , 0 ) ;
7270
7371 // Renders leaderboard
7472 renderLeaderboard ( ) . then ( ) ;
7573
74+
75+ // Adds the restart button
76+ new Button ( globalConsts . gameWidth * 0.8 , globalConsts . gameHeight * 0.85 , 5 , "button_play" , scene , ( ) => exit ( ) , 'ENTER' , 3 , buttonManager ) . button . setVisible ( true ) ;
77+
7678 // Save score button
77- saveButton = new Button ( 700 , 650 , 7 , "button_save" , scene , ( ) => prompt ( ) , 'S' , 0 , buttonManager ) ;
78-
79- // Clicker
80- const blocker : Rectangle = this . add . rectangle ( 0 , 0 , globalConsts . gameWidth , globalConsts . gameHeight , 0x000000 , 0 )
81- . setOrigin ( 0 )
82- . setInteractive ( ) ;
83- blocker . setDepth ( - 1 ) ;
84- blocker . once ( 'pointerdown' , ( ) => {
85- window . location . reload ( ) ;
86- } ) ;
79+ saveButton = new Button ( 600 , 650 , 7 , "button_save" , scene , ( ) => prompt ( ) , 'S' , 1 , buttonManager ) ;
8780
8881 // Add navigation instructions
89- scene . add . text ( globalConsts . gameWidth * 0.67 , globalConsts . gameHeight * 0.92 , "Press S to save" , {
82+ scene . add . text ( globalConsts . gameWidth * 0.67 , globalConsts . gameHeight * 0.92 , "Press S or 1 to save" , {
9083 font : "16px " + globalConsts . pixelFont ,
9184 color : "#ffffff" ,
9285 align : 'center'
9386 } ) . setOrigin ( 0.5 ) ;
9487
95- scene . add . text ( globalConsts . gameWidth * 0.67 , globalConsts . gameHeight * 0.95 , "Press ENTER to restart" , {
88+ scene . add . text ( globalConsts . gameWidth * 0.67 , globalConsts . gameHeight * 0.95 , "Press ENTER or 3 to restart" , {
9689 font : "16px " + globalConsts . pixelFont ,
9790 color : "#ffffff" ,
9891 align : 'center'
9992 } ) . setOrigin ( 0.5 ) ;
93+
94+ // Sets gamepad
95+ if ( this . input . gamepad && this . input . gamepad . gamepads . length > 0 ) {
96+ gamePad = this . input . gamepad . getPad ( 0 ) ;
97+ }
10098 }
99+
100+ update ( ) : void {
101+ // 1 -> Save
102+ if ( get1 ( gamePad ) ) {
103+ prompt ( ) ;
104+ return ;
105+ }
106+
107+ // 3 -> back
108+ if ( get3 ( gamePad ) ) {
109+ exit ( ) ;
110+ return
111+ }
112+ }
113+ }
114+
115+ // Exit to the main Menu
116+ function exit ( ) : void {
117+ window . location . reload ( ) ;
101118}
102119
120+ // Prompt to save
103121function prompt ( ) : void {
122+ // Already saved score | TODO | fix me
123+ if ( savedScore ) return ;
124+
104125 // Check if the leaderboard is loaded
105126 if ( ! leaderboardIsLoaded ) {
106127 alert ( "The leaderboard could not be loaded\nAnd therefore no score can be uploaded" ) ;
@@ -119,7 +140,7 @@ function prompt(): void {
119140 return ;
120141 }
121142
122- /// Neither does xxx
143+ // Neither does xxx
123144 if ( prompt . toLowerCase ( ) == "xxx" ) {
124145 alert ( "xxx doesn't work either" ) ;
125146 return ;
@@ -157,9 +178,9 @@ function prompt(): void {
157178 clearsLeaderboardLine ( ) ;
158179 renderLeaderboard ( ) ;
159180
160- // Resets button
181+ // Disables button
161182 saveButton . setImage ( "button_saved" ) ;
162- saveButton . button . removeListener ( "pointerdown" ) ;
183+ savedScore = true ;
163184 } )
164185}
165186
0 commit comments