Skip to content

Commit a315b3f

Browse files
committed
add lives, score increases quicker, invincibility frames, difficulty curve is harsher
1 parent 2b91986 commit a315b3f

File tree

3 files changed

+67
-14
lines changed

3 files changed

+67
-14
lines changed

.yarn/install-state.gz

8.57 KB
Binary file not shown.

src/game/Player.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import Phaser from "phaser";
33
export class Player {
44
private scene: Phaser.Scene;
55
private sprite: Phaser.Physics.Arcade.Sprite;
6+
private lives: number = 3;
7+
private invincible: boolean = false;
68
private horizontal: number = 0;
79
private body!: Phaser.Physics.Arcade.Body;
810

@@ -26,6 +28,37 @@ export class Player {
2628
this.sprite.setPosition(newX, this.scene.cameras.main.height - 16);
2729
}
2830

31+
depleteLives() {
32+
this.lives -= 1;
33+
}
34+
35+
getLives() {
36+
return this.lives;
37+
}
38+
39+
isInvincible(): boolean {
40+
return this.invincible;
41+
}
42+
43+
setInvincible(state: boolean) {
44+
this.invincible = state;
45+
}
46+
47+
startInvincibility(duration: number = 1000) {
48+
this.invincible = true;
49+
this.scene.tweens.add({
50+
targets: this.sprite,
51+
alpha: 0.2,
52+
duration: 100,
53+
yoyo: true,
54+
repeat: duration / 100 / 2,
55+
onComplete: () => {
56+
this.invincible = false;
57+
this.sprite.setAlpha(1);
58+
},
59+
});
60+
}
61+
2962
getBody() {
3063
return this.sprite;
3164
}

src/game/scenes/GameScene.ts

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)