@openbooknetwork/hesap 0.3.1 → 0.3.3
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 +1 -1
- package/src/client.css +111 -1
- package/src/families/counters/add-figures.js +66 -0
- package/src/families/counters/add-goals.js +30 -2
- package/src/families/counters/add-model.js +60 -10
- package/src/families/counters/add.js +269 -33
- package/src/families/counters/add.mjs +85 -11
- package/src/families/counters/bar-goals.js +107 -0
- package/src/families/counters/bar-model.js +38 -0
- package/src/families/counters/bar.js +412 -0
- package/src/families/counters/bar.mjs +79 -0
- package/src/families/counters/bonds.js +46 -7
- package/src/families/counters/client.css +102 -0
- package/src/families/counters/client.js +2 -1
- package/src/families/counters/counter.js +13 -0
- package/src/families/counters/facts.js +73 -0
- package/src/families/counters/figures.js +12 -4
- package/src/families/counters/i18n/en.yaml +64 -0
- package/src/families/counters/i18n/tr.yaml +64 -0
- package/src/families/counters/index.mjs +9 -7
- package/src/families/line/i18n/en.yaml +2 -0
- package/src/families/line/i18n/tr.yaml +3 -0
- package/src/families/line/index.mjs +34 -5
- package/src/families/line/line.js +34 -12
- package/src/families/line/model.js +50 -16
- package/src/i18n/en.yaml +67 -0
- package/src/i18n/tr.yaml +68 -0
- package/src/kit/goal.js +8 -1
- package/src/kit/i18n/en.yaml +1 -0
- package/src/kit/i18n/tr.yaml +1 -0
- package/src/kit/kit.css +9 -1
- package/src/kit/written.js +35 -2
|
@@ -0,0 +1,412 @@
|
|
|
1
|
+
// The bar scene: a bar model across the stage, a long bar for the whole split into two parts drawn to scale, the
|
|
2
|
+
// first part red on the left and the second part blue on the right. The whole's number stands above the bar over a
|
|
3
|
+
// bracket, and each part's number in its part. The reader drags the split or the end in whole steps, covers a number
|
|
4
|
+
// with a card, or taps a fact of the family under the bar. The chocolate look breaks a bar of chocolate at the split.
|
|
5
|
+
// A challenge draws a fact or two numbers, and the reader sets the bar that holds them.
|
|
6
|
+
import { opening, counts, lineOf, LINES, measuresOf, readable, labelKeys, factsListed, family as isFamily, WHOLE } from './bar-model.js';
|
|
7
|
+
import { BAR_GOALS, barStart, partsOf, thirdOf, USES } from './bar-goals.js';
|
|
8
|
+
import { counter, put } from './counter.js';
|
|
9
|
+
import { press } from './move.js';
|
|
10
|
+
import { driver } from './driver.js';
|
|
11
|
+
import { factsBlock } from './facts.js';
|
|
12
|
+
import { svg as el, svgPoint } from '../../kit/dom.js';
|
|
13
|
+
import { drag, nudge } from '../../kit/drag.js';
|
|
14
|
+
import { chip, row, check, segment, stepper } from '../../kit/panel.js';
|
|
15
|
+
import { goalRow } from '../../kit/goal.js';
|
|
16
|
+
import { live } from '../../kit/text.js';
|
|
17
|
+
import { numbers } from '../../kit/format.js';
|
|
18
|
+
import { dice, fresh, another } from '../../kit/random.js';
|
|
19
|
+
import { writtenLine, words, reading, SIZE, DASH, FACT_IDS, sameFact, takenBy } from '../../kit/written.js';
|
|
20
|
+
|
|
21
|
+
const PAD = 14; // The figure keeps this far from the stage's edges, in pixels.
|
|
22
|
+
const UNIT_MAX = 48; // A 1 of the bar is at most this long, in pixels.
|
|
23
|
+
const FINGER = 44; // A handle, a number to cover and a fact are at least this big, in pixels.
|
|
24
|
+
const WHOLE_ROOM = 58; // The room of the whole's number and its bracket above the bar.
|
|
25
|
+
const UNDER = 34; // The room under the bar for a number too wide for its part.
|
|
26
|
+
const SPACE = 22; // The gap between the bar and the line or the facts under it.
|
|
27
|
+
const LINE = SIZE + 12; // The room of a written line.
|
|
28
|
+
const BREAK = 8; // The gap between the two pieces of a bar of chocolate.
|
|
29
|
+
const DIGIT = 15; // A digit of a part's number is about this wide.
|
|
30
|
+
const PAINT = { first: 'counters-a', second: 'counters-b' };
|
|
31
|
+
const TWO_UNITS = 14; // The bar of bar-for-two is this many 1s long, as its lengths stand for no count.
|
|
32
|
+
const txt = (cls, attrs = {}) => el('text', { class: cls, ...attrs });
|
|
33
|
+
|
|
34
|
+
export default function bar({ host, stage, panel, T, lang, moving, motion: fx }) {
|
|
35
|
+
const go = driver(fx);
|
|
36
|
+
const back = el('g'), row_ = el('g', { class: 'counters-bar-row' }), front = el('g');
|
|
37
|
+
const svg = el('svg', { class: 'counters-svg' }, [back, row_, front]);
|
|
38
|
+
stage.appendChild(svg);
|
|
39
|
+
// base is the page as written, and spec the page with the bar a challenge gives it. s holds the bar: its whole, its
|
|
40
|
+
// first part, the number under the card, the number the line finds and whether the reader pressed Check. geo holds
|
|
41
|
+
// the figure as drawn, and dots the counters over the bar.
|
|
42
|
+
let base = null, spec = null, s = null, geo = null, area = { w: 0, h: 0 }, target = null, goal = null, chips = [];
|
|
43
|
+
let block = null, line = null, picked = null, solved = false, dots = [], useSeg = null;
|
|
44
|
+
const lit = new Set();
|
|
45
|
+
|
|
46
|
+
const int = n => numbers(lang()).int(n);
|
|
47
|
+
const second = () => s.whole - s.first;
|
|
48
|
+
const drags = h => spec.drag?.includes(h);
|
|
49
|
+
const family = () => isFamily(spec);
|
|
50
|
+
const choc = () => spec.look === 'chocolate';
|
|
51
|
+
const g = () => base.challenge && BAR_GOALS[base.challenge.goal];
|
|
52
|
+
// two says that the page is a bar-for-two challenge, whose bar shows the drawn numbers and a third number.
|
|
53
|
+
const two = () => base.challenge?.goal === 'bar-for-two';
|
|
54
|
+
// The range the end runs over, which the page may narrow.
|
|
55
|
+
const ends = () => spec.end ?? WHOLE;
|
|
56
|
+
// A change of the stage's size during a drag waits for the drag to end, so the figure stays under the finger.
|
|
57
|
+
let held = false, waiting = false;
|
|
58
|
+
const resized = () => { const r = svg.getBoundingClientRect(); if (spec && (Math.round(r.width) !== area.w || Math.round(r.height) !== area.h)) build(); };
|
|
59
|
+
new ResizeObserver(() => { if (held) waiting = true; else resized(); }).observe(svg);
|
|
60
|
+
|
|
61
|
+
// tint gives a line the colours of the parts its numbers count, by the link id of each number. A challenge writes its
|
|
62
|
+
// drawn fact in ink until the goal holds, since the colours would tell which number is the whole.
|
|
63
|
+
const tint = l => {
|
|
64
|
+
const refs = l.refs ?? { a: 'first', b: 'second', c: 'whole' };
|
|
65
|
+
if (g() && !solved) return { ...l, refs, cls: {} };
|
|
66
|
+
return { ...l, refs, cls: Object.fromEntries(Object.entries(refs).filter(([, id]) => PAINT[id]).map(([r, id]) => [r, PAINT[id]])) };
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
// deal sets the bar as the page opens. A challenge draws its target and opens on the goal's bar.
|
|
70
|
+
function deal() {
|
|
71
|
+
const c = base.challenge;
|
|
72
|
+
spec = base; picked = null; solved = false;
|
|
73
|
+
if (c) { target = another(BAR_GOALS[c.goal], c, base, dice(fresh()), target); spec = { ...base, ...BAR_GOALS[c.goal].page(target) }; }
|
|
74
|
+
s = { ...opening(spec), ...(c ? barStart(target) : {}), checked: false };
|
|
75
|
+
s.want = s.first; // The first part the reader set.
|
|
76
|
+
block = family() ? factsBlock({ tint, pick }) : null;
|
|
77
|
+
line = spec.written === true ? Object.assign(writtenLine(), { whole: ['written', 'reading'] }) : null;
|
|
78
|
+
}
|
|
79
|
+
const holds = () => g() ? g().holds({ ...s }, spec, base.challenge, target) : { done: false };
|
|
80
|
+
|
|
81
|
+
// build draws the figure to fit the stage. Every 1 has the same length: the end's farthest place fills the width on
|
|
82
|
+
// a page whose end drags, and the whole on any other page.
|
|
83
|
+
function build() {
|
|
84
|
+
const r = svg.getBoundingClientRect(); area = { w: Math.round(r.width), h: Math.round(r.height) };
|
|
85
|
+
if (!area.w || !area.h) { geo = null; return; } // A phone is showing the text, so the figure is drawn when the stage shows.
|
|
86
|
+
const units = two() ? TWO_UNITS : drags('end') ? ends()[1] : s.whole, endRoom = drags('end') ? FINGER : 0, gap = choc() ? BREAK : 0;
|
|
87
|
+
const u = Math.min(UNIT_MAX, (area.w - 2 * PAD - endRoom - gap) / units);
|
|
88
|
+
const barH = choc() ? Math.max(40, Math.min(u, 56)) : Math.max(44, Math.min(64, u * 1.4));
|
|
89
|
+
const dotR = Math.min(16, u * 0.42), dotRoom = spec.counters ? 2 * dotR + 14 : 0;
|
|
90
|
+
// The block under the bar: the facts in two rows of two as wide as the stage allows, each row tall enough for a
|
|
91
|
+
// finger, or the line.
|
|
92
|
+
let blockAt = null;
|
|
93
|
+
if (block) {
|
|
94
|
+
const room = area.w - 2 * PAD;
|
|
95
|
+
let rowH = LINE, k = 1, way = null;
|
|
96
|
+
for (let i = 0; i < 4; i++) { way = block.ways(rowH)[0]; k = Math.min(1.3, room / way.w); if (rowH * k >= FINGER - 0.5) break; rowH = FINGER / k; }
|
|
97
|
+
blockAt = { k, way: block.ways(rowH)[0], rowH };
|
|
98
|
+
}
|
|
99
|
+
// plan sets the figure out from the top, with its gaps narrowed on a short stage. A stage still too short for it,
|
|
100
|
+
// such as a phone's with the text card open and a hint in the panel, takes the whole figure at a smaller scale.
|
|
101
|
+
const plan = tight => {
|
|
102
|
+
const wholeRoom = tight ? 48 : WHOLE_ROOM, under = (drags('split') ? FINGER : 0) + (tight ? UNDER - 8 : UNDER), space = tight ? 10 : SPACE;
|
|
103
|
+
const top = wholeRoom + dotRoom, figH = top + barH + under, below = block ? space + blockAt.way.h * blockAt.k : line ? space + LINE : 0;
|
|
104
|
+
return { wholeRoom, top, figH, space, pad: tight ? 6 : PAD, H: figH + below };
|
|
105
|
+
};
|
|
106
|
+
let p = plan(false);
|
|
107
|
+
if (p.H > area.h - 2 * p.pad) p = plan(true);
|
|
108
|
+
const f = Math.min(1, (area.h - 2 * p.pad) / p.H), vw = area.w / f, vh = area.h / f;
|
|
109
|
+
svg.setAttribute('viewBox', `${(area.w - vw) / 2} 0 ${vw} ${vh}`);
|
|
110
|
+
const { wholeRoom, top, figH, space } = p;
|
|
111
|
+
const y0 = Math.max(p.pad, (vh - p.H) / 2), x0 = (area.w - (units * u + endRoom + gap)) / 2;
|
|
112
|
+
geo = { u, x0, y0, barTop: y0 + top, barH, dotR, dotY: y0 + wholeRoom + dotR + 4, wholeY: y0 + wholeRoom * 0.31, bracketY: y0 + wholeRoom * 0.69,
|
|
113
|
+
underY: y0 + top + barH + (drags('split') ? FINGER : 0) + (figH - top - barH - (drags('split') ? FINGER : 0)) / 2 + 2, gap,
|
|
114
|
+
// A handle's touch area is a finger wide on the screen at any scale the figure takes, while its knob keeps the
|
|
115
|
+
// figure's scale.
|
|
116
|
+
hit: FINGER / f };
|
|
117
|
+
draw();
|
|
118
|
+
const below = y0 + figH + space;
|
|
119
|
+
if (block) {
|
|
120
|
+
const k = blockAt.k, w = blockAt.way.w * k;
|
|
121
|
+
block.el.setAttribute('transform', `translate(${(area.w - w) / 2} ${below}) scale(${k})`);
|
|
122
|
+
block.at(0, 0, blockAt.way.cols, blockAt.rowH);
|
|
123
|
+
}
|
|
124
|
+
if (line) line.at(area.w / 2, below + LINE / 2 - 6);
|
|
125
|
+
render('jump');
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
// draw makes the nodes of the figure once per build, so a drag moves them and keeps the pointer it captured.
|
|
129
|
+
function draw() {
|
|
130
|
+
const part = k => {
|
|
131
|
+
const rect = el('rect', { class: `counters-bar-part${choc() ? ' counters-choc' : ''}`, 'data-ref': k, rx: choc() ? 4 : 0 }), grooves = el('g', { class: 'counters-choc-grooves' });
|
|
132
|
+
const num = txt('counters-bar-num', { 'data-ref': k }), lead = el('line', { class: 'counters-bar-lead' }), badge = el('circle', { class: 'counters-choc-badge', r: 15 });
|
|
133
|
+
return { k, g: el('g', { class: `counters-bar-piece ${PAINT[k]}` }, [rect, grooves, lead, ...(choc() ? [badge] : []), num]), rect, grooves, num, lead, badge };
|
|
134
|
+
};
|
|
135
|
+
const parts = { first: part('first'), second: part('second') };
|
|
136
|
+
const outline = el('rect', { class: 'counters-bar-outline', 'data-ref': 'whole' }), split = el('line', { class: 'counters-bar-split' });
|
|
137
|
+
const bracket = el('path', { class: 'counters-bracket', 'data-ref': 'whole' }), whole = txt('counters-bar-whole', { 'data-ref': 'whole' });
|
|
138
|
+
back.replaceChildren(bracket, whole, parts.first.g, parts.second.g, ...(choc() ? [] : [outline, split]));
|
|
139
|
+
// A card covers one number. Each number is a button on a cover page, and the card lifts with a tap.
|
|
140
|
+
const taps = Object.fromEntries((spec.cover ? ['whole', 'first', 'second'] : []).map(k => {
|
|
141
|
+
const hit = el('rect', { class: 'counters-bar-tap', role: 'button', tabindex: 0, rx: 8 });
|
|
142
|
+
press(hit, () => coverNumber(k));
|
|
143
|
+
return [k, hit];
|
|
144
|
+
}));
|
|
145
|
+
const card = el('g', { class: 'counters-bar-card', role: 'button', tabindex: 0, display: 'none' }, [el('rect', { class: 'counters-bar-card-box', rx: 8 }), txt('counters-bar-card-q', {})]);
|
|
146
|
+
card.lastChild.textContent = '?';
|
|
147
|
+
press(card, lift);
|
|
148
|
+
const handles = {};
|
|
149
|
+
for (const h of spec.drag ?? []) handles[h] = handle(h);
|
|
150
|
+
front.replaceChildren(...Object.values(taps), card, ...Object.values(handles).map(x => x.g), ...(line ? [line.el] : []), ...(block ? [block.el] : []));
|
|
151
|
+
row_.replaceChildren(); dots = [];
|
|
152
|
+
Object.assign(geo, { parts, outline, split, bracket, whole, taps, card, handles });
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
// handle makes the split's handle under the bar or the end's handle right of it. A drag moves it in whole steps,
|
|
156
|
+
// and a focused handle moves one step on each arrow key.
|
|
157
|
+
function handle(h) {
|
|
158
|
+
const hit = el('rect', { class: 'counters-handle-hit' });
|
|
159
|
+
const knob = h === 'split' ? el('path', { class: 'counters-knob' }) : el('rect', { class: 'counters-knob', rx: 6 });
|
|
160
|
+
const grip = el('path', { class: 'counters-grip' });
|
|
161
|
+
const node = el('g', { class: 'counters-handle', role: 'slider', tabindex: 0, 'aria-orientation': 'horizontal', 'data-ref': h }, [hit, knob, grip]);
|
|
162
|
+
let dx = 0;
|
|
163
|
+
const at = p => Math.round((p.x + dx - geo.x0) / geo.u);
|
|
164
|
+
drag(node, {
|
|
165
|
+
point: e => svgPoint(svg, e),
|
|
166
|
+
start(p) { held = true; dx = (h === 'split' ? splitX() : endX()) - p.x; },
|
|
167
|
+
move(p) { step(h, at(p)); },
|
|
168
|
+
drop() { held = false; render('jump'); if (waiting) { waiting = false; resized(); } },
|
|
169
|
+
});
|
|
170
|
+
nudge(node, { step: (x, y) => step(h, (h === 'split' ? s.first : s.whole) + (x || -y)) });
|
|
171
|
+
return { g: node, hit, knob, grip };
|
|
172
|
+
}
|
|
173
|
+
// step moves the split to a first part of n, or the end to a whole of n. The end pushes the split along, so each
|
|
174
|
+
// part keeps 1 at least, and the split goes back to where the reader set it as the end grows again. A move takes
|
|
175
|
+
// back a check and lets a picked fact go.
|
|
176
|
+
function step(h, n) {
|
|
177
|
+
const was = [s.whole, s.first];
|
|
178
|
+
if (h === 'split') s.want = s.first = Math.max(1, Math.min(s.whole - 1, n));
|
|
179
|
+
else { s.whole = Math.max(ends()[0], Math.min(ends()[1], n)); s.first = Math.min(s.want, s.whole - 1); }
|
|
180
|
+
if (was[0] === s.whole && was[1] === s.first) return;
|
|
181
|
+
picked = null; s.checked = false;
|
|
182
|
+
render('move');
|
|
183
|
+
}
|
|
184
|
+
const splitX = () => geo.x0 + s.first * geo.u;
|
|
185
|
+
const endX = () => geo.x0 + s.whole * geo.u + geo.gap;
|
|
186
|
+
// shape gives the numbers of the bar, the number that carries a question mark, and the length of each part. The
|
|
187
|
+
// bar of bar-for-two holds the two drawn numbers where the segment puts them and the third number with a question
|
|
188
|
+
// mark until the goal holds. As two parts, the known parts are to scale under the whole. As the whole and a part,
|
|
189
|
+
// the bar is a sketch with the known part and the missing part half and half, since a missing part to scale would
|
|
190
|
+
// give its number by eye. Once the goal holds the reader's family is drawn to scale.
|
|
191
|
+
function shape() {
|
|
192
|
+
if (!two()) return { n: { whole: s.whole, first: s.first, second: second() }, unknown: null, len: { first: s.first * geo.u, second: second() * geo.u } };
|
|
193
|
+
const n = partsOf(target, s.use, s.third), w = TWO_UNITS * geo.u, unknown = solved ? null : s.use === 'parts' ? 'whole' : 'second';
|
|
194
|
+
const real = partsOf(target, s.use, thirdOf(target, s.use)), share = unknown === 'second' ? 0.5 : real.first / (real.first + real.second);
|
|
195
|
+
return { n, unknown, len: { first: w * share, second: w * (1 - share) } };
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
// coverNumber puts the card on a number of the bar, and the line turns into the calculation that finds it. lift
|
|
199
|
+
// takes the card off, and the line takes its answer. A key that covered a number moves on to the card.
|
|
200
|
+
function coverNumber(k) {
|
|
201
|
+
const keys = document.activeElement?.classList?.contains('counters-bar-tap');
|
|
202
|
+
s.cover = k; s.found = k;
|
|
203
|
+
render('move');
|
|
204
|
+
if (keys) geo?.card.focus();
|
|
205
|
+
}
|
|
206
|
+
function lift() {
|
|
207
|
+
const keys = document.activeElement === geo?.card, k = s.cover;
|
|
208
|
+
s.cover = null;
|
|
209
|
+
render('move');
|
|
210
|
+
if (keys && k) geo.taps[k]?.focus();
|
|
211
|
+
}
|
|
212
|
+
// pick picks a fact, or lets it go when it is picked already. A challenge's facts take taps once the goal holds.
|
|
213
|
+
function pick(id) {
|
|
214
|
+
if (g() && !solved) return;
|
|
215
|
+
picked = picked === id ? null : id;
|
|
216
|
+
if (geo) render('jump');
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
// render puts the bar, its numbers, the counters, the handles and the card where s says, and writes the lines.
|
|
220
|
+
function render(how) {
|
|
221
|
+
const r = holds();
|
|
222
|
+
solved = r.done;
|
|
223
|
+
// A third number that makes the family in the other use solves the goal too, and the segment moves to that use.
|
|
224
|
+
if (r.use && r.use !== s.use) { s.use = r.use; useSeg?.set(r.use); }
|
|
225
|
+
if (!geo) return refresh();
|
|
226
|
+
const anim = how === 'move' && moving(), { u, x0, barTop, barH } = geo, { n, unknown, len } = shape();
|
|
227
|
+
const sx = x0 + len.first, ex = sx + geo.gap + len.second;
|
|
228
|
+
const span = { first: [x0, sx], second: [sx + geo.gap, ex] };
|
|
229
|
+
// A number with a question mark shows the mark, and its part or its bracket is dashed.
|
|
230
|
+
const label = k => s.cover === k ? '' : unknown === k ? '?' : int(n[k]);
|
|
231
|
+
for (const k of ['first', 'second']) geo.parts[k].g.classList.toggle('counters-bar-unknown', unknown === k);
|
|
232
|
+
geo.bracket.classList.toggle('counters-bracket-unknown', unknown === 'whole');
|
|
233
|
+
// The bracket and the whole above the bar.
|
|
234
|
+
const by = geo.bracketY;
|
|
235
|
+
geo.bracket.setAttribute('d', `M ${x0} ${by + 8} V ${by} H ${ex} V ${by + 8} M ${(x0 + ex) / 2} ${by} V ${by - 6}`);
|
|
236
|
+
place(geo.whole, (x0 + ex) / 2, geo.wholeY, label('whole'));
|
|
237
|
+
// Each part: its rect, the grooves of a piece of chocolate, and its number inside it or under it with a line.
|
|
238
|
+
const spots = { whole: { x: (x0 + ex) / 2, y: geo.wholeY } }, room = {}, mid = {};
|
|
239
|
+
for (const k of ['first', 'second']) {
|
|
240
|
+
const [a, b] = span[k], w = b - a;
|
|
241
|
+
room[k] = Math.max(FINGER, String(n[k]).length * DIGIT + 10); mid[k] = (a + b) / 2;
|
|
242
|
+
spots[k] = { x: mid[k], y: w >= String(n[k]).length * DIGIT + 10 ? barTop + barH / 2 : geo.underY, under: w < String(n[k]).length * DIGIT + 10 };
|
|
243
|
+
}
|
|
244
|
+
// Two numbers under the bar that would run together move apart, the first to the left and the second to the right.
|
|
245
|
+
const over = (room.first + room.second) / 2 - (spots.second.x - spots.first.x);
|
|
246
|
+
if (spots.first.under && spots.second.under && over > 0) { spots.first.x -= over / 2; spots.second.x += over / 2; }
|
|
247
|
+
for (const k of ['first', 'second']) {
|
|
248
|
+
const p = geo.parts[k], [a, b] = span[k], spot = spots[k];
|
|
249
|
+
for (const [attr, v] of Object.entries({ x: a, y: barTop, width: b - a, height: barH })) p.rect.setAttribute(attr, v);
|
|
250
|
+
if (choc()) p.grooves.replaceChildren(...Array.from({ length: n[k] - 1 }, (_, i) => el('line', { x1: a + (i + 1) * u, y1: barTop + 3, x2: a + (i + 1) * u, y2: barTop + barH - 3 })));
|
|
251
|
+
place(p.num, spot.x, spot.y, label(k));
|
|
252
|
+
p.lead.setAttribute('display', spot.under ? 'inline' : 'none');
|
|
253
|
+
for (const [attr, v] of Object.entries({ x1: mid[k], y1: barTop + barH, x2: spot.x, y2: spot.y - 12 })) p.lead.setAttribute(attr, v);
|
|
254
|
+
if (choc()) { p.badge.setAttribute('cx', spot.x); p.badge.setAttribute('cy', spot.y); p.badge.setAttribute('display', s.cover === k ? 'none' : 'inline'); }
|
|
255
|
+
}
|
|
256
|
+
if (!choc()) {
|
|
257
|
+
// The outline goes round the known part alone while the second part is a sketch, so its dashes show.
|
|
258
|
+
for (const [attr, v] of Object.entries({ x: x0, y: barTop, width: (unknown === 'second' ? sx : ex) - x0, height: barH })) geo.outline.setAttribute(attr, v);
|
|
259
|
+
for (const [attr, v] of Object.entries({ x1: sx, y1: barTop - 6, x2: sx, y2: barTop + barH + 6 })) geo.split.setAttribute(attr, v);
|
|
260
|
+
}
|
|
261
|
+
// The places a tap covers, and the card on the covered number.
|
|
262
|
+
for (const [k, hit] of Object.entries(geo.taps)) {
|
|
263
|
+
const w = Math.max(FINGER, (k === 'whole' ? 2 : String(n[k]).length) * DIGIT + 16), sp = spots[k];
|
|
264
|
+
for (const [attr, v] of Object.entries({ x: sp.x - w / 2, y: sp.y - FINGER / 2, width: w, height: FINGER })) hit.setAttribute(attr, v);
|
|
265
|
+
hit.setAttribute('aria-label', T(`bar-cover-${k}`));
|
|
266
|
+
}
|
|
267
|
+
geo.card.setAttribute('display', s.cover ? 'inline' : 'none');
|
|
268
|
+
if (s.cover) {
|
|
269
|
+
const sp = spots[s.cover], box = geo.card.firstChild, q = geo.card.lastChild;
|
|
270
|
+
for (const [attr, v] of Object.entries({ x: sp.x - 20, y: sp.y - 20, width: 40, height: 40 })) box.setAttribute(attr, v);
|
|
271
|
+
q.setAttribute('x', sp.x); q.setAttribute('y', sp.y);
|
|
272
|
+
geo.card.setAttribute('aria-label', T('bar-lift'));
|
|
273
|
+
}
|
|
274
|
+
// The handles: the split's under the bar, the end's right of it.
|
|
275
|
+
const hs = geo.handles;
|
|
276
|
+
if (hs.split) {
|
|
277
|
+
const y = barTop + barH, sx = splitX() + geo.gap / 2; // On the chocolate the handle stands under the break.
|
|
278
|
+
for (const [attr, v] of Object.entries({ x: sx - geo.hit / 2, y, width: geo.hit, height: geo.hit })) hs.split.hit.setAttribute(attr, v);
|
|
279
|
+
hs.split.knob.setAttribute('d', `M ${sx} ${y + 4} L ${sx + 12} ${y + 20} V ${y + 34} H ${sx - 12} V ${y + 20} Z`);
|
|
280
|
+
hs.split.grip.setAttribute('d', `M ${sx - 4} ${y + 22} V ${y + 30} M ${sx + 4} ${y + 22} V ${y + 30}`);
|
|
281
|
+
aria(hs.split.g, T('bar-split'), 1, s.whole - 1, s.first);
|
|
282
|
+
}
|
|
283
|
+
if (hs.end) {
|
|
284
|
+
const y = barTop + barH / 2 - Math.max(geo.hit, barH) / 2;
|
|
285
|
+
for (const [attr, v] of Object.entries({ x: ex, y, width: geo.hit, height: Math.max(geo.hit, barH) })) hs.end.hit.setAttribute(attr, v);
|
|
286
|
+
for (const [attr, v] of Object.entries({ x: ex + 6, y: barTop + 4, width: 16, height: barH - 8 })) hs.end.knob.setAttribute(attr, v);
|
|
287
|
+
hs.end.grip.setAttribute('d', `M ${ex + 11} ${barTop + barH / 2 - 7} V ${barTop + barH / 2 + 7} M ${ex + 17} ${barTop + barH / 2 - 7} V ${barTop + barH / 2 + 7}`);
|
|
288
|
+
aria(hs.end.g, T('bar-end'), ends()[0], ends()[1], s.whole);
|
|
289
|
+
}
|
|
290
|
+
if (spec.counters) countersRow(anim);
|
|
291
|
+
// The written line and the facts.
|
|
292
|
+
if (line && line.set(tint(lineOf(spec, s)), int) && anim) go.flash(line.el);
|
|
293
|
+
if (block) {
|
|
294
|
+
const f = g() ? g().facts(target, s, solved) : { first: s.first, second: second(), listed: factsListed(spec) };
|
|
295
|
+
for (const changed of block.set({ ...f, int, T, picked })) if (anim) go.flash(changed);
|
|
296
|
+
}
|
|
297
|
+
light(); refresh();
|
|
298
|
+
}
|
|
299
|
+
const place = (t, x, y, words) => { t.setAttribute('x', x); t.setAttribute('y', y); if (t.textContent !== words) t.textContent = words; };
|
|
300
|
+
const aria = (node, label, lo, hi, v) => { node.setAttribute('aria-label', label); node.setAttribute('aria-valuemin', lo); node.setAttribute('aria-valuemax', hi); node.setAttribute('aria-valuenow', v); node.setAttribute('aria-valuetext', int(v)); };
|
|
301
|
+
|
|
302
|
+
// countersRow keeps one counter over each 1 of the bar: round red counters over the first part and square blue
|
|
303
|
+
// counters over the second. A counter that joins the row pops in.
|
|
304
|
+
function countersRow(anim) {
|
|
305
|
+
const kinds = Array.from({ length: s.whole }, (_, i) => i < s.first ? 'disc' : 'square');
|
|
306
|
+
dots = dots.filter((d, i) => { const keep = i < kinds.length && d.kind === kinds[i]; if (!keep) d.n.g.remove(); return keep; });
|
|
307
|
+
for (let i = dots.length; i < kinds.length; i++) {
|
|
308
|
+
const kind = kinds[i], n = counter(kind, kind === 'disc' ? 'a' : 'b', geo.dotR);
|
|
309
|
+
row_.append(n.g); dots.push({ kind, n, part: kind === 'disc' ? 'first' : 'second' });
|
|
310
|
+
if (anim) go.pop(n.pop);
|
|
311
|
+
}
|
|
312
|
+
dots.forEach((d, i) => put(d.n, { x: geo.x0 + (i + 0.5) * geo.u, y: geo.dotY }));
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
// shown gives the fact the figure shows: the fact of a link the reader points at, else the fact picked.
|
|
316
|
+
function shown() {
|
|
317
|
+
if (!family() || (g() && !solved)) return null;
|
|
318
|
+
const pointed = [...lit].filter(id => FACT_IDS.includes(id)).at(-1);
|
|
319
|
+
const p = two() ? partsOf(target, s.use, s.third) : { first: s.first, second: second() };
|
|
320
|
+
return pointed ? sameFact(pointed, p.first, p.second) : picked;
|
|
321
|
+
}
|
|
322
|
+
// light marks what each link points at, and shows a fact on the bar: an addition lights both parts and the whole,
|
|
323
|
+
// and a subtraction draws the part it takes away pale with a dashed outline and lights the part left.
|
|
324
|
+
function light() {
|
|
325
|
+
if (!geo) return;
|
|
326
|
+
const f = shown(), away = f && takenBy(f), on = new Set(lit);
|
|
327
|
+
if (f) for (const id of away ? [away === 'first' ? 'second' : 'first'] : ['first', 'second', 'whole']) on.add(id);
|
|
328
|
+
for (const node of back.querySelectorAll('[data-ref]')) node.classList.toggle('counters-hot', on.has(node.dataset.ref));
|
|
329
|
+
for (const k of ['first', 'second']) geo.parts[k].g.classList.toggle('counters-bar-gone', away === k);
|
|
330
|
+
for (const d of dots) d.n.g.classList.toggle('counters-lit', on.has(d.part) || on.has('whole'));
|
|
331
|
+
for (const [h, x] of Object.entries(geo.handles)) x.g.classList.toggle('counters-hot', lit.has(h));
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
// refresh writes every value that follows the bar, in the readouts and the text, and judges a challenge.
|
|
335
|
+
function refresh() {
|
|
336
|
+
const ids = measuresOf(spec), c = counts(spec, s);
|
|
337
|
+
const value = id => {
|
|
338
|
+
if (id in LINES) { const l = LINES[id](spec, s); return !l ? DASH : id.startsWith('written') ? words(l, int) : reading(l, int, T); }
|
|
339
|
+
return c[id] == null ? DASH : int(c[id]);
|
|
340
|
+
};
|
|
341
|
+
for (const k of chips) if (k.set(value(k.id)) && moving()) go.flash(k.el);
|
|
342
|
+
live(host, { value: id => ids.includes(id) ? value(id) : null });
|
|
343
|
+
judge();
|
|
344
|
+
}
|
|
345
|
+
// vars gives a challenge's words: its drawn numbers and the reader's bar.
|
|
346
|
+
const vars = () => g().words(base.challenge, target, s, { int, written: l => words(l, int) });
|
|
347
|
+
function judge() {
|
|
348
|
+
if (!goal) return;
|
|
349
|
+
const c = base.challenge, r = holds(), v = vars();
|
|
350
|
+
goal.say(T(r.done ? `goal-${c.goal}-done` : `goal-${c.goal}`, v));
|
|
351
|
+
// A goal reached closes its hint, which gives its room back to the facts that show.
|
|
352
|
+
if (goal.show(r.done, r.note ? T(`note-${c.goal}-${r.note}`, v) : undefined) && goal.hinted()) goal.closeHint();
|
|
353
|
+
}
|
|
354
|
+
const hint = () => T(`hint-${base.challenge.goal}${g().hint ? `-${g().hint(target, s)}` : ''}`, vars());
|
|
355
|
+
// commit presses Check. Once the goal holds the facts fill their places and the bar's parts swell.
|
|
356
|
+
function commit() {
|
|
357
|
+
if (s.checked) return;
|
|
358
|
+
s.checked = true;
|
|
359
|
+
render('move');
|
|
360
|
+
if (solved && moving()) go.cheer([geo?.parts.first.g, geo?.parts.second.g].filter(Boolean));
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
// setTwo keeps the use or the third number of bar-for-two, and takes back a check.
|
|
364
|
+
function setTwo(k, v) {
|
|
365
|
+
s[k] = v; s.checked = false; picked = null;
|
|
366
|
+
render('move');
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
// name gives a readout or a handle its name: on the chocolate "Squares" or "First piece".
|
|
370
|
+
const name = id => labelKeys(spec, id).map(k => T(k)).find(Boolean) ?? (id.startsWith('written') ? T('written') : T('written-reading'));
|
|
371
|
+
// The panel holds a challenge's line and Check, and the readouts.
|
|
372
|
+
function drawPanel() {
|
|
373
|
+
const c = base.challenge, ctls = [];
|
|
374
|
+
goal = null;
|
|
375
|
+
// bar-for-two sets the use of its two numbers with a segment and the third number with a stepper.
|
|
376
|
+
useSeg = null;
|
|
377
|
+
if (spec.useCtl) ctls.push(useSeg = segment({ id: 'use', label: T('bar-use'), items: USES.map(u => [u, T(`bar-use-${u}`)]), value: s.use, pick: v => setTwo('use', v) }));
|
|
378
|
+
if (spec.thirdCtl) ctls.push(stepper({ id: 'third', label: T('bar-third'), min: spec.thirdCtl.lo, max: spec.thirdCtl.hi, value: s.third, write: int, change: v => setTwo('third', v) }));
|
|
379
|
+
if (c) {
|
|
380
|
+
goal = goalRow({ T, motion: moving() ? fx : null, hint: on => on ? hint() : null,
|
|
381
|
+
another: () => { deal(); drawPanel(); build(); if (!geo) refresh(); } });
|
|
382
|
+
ctls.push(check({ T, press: commit }));
|
|
383
|
+
}
|
|
384
|
+
const wide = id => id in LINES;
|
|
385
|
+
chips = (spec.readout ?? []).filter(id => readable(spec).includes(id)).map(id => chip({ id, label: name(id), cls: wide(id) ? 'counters-chip-wide' : '' }));
|
|
386
|
+
for (const k of chips) if (!wide(k.id)) k.width(2);
|
|
387
|
+
panel.replaceChildren(...[goal?.el, ctls.length ? row('ctls', ctls) : null, chips.length ? row('out', chips) : null].filter(Boolean));
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
function point(id, on) {
|
|
391
|
+
if (!spec) return;
|
|
392
|
+
if (on) lit.add(id); else lit.delete(id);
|
|
393
|
+
line?.light(id, on, line.whole);
|
|
394
|
+
block?.light(id, on);
|
|
395
|
+
light();
|
|
396
|
+
for (const part of panel.querySelectorAll('[data-ref]')) part.classList.toggle('hot', on && part.dataset.ref === id);
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
return {
|
|
400
|
+
show(page) {
|
|
401
|
+
base = page; target = null; lit.clear(); deal();
|
|
402
|
+
drawPanel(); build();
|
|
403
|
+
if (!geo) render('jump');
|
|
404
|
+
},
|
|
405
|
+
hide() {
|
|
406
|
+
spec = null; base = null; goal = null; geo = null; block = null; line = null; dots = [];
|
|
407
|
+
for (const n of [back, row_, front]) n.replaceChildren();
|
|
408
|
+
},
|
|
409
|
+
point,
|
|
410
|
+
lang() { if (!spec) return; const was = goal; drawPanel(); goal?.keep(was); build(); if (!geo) render('jump'); },
|
|
411
|
+
};
|
|
412
|
+
}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
// The Node half of the bar scene: a bar model whose whole is split into two parts drawn to scale, with the written
|
|
2
|
+
// line or the facts of its family under it. It checks a page's fields and says what each link shows when the page
|
|
3
|
+
// opens.
|
|
4
|
+
import { schema } from '@openbooknetwork/kitap';
|
|
5
|
+
import { BAR_LOOKS, WHOLE, HANDLES, NUMBERS, opening, LINES, counts, measuresOf, readable, family } from './bar-model.js';
|
|
6
|
+
import { BAR_GOALS, barStart } from './bar-goals.js';
|
|
7
|
+
import { words, reading, FACTS } from '../../kit/written.js';
|
|
8
|
+
import { numbers } from '../../kit/format.js';
|
|
9
|
+
import { list, string, challenge, drawn } from '../../node.mjs';
|
|
10
|
+
|
|
11
|
+
const { z, check } = schema;
|
|
12
|
+
const Int = z.number().int();
|
|
13
|
+
const Range = z.tuple([Int, Int]);
|
|
14
|
+
const Fields = z.object({
|
|
15
|
+
look: z.enum(BAR_LOOKS).default('bar'), // The plain bar model, or a bar of chocolate with a square for each 1.
|
|
16
|
+
whole: Int.optional(), // The whole as the page opens, the length of the bar.
|
|
17
|
+
first: Int.optional(), // The first part as the page opens. The second part is the whole minus the first.
|
|
18
|
+
drag: z.array(z.enum(HANDLES)).default([]), // The handles the reader drags: the split and the end of the bar.
|
|
19
|
+
end: Range.optional(), // The places the end runs over, from 2 to 20 when left out.
|
|
20
|
+
counters: z.boolean().default(false), // A row of counters over the bar, one over each 1 of its length.
|
|
21
|
+
// The written line under the bar, or with family the four facts of the family.
|
|
22
|
+
written: z.union([z.literal(true), z.literal('family')]).optional(),
|
|
23
|
+
facts: z.array(z.enum(FACTS)).optional(), // The facts a family page writes, all four when left out.
|
|
24
|
+
ask: z.enum(NUMBERS).optional(), // The number a card covers as the page opens.
|
|
25
|
+
cover: z.boolean().default(false), // A tap on a number of the bar covers it with a card, and a tap on the card lifts it.
|
|
26
|
+
readout: z.array(z.string()).default([]),
|
|
27
|
+
// A challenge names a goal of bar-goals.js and the range it draws its target from.
|
|
28
|
+
challenge: z.object({ goal: z.string(), whole: Range.optional(), numbers: Range.optional() }).strict().optional(),
|
|
29
|
+
}).strict();
|
|
30
|
+
|
|
31
|
+
function read(fields, { at }, ctx) {
|
|
32
|
+
const pg = check(Fields, fields, at), fail = words => ctx.fail(at, words);
|
|
33
|
+
const spec = { ...pg, scene: 'bar' };
|
|
34
|
+
if (pg.challenge) { readChallenge(pg, spec, at, ctx); return { data: spec, has: () => false, offers: 'nothing, a challenge page draws its numbers as it opens' }; }
|
|
35
|
+
if (pg.whole == null || pg.whole < WHOLE[0] || pg.whole > WHOLE[1]) fail(`whole is ${pg.whole ?? 'missing'}, write the whole as a number from ${WHOLE[0]} to ${WHOLE[1]}, such as 8`);
|
|
36
|
+
else if (pg.first == null || pg.first < 1 || pg.first >= pg.whole) fail(`first is ${pg.first ?? 'missing'}, write the first part as a number from 1 to ${pg.whole - 1}, below the whole`);
|
|
37
|
+
if (new Set(pg.drag).size < pg.drag.length) fail(`drag lists ${list(pg.drag)}, write each handle once`);
|
|
38
|
+
if (pg.end) {
|
|
39
|
+
const [lo, hi] = pg.end;
|
|
40
|
+
if (!pg.drag.includes('end')) fail('end sets the places a dragged end runs over, write drag: [end] as well');
|
|
41
|
+
if (lo > hi || lo < WHOLE[0] || hi > WHOLE[1] || (pg.whole != null && (pg.whole < lo || pg.whole > hi))) fail(`end is [${lo}, ${hi}], write [low, high] from ${WHOLE[0]} to ${WHOLE[1]} with the whole ${pg.whole ?? ''} between them`);
|
|
42
|
+
}
|
|
43
|
+
if (pg.look === 'chocolate') {
|
|
44
|
+
if (pg.drag.includes('end')) fail('drag lists end, a bar of chocolate keeps its squares, write drag: [split]');
|
|
45
|
+
if (pg.counters) fail('counters belongs to look: bar, the chocolate shows its squares');
|
|
46
|
+
}
|
|
47
|
+
if ((pg.ask || pg.cover) && family(pg)) fail('ask and cover write the calculation that finds a number, write written: true or leave written out');
|
|
48
|
+
if (pg.ask && !pg.cover) fail('ask opens with a card on a number, write cover: true so the reader can lift it');
|
|
49
|
+
if (pg.facts && !family(pg)) fail('facts goes with written: family, write written: family or leave facts out');
|
|
50
|
+
if (pg.facts && (!pg.facts.length || new Set(pg.facts).size < pg.facts.length)) fail(`facts lists ${list(pg.facts) || 'nothing'}, write each fact once, out of ${list(FACTS)}`);
|
|
51
|
+
const ids = readable(spec);
|
|
52
|
+
for (const id of pg.readout) if (!ids.includes(id)) fail(`readout names "${id}", this page reads out ${list(ids)}`);
|
|
53
|
+
const linked = measuresOf(spec);
|
|
54
|
+
return { data: spec, has: id => linked.includes(id), offers: `one of ${list(linked)}` };
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// A challenge page names its goal and the range it draws from, and may write the facts once the goal holds. The goal
|
|
58
|
+
// sets the bar and both handles, so the page leaves every other field out.
|
|
59
|
+
function readChallenge(pg, spec, at, ctx) {
|
|
60
|
+
const c = pg.challenge, goal = challenge(c, BAR_GOALS, { scene: 'bar', at, ctx });
|
|
61
|
+
if (!goal) return;
|
|
62
|
+
for (const [f, v] of Object.entries(pg)) if (!['challenge', 'look', 'written'].includes(f) && v !== undefined && v !== false && !(Array.isArray(v) && !v.length)) ctx.fail(at, `${f} is set by challenge.goal ${c.goal}, leave it out`);
|
|
63
|
+
if (pg.look !== 'bar') ctx.fail(at, `look is set by challenge.goal ${c.goal}, leave it out`);
|
|
64
|
+
if (pg.written === true) ctx.fail(at, `written: true writes one line, a challenge writes the facts, write written: family or leave it out`);
|
|
65
|
+
const problem = goal.problem(c);
|
|
66
|
+
if (problem) return ctx.fail(at, `challenge.${problem}`);
|
|
67
|
+
// The page must not open solved, so the opening bar is checked as if the reader pressed Check at once.
|
|
68
|
+
drawn(goal, c, spec, { at, ctx, starts: (sp, t) => ({ ...barStart(t), checked: true }) });
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
// A link shows a number of the bar, a written line or its words as the page opens.
|
|
72
|
+
function start(spec, lang, id) {
|
|
73
|
+
if (!measuresOf(spec).includes(id)) return null;
|
|
74
|
+
const s = opening(spec), int = numbers(lang).int;
|
|
75
|
+
if (id in LINES) { const line = LINES[id](spec, s); return id.startsWith('written') ? words(line, int) : reading(line, int, (key, vars) => string(lang, `hesap_${key}`, vars)); }
|
|
76
|
+
return int(counts(spec, s)[id]);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export default { read, start };
|
|
@@ -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
|
|
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
|
-
|
|
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;
|