Files
joke-app/apps/web/src/games/board-motion.ts
EJClaw 5e1e74da18 오목·사목·오델로·바둑 소리·움직임, 바둑 사석 확인 무제한
- 공통(보드게임 8종용) board-motion: 직전 판 비교(useMoveDiff), 끝난 잔상 치우기(useAnimOver), 내려앉기·미끄러지기·사라지기·고리·반짝임 클래스
- 오목: 돌 내려앉기, 마지막 수 고리, 무르기 돌 사라짐, 승리 줄 차례로 반짝임, 금수 누르면 작은 소리, 돌 그림자
- 사목: 바닥에 닿아 한 번 튀기, 닿는 순간에 맞춘 "탁" 소리, 마지막 수 점은 닿은 뒤, 이긴 줄 차례로 깜빡임
- 오델로: 놓은 돌 먼저 내려앉고 뒤집기(들리며 뒤집힘), 뒤집힐 때마다 딸깍, 패스·무르기 소리, 마지막 수 고리
- 바둑: 돌 내려앉기, 따낸 돌 한 알씩 사라짐+딸깍, 패스 소리, 무르기 되돌림, 사석 표시 바뀜 소리
- 바둑 사석 확인 제한 시간에 무제한(0) 추가(기본 3분 그대로), 테스트·문서
- 숨은 탭 내 차례 소리는 방 화면이 이미 내므로 게임 쪽 중복 제거
- games E2E에 오목 기본 계획 추가

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-13 04:21:25 +09:00

63 lines
3.9 KiB
TypeScript

/**
* 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<T>(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<unknown>(undefined);
return [ended === key, () => setEnded(key)];
}
/**
* SVG-friendly classes (scale around the element's own centre). Wrap the moving thing in its own `<g>` 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; } }
`;