|
1436 | 1436 | <g class="nvfx rings"><circle cx="100" cy="98" r="86"/><circle cx="100" cy="98" r="100"/></g> |
1437 | 1437 | </svg>`; |
1438 | 1438 | const NV_CALM = !!(window.matchMedia && matchMedia('(prefers-reduced-motion: reduce)').matches); |
| 1439 | + // Clipes animados da nuvem (feitos à mão, 30 fps, com transparência; img/nuvem-anim/<nome>.webp). Só no ecrã grande: |
| 1440 | + // descarregam-se quando a mãe abre a nuvem e tocam por cima do boneco montado (que fica escondido enquanto tocam). |
| 1441 | + // Todos começam e acabam na pose normal, por isso a troca entre clipe e boneco não salta. Sem clipe, fica o boneco. |
| 1442 | + const NV_ANIM = { acenar: 2, contente: 2, rir: 1.8, ouvir: 0, falar: 0, carinho: 2.5, adormecer: 2 }; // segundos; 0 = em ciclo |
| 1443 | + const nvAnimBlobs = {}; |
| 1444 | + let nvAnimLoading = null; |
| 1445 | + const nvAnimLoad = () => nvAnimLoading || (nvAnimLoading = Promise.all(Object.keys(NV_ANIM).map(n => |
| 1446 | + fetch(`img/nuvem-anim/${n}.webp`).then(r => r.ok ? r.blob() : null).then(b => { if (b) nvAnimBlobs[n] = b; }).catch(() => {})))); |
| 1447 | + const nvClip = (node, name, then) => { |
| 1448 | + const img = node && !NV_CALM && node.querySelector('.nvclip'); |
| 1449 | + if (!img || !nvAnimBlobs[name]) return false; |
| 1450 | + clearTimeout(node.clipT); |
| 1451 | + if (img.dataset.url) URL.revokeObjectURL(img.dataset.url); |
| 1452 | + // Um endereço novo de cada vez: assim o clipe recomeça sempre do início (também no iPhone). |
| 1453 | + img.dataset.url = URL.createObjectURL(nvAnimBlobs[name]); img.src = img.dataset.url; img.dataset.clip = name; |
| 1454 | + node.classList.add('clipping'); |
| 1455 | + if (NV_ANIM[name]) node.clipT = setTimeout(() => { nvClipStop(node); if (then) then(); }, NV_ANIM[name] * 1000); |
| 1456 | + return true; |
| 1457 | + }; |
| 1458 | + const nvClipStop = node => { if (!node) return; clearTimeout(node.clipT); node.classList.remove('clipping'); const img = node.querySelector('.nvclip'); if (img) delete img.dataset.clip; }; |
| 1459 | + // Que clipe toca em cada estado (só no ecrã grande). |
| 1460 | + const nvClipMood = (node, mood) => { |
| 1461 | + if (!node || !node.querySelector('.nvclip')) return; |
| 1462 | + // Contente: o salto quando se guarda uma refeição; o risinho quando a mãe conta uma coisa boa ou lhe dá um nome. |
| 1463 | + const want = { talk: 'falar', listen: 'ouvir', think: 'ouvir', happy: node.dataset.joy || 'contente', care: 'carinho', sleep: 'adormecer' }[mood]; |
| 1464 | + const cur = node.querySelector('.nvclip').dataset.clip; |
| 1465 | + if (!want) { if (cur && !NV_ANIM[cur]) nvClipStop(node); return; } |
| 1466 | + if (cur === want) return; |
| 1467 | + if (!nvClip(node, want) && cur) nvClipStop(node); |
| 1468 | + }; |
1439 | 1469 | const nuvemArt = cls => { |
1440 | 1470 | const fx = el('span', { class: 'nvfxs' }); fx.innerHTML = NUVEM_FX; |
1441 | 1471 | // Corpo com os olhos fechados (serve de «a dormir» e de pestanejo) e, por cima, só a cara de cada estado. |
1442 | 1472 | const face = n => el('img', { class: 'nvface f-' + n, src: `img/nuvem-${n}.webp`, alt: '', width: 421, height: 195, draggable: false }); |
1443 | 1473 | return el('span', { class: 'nvchar ' + (cls || '') }, el('span', { class: 'nvshadow' }), |
1444 | 1474 | el('span', { class: 'nvbody' }, el('span', { class: 'nvgest' }, el('img', { class: 'nvimg', src: 'img/nuvem.webp', alt: '', width: 663, height: 629, draggable: false }), |
1445 | | - ['open', 'happy', 'listen', 'talk', 'care'].map(face))), fx); |
| 1475 | + ['open', 'happy', 'listen', 'talk', 'care'].map(face)), |
| 1476 | + cls === 'big' ? el('img', { class: 'nvclip', alt: '', draggable: false }) : null), fx); |
1446 | 1477 | }; |
1447 | 1478 | const nightNow = () => { const h = new Date().getHours(); return h >= 22 || h < 6; }; |
1448 | 1479 | // Cada mudança de cara cancela as anteriores (temporizadores e fim de fala), para não voltar a uma cara antiga. |
|
1452 | 1483 | // A falar, o corpo todo acompanha a voz: pequenos impulsos irregulares, como sílabas. |
1453 | 1484 | if (mood === 'talk' && !NV_CALM) node.ampT = setInterval(() => node.style.setProperty('--amp', (Math.random() ** 1.6).toFixed(2)), 110); |
1454 | 1485 | else node.style.removeProperty('--amp'); |
1455 | | - if (ms) node.moodT = setTimeout(() => { node.dataset.mood = nightNow() ? 'sleep' : 'idle'; }, ms); |
| 1486 | + nvClipMood(node, mood); |
| 1487 | + if (ms) node.moodT = setTimeout(() => { node.dataset.mood = nightNow() ? 'sleep' : 'idle'; nvClipMood(node, node.dataset.mood); }, ms); |
1456 | 1488 | }; |
1457 | 1489 | // Vida da nuvem (como o Tolan): olha para o dedo da mãe, faz pequenos gestos sozinha, achata-se quando lhe tocam, |
1458 | 1490 | // inclina-se quando é arrastada e passeia um bocadinho. Nada disto acontece com «reduzir movimento» ligado no telemóvel. |
|
1883 | 1915 | else if (r.mood === 'refusal') { |
1884 | 1916 | const g = ((window.DiaryGuides || {}).pt || []).find(x => x.id === 'refusal'); |
1885 | 1917 | say(NV.pick(NV.PHRASES.refusal), 'care', g ? [el('button', { class: 'ghost small', onclick: () => openGuide(g) }, t().nvReadGuide(g.title))] : []); |
1886 | | - } else if (r.mood === 'proud') say(NV.pick(NV.PHRASES.proud), 'happy'); |
1887 | | - else if (r.mood === 'ok' && !r.items.length) say(NV.pick(NV.PHRASES.ok), 'happy'); |
| 1918 | + } else if (r.mood === 'proud') { char.dataset.joy = 'rir'; say(NV.pick(NV.PHRASES.proud), 'happy'); } |
| 1919 | + else if (r.mood === 'ok' && !r.items.length) { char.dataset.joy = 'rir'; say(NV.pick(NV.PHRASES.ok), 'happy'); } |
1888 | 1920 | else if (!r.items.length) say(NV.PHRASES.notUnderstood, 'idle'); |
1889 | 1921 | if (r.items.length) confirmCard(r, !r.mood); |
1890 | 1922 | }; |
|
1904 | 1936 | const understand = text => { |
1905 | 1937 | text = String(text || '').trim(); |
1906 | 1938 | if (!text) return; |
1907 | | - stopBreath(); heard.textContent = '«' + text + '»'; aiNote.textContent = ''; |
| 1939 | + stopBreath(); heard.textContent = '«' + text + '»'; aiNote.textContent = ''; delete char.dataset.joy; |
1908 | 1940 | const named = text.match(NAME_RE); |
1909 | 1941 | if (named && cleanName(named[1])) { closePanel(); setName(cleanName(named[1])); return; } |
1910 | 1942 | const r = NV.parse(text, F.foods); |
|
1997 | 2029 | // Nome da nuvem: dado pela mãe (por voz, «chama-te…», ou no botão do nome). Aparece por cima da nuvem. |
1998 | 2030 | const nameTag = el('p', { class: 'nvname' }, nvName()); |
1999 | 2031 | const setName = n => { |
2000 | | - const m = nvMe(); m.name = n; nvMeSave(m); nameTag.textContent = n; nuvemSync(); |
| 2032 | + const m = nvMe(); m.name = n; nvMeSave(m); nameTag.textContent = n; nuvemSync(); char.dataset.joy = 'rir'; |
2001 | 2033 | say(n ? NV.PHRASES.nameThanks : NV.PHRASES.nameClear, 'happy'); |
2002 | 2034 | }; |
2003 | 2035 | const nameCard = () => { |
|
2045 | 2077 | el('button', { class: 'primary', onclick: () => i < steps.length - 1 ? step(i + 1) : end() }, i < steps.length - 1 ? t().nvNext : t().nvStart), |
2046 | 2078 | el('p', { class: 'fine nvsteps' }, `${i + 1} / ${steps.length}`))]); |
2047 | 2079 | heard.textContent = ''; step(0); |
2048 | | - } else greet(); |
| 2080 | + } else if (!nightNow() && nvClip(char, 'acenar')) { heard.textContent = t().nvHint; setTimeout(() => { if (stage.open) greet(); }, 1300); } // acena antes de falar |
| 2081 | + else greet(); |
| 2082 | + // Os clipes descarregam-se na primeira vez que a nuvem abre; quando chegam, entram no estado em que ela estiver. |
| 2083 | + nvAnimLoad().then(() => { if (stage.open) nvClipMood(char, char.dataset.mood); }); |
2049 | 2084 | if (!SR) typeForm.hidden = false; |
2050 | 2085 | }; |
2051 | 2086 |
|
|
0 commit comments