@jamesrock/rockjs 1.30.0 → 1.32.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +85 -12
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -121,12 +121,13 @@ export const makeOutput = (rows = 7) => {
|
|
|
121
121
|
|
|
122
122
|
export const makeContainer = (className) => makeNode('div', className);
|
|
123
123
|
|
|
124
|
-
export const makeSelect = (options) => {
|
|
124
|
+
export const makeSelect = (options, defaultValue = options[0][1]) => {
|
|
125
125
|
const node = makeNode('select');
|
|
126
126
|
options.forEach(([label, value]) => {
|
|
127
127
|
const option = makeOption(label, value);
|
|
128
128
|
node.appendChild(option);
|
|
129
|
-
})
|
|
129
|
+
});
|
|
130
|
+
node.value = defaultValue;
|
|
130
131
|
return node;
|
|
131
132
|
};
|
|
132
133
|
|
|
@@ -220,6 +221,47 @@ export const makeHexMap = (full = true) => {
|
|
|
220
221
|
return out;
|
|
221
222
|
};
|
|
222
223
|
|
|
224
|
+
export const makeMap = (items, key = 'id') => {
|
|
225
|
+
const out = {};
|
|
226
|
+
items.forEach((item) => {
|
|
227
|
+
out[item[key]] = item;
|
|
228
|
+
});
|
|
229
|
+
return out;
|
|
230
|
+
};
|
|
231
|
+
|
|
232
|
+
export const addDragListeners = (target, tolerance) => {
|
|
233
|
+
|
|
234
|
+
const rounder = new Rounder(tolerance);
|
|
235
|
+
let touch = null;
|
|
236
|
+
|
|
237
|
+
target.addEventListener('touchstart', (e) => {
|
|
238
|
+
|
|
239
|
+
touch = e.touches[0];
|
|
240
|
+
e.preventDefault();
|
|
241
|
+
|
|
242
|
+
});
|
|
243
|
+
|
|
244
|
+
target.addEventListener('touchmove', (e) => {
|
|
245
|
+
|
|
246
|
+
const {clientX: originalClientX, clientY: originalClientY} = touch;
|
|
247
|
+
const {clientX, clientY} = e.touches[0];
|
|
248
|
+
const x = rounder.round(clientX - originalClientX);
|
|
249
|
+
const y = rounder.round(clientY - originalClientY);
|
|
250
|
+
|
|
251
|
+
if(x) {
|
|
252
|
+
touch = e.touches[0];
|
|
253
|
+
target.dispatchEvent(new Event(x > 0 ? 'drag-right' : 'drag-left'));
|
|
254
|
+
};
|
|
255
|
+
|
|
256
|
+
if(y) {
|
|
257
|
+
touch = e.touches[0];
|
|
258
|
+
target.dispatchEvent(new Event(y > 0 ? 'drag-down' : 'drag-up'));
|
|
259
|
+
};
|
|
260
|
+
|
|
261
|
+
});
|
|
262
|
+
|
|
263
|
+
};
|
|
264
|
+
|
|
223
265
|
export class Storage {
|
|
224
266
|
constructor(namespace) {
|
|
225
267
|
|
|
@@ -699,7 +741,7 @@ export class DeckOfPlayingCards {
|
|
|
699
741
|
this.sprite = sprite;
|
|
700
742
|
this.cardMaker = cardMaker;
|
|
701
743
|
this.cards = saved.length ? this.makeFromSaved(saved) : this.make();
|
|
702
|
-
this.map = this.
|
|
744
|
+
this.map = makeMap(this.cards);
|
|
703
745
|
this.shuffledMap = this.makeShuffledMap();
|
|
704
746
|
|
|
705
747
|
};
|
|
@@ -707,15 +749,6 @@ export class DeckOfPlayingCards {
|
|
|
707
749
|
|
|
708
750
|
return shuffle(shuffle(this.makeDeckValues().map(([value, suit]) => this.cardMaker(this, value, suit))));
|
|
709
751
|
|
|
710
|
-
};
|
|
711
|
-
makeMap() {
|
|
712
|
-
|
|
713
|
-
var out = {};
|
|
714
|
-
this.cards.forEach((card) => {
|
|
715
|
-
out[card.id] = card;
|
|
716
|
-
});
|
|
717
|
-
return out;
|
|
718
|
-
|
|
719
752
|
};
|
|
720
753
|
makeShuffledMap() {
|
|
721
754
|
|
|
@@ -833,6 +866,46 @@ export class DeckOfPlayingCards {
|
|
|
833
866
|
};
|
|
834
867
|
};
|
|
835
868
|
|
|
869
|
+
export class SoundManager {
|
|
870
|
+
constructor(sounds) {
|
|
871
|
+
|
|
872
|
+
this.context = new AudioContext();
|
|
873
|
+
this.sounds = sounds;
|
|
874
|
+
this.buffers = {};
|
|
875
|
+
|
|
876
|
+
};
|
|
877
|
+
async load() {
|
|
878
|
+
|
|
879
|
+
return Promise.all(Object.keys(this.sounds).map((key) => this.loadBuffer(key, this.sounds[key]))).then((items) => {
|
|
880
|
+
items.forEach(([name, buffer]) => {
|
|
881
|
+
this.buffers[name] = buffer;
|
|
882
|
+
});
|
|
883
|
+
});
|
|
884
|
+
|
|
885
|
+
};
|
|
886
|
+
async loadBuffer(name, path) {
|
|
887
|
+
|
|
888
|
+
const response = await fetch(path);
|
|
889
|
+
const arrayBuffer = await response.arrayBuffer();
|
|
890
|
+
const audioBuffer = await this.context.decodeAudioData(arrayBuffer);
|
|
891
|
+
return [name, audioBuffer];
|
|
892
|
+
|
|
893
|
+
};
|
|
894
|
+
play(sound = 'point') {
|
|
895
|
+
|
|
896
|
+
if(!this.buffers[sound]) {
|
|
897
|
+
console.log(`SoundManager: '${sound}' not loaded!`);
|
|
898
|
+
return;
|
|
899
|
+
};
|
|
900
|
+
|
|
901
|
+
const source = this.context.createBufferSource();
|
|
902
|
+
source.buffer = this.buffers[sound];
|
|
903
|
+
source.connect(this.context.destination);
|
|
904
|
+
source.start();
|
|
905
|
+
|
|
906
|
+
};
|
|
907
|
+
};
|
|
908
|
+
|
|
836
909
|
// temporary alias
|
|
837
910
|
export const createNode = makeNode;
|
|
838
911
|
export const createSVGNode = makeSVGNode;
|