Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

snek audio - temporarily disabled #692

Merged
merged 1 commit into from
Oct 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/app/features/snek/models/audio/snek-audio.enum.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export enum SnekAudio {
SCORE_SMALL = 'dsitemup',
SCORE_MEDIUM = 'dsgetpow',
GAME_OVER = 'dsnoway',
}
54 changes: 54 additions & 0 deletions src/app/features/snek/services/core/snek-audio.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { Injectable, OnDestroy } from '@angular/core';
import { filter, map } from 'rxjs/operators';
import { SnekAudio } from 'src/app/features/snek/models/audio/snek-audio.enum';
import { SnekStateService } from 'src/app/features/snek/services/core/snek-state.service';
import { SubSink } from 'subsink';

@Injectable()
export class SnekAudioService implements OnDestroy {
private readonly subscriptions = new SubSink();

private readonly soundEffects = this.initializeSoundEffects();

public constructor(
private readonly snekStateService: SnekStateService,
) {
this.initializeScoreEvents();
this.initializeGameOverEvents();
}

public ngOnDestroy(): void {
this.subscriptions.unsubscribe();
}

private initializeSoundEffects(): ReadonlyMap<SnekAudio, HTMLAudioElement> {
const soundEffects = new Map<SnekAudio, HTMLAudioElement>();

console.info('Snek Audio is currently disabled for copyright purposes, until I can produce custom audio tracks for the game.');

// for (const soundEffectName of Object.values(SnekAudio)) {
// const soundEffect = new Audio(`assets/audio/snek/${ soundEffectName }.wav`);
// soundEffects.set(soundEffectName, soundEffect);
// soundEffect.load();
// }

return soundEffects;
}

private initializeScoreEvents(): void {
this.subscriptions.sink = this.snekStateService.score$
.pipe(
filter(score => score !== 0),
map(score => score % 10 === 0 ? SnekAudio.SCORE_MEDIUM : SnekAudio.SCORE_SMALL),
).subscribe(soundEffectName => this.playSoundEffect(soundEffectName));
}

private initializeGameOverEvents(): void {
this.subscriptions.sink = this.snekStateService.gameOver$
.subscribe(() => this.playSoundEffect(SnekAudio.GAME_OVER));
}

private async playSoundEffect(soundEffectName: SnekAudio): Promise<void> {
await this.soundEffects.get(soundEffectName)?.play();
}
}
3 changes: 3 additions & 0 deletions src/app/features/snek/snek.component.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { ChangeDetectionStrategy, Component, inject } from '@angular/core';
import { SnekGridComponent } from 'src/app/features/snek/components/snek-grid/snek-grid.component';
import { SnekOptionsComponent } from 'src/app/features/snek/components/snek-options/snek-options.component';
import { SnekAudioService } from 'src/app/features/snek/services/core/snek-audio.service';
import { SnekDialogService } from 'src/app/features/snek/services/core/snek-dialog.service';
import { SnekResolutionService } from 'src/app/features/snek/services/core/snek-resolution.service';
import { SnekStateService } from 'src/app/features/snek/services/core/snek-state.service';
Expand All @@ -21,6 +22,7 @@ import { SnekStatisticsService } from 'src/app/features/snek/services/peripheral
}` ],
changeDetection: ChangeDetectionStrategy.OnPush,
providers: [
SnekAudioService,
SnekDialogService,
SnekResolutionService,
SnekStateService,
Expand All @@ -32,6 +34,7 @@ import { SnekStatisticsService } from 'src/app/features/snek/services/peripheral
imports: [ SnekGridComponent, SnekOptionsComponent ],
})
export class SnekComponent {
private readonly snekAudioService = inject(SnekAudioService);
private readonly snekDialogService = inject(SnekDialogService);
private readonly snekResolutionService = inject(SnekResolutionService);
private readonly snekStateService = inject(SnekStateService);
Expand Down