@@ -11,6 +11,10 @@ export class GameScene extends Phaser.Scene {
1111 private obstacles : Obstacle [ ] = [ ] ;
1212 private obstacleTimer : number = 0 ;
1313
14+ private score : number = 0 ;
15+ private scoreText ! : Phaser . GameObjects . Text ;
16+ private scoreTimer ! : Phaser . Time . TimerEvent ;
17+
1418 private switchy ! : Phaser . GameObjects . Sprite ;
1519
1620 private background ! : Phaser . GameObjects . TileSprite ;
@@ -54,6 +58,7 @@ export class GameScene extends Phaser.Scene {
5458
5559 create ( ) {
5660 this . gameEnded = false ;
61+ this . score = 0 ;
5762 // Get current game dimensions
5863 const gameWidth = this . cameras . main . width ;
5964 const gameHeight = this . cameras . main . height ;
@@ -70,6 +75,26 @@ export class GameScene extends Phaser.Scene {
7075 // Load sprites from asset handler
7176 SpriteAssets . createSprites ( this ) ;
7277
78+ document . fonts . load ( '24px "Monogram"' ) . then ( ( ) => {
79+ this . scoreText = this . add
80+ . text ( 8 , 0 , `${ this . score } ` , {
81+ fontFamily : "Monogram" ,
82+ fontSize : "24px" ,
83+ color : "#0f380f" ,
84+ } )
85+ . setOrigin ( 0 , 0 )
86+ . setDepth ( 1000 ) ;
87+ } ) ;
88+
89+ this . scoreTimer = this . time . addEvent ( {
90+ delay : 2000 ,
91+ loop : true ,
92+ callback : ( ) => {
93+ this . score ++ ;
94+ this . scoreText . setText ( `${ this . score } ` ) ;
95+ } ,
96+ } ) ;
97+
7398 // Set up Switchy (the player character)
7499 this . player = new Player ( this , gameWidth / 2 , gameHeight - 16 , "switchy" ) ;
75100
@@ -127,9 +152,12 @@ export class GameScene extends Phaser.Scene {
127152
128153 handlePlayerHit ( ) {
129154 if ( this . gameEnded ) return ;
155+ this . scoreTimer . remove ( false ) ;
130156 this . physics . pause ( ) ;
131157 this . gameEnded = true ;
132- this . playWipeTransition ( ( ) => this . scene . start ( "GameOverScene" ) ) ;
158+ this . playWipeTransition ( ( ) =>
159+ this . scene . start ( "GameOverScene" , { score : this . score } )
160+ ) ;
133161 }
134162
135163 playWipeTransition ( onComplete : ( ) => void ) {
0 commit comments