-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patholdsoundEffects.js
More file actions
104 lines (93 loc) · 3.46 KB
/
oldsoundEffects.js
File metadata and controls
104 lines (93 loc) · 3.46 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
import {debug, Entity, Component, System, DeleteComponent, TransformComponent, Game} from './engine.js';
import {AudioElementPool} from './music.js';
export class SoundEffectComponent extends Component {
constructor(url, instruction) {
super("soundEffect");
this.url = url;
this.audioElement = null;
this.instruction = instruction;
this.remove = false;
}
cleanUp() {
// Stop the audio element from playing
if(this.remove){
this.shouldProcessBeforeDelete = 0;
}
if(this.audioElement){
//this.audioElement.pause();
this.remove = true;
}
else{
this.shouldProcessBeforeDelete = 0;
}
}
}
export class SoundEffectSystem extends System{
constructor() {
super("old sounde effects")
this.audioElementPool = new AudioElementPool(20);
}
update(entities, dt) {
for (const entity of entities) {
let soundEffects = entity.getComponents("soundEffect");
if(soundEffects){
for (let soundEffect of soundEffects){
if (soundEffect.instruction === "play") {
if(soundEffect.audioElement === null){
soundEffect.audioElement = this.audioElementPool.getAudioElement();
}
if(soundEffect.audioElement.src != soundEffect.url){
soundEffect.audioElement.pause();
soundEffect.audioElement.src = soundEffect.url;
soundEffect.audioElement.preload = 'auto';
soundEffect.audioElement.load();
}
soundEffect.shouldProcessBeforeDelete =123456789;
// if(soundEffect.audioElement.readyState >= 4) {
soundEffect.audioElement.currentTime = 0;
soundEffect.audioElement.play();
soundEffect.instruction = "noop";
// }
}
if (soundEffect.instruction === "loop") {
let audioElement = null;
if(soundEffect.audioElement == null){
soundEffect.audioElement = audioElement = this.audioElementPool.getAudioElement();
audioElement.src = soundEffect.url;
}else{
audioElement = soundEffect.audioElement;
}
audioElement.currentTime = 0;
audioElement.play();
soundEffect.instruction = "noop";
audioElement.isPlaying = true;
// Set processBeforeDeleting to 0
soundEffect.processBeforeDeleting = 64;
}
if (soundEffect.instruction === "preload") {
if(soundEffect.audioElement == null){
soundEffect.audioElement = this.audioElementPool.getAudioElement();
}
if(soundEffect.audioElement.src != soundEffect.url){
soundEffect.audioElement.pause();
soundEffect.audioElement.src = soundEffect.url;
soundEffect.audioElement.preload = 'auto';
soundEffect.audioElement.load();
}else{
soundEffect.audioElement.currentTime = 0;
}
soundEffect.instruction = "noop";
//soundEffect.audioElement.isPlaying = true;
// Set processBeforeDeleting to 0
soundEffect.shouldProcessBeforeDelete =123456789;
}
if (soundEffect.audioElement && soundEffect.remove){
this.audioElementPool.releaseWhenDone(soundEffect.audioElement);
//soundEffect.audioElement = null;
soundEffect.shouldProcessBeforeDelete = 0;
}
}
}
}
}
}