forked from gbtami/pychess-variants
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsound.ts
170 lines (144 loc) · 5.13 KB
/
sound.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
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import { h, VNode } from 'snabbdom';
import { Howl } from 'howler';
import { _ } from './i18n';
import { Variant } from './variants';
import { StringSettings, NumberSettings } from './settings';
import { radioList, slider } from './view';
class Sounds {
private static readonly trackNames = {
GenericNotify: 'GenericNotify',
SocialNotify: 'SocialNotify',
Move: 'Move',
Capture: 'Capture',
Check: 'Check',
Draw: 'Draw',
Victory: 'Victory',
Defeat: 'Defeat',
ShogiMove: 'shogisnap',
ShogiCapture: 'shogislam',
Chat: 'chat',
Setup: 'dinding',
LowTime: 'LowTime',
Tick: 'Tick',
Explosion: 'Explosion',
Berserk: 'Berserk',
};
tracks: { [key: string]: Howl };
constructor() {
this.tracks = {};
}
updateVolume() {
const volume = volumeSettings.value;
Object.keys(this.tracks).forEach(key => {
this.tracks[key].volume(volume);
});
}
updateSoundTheme(assetURL: string) {
Object.keys(Sounds.trackNames).forEach( (key: keyof typeof Sounds.trackNames) => {
this.tracks[key] = this.buildSound(assetURL, Sounds.trackNames[key]);
});
}
private buildSound(assetURL: string, trackName: string) {
const soundTheme = soundThemeSettings.value;
const soundTrack = (soundTheme === 'silent') ? 'Silence' : trackName;
const sound = new Howl({
src: [
assetURL + '/sound/' + soundTheme + '/' + soundTrack + '.ogg',
assetURL + '/sound/' + soundTheme + '/' + soundTrack + '.mp3'
],
onplayerror: function() {
sound.once('unlock', function() {
sound.play();
});
},
volume: volumeSettings.value,
});
return sound;
}
private audio() {
return soundThemeSettings.value !== 'silent';
}
genericNotify() { if (this.audio()) this.tracks.GenericNotify.play(); }
socialNotify() { if (this.audio()) this.tracks.SocialNotify.play(); }
move() { if (this.audio()) this.tracks.Move.play(); }
capture() { if (this.audio()) this.tracks.Capture.play(); }
check() { if (this.audio()) this.tracks.Check.play(); }
draw() { if (this.audio()) this.tracks.Draw.play(); }
victory() { if (this.audio()) this.tracks.Victory.play(); }
defeat() { if (this.audio()) this.tracks.Defeat.play(); }
shogimove() { if (this.audio()) this.tracks.ShogiMove.play(); }
shogicapture() { if (this.audio()) this.tracks.ShogiCapture.play(); }
chat() { if (this.audio()) this.tracks.Chat.play(); }
setup() { if (this.audio()) this.tracks.Setup.play(); }
lowTime() { if (this.audio()) this.tracks.LowTime.play(); }
tick() { if (this.audio()) this.tracks.Tick.play(); }
explosion() { if (this.audio()) this.tracks.Explosion.play(); }
berserk() { if (this.audio()) this.tracks.Berserk.play(); }
private moveSoundSet: {[k:string]: { move: ()=> void; capture: ()=>void;}} = {
regular: { move: () => this.move(), capture: () => this.capture() },
shogi: { move: () => this.shogimove(), capture: () => this.shogicapture() },
atomic: { move: () => this.move(), capture: () => this.explosion() },
};
moveSound(variant: Variant, capture: boolean) {
const soundSet = variant.ui.pieceSound in this.moveSoundSet? this.moveSoundSet[variant.ui.pieceSound] : this.moveSoundSet.regular;
if (capture)
soundSet.capture();
else
soundSet.move();
}
gameEndSound(result: string, color: string) {
switch (result) {
case "1/2-1/2":
this.draw();
break;
case "1-0":
if (color === "white")
this.victory();
else
this.defeat();
break;
case "0-1":
if (color === "black")
this.victory();
else
this.defeat();
break;
}
}
}
class VolumeSettings extends NumberSettings {
constructor() {
super('volume', 1);
}
update(): void {
sound.updateVolume();
}
view(): VNode {
return h('div', slider(this, 'sound-volume', 0, 1, 0.01, _('Volume')));
}
}
const soundThemes = {
silent: "Silent",
standard: "Standard",
piano: "Piano",
nes: "NES",
sfx: "SFX",
futuristic: "Futuristic",
lisp: "Lisp",
robot: "Robot",
};
class SoundThemeSettings extends StringSettings {
assetURL: string;
constructor() {
super('soundtheme', 'standard');
}
update(): void {
sound.updateSoundTheme(this.assetURL);
}
view(): VNode {
return h('div#soundtheme.radio-list', radioList(this, 'soundtheme', soundThemes, (_, key) => this.value = key));
}
}
export const sound = new(Sounds);
export const volumeSettings = new VolumeSettings();
export const soundThemeSettings = new SoundThemeSettings();