@jamesrock/rockjs 1.8.0 → 1.9.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.
Files changed (2) hide show
  1. package/index.js +79 -0
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -233,3 +233,82 @@ export class GameBase extends DisplayObject {
233
233
 
234
234
  };
235
235
  };
236
+
237
+ export class BrickMaker extends DisplayObject {
238
+ constructor(color, size = 4, type = 'digits') {
239
+
240
+ super();
241
+
242
+ this.color = color;
243
+ this.size = size;
244
+ this.type = type;
245
+
246
+ const node = this.node = createNode('div', 'brick-maker');
247
+ const bob = 30;
248
+ const gap = 1;
249
+ const bits = [];
250
+ const hexMap = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'];
251
+
252
+ const calculate = () => {
253
+ let total = null;
254
+ switch(this.type) {
255
+ case 'digits':
256
+ total = makeArray(size*size, () => 0);
257
+ bits.forEach((bit, i) => {
258
+ if(bit.dataset.active==='Y') {
259
+ total[i] = Number(bit.dataset.value);
260
+ };
261
+ });
262
+ this.value = JSON.stringify(total);
263
+ break;
264
+ case 'binary':
265
+ total = makeArray(size, () => 0);
266
+ makeArray(size).forEach((y) => {
267
+ bits.filter((bit) => bit.dataset.y == y).forEach((bit) => {
268
+ if(bit.dataset.active==='Y') {
269
+ total[y] += Number(bit.dataset.value);
270
+ };
271
+ });
272
+ total[y] = hexMap[total[y]];
273
+ });
274
+ this.value = `0x${total.join('')}`;
275
+ break;
276
+ };
277
+ this.dispatchEvent('result');
278
+ };
279
+
280
+ node.style.setProperty('--color', this.color);
281
+
282
+ node.style.width = node.style.height = `${bob*size + (gap*(size-1))}px`;
283
+ node.style.gap = `${gap}px`;
284
+
285
+ makeArray(size).forEach((y) => {
286
+ makeArray(size).forEach((x) => {
287
+
288
+ const bit = createNode('div', 'brick-maker-bit');
289
+ let active = false;
290
+ bit.style.width = bit.style.height = `${bob}px`;
291
+ bit.dataset.x = x;
292
+ bit.dataset.y = y;
293
+ bit.dataset.value = this.typeValues[this.type][x];
294
+ bit.dataset.active = 'N';
295
+ node.appendChild(bit);
296
+
297
+ bits.push(bit);
298
+
299
+ bit.addEventListener('click', () => {
300
+ active = !active;
301
+ bit.dataset.active = active ? 'Y' : 'N';
302
+ calculate();
303
+ });
304
+
305
+ });
306
+ });
307
+
308
+ };
309
+ typeValues = {
310
+ 'digits': makeArray(16, () => 1),
311
+ 'binary': [8, 4, 2, 1]
312
+ };
313
+ value = null;
314
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jamesrock/rockjs",
3
- "version": "1.8.0",
3
+ "version": "1.9.0",
4
4
  "description": "utility bliss",
5
5
  "license": "ISC",
6
6
  "author": "James Rock",