-
Notifications
You must be signed in to change notification settings - Fork 0
/
soundmanager.ts
55 lines (50 loc) · 1.56 KB
/
soundmanager.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
import GM from "./gm";
import DefaultScene from "./scenes/default";
export default class SoundManager {
constructor(
private scene: DefaultScene
) {
}
playFromLocation(name: string, x: number, y: number) {
if (this.scene != null) {
var distance = GM.pointDistance(this.scene.playerShip.x, this.scene.playerShip.y, x, y);
var maxDistance = 450.0;
var volume = (distance < maxDistance) ? ((maxDistance - distance) / maxDistance) * 0.3 : 0.0;
this.play(name, volume);
}
}
play(name: string, volume: number = null) {
if (this.scene != null) {
var marker = {
name: 'marker',
start: 0,
config: {
loop: false
}
};
var music = this.scene.sound.add(name)
music.addMarker(marker);
music.play('marker', {
delay: 0.0,
volume: (volume != null) ? volume : 1.0
});
}
}
playLoop(name: string, duration: number, volume: number = null) {
if (this.scene != null) {
var loopMarker = {
name: 'loop',
start: 0,
duration: duration,
config: {
loop: true
}
};
var music = this.scene.sound.add(name);
music.addMarker(loopMarker);
music.play('loop', {
volume: (volume != null) ? volume : 1.0
});
}
}
};