@@ -15,6 +15,8 @@ export class GameScene extends Phaser.Scene {
1515 private scoreText ! : Phaser . GameObjects . Text ;
1616 private scoreTimer ! : Phaser . Time . TimerEvent ;
1717
18+ private livesText ! : Phaser . GameObjects . Text ;
19+
1820 private switchy ! : Phaser . GameObjects . Sprite ;
1921
2022 private background ! : Phaser . GameObjects . TileSprite ;
@@ -75,29 +77,40 @@ export class GameScene extends Phaser.Scene {
7577 . setOrigin ( 0 , 0 )
7678 . setScrollFactor ( 0 ) ;
7779
80+ // Set up Switchy (the player character)
81+ this . player = new Player ( this , gameWidth / 2 , gameHeight - 16 , "switchy" ) ;
82+
7883 document . fonts . load ( '24px "Monogram"' ) . then ( ( ) => {
7984 this . scoreText = this . add
80- . text ( 8 , 0 , `${ this . score } ` , {
85+ . text ( 8 , 0 , `SCORE: ${ this . score } ` , {
8186 fontFamily : "Monogram" ,
82- fontSize : "24px " ,
87+ fontSize : "16px " ,
8388 color : "#0f380f" ,
8489 } )
8590 . setOrigin ( 0 , 0 )
8691 . setDepth ( 1000 ) ;
92+
93+ this . livesText = this . add . text (
94+ 8 ,
95+ 12 ,
96+ `LIVES: ${ this . player . getLives ( ) } ` ,
97+ {
98+ fontFamily : "Monogram" ,
99+ fontSize : "16px" ,
100+ color : "#0f380f" ,
101+ }
102+ ) ;
87103 } ) ;
88104
89105 this . scoreTimer = this . time . addEvent ( {
90- delay : 2000 ,
106+ delay : 500 ,
91107 loop : true ,
92108 callback : ( ) => {
93109 this . score ++ ;
94- this . scoreText . setText ( `${ this . score } ` ) ;
110+ this . scoreText . setText ( `SCORE: ${ this . score } ` ) ;
95111 } ,
96112 } ) ;
97113
98- // Set up Switchy (the player character)
99- this . player = new Player ( this , gameWidth / 2 , gameHeight - 16 , "switchy" ) ;
100-
101114 // Collision optimizations
102115 // this.switchy.setCollideWorldBounds(true);
103116 this . physics . world . setBoundsCollision ( true , true , true , true ) ;
@@ -176,13 +189,20 @@ export class GameScene extends Phaser.Scene {
176189 }
177190
178191 handlePlayerHit ( ) {
179- if ( this . gameEnded ) return ;
180- this . scoreTimer . remove ( false ) ;
181- this . physics . pause ( ) ;
182- this . gameEnded = true ;
183- this . playWipeTransition ( ( ) =>
184- this . scene . start ( "GameOverScene" , { score : this . score } )
185- ) ;
192+ if ( this . player . isInvincible ( ) ) return ;
193+
194+ const lives = this . player . getLives ( ) ;
195+ if ( lives <= 1 ) {
196+ this . scoreTimer . remove ( false ) ;
197+ this . physics . pause ( ) ;
198+ this . playWipeTransition ( ( ) =>
199+ this . scene . start ( "GameOverScene" , { score : this . score } )
200+ ) ;
201+ } else {
202+ this . player . depleteLives ( ) ;
203+ this . livesText . setText ( `LIVES: ${ this . player . getLives ( ) } ` ) ;
204+ this . player . startInvincibility ( ) ;
205+ }
186206 }
187207
188208 playWipeTransition ( onComplete : ( ) => void ) {
0 commit comments