-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsoundEffects.js
More file actions
149 lines (137 loc) · 4.1 KB
/
soundEffects.js
File metadata and controls
149 lines (137 loc) · 4.1 KB
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import {Component, System} from "./engine.js"
export class SoundEffectComponent extends Component {
constructor(type, instruction,data = 1.0) {
super("soundEffect");
this.type = type;
this.instruction = instruction;
this.data = data;
}
}
class SoundEffect {
constructor(url, type, minSize, volume = 1.0) {
this.url = url;
this.type = type;
this.minSize = minSize;
this.volume = volume;
this.pool = [];
this.inUse = new Set();
this.initilize();
this.muted = false;
this.dirty = false;
}
setVolume(value){
this.volume = value;
this.dirty = true;
}
setMute(value){
this.muted = value;
this.dirty = true;
}
initilize(){
for(let i = 0; i < this.minSize*2; i++){
this.create();
}
}
get(){
if(this.pool.length < this.minSize){
for(let i = 0; i < this.minSize; i++){
this.create();
}
}
const soundEffect = this.pool.pop();
this.inUse.add(soundEffect);
soundEffect.volume = this.volume;
soundEffect.muted = this.muted;
soundEffect.type = this.type;
return soundEffect;
}
release(audioElement) {
this.inUse.delete(audioElement);
this.pool.push(audioElement);
}
create() {
const effect = document.createElement("audio");//new Audio();
//document.body.appendChild(effect);
effect.src = this.url;
effect.preload = "auto";
effect.controls = true;
this.pool.push(effect);
return effect;
}
}
export class SoundEffectSystem extends System{
constructor() {
super("soundEffects");
this.effects = new Map();
this.volume = 1.0;
this.muted = false;
this.dirty = false;
}
registerSoundEffect(url, type, minSize = 1, volume = 1.0) {
// Create a new sound effect and add it to the map
this.effects.set(type, new SoundEffect(url, type, minSize, volume));
}
releaseAudioElement(audioElement, type) {
this.effects.get(type).inUse.delete(audioElement);
this.effects.get(type).pool.push(audioElement);
}
playSoundEffect(type) {
const audioElement = this.effects.get(type).get();
audioElement.currentTime = 0;
audioElement.volume *= this.volume;
audioElement.muted = audioElement.muted || this.muted;
audioElement.play();
audioElement.addEventListener('ended', () => {
audioElement.pause();
audioElement.currentTime = 0;
this.effects.get(type).release(audioElement);
audioElement.removeEventListener('ended', this);
});
}
stopSoundEffect(type) {
//Stop all audio elements that are in use for this sound effect
for (const audioElement of this.soundEffectPool.inUse.get(type)) {
audioElement.pause();
audioElement.currentTime = 0;
this.soundEffectPool.release(audioElement);
}
}
update(entities) {
for (const entity of entities) {
let soundEffectComponents = entity.getComponents("soundEffect");
if (soundEffectComponents) {
for (let soundEffectComponent of soundEffectComponents) {
if (soundEffectComponent.instruction === "play") {
this.playSoundEffect(soundEffectComponent.type);
soundEffectComponent.instruction = "noop";
} else if (soundEffectComponent.instruction === "stop") {
this.stopSoundEffect(soundEffectComponent.type);
soundEffectComponent.instruction = "noop";
}else if (soundEffectComponent.instruction === "volume") {
this.setGlobalVolume(soundEffectComponent.type);
soundEffectComponent.instruction = "noop";
}
}
}
}
for(const effect of this.effects.values()){
for(const element of effect.inUse.values()){
if(element.dirty || this.dirty){
element.volume = effect.volume*this.volume;
element.muted = effect.muted || this.muted;
this.dirty = false;
}
}
}
}
setGlobalVolume(volume) {
this.volume = volume;
for(const effect of this.effects){
for (const audioElements of effect.inUse) {
for (const audioElement of audioElements) {
audioElement.volume = volume*audioElements.volume;
}
}
}
}
}