-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAudioManager.ts
80 lines (65 loc) · 1.78 KB
/
AudioManager.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import { GAME_STATUS } from "./constats";
const theme = require("./assets/sound/theme.mp3");
const death = require("./assets/sound/death.mp3");
const run = require("./assets/sound/running.mp3");
const SOUNDS = {
death,
theme,
run,
};
class AudioManager {
private theme: HTMLAudioElement;
private run: HTMLAudioElement;
private death: HTMLAudioElement;
private context: AudioContext;
private gain: GainNode;
constructor() {
// for legacy browsers
this.context = new AudioContext();
this.gain = this.context.createGain();
this.gain.gain.value = 0.1;
this.theme = this.addAudioSrc("theme");
this.death = this.addAudioSrc("death");
this.run = this.addAudioSrc("run");
}
private addAudioSrc(name: "death" | "run" | "theme") {
const el = document.createElement("audio");
el.src = SOUNDS[name];
el.setAttribute("preload", "auto");
el.setAttribute("controls", "none");
// this.sound.setAttribute("autoplay", "none");
el.style.display = "none";
document.body.appendChild(el);
const track = this.context.createMediaElementSource(el);
track.connect(this.gain).connect(this.context.destination);
return el;
}
public control({ current: currentState, prev: prevState }) {
if (
currentState.game.status === GAME_STATUS.PLAY &&
currentState.world.x === 10
) {
this.playTheme();
}
if (
currentState.game.status === GAME_STATUS.OVER &&
currentState.game.status !== prevState.game.status
) {
this.stopTheme();
this.playDeath();
}
}
private playDeath() {
this.death.play();
}
private playTheme() {
this.theme.play();
}
private playRun() {
this.run.play();
}
private stopTheme() {
this.theme.pause();
}
}
export default AudioManager;