@openbooknetwork/hesap 0.3.1 → 0.3.2

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@openbooknetwork/hesap",
3
- "version": "0.3.1",
3
+ "version": "0.3.2",
4
4
  "type": "module",
5
5
  "description": "Hesap: the maths engine of the Open Book Network, figures a reader can move with the numbers and formulas that follow them",
6
6
  "keywords": [
package/src/client.css CHANGED
@@ -167,6 +167,9 @@ aside.story .place[data-formula] .katex-display>.katex{text-align:start}
167
167
  .hesap-counters .counters-pin-stripe{fill:var(--z-counters-red,#d1453b)}
168
168
  .hesap-counters .counters-spot{fill:var(--rule)}
169
169
  .hesap-counters .counters-down .counters-pin-body{opacity:.45}
170
+ .hesap-counters .counters-ball{pointer-events:none}
171
+ .hesap-counters .counters-ball-shape{fill:var(--z-counters-blue,#2a6bd1);stroke:var(--ink);stroke-width:2}
172
+ .hesap-counters .counters-ball-hole{fill:var(--ink)}
170
173
  .hesap-counters .counters-pin:focus-visible .counters-hit{stroke:var(--accent);stroke-width:2.5}
171
174
 
172
175
  /* The car park: two rows of spaces, an aisle, and a sign that shows the free spaces. */
@@ -29,7 +29,7 @@ export default function bonds({ host, stage, panel, T, lang, moving, motion: fx
29
29
  // geo the places and parts of the figure as drawn, and nodes the drawn counter of each id. target is the
30
30
  // challenge's draw, and cs the reader's answer to it.
31
31
  let spec = null, s = null, st = null, geo = null, area = { w: 0, h: 0 }, nodes = new Map(), seq = 0, hold = null;
32
- let goal = null, chips = [], target = null, cs = null, blueCtl = null, liftBtn = null, fillBtn = null, cupAt = null, pinned = [];
32
+ let goal = null, chips = [], target = null, cs = null, blueCtl = null, liftBtn = null, fillBtn = null, cupAt = null, pinned = [], rolling = false;
33
33
 
34
34
  const int = n => numbers(lang()).int(n);
35
35
  const label = id => T(`${contextOf(spec)}-${id}`) ?? T(id);
@@ -116,6 +116,8 @@ export default function bonds({ host, stage, panel, T, lang, moving, motion: fx
116
116
  layer.replaceChildren(); nodes = new Map();
117
117
  svg.classList.toggle('counters-tapping', !!spec.tap);
118
118
  cupAt = null; pinned = [];
119
+ const ball = geo.refs.pins?.ball;
120
+ if (ball) place(ball, ball.rest, 0);
119
121
  wire(); render('jump');
120
122
  }
121
123
 
@@ -245,15 +247,52 @@ export default function bonds({ host, stage, panel, T, lang, moving, motion: fx
245
247
  function lower() { if (spec.challenge?.goal === 'hidden-part' && cs.lifted) { cs.lifted = false; liftWords(); } }
246
248
 
247
249
  // A tap on a cell adds a counter of the page's part or takes it away. A tap on a pin knocks it down or stands it up.
248
- // roll knocks down a random number of the pins still standing, at least one, and setUp stands every pin again.
250
+ // roll knocks down some of the pins still standing, at least one, and setUp stands every pin again. Both wait while
251
+ // the ball is rolling.
249
252
  function roll() {
250
253
  const up = s.cells.flatMap((id, i) => id ? [i] : []);
251
- if (!up.length) return;
252
- const d = dice(fresh());
253
- for (const i of d.some(up, d.int(1, up.length))) s.cells[i] = null;
254
+ if (rolling || !up.length) return;
255
+ const d = dice(fresh()), ball = geo?.refs.pins?.ball;
256
+ if (!ball) { for (const i of d.some(up, d.int(1, up.length))) s.cells[i] = null; return changed(); }
257
+ const { hit, aim } = strike(d, up, ball);
258
+ if (moving()) return bowl(ball, hit, aim);
259
+ for (const i of hit) s.cells[i] = null;
254
260
  changed();
255
261
  }
256
- function setUp() { s.cells = s.cells.map(id => id ?? newId('a')); changed(); }
262
+ // strike picks the pins a roll knocks down. The ball runs from rest through a standing pin, and knocks down every
263
+ // standing pin on its way. A pin that falls knocks each standing pin just behind it down half the time.
264
+ function strike(d, up, ball) {
265
+ const all = geo.refs.pins.pins, at = all[d.pick(up)], off = d.int(-8, 8), r = ball.rest;
266
+ const x = y => r.x + (at.x + off - r.x) * (r.y - y) / (r.y - at.y);
267
+ const hit = new Set(up.filter(i => Math.abs(all[i].x - x(all[i].y)) < 28));
268
+ const behind = (i, j) => all[j].y > all[i].y && all[j].y - all[i].y < 60 && Math.abs(all[j].x - all[i].x) < 40;
269
+ for (const i of [...up].sort((a, b) => all[b].y - all[a].y)) if (!hit.has(i) && [...hit].some(j => behind(i, j)) && d.int(0, 1)) hit.add(i);
270
+ // A pin falls away from the ball's path.
271
+ for (const i of hit) all[i].side = all[i].x < x(all[i].y) ? -1 : 1;
272
+ return { hit: [...hit], aim: x(ball.end.y) };
273
+ }
274
+ function setUp() { if (rolling) return; s.cells = s.cells.map(id => id ?? newId('a')); changed(); }
275
+ // bowl rolls the ball up the lane towards the pins it knocks down, and each pin falls as the ball reaches its row.
276
+ // The ball then goes on past the back row, and comes back to rest at the near end.
277
+ function bowl(ball, hit, aim) {
278
+ const all = geo.refs.pins.pins, left = new Set(hit);
279
+ const p = { x: ball.rest.x, y: ball.rest.y, t: 0 };
280
+ const knock = y => {
281
+ const fall = [...left].filter(i => all[i].y >= y - 4);
282
+ if (!fall.length) return;
283
+ for (const i of fall) { s.cells[i] = null; left.delete(i); }
284
+ changed();
285
+ };
286
+ const end = () => { rolling = false; place(ball, ball.rest, 0); };
287
+ rolling = true;
288
+ fx.run(p, { x: aim, y: ball.end.y, t: 1, duration: 900, ease: 'linear', onUpdate: () => { place(ball, p, p.t); knock(p.y); } },
289
+ () => { knock(-Infinity); end(); go.pop(ball.look); }, end);
290
+ }
291
+ // place puts the ball at p, a little smaller and fading out as it goes further down the lane, t from 0 to 1.
292
+ function place(ball, p, t) {
293
+ ball.g.setAttribute('transform', `translate(${p.x} ${p.y}) scale(${1 - 0.3 * t})`);
294
+ ball.g.style.opacity = t > 0.8 ? (1 - t) / 0.2 : 1;
295
+ }
257
296
  function tapCell(i) {
258
297
  if (spec.look === 'pins') { s.cells[i] = s.cells[i] ? null : newId('a'); return changed(); }
259
298
  const colour = spec.tap === 'second' ? 'b' : 'a', id = s.cells[i];
@@ -302,7 +341,7 @@ export default function bonds({ host, stage, panel, T, lang, moving, motion: fx
302
341
  for (const [i, p] of (geo.refs.pins?.pins ?? []).entries()) {
303
342
  const up = !!s.cells[i], was = pinned[i];
304
343
  p.g.classList.toggle('counters-down', !up); p.g.setAttribute('aria-label', T(up ? 'pin-up' : 'pin-down', int(i + 1)));
305
- const a = up ? 0 : 80, draw = () => p.body.setAttribute('transform', `rotate(${p.tilt.a}) scale(${1 - p.tilt.a / 400})`);
344
+ const a = up ? 0 : 80 * (p.side ?? 1), draw = () => p.body.setAttribute('transform', `rotate(${p.tilt.a}) scale(${1 - Math.abs(p.tilt.a) / 400})`);
306
345
  if (was !== undefined && was !== up && anim) { p.tween?.pause(); p.tween = go.tween(p.tilt, { a }, draw, 420, up ? 'outBack' : 'outBounce'); }
307
346
  else { p.tilt.a = a; draw(); }
308
347
  pinned[i] = up;
@@ -68,6 +68,9 @@
68
68
  .hesap-counters .counters-pin-stripe{fill:var(--z-counters-red,#d1453b)}
69
69
  .hesap-counters .counters-spot{fill:var(--rule)}
70
70
  .hesap-counters .counters-down .counters-pin-body{opacity:.45}
71
+ .hesap-counters .counters-ball{pointer-events:none}
72
+ .hesap-counters .counters-ball-shape{fill:var(--z-counters-blue,#2a6bd1);stroke:var(--ink);stroke-width:2}
73
+ .hesap-counters .counters-ball-hole{fill:var(--ink)}
71
74
  .hesap-counters .counters-pin:focus-visible .counters-hit{stroke:var(--accent);stroke-width:2.5}
72
75
 
73
76
  /* The car park: two rows of spaces, an aisle, and a sign that shows the free spaces. */
@@ -99,9 +99,11 @@ export function splits(ways) {
99
99
 
100
100
  // A bowling pin's outline, standing on 0, 0.
101
101
  const PIN = 'M -7 0 C -13 -12 -12 -26 -6 -36 C -4 -40 -5 -44 -5 -48 C -6 -56 -3 -62 0 -62 C 3 -62 6 -56 5 -48 C 5 -44 4 -40 6 -36 C 12 -26 13 -12 7 0 Z';
102
- // pins stand at the end of a lane in rows of 4, 3, 2 and 1. Cell 0 is the head pin at the front.
102
+ const RUN = 84; // The length of lane in front of the head pin that the ball rolls along.
103
+ // pins stand at the end of a lane in rows of 4, 3, 2 and 1, and the ball rests at the near end. Cell 0 is the head pin
104
+ // at the front.
103
105
  export function pins() {
104
- const dx = 58, dy = 46, w = 4 * dx + 56, h = 70 + 3 * dy + 40;
106
+ const dx = 58, dy = 46, w = 4 * dx + 56, h = 70 + 3 * dy + 40 + RUN;
105
107
  return [{ w, h, draw(x, y) {
106
108
  const spots = [], order = [[3, 1], [2, 2], [1, 3], [0, 4]];
107
109
  for (const [row, n] of order) for (let k = 0; k < n; k++) spots.push({ x: x + w / 2 + (k - (n - 1) / 2) * dx, y: y + 70 + row * dy });
@@ -109,10 +111,16 @@ export function pins() {
109
111
  const pinsOf = spots.map((p, i) => {
110
112
  const body = el('g', { class: 'counters-pin-body' }, [el('path', { class: 'counters-pin-shape', d: PIN }), el('rect', { class: 'counters-pin-stripe', x: -5, y: -44, width: 10, height: 3 }), el('rect', { class: 'counters-pin-stripe', x: -5, y: -39, width: 10, height: 3 })]);
111
113
  const g = el('g', { class: 'counters-pin', transform: `translate(${p.x} ${p.y})`, tabindex: 0, role: 'button', 'data-cell': i }, [el('rect', { class: 'counters-hit', x: -27, y: -48, width: 54, height: 54 }), el('ellipse', { class: 'counters-spot', rx: 10, ry: 4 }), body]);
112
- return { g, body, tilt: { a: 0 } };
114
+ return { g, body, tilt: { a: 0 }, x: p.x, y: p.y };
113
115
  });
116
+ // The ball goes behind the pins, so a pin it reaches covers it. It rolls from rest to past the back row.
117
+ // The outer group takes the ball's place, and the inner one takes the pop, whose CSS transform would replace it.
118
+ const look = el('g', {}, [el('circle', { class: 'counters-ball-shape', r: 15 }),
119
+ ...[[-4, -6], [3, -7], [-1, 0]].map(([cx, cy]) => el('circle', { class: 'counters-ball-hole', cx, cy, r: 2.4 }))]);
120
+ const ball = el('g', { class: 'counters-ball' }, [look]);
121
+ const rest = { x: x + w / 2, y: y + h - 26 }, end = { y: y + 30 };
114
122
  // The pins at the back are drawn first, so the front pins stand before them.
115
- return { nodes: [lane, ...[...pinsOf].reverse().map(p => p.g)], places: {}, refs: { pins: pinsOf, lane } };
123
+ return { nodes: [lane, ball, ...[...pinsOf].reverse().map(p => p.g)], places: {}, refs: { pins: pinsOf, lane, ball: { g: ball, look, rest, end } } };
116
124
  } }];
117
125
  }
118
126