Skip to content

Commit 0458a26

Browse files
2 parents 93df0d8 + d35469b commit 0458a26

File tree

3 files changed

+24
-17
lines changed

3 files changed

+24
-17
lines changed

js/game.js

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ class Game {
151151
}
152152

153153
gameOver() {
154-
// Switch to intro screen with game over flag
154+
// Create new intro screen with game over state
155155
this.screens.intro = new IntroScreen(this.ctx, {
156156
virtualWidth: this.virtualWidth,
157157
virtualHeight: this.virtualHeight,
@@ -160,7 +160,7 @@ class Game {
160160
finalScore: this.gameState.score,
161161
highScore: this.gameState.highScore
162162
});
163-
this.currentScreen = 'intro';
163+
this.switchScreen('intro');
164164
}
165165

166166
reset() {
@@ -170,26 +170,29 @@ class Game {
170170
}
171171

172172
switchScreen(screenName) {
173-
// Start music when transitioning from startup to intro
174-
if (this.currentScreen === 'startup' && screenName === 'intro') {
175-
this.musicPlayer.start().then(() => {
176-
this.musicPlayer.fadeIn(3);
177-
});
173+
// Cleanup current screen
174+
if (this.screens[this.currentScreen]?.cleanup) {
175+
this.screens[this.currentScreen].cleanup();
178176
}
179177

180-
// Fade out music when going to game over
181-
if (screenName === 'intro' && this.screens[this.currentScreen]?.isGameOver) {
182-
this.musicPlayer.fadeOut(2);
178+
// Special handling for game screen
179+
if (screenName === 'game') {
180+
// Always create a new game screen instance
181+
this.screens.game = new GameScreen(this.ctx, {
182+
virtualWidth: this.virtualWidth,
183+
virtualHeight: this.virtualHeight,
184+
bgScroller: this.bgScroller,
185+
gameState: this.gameState
186+
});
183187
}
184188

185-
// Cleanup current screen if it exists
186-
if (this.screens[this.currentScreen]?.cleanup) {
187-
this.screens[this.currentScreen].cleanup();
189+
// Handle music transitions
190+
if (this.currentScreen === 'startup' && screenName === 'intro') {
191+
this.musicPlayer.start().then(() => {
192+
this.musicPlayer.fadeIn(3);
193+
});
188194
}
189195

190-
if (screenName === 'game' && !this.screens.game) {
191-
this.initGameScreen();
192-
}
193196
this.currentScreen = screenName;
194197
this.inputManager.setCurrentScreen(screenName);
195198
}

js/managers/GameStateManager.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ class GameStateManager {
4343
this.playerHit = false;
4444
this.playerInvulnerable = false;
4545
this.invulnerabilityTimer = 0;
46+
// Don't reset highScore as it should persist
4647
}
4748

4849
getInvulnerabilityAlpha() {

js/screens/IntroScreen.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,10 @@ class IntroScreen {
113113

114114
handleInput(key) {
115115
if (key === ' ' && this.pressSpaceVisible) {
116-
return 'game'; // Signal to switch to game screen
116+
// Always reset game state when starting a new game
117+
window.game.gameState.reset();
118+
this.cleanup();
119+
return 'game';
117120
}
118121
return null;
119122
}

0 commit comments

Comments
 (0)