-
Notifications
You must be signed in to change notification settings - Fork 163
/
Copy pathSounds.js
91 lines (78 loc) · 1.92 KB
/
Sounds.js
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
81
82
83
84
85
86
87
88
89
90
91
import Storage from "./Storage.js";
/**
* Sound Controller
*/
export default class Sounds {
/**
* Sound Controller Constructor
* @constructor
* @param {String} storageName
*/
constructor(storageName) {
this.data = new Storage(storageName, true);
this.mute = !!this.data.get();
this.old = this.mute;
/** @type {HTMLElement} */
this.audio = document.querySelector(".audio");
/** @type {HTMLElement} */
this.waves = document.querySelector(".waves");
/** @type {HTMLElement} */
this.element = document.querySelector(".mute");
this.setDisplay();
}
/**
* Plays a Sound
* @param {String} sound
* @returns {Void}
*/
play(sound) {
if (!this.mute) {
const audio = new Audio(`audio/${sound}.mp3`);
audio.play();
}
}
/**
* Mute/Unmute the sound
* @param {Boolean} mute
* @returns {Void}
*/
toggle(mute) {
this.mute = mute !== undefined ? mute : !this.mute;
this.setDisplay();
this.data.set(this.mute ? 1 : 0);
}
/**
* Used to mute the sound for a short period
* @returns {Void}
*/
startMute() {
this.old = this.mute;
this.toggle(true);
}
/**
* Resets the Mute to the original value
* @returns {Void}
*/
endMute() {
this.toggle(this.old);
}
/**
* Returns true if the sound is off and false if is on
* @returns {Boolean}
*/
isMute() {
return this.mute;
}
/**
* Sets the display of the sound waves
* @returns {Void}
*/
setDisplay() {
if (this.audio && this.waves) {
this.waves.style.display = this.mute ? "none" : "block";
}
if (this.element) {
this.element.innerHTML = this.mute ? "Un<u>m</u>ute" : "<u>M</u>ute";
}
}
}