@jamesrock/rockjs 1.7.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 +134 -0
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -178,3 +178,137 @@ export class Rounder {
178
178
  return Math.round(value / this.tolerance) * this.tolerance;
179
179
  };
180
180
  };
181
+
182
+ export class DisplayObject {
183
+ appendTo(to) {
184
+
185
+ to.appendChild(this.node);
186
+ return this;
187
+
188
+ };
189
+ addEventListener(event, handler, passive = true) {
190
+
191
+ this.node.addEventListener(event, handler, {passive});
192
+ return this;
193
+
194
+ };
195
+ dispatchEvent(event) {
196
+
197
+ this.node.dispatchEvent(new Event(event));
198
+ return this;
199
+
200
+ };
201
+ };
202
+
203
+ export class GameBase extends DisplayObject {
204
+ constructor(name) {
205
+
206
+ super();
207
+
208
+ this.node = createNode('div', name);
209
+ this.gameOverNode = createNode('div', 'game-over');
210
+ this.canvas = createNode('canvas', 'game-canvas');
211
+ this.ctx = this.canvas.getContext('2d');
212
+ this.storage = new Storage(`me.jamesrock.${name}`);
213
+
214
+ };
215
+ showGameOverScreen() {
216
+
217
+ const best = this.storage.get('best') || 0;
218
+ this.storage.set('best', this.score > best ? this.score : best);
219
+
220
+ this.gameOverNode.innerHTML = `\
221
+ <div class="game-over-body">\
222
+ <h1>Game over!</h1>\
223
+ <div>\
224
+ <p class="score">${formatNumber(this.score)}</p>\
225
+ <p class="best">Best: ${formatNumber(this.storage.get('best'))}</p>\
226
+ </div>\
227
+ <p class="continue">Tap to continue</p>\
228
+ </div>`;
229
+ this.gameOver = true;
230
+ this.gameOverNode.dataset.active = true;
231
+
232
+ return this;
233
+
234
+ };
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.7.0",
3
+ "version": "1.9.0",
4
4
  "description": "utility bliss",
5
5
  "license": "ISC",
6
6
  "author": "James Rock",