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 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 | 'use client' import { useSpring, useSprings, animated, to } from '@react-spring/web' import { useMemo, useRef } from 'react' import type { PlayerState } from '@/arcade-games/complement-race/types' import type { RailroadTrackGenerator } from '../../lib/RailroadTrackGenerator' // Overlap threshold: if ghost car is within this distance of any local car, make it ghostly const OVERLAP_THRESHOLD = 20 // % of track length const GHOST_OPACITY = 0.35 // Opacity when overlapping const SOLID_OPACITY = 1.0 // Opacity when separated interface GhostTrainProps { player: PlayerState trainPosition: number localTrainCarPositions: number[] // [locomotive, car1, car2, car3] maxCars: number carSpacing: number trackGenerator: RailroadTrackGenerator pathRef: React.RefObject<SVGPathElement> } interface CarTransform { x: number y: number rotation: number opacity: number position: number } /** * Calculate opacity for a ghost car based on distance to nearest local car */ function calculateCarOpacity(ghostCarPosition: number, localCarPositions: number[]): number { // Find minimum distance to any local car const minDistance = Math.min( ...localCarPositions.map((localPos) => Math.abs(ghostCarPosition - localPos)) ) // If within threshold, use ghost opacity; otherwise solid return minDistance < OVERLAP_THRESHOLD ? GHOST_OPACITY : SOLID_OPACITY } /** * GhostTrain - Renders a semi-transparent train for other players in multiplayer * Uses per-car adaptive opacity: cars are ghostly when overlapping local train, * solid when separated */ export function GhostTrain({ player, trainPosition, localTrainCarPositions, maxCars, carSpacing, trackGenerator, pathRef, }: GhostTrainProps) { const ghostRef = useRef<SVGGElement>(null) // Calculate target transform for locomotive (used by spring animation) const locomotiveTarget = useMemo<CarTransform | null>(() => { if (!pathRef.current) { return null } const pathLength = pathRef.current.getTotalLength() const targetDistance = (trainPosition / 100) * pathLength const point = pathRef.current.getPointAtLength(targetDistance) // Calculate tangent for rotation const tangentDelta = 1 const tangentDistance = Math.min(targetDistance + tangentDelta, pathLength) const tangentPoint = pathRef.current.getPointAtLength(tangentDistance) const rotation = (Math.atan2(tangentPoint.y - point.y, tangentPoint.x - point.x) * 180) / Math.PI return { x: point.x, y: point.y, rotation, position: trainPosition, opacity: calculateCarOpacity(trainPosition, localTrainCarPositions), } }, [trainPosition, localTrainCarPositions, pathRef]) // Animated spring for smooth locomotive movement const locomotiveSpring = useSpring({ x: locomotiveTarget?.x ?? 0, y: locomotiveTarget?.y ?? 0, rotation: locomotiveTarget?.rotation ?? 0, opacity: locomotiveTarget?.opacity ?? 1, config: { tension: 280, friction: 60 }, // Smooth but responsive }) // Calculate target transforms for cars (used by spring animations) const carTargets = useMemo<CarTransform[]>(() => { if (!pathRef.current) { return [] } const pathLength = pathRef.current.getTotalLength() const cars: CarTransform[] = [] for (let i = 0; i < maxCars; i++) { const carPosition = Math.max(0, trainPosition - (i + 1) * carSpacing) const targetDistance = (carPosition / 100) * pathLength const point = pathRef.current.getPointAtLength(targetDistance) // Calculate tangent for rotation const tangentDelta = 1 const tangentDistance = Math.min(targetDistance + tangentDelta, pathLength) const tangentPoint = pathRef.current.getPointAtLength(tangentDistance) const rotation = (Math.atan2(tangentPoint.y - point.y, tangentPoint.x - point.x) * 180) / Math.PI cars.push({ x: point.x, y: point.y, rotation, position: carPosition, opacity: calculateCarOpacity(carPosition, localTrainCarPositions), }) } return cars }, [trainPosition, maxCars, carSpacing, localTrainCarPositions, pathRef]) // Animated springs for smooth car movement (useSprings for multiple cars) const carSprings = useSprings( carTargets.length, carTargets.map((target) => ({ x: target.x, y: target.y, rotation: target.rotation, opacity: target.opacity, config: { tension: 280, friction: 60 }, })) ) // Don't render if position data isn't ready if (!locomotiveTarget) { return null } return ( <g ref={ghostRef} data-component="ghost-train" data-player-id={player.id}> {/* Ghost locomotive - animated */} <animated.g transform={to( [locomotiveSpring.x, locomotiveSpring.y, locomotiveSpring.rotation], (x, y, rot) => `translate(${x}, ${y}) rotate(${rot}) scale(-1, 1)` )} opacity={locomotiveSpring.opacity} > <text data-element="ghost-locomotive" x={0} y={0} textAnchor="middle" style={{ fontSize: '100px', filter: `drop-shadow(0 2px 8px ${player.color || 'rgba(100, 100, 255, 0.6)'})`, pointerEvents: 'none', }} > 🚂 </text> {/* Player name label - positioned above locomotive */} <text data-element="ghost-label" x={0} y={-60} textAnchor="middle" style={{ fontSize: '18px', fontWeight: 'bold', fill: player.color || '#6366f1', filter: 'drop-shadow(0 1px 2px rgba(0, 0, 0, 0.3))', pointerEvents: 'none', transform: 'scaleX(-1)', // Counter the parent's scaleX(-1) }} > {player.name || `Player ${player.id.slice(0, 4)}`} </text> {/* Score indicator - positioned below locomotive */} <text data-element="ghost-score" x={0} y={50} textAnchor="middle" style={{ fontSize: '14px', fontWeight: 'bold', fill: 'rgba(255, 255, 255, 0.9)', filter: 'drop-shadow(0 1px 2px rgba(0, 0, 0, 0.5))', pointerEvents: 'none', transform: 'scaleX(-1)', // Counter the parent's scaleX(-1) }} > {player.score} </text> </animated.g> {/* Ghost cars - each with individual animated opacity and position */} {carSprings.map((spring, index) => ( <animated.g key={`car-${index}`} transform={to( [spring.x, spring.y, spring.rotation], (x, y, rot) => `translate(${x}, ${y}) rotate(${rot}) scale(-1, 1)` )} opacity={spring.opacity} > <text data-element={`ghost-car-${index}`} x={0} y={0} textAnchor="middle" style={{ fontSize: '85px', filter: `drop-shadow(0 2px 6px ${player.color || 'rgba(100, 100, 255, 0.4)'})`, pointerEvents: 'none', }} > 🚃 </text> </animated.g> ))} </g> ) } |