Skip to content

Commit 7ba7fc7

Browse files
committed
Fix: Too many enemies spawned when starting a new game
A bug happened when the game was restarted without a website reload. This is now fixed and a game can be started. And added a Button to get fron the gameOver Sceen to the big leaderboard
1 parent 084dff2 commit 7ba7fc7

File tree

4 files changed

+32
-8
lines changed

4 files changed

+32
-8
lines changed

src/game/main.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,3 +120,10 @@ export function calculateScale(
120120
const ratio = targetDiagonal / refDiagonal;
121121
return parseFloat((refScale * ratio).toFixed(2)); // round to 2 digits
122122
}
123+
124+
// Resets speeds to default
125+
export function resetSpeed(): void {
126+
globalConsts.backgroundSpeed = speed;
127+
globalConsts.houseSpeed = speed;
128+
globalConsts.spriteSpeed = speed;
129+
}

src/game/scenes/GameOver.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,14 @@ export class GameOver extends Scene {
7272
// Renders leaderboard
7373
renderLeaderboard().then();
7474

75+
// Save score button
76+
saveButton = new Button(globalConsts.gameWidth * 0.45, globalConsts.gameHeight * 0.853, calculateScale(8), "button_save", scene, () => prompt(), 'S', 1, buttonManager);
77+
7578
// Adds the restart button
7679
new Button(globalConsts.gameWidth * 0.67, globalConsts.gameHeight * 0.84, calculateScale(5), "button_play", scene, () => exit(), 'ENTER', 3, buttonManager).button.setVisible(true);
7780

78-
// Save score button
79-
saveButton = new Button(globalConsts.gameWidth * 0.45, globalConsts.gameHeight * 0.853, calculateScale(8), "button_save", scene, () => prompt(), 'S', 1, buttonManager);
81+
// To leaderboard menu
82+
new Button(globalConsts.gameWidth * 0.85, globalConsts.gameHeight * 0.84, calculateScale(4), "button_leaderboard", scene, () => this.scene.start("leaderboard"), 'ENTER', 3, buttonManager).button.setVisible(true);
8083

8184
// Add navigation instructions
8285
scene.add.text(globalConsts.gameWidth * 0.67, globalConsts.gameHeight * 0.92, "Press S or 1 to save", {
@@ -114,7 +117,7 @@ export class GameOver extends Scene {
114117

115118
// Exit to the main Menu
116119
function exit(): void {
117-
window.location.reload();
120+
scene.scene.start("mainMenu")
118121
}
119122

120123
// Prompt to save

src/game/thatFolder/ThatGame.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {ThatPlayer} from './ThatPlayer.ts';
22
import {ThatGround} from './ThatGround.ts';
3-
import {displayDebug, globalConsts} from '../main.ts';
3+
import {calculateScale, displayDebug, globalConsts, resetSpeed} from '../main.ts';
44
import {ThatSection} from './ThatSection.ts';
55
import {spawnHouses, updateMovement} from '../custom_classes/Background.ts';
66
import {generateCode} from '../scenes/GameOver.ts';
@@ -40,7 +40,7 @@ export class ThatGame extends Phaser.Scene {
4040
this.player.setScore(0);
4141
this.player.setGifts(0);
4242

43-
// Timer
43+
// Playtime-Timer
4444
this.time.addEvent({
4545
delay: 100,
4646
callback: () => {
@@ -50,7 +50,10 @@ export class ThatGame extends Phaser.Scene {
5050
loop: true
5151
});
5252

53-
//Timer
53+
// Resets speed
54+
resetSpeed();
55+
56+
// Speed-Timer
5457
this.time.addEvent({
5558
delay: 1000,
5659
callback: () => {
@@ -86,14 +89,15 @@ export class ThatGame extends Phaser.Scene {
8689

8790
// Display a note that you can collect gifts when starting the game
8891
const infoText: Text = this.add.text(
89-
this.cameras.main.centerX, globalConsts.gameHeight * 0.25,
92+
globalConsts.gameWidth / 2, globalConsts.gameHeight * 0.25,
9093
"Collect the gifts to double Jump!",
9194
{
9295
font: "22px " + globalConsts.pixelFont,
9396
color: "#ffffff",
9497
fontStyle: "bold"
9598
}
96-
).setOrigin(0.5);
99+
).setOrigin(0.5)
100+
.setScale(calculateScale(1));
97101

98102
// Fade-out
99103
this.tweens.add({
@@ -191,10 +195,16 @@ export class ThatGame extends Phaser.Scene {
191195
section.destroyAll();
192196
}
193197

198+
// clears sections
199+
this.sections = [];
200+
194201
// Saves score
195202
localStorage.setItem("last.score", this.player.score.toString());
196203
localStorage.setItem("last.jumpsLeft", this.player.jumpLefts.toString());
197204

205+
// Stops scene
206+
this.scene.stop(this.scene.key)
207+
198208
// Switch du different scene
199209
this.scene.start("gameOver");
200210

src/game/thatFolder/ThatSection.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,13 @@ export class ThatSection {
122122

123123
// Destroys all obstacles
124124
destroyAll(): void {
125+
this.marker.sprite.destroy(false)
125126
for (let obstacle of this.obstacles) {
126127
obstacle.sprite.body?.destroy();
127128
obstacle.sprite.destroy(true);
128129
}
130+
131+
// Clears array
132+
this.obstacles = [];
129133
}
130134
}

0 commit comments

Comments
 (0)