/** * Shared movement for the two-player board games (오목·사목·오델로·체스·장기·바둑·쿼리도·윷놀이). * Everything reacts to the view changing, so the opponent's (and the computer's) moves get the same sound * and animation as mine. Animations are short and never take pointer events, so clicks are not slowed. * prefers-reduced-motion is handled by the global rule in styles/global.css. */ import { useRef, useState } from 'react'; /** * The snapshot from before the move counter last changed, and whether that change was a step forward. * `before` is null until the counter changes while the board is on screen, so a page load / reconnect shows * the position without replaying the last move. Computed during render (no extra frame without the piece). */ export function useMoveDiff(snapshot: T, count: number): { before: T | null; forward: boolean } { const r = useRef<{ count: number; cur: T; before: T | null; forward: boolean }>({ count, cur: snapshot, before: null, forward: false }); const s = r.current; if (count !== s.count) { s.before = s.cur; s.forward = count > s.count; s.count = count; } s.cur = snapshot; return { before: s.before, forward: s.forward }; } /** * For things shown only while they animate (captured / taken-back stones): `[over, end]`. Render them while * `over` is false and pass `end` to the last one's onAnimationEnd, so finished ghosts leave the DOM (and do not * count as pieces for tests or screen readers). A new `key` (move number) shows them again. */ export function useAnimOver(key: unknown): [boolean, () => void] { const [ended, setEnded] = useState(undefined); return [ended === key, () => setEnded(key)]; } /** * SVG-friendly classes (scale around the element's own centre). Wrap the moving thing in its own `` without a * `transform` attribute, because a CSS transform replaces the attribute while it runs. * - `bm-place`: a stone / piece set down (lands from a little above, slightly bigger, with a tiny settle). * - `bm-slide`: slides in from `--bm-dx/--bm-dy` (board units, e.g. `-2px` = two cells left) over `--bm-dur`. * - `bm-gone`: a captured piece shrinking and fading (key it by the move number; remove it with useAnimOver). * - `bm-ring`: one ripple ring around the last move. * - `bm-win`: winning line pulsing, staggered with `--bm-delay`. * - `bm-flash`: a quick bright flash (check / 장군). */ export const BOARD_MOTION_CSS = ` .bm-place, .bm-gone, .bm-ring, .bm-win, .bm-flash { transform-box: fill-box; transform-origin: center; } .bm-place { animation: bm-place .26s cubic-bezier(.25, .9, .35, 1.25) both; pointer-events: none; } @keyframes bm-place { 0% { transform: translateY(-18%) scale(1.3); opacity: .15; } 70% { transform: translateY(0) scale(.97); opacity: 1; } 100% { transform: none; } } .bm-slide { animation: bm-slide var(--bm-dur, .24s) cubic-bezier(.3, .75, .35, 1) both; } @keyframes bm-slide { from { transform: translate(var(--bm-dx, 0px), var(--bm-dy, 0px)); } } .bm-gone { animation: bm-gone .42s ease-in forwards; pointer-events: none; } @keyframes bm-gone { 0% { opacity: 1; transform: scale(1); } 35% { opacity: .9; transform: scale(1.08); } 100% { opacity: 0; transform: scale(.35); } } .bm-ring { animation: bm-ring .55s ease-out .1s both; pointer-events: none; } @keyframes bm-ring { from { opacity: .85; transform: scale(.5); } to { opacity: 0; transform: scale(1.6); } } .bm-win { animation: bm-win 1.1s ease-in-out infinite; animation-delay: var(--bm-delay, 0ms); pointer-events: none; } @keyframes bm-win { 0%, 100% { opacity: 1; transform: scale(1); } 50% { opacity: .45; transform: scale(1.12); } } .bm-flash { animation: bm-flash .7s ease-out both; pointer-events: none; } @keyframes bm-flash { 0% { opacity: 0; } 20% { opacity: 1; } 100% { opacity: .55; } } /* The global reduced-motion rule makes animations 0.01ms long; an endless pulse would then flicker. */ @media (prefers-reduced-motion: reduce) { .bm-win { animation: none; } } `;