All files / web/src/arcade-games/rithmomachia/components/capture NumberBondVisualization.tsx

0% Statements 0/209
0% Branches 0/1
0% Functions 0/1
0% Lines 0/209

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210                                                                                                                                                                                                                                                                                                                                                                                                                                   
'use client'

import { animated, to, useSpring } from '@react-spring/web'
import { useEffect, useState } from 'react'
import { useAbacusSettings } from '@/hooks/useAbacusSettings'
import { useCaptureContext } from '../../contexts/CaptureContext'
import { getRelationColor, getRelationOperator } from '../../constants/captureRelations'
import { getEffectiveValue } from '../../utils/pieceSetup'
import { getSquarePosition } from '../../utils/boardCoordinates'
import { PieceRenderer } from '../PieceRenderer'

interface NumberBondVisualizationProps {
  onConfirm: () => void
  moverStartPos: { x: number; y: number }
  helperStartPos: { x: number; y: number }
}

/**
 * Number Bond Visualization - uses actual piece positions for smooth rotation/collapse
 * Pieces start at their actual positions (mover on board, helper in ring, target on board)
 * Animation: Rotate and collapse to target position, only mover remains
 */
export function NumberBondVisualization({
  onConfirm,
  moverStartPos,
  helperStartPos,
}: NumberBondVisualizationProps) {
  const { layout, pieces, selectedRelation, closing } = useCaptureContext()
  const { targetPos, cellSize, padding, gap } = layout
  const { mover: moverPiece, target: targetPiece, helper: helperPiece } = pieces
  const relation = selectedRelation!

  // Get abacus settings
  const { data: abacusSettings } = useAbacusSettings()
  const useNativeAbacusNumbers = abacusSettings?.nativeAbacusNumbers ?? false

  const autoAnimate = true
  const [animating, setAnimating] = useState(false)

  // Auto-trigger animation immediately when component mounts (after helper selection)
  useEffect(() => {
    if (!autoAnimate) return
    const timer = setTimeout(() => {
      setAnimating(true)
    }, 300) // Short delay to show the triangle briefly
    return () => clearTimeout(timer)
  }, [autoAnimate])

  const color = getRelationColor(relation)
  const operator = getRelationOperator(relation)

  // Calculate actual board position for target
  const targetBoardPos = getSquarePosition(targetPiece.square, {
    cellSize,
    gap,
    padding,
  })

  // Animation: Rotate and collapse from actual positions to target
  const captureAnimation = useSpring({
    from: { rotation: 0, progress: 0, opacity: 1 },
    rotation: animating ? Math.PI * 20 : 0, // 10 full rotations
    progress: animating ? 1 : 0, // 0 = at start positions, 1 = at target position
    opacity: animating ? 0 : 1,
    config: animating ? { duration: 2500 } : { tension: 280, friction: 20 },
    onRest: () => {
      if (animating) {
        onConfirm()
      }
    },
  })

  // Type guard - this component should only be rendered when helper is selected
  // Must be after all hooks to follow Rules of Hooks
  if (!helperPiece) {
    return null
  }

  // Get piece values
  const getMoverValue = () => getEffectiveValue(moverPiece)
  const getHelperValue = () => getEffectiveValue(helperPiece)
  const getTargetValue = () => getEffectiveValue(targetPiece)

  return (
    <g>
      {/* Triangle connecting lines between actual piece positions - fade during animation */}
      <animated.g opacity={to([captureAnimation.opacity], (op) => (animating ? op * 0.5 : 0.5))}>
        <line
          x1={moverStartPos.x}
          y1={moverStartPos.y}
          x2={helperStartPos.x}
          y2={helperStartPos.y}
          stroke={color}
          strokeWidth={4}
        />
        <line
          x1={moverStartPos.x}
          y1={moverStartPos.y}
          x2={targetBoardPos.x}
          y2={targetBoardPos.y}
          stroke={color}
          strokeWidth={4}
        />
        <line
          x1={helperStartPos.x}
          y1={helperStartPos.y}
          x2={targetBoardPos.x}
          y2={targetBoardPos.y}
          stroke={color}
          strokeWidth={4}
        />
      </animated.g>

      {/* Operator symbol in center of triangle - fade during animation */}
      <animated.text
        x={(moverStartPos.x + helperStartPos.x + targetBoardPos.x) / 3}
        y={(moverStartPos.y + helperStartPos.y + targetBoardPos.y) / 3}
        textAnchor="middle"
        dominantBaseline="central"
        fill={color}
        fontSize={cellSize * 0.8}
        fontWeight="900"
        fontFamily="Georgia, 'Times New Roman', serif"
        opacity={to([captureAnimation.opacity], (op) => (animating ? op * 0.9 : 0.9))}
      >
        {operator}
      </animated.text>

      {/* Mover piece - starts at board position, spirals to target, STAYS VISIBLE */}
      <animated.g
        transform={to([captureAnimation.rotation, captureAnimation.progress], (rot, prog) => {
          // Interpolate from start position to target position
          const x = moverStartPos.x + (targetBoardPos.x - moverStartPos.x) * prog
          const y = moverStartPos.y + (targetBoardPos.y - moverStartPos.y) * prog

          // Add spiral rotation around the interpolated center
          const spiralRadius = (1 - prog) * cellSize * 0.5
          const spiralX = x + Math.cos(rot) * spiralRadius
          const spiralY = y + Math.sin(rot) * spiralRadius

          return `translate(${spiralX}, ${spiralY})`
        })}
        opacity={1} // Mover stays fully visible
      >
        <g transform={`translate(${-cellSize / 2}, ${-cellSize / 2})`}>
          <PieceRenderer
            type={moverPiece.type}
            color={moverPiece.color}
            value={getMoverValue() || 0}
            size={cellSize}
            useNativeAbacusNumbers={useNativeAbacusNumbers}
          />
        </g>
      </animated.g>

      {/* Helper piece - starts in ring, spirals to target, FADES OUT */}
      <animated.g
        transform={to([captureAnimation.rotation, captureAnimation.progress], (rot, prog) => {
          const x = helperStartPos.x + (targetBoardPos.x - helperStartPos.x) * prog
          const y = helperStartPos.y + (targetBoardPos.y - helperStartPos.y) * prog

          const spiralRadius = (1 - prog) * cellSize * 0.5
          const angle = rot + (Math.PI * 2) / 3 // Offset by 120°
          const spiralX = x + Math.cos(angle) * spiralRadius
          const spiralY = y + Math.sin(angle) * spiralRadius

          return `translate(${spiralX}, ${spiralY})`
        })}
        opacity={to([captureAnimation.opacity], (op) => (animating ? op : 1))}
      >
        <g transform={`translate(${-cellSize / 2}, ${-cellSize / 2})`}>
          <PieceRenderer
            type={helperPiece.type}
            color={helperPiece.color}
            value={getHelperValue() || 0}
            size={cellSize}
            useNativeAbacusNumbers={useNativeAbacusNumbers}
          />
        </g>
      </animated.g>

      {/* Target piece - stays at board position, spirals in place, FADES OUT */}
      <animated.g
        transform={to([captureAnimation.rotation, captureAnimation.progress], (rot, prog) => {
          const x = targetBoardPos.x
          const y = targetBoardPos.y

          const spiralRadius = (1 - prog) * cellSize * 0.5
          const angle = rot + (Math.PI * 4) / 3 // Offset by 240°
          const spiralX = x + Math.cos(angle) * spiralRadius
          const spiralY = y + Math.sin(angle) * spiralRadius

          return `translate(${spiralX}, ${spiralY})`
        })}
        opacity={to([captureAnimation.opacity], (op) => (animating ? op : 1))}
      >
        <g transform={`translate(${-cellSize / 2}, ${-cellSize / 2})`}>
          <PieceRenderer
            type={targetPiece.type}
            useNativeAbacusNumbers={useNativeAbacusNumbers}
            color={targetPiece.color}
            value={getTargetValue() || 0}
            size={cellSize}
          />
        </g>
      </animated.g>
    </g>
  )
}