Skip to content

Commit

Permalink
refactor(game): improve cleaning game when leaving
Browse files Browse the repository at this point in the history
  • Loading branch information
Mathieu Hermann committed Nov 14, 2024
1 parent 27d7b6e commit 2f0f605
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 17 deletions.
5 changes: 5 additions & 0 deletions src/app/game/game.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,11 @@ export class Game extends Engine {
if (_engine.input.keyboard.wasPressed(Config.KEYBOARD_GHOST_KEY)) {
this.settingsService.setGhosts(!this.settingsService.getSettings().ghosts);
}
}

public stopProperly(): void {
this.stop();
(this.currentScene as Race).cleanRace();
}

private getLoader(): Loader {
Expand Down
24 changes: 12 additions & 12 deletions src/app/game/scenes/race.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,18 @@ export class Race extends Scene {
(this.engine as Game).soundPlayer.playSound(Resources.FinishRaceSound, Config.FINISH_SOUND_VOLUME);
}

public cleanRace(): void {
this.startTime = undefined;
this.endTime = undefined;
this.uiManager.hideUi();
this.gates = [];
(this.engine as Game).soundPlayer.stopSound(Resources.WinterSound);
(this.engine as Game).soundPlayer.stopSound(Resources.TurningSound);
Resources.FinishRaceSound.stop();
this.killActors();
this.clear();
}

public addPenalty(): void {
this.startTime! -= Config.MISSED_GATE_PENALTY_TIME;
(this.engine as Game).soundPlayer.playSound(Resources.GateMissedSound, Config.GATE_MISSED_SOUND_VOLUME);
Expand Down Expand Up @@ -215,18 +227,6 @@ export class Race extends Scene {
(this.engine as Game).soundPlayer.playSound(Resources.WinterSound, Config.RACE_AMBIANCE_SOUND_VOLUME, true);
}

private cleanRace(): void {
this.startTime = undefined;
this.endTime = undefined;
this.uiManager.hideUi();
this.gates = [];
(this.engine as Game).soundPlayer.stopSound(Resources.WinterSound);
(this.engine as Game).soundPlayer.stopSound(Resources.TurningSound);
Resources.FinishRaceSound.stop();
this.killActors();
this.clear();
}

private killActors(): void {
for (const actor of this.actors) {
actor.kill();
Expand Down
7 changes: 2 additions & 5 deletions src/app/pages/race/race.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export class RaceComponent extends Destroyable implements OnInit, OnDestroy{

constructor() {
super();
if (!event) {
if (!this.localEventService.getEvent()) {
this.router.navigate(['/local-event']);
}
}
Expand All @@ -111,13 +111,10 @@ export class RaceComponent extends Destroyable implements OnInit, OnDestroy{
}

public override ngOnDestroy(): void {
this.game?.stop();
this.game = undefined;
this.game?.stopProperly();
}

protected exitRace(): void {
this.game!.stop();
this.game = undefined;
this.router.navigate(['/local-event']);
}

Expand Down

0 comments on commit 2f0f605

Please sign in to comment.