@jamesrock/rockjs 1.8.0 → 1.10.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 +104 -1
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -74,13 +74,37 @@ export const createSelect = (options) => {
|
|
|
74
74
|
return node;
|
|
75
75
|
};
|
|
76
76
|
|
|
77
|
-
const createOption = (label = '{label}', value = 0) => {
|
|
77
|
+
export const createOption = (label = '{label}', value = 0) => {
|
|
78
78
|
const node = createNode('option');
|
|
79
79
|
node.innerText = label;
|
|
80
80
|
node.value = value;
|
|
81
81
|
return node;
|
|
82
82
|
};
|
|
83
83
|
|
|
84
|
+
export const makeBitArray = (size) => {
|
|
85
|
+
let bob = 2**size;
|
|
86
|
+
return makeArray(size, () => {
|
|
87
|
+
bob = bob/2;
|
|
88
|
+
return bob;
|
|
89
|
+
});
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
export const makeHexMap = (full = true) => {
|
|
93
|
+
const out = [];
|
|
94
|
+
const hexMap = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'];
|
|
95
|
+
if(full) {
|
|
96
|
+
makeArray(hexMap.length).forEach((x) => {
|
|
97
|
+
makeArray(hexMap.length).forEach((y) => {
|
|
98
|
+
out.push(`${hexMap[x]}${hexMap[y]}`);
|
|
99
|
+
});
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
else {
|
|
103
|
+
return hexMap;
|
|
104
|
+
};
|
|
105
|
+
return out;
|
|
106
|
+
};
|
|
107
|
+
|
|
84
108
|
export class Storage {
|
|
85
109
|
constructor(namespace) {
|
|
86
110
|
|
|
@@ -233,3 +257,82 @@ export class GameBase extends DisplayObject {
|
|
|
233
257
|
|
|
234
258
|
};
|
|
235
259
|
};
|
|
260
|
+
|
|
261
|
+
export class BrickMaker extends DisplayObject {
|
|
262
|
+
constructor(color, size = 4, type = 'digits') {
|
|
263
|
+
|
|
264
|
+
super();
|
|
265
|
+
|
|
266
|
+
this.color = color;
|
|
267
|
+
this.size = size;
|
|
268
|
+
this.type = type;
|
|
269
|
+
|
|
270
|
+
const node = this.node = createNode('div', 'brick-maker');
|
|
271
|
+
const bob = 30;
|
|
272
|
+
const gap = 1;
|
|
273
|
+
const bits = [];
|
|
274
|
+
const hexMap = size*size > 16 ? makeHexMap() : makeHexMap(false);
|
|
275
|
+
const typeValues = {
|
|
276
|
+
'digits': makeArray(size*size, () => 1),
|
|
277
|
+
'binary': makeBitArray(size)
|
|
278
|
+
};
|
|
279
|
+
|
|
280
|
+
const calculate = () => {
|
|
281
|
+
let total = null;
|
|
282
|
+
switch(this.type) {
|
|
283
|
+
case 'digits':
|
|
284
|
+
total = makeArray(size*size, () => 0);
|
|
285
|
+
bits.forEach((bit, i) => {
|
|
286
|
+
if(bit.dataset.active==='Y') {
|
|
287
|
+
total[i] = Number(bit.dataset.value);
|
|
288
|
+
};
|
|
289
|
+
});
|
|
290
|
+
this.value = JSON.stringify(total);
|
|
291
|
+
break;
|
|
292
|
+
case 'binary':
|
|
293
|
+
total = makeArray(size, () => 0);
|
|
294
|
+
makeArray(size).forEach((y) => {
|
|
295
|
+
bits.filter((bit) => bit.dataset.y == y).forEach((bit) => {
|
|
296
|
+
if(bit.dataset.active==='Y') {
|
|
297
|
+
total[y] += Number(bit.dataset.value);
|
|
298
|
+
};
|
|
299
|
+
});
|
|
300
|
+
total[y] = hexMap[total[y]];
|
|
301
|
+
});
|
|
302
|
+
this.value = `0x${total.join('')}`;
|
|
303
|
+
break;
|
|
304
|
+
};
|
|
305
|
+
this.dispatchEvent('result');
|
|
306
|
+
};
|
|
307
|
+
|
|
308
|
+
node.style.setProperty('--color', this.color);
|
|
309
|
+
|
|
310
|
+
node.style.width = node.style.height = `${bob*size + (gap*(size-1))}px`;
|
|
311
|
+
node.style.gap = `${gap}px`;
|
|
312
|
+
|
|
313
|
+
makeArray(size).forEach((y) => {
|
|
314
|
+
makeArray(size).forEach((x) => {
|
|
315
|
+
|
|
316
|
+
const bit = createNode('div', 'brick-maker-bit');
|
|
317
|
+
let active = false;
|
|
318
|
+
bit.style.width = bit.style.height = `${bob}px`;
|
|
319
|
+
bit.dataset.x = x;
|
|
320
|
+
bit.dataset.y = y;
|
|
321
|
+
bit.dataset.value = typeValues[this.type][x];
|
|
322
|
+
bit.dataset.active = 'N';
|
|
323
|
+
node.appendChild(bit);
|
|
324
|
+
|
|
325
|
+
bits.push(bit);
|
|
326
|
+
|
|
327
|
+
bit.addEventListener('click', () => {
|
|
328
|
+
active = !active;
|
|
329
|
+
bit.dataset.active = active ? 'Y' : 'N';
|
|
330
|
+
calculate();
|
|
331
|
+
});
|
|
332
|
+
|
|
333
|
+
});
|
|
334
|
+
});
|
|
335
|
+
|
|
336
|
+
};
|
|
337
|
+
value = null;
|
|
338
|
+
};
|