Skip to content

Commit aa20ddb

Browse files
committed
Added Volume Slider
1 parent 55d305e commit aa20ddb

File tree

8 files changed

+27
-35
lines changed

8 files changed

+27
-35
lines changed

src/app/app.module.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {routes} from "./modules/route/app.routes";
99
import {FormsModule, ReactiveFormsModule} from "@angular/forms";
1010
import {GameComponent} from './components/game/game.component';
1111
import { IntroComponent } from './components/intro/intro.component';
12-
import {LeaderboardService} from "./services/leaderboard.service";
12+
import {GameService} from "./services/game.service";
1313

1414

1515
@NgModule({
@@ -28,7 +28,7 @@ import {LeaderboardService} from "./services/leaderboard.service";
2828
FormsModule
2929

3030
],
31-
providers: [LeaderboardService],
31+
providers: [GameService],
3232
bootstrap: [AppComponent]
3333
})
3434
export class AppModule {

src/app/classes/audio/audio.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import {GameComponent} from "../../components/game/game.component";
2+
import {inject} from "@angular/core";
3+
import {GameService} from "../../services/game.service";
24

35
export class GameAudio {
46

@@ -7,10 +9,10 @@ export class GameAudio {
79

810
private audio: HTMLAudioElement;
911

10-
constructor(name: string, audioSrc: string, loop: boolean, volume: number = GameComponent.volume) {
12+
constructor(name: string, audioSrc: string, loop: boolean, volume?: number) {
1113
this.audio = new Audio(audioSrc);
1214
this.audio.loop = loop;
13-
this.audio.volume = volume;
15+
this.audio.volume = volume ?? localStorage.getItem('volume') ? parseFloat(localStorage.getItem('volume') ?? '0.5') : 0.5;
1416

1517
GameAudio.map.set(name, this);
1618
}
@@ -26,7 +28,7 @@ export class GameAudio {
2628

2729
public play(onEnded?: () => void): void {
2830
const clone = this.audio.cloneNode(true) as HTMLAudioElement; // Create a clone of the audio element
29-
clone.volume = GameComponent.volume;
31+
clone.volume = localStorage.getItem('volume') ? parseFloat(localStorage.getItem('volume') ?? '0.5') : 0.5;
3032
clone.play().then();
3133
clone.addEventListener("ended", function () {
3234
onEnded?.();

src/app/classes/gui/window/title.ts

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,7 @@ export class TitleScreen {
1616
GameComponent.isTitleScreen = false;
1717
GameComponent.player.preventInput = false;
1818
}),
19-
new Button('../../../assets/gui/mobile/down.png', new Position(100, 200), new Scale(0.25), () => {
20-
if (GameComponent.volume - 0.1 <= 0.0) {
21-
GameComponent.volume = 0.0;
22-
return;
23-
}
2419

25-
GameComponent.volume -= 0.1;
26-
}),
27-
new Button('../../../assets/gui/mobile/up.png', new Position(100, 250), new Scale(0.25), () => {
28-
if (GameComponent.volume + 0.1 >= 1.0) {
29-
GameComponent.volume = 1.0;
30-
return;
31-
32-
}
33-
GameComponent.volume += 0.1;
34-
}),
3520
];
3621
TitleScreen.buttons = this.buttons;
3722

src/app/components/game/game.component.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {TitleScreen} from "../../classes/gui/window/title";
88
import {initializeSounds} from "../../classes/audio/audio";
99
import {Mobile} from "../../classes/gui/window/mobile";
1010
import {PotionEffectType} from "../../classes/collectibles/potion/potioneffect";
11-
import {LeaderboardService} from "../../services/leaderboard.service";
11+
import {GameService} from "../../services/game.service";
1212

1313
@Component({
1414
selector: 'app-game',
@@ -21,8 +21,7 @@ export class GameComponent implements AfterViewInit {
2121
public static canvasWidth = 64 * 16;
2222
public static canvasHeight = 64 * 9;
2323
public static player: Player;
24-
public static volume: number = 1.0;
25-
public static isTitleScreen: boolean = false;
24+
public static isTitleScreen: boolean = true;
2625
public static hasInteracted: boolean = false;
2726
public static isFlashLightShaderOn: boolean = true;
2827
public static levels = [level1, level4];
@@ -35,13 +34,12 @@ export class GameComponent implements AfterViewInit {
3534
public cameraContext: CanvasRenderingContext2D | undefined;
3635
public context: CanvasRenderingContext2D | undefined;
3736
public flashLightShader = new FlashlightShader();
38-
public volume: number = localStorage.getItem('volume') ? parseFloat(localStorage.getItem('volume')!) : 1.0;
3937
private oldFrameTime: number = 1;
4038

4139
public static isFinished: boolean = false;
4240

4341

44-
constructor(public readonly leaderboard: LeaderboardService) {
42+
constructor(public readonly leaderboard: GameService) {
4543
GameComponent.player = new Player();
4644
initializeSounds();
4745
this.registerGuiListener();
@@ -197,8 +195,6 @@ export class GameComponent implements AfterViewInit {
197195

198196
}
199197

200-
GameComponent.volume = parseFloat(localStorage.getItem('volume') || '1.0');
201-
localStorage.setItem('volume', this.volume.toString());
202198
this.changeCanvasSize(GameComponent.getCurrentLevel().getBackground().getWidth(), GameComponent.getCurrentLevel().getBackground().getHeight());
203199

204200

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,6 @@
1-
<input [(ngModel)]="leaderboardService.name" placeholder="Enter Name">
1+
<input [(ngModel)]="gameService.name" placeholder="Enter Name">
22
<button (click)="redirect()">Start Playing</button>
3+
<div>
4+
<p>Volume Slider</p>
5+
<input [(ngModel)]="gameService.volume" type="range" min="0" max="1" step="0.01">
6+
</div>

src/app/components/intro/intro.component.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {Component, signal} from '@angular/core';
2-
import {LeaderboardService} from "../../services/leaderboard.service";
2+
import {GameService} from "../../services/game.service";
33
import {Router} from "@angular/router";
44
import {distinctUntilChanged} from "rxjs";
55
import {FormControl} from "@angular/forms";
@@ -15,15 +15,16 @@ export class IntroComponent {
1515
public name: FormControl<string | null> = new FormControl('');
1616

1717

18-
constructor(public readonly leaderboardService: LeaderboardService,
18+
constructor(public readonly gameService: GameService,
1919
public readonly router: Router) {
2020

2121

2222

2323
}
2424

2525
public redirect(): void {
26-
localStorage.setItem('name', this.leaderboardService.name);
26+
localStorage.setItem('name', this.gameService.name);
27+
localStorage.setItem('volume', this.gameService.volume.toString());
2728
this.router.navigate(['/game']).then(r => console.log(r));
2829
}
2930

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,18 @@ import {Subject} from "rxjs";
44
@Injectable({
55
providedIn: 'root'
66
})
7-
export class LeaderboardService {
7+
export class GameService {
88

99
public name: string = 'Anonymous';
1010
public time: number = 0;
11+
public volume: number = 0.5;
1112
constructor() {
1213
if(localStorage.getItem('name')) {
1314
this.name = localStorage.getItem('name') ?? 'Anonymous';
1415
}
16+
if(localStorage.getItem('volume')) {
17+
this.volume = parseFloat(localStorage.getItem('volume') ?? '0.5');
18+
}
1519
}
1620

1721

src/app/services/leaderboard.service.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import { TestBed } from '@angular/core/testing';
22

3-
import { LeaderboardService } from './leaderboard.service';
3+
import { GameService } from './game.service';
44

55
describe('LeaderboardService', () => {
6-
let service: LeaderboardService;
6+
let service: GameService;
77

88
beforeEach(() => {
99
TestBed.configureTestingModule({});
10-
service = TestBed.inject(LeaderboardService);
10+
service = TestBed.inject(GameService);
1111
});
1212

1313
it('should be created', () => {

0 commit comments

Comments
 (0)