Skip to content

Commit d35469b

Browse files
fix game reset
1 parent 1b24727 commit d35469b

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
@@ -114,7 +114,7 @@ class Game {
114114
}
115115

116116
gameOver() {
117-
// Switch to intro screen with game over flag
117+
// Create new intro screen with game over state
118118
this.screens.intro = new IntroScreen(this.ctx, {
119119
virtualWidth: this.virtualWidth,
120120
virtualHeight: this.virtualHeight,
@@ -123,7 +123,7 @@ class Game {
123123
finalScore: this.gameState.score,
124124
highScore: this.gameState.highScore
125125
});
126-
this.currentScreen = 'intro';
126+
this.switchScreen('intro');
127127
}
128128

129129
reset() {
@@ -133,26 +133,29 @@ class Game {
133133
}
134134

135135
switchScreen(screenName) {
136-
// Start music when transitioning from startup to intro
137-
if (this.currentScreen === 'startup' && screenName === 'intro') {
138-
this.musicPlayer.start().then(() => {
139-
this.musicPlayer.fadeIn(3);
140-
});
136+
// Cleanup current screen
137+
if (this.screens[this.currentScreen]?.cleanup) {
138+
this.screens[this.currentScreen].cleanup();
141139
}
142140

143-
// Fade out music when going to game over
144-
if (screenName === 'intro' && this.screens[this.currentScreen]?.isGameOver) {
145-
this.musicPlayer.fadeOut(2);
141+
// Special handling for game screen
142+
if (screenName === 'game') {
143+
// Always create a new game screen instance
144+
this.screens.game = new GameScreen(this.ctx, {
145+
virtualWidth: this.virtualWidth,
146+
virtualHeight: this.virtualHeight,
147+
bgScroller: this.bgScroller,
148+
gameState: this.gameState
149+
});
146150
}
147151

148-
// Cleanup current screen if it exists
149-
if (this.screens[this.currentScreen]?.cleanup) {
150-
this.screens[this.currentScreen].cleanup();
152+
// Handle music transitions
153+
if (this.currentScreen === 'startup' && screenName === 'intro') {
154+
this.musicPlayer.start().then(() => {
155+
this.musicPlayer.fadeIn(3);
156+
});
151157
}
152158

153-
if (screenName === 'game' && !this.screens.game) {
154-
this.initGameScreen();
155-
}
156159
this.currentScreen = screenName;
157160
this.inputManager.setCurrentScreen(screenName);
158161
}

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)