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 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 | 'use client' import { animated, useSpring } from '@react-spring/web' import { memo, useMemo, useRef, useState } from 'react' import { useGameMode } from '@/contexts/GameModeContext' import { useUserProfile } from '@/contexts/UserProfileContext' import { useComplementRace } from '@/arcade-games/complement-race/Provider' import { type BoardingAnimation, type DisembarkingAnimation, usePassengerAnimations, } from '../../hooks/usePassengerAnimations' import type { ComplementQuestion } from '../../lib/gameTypes' import { useSteamJourney } from '../../hooks/useSteamJourney' import { useTrackManagement } from '../../hooks/useTrackManagement' import { useTrainTransforms } from '../../hooks/useTrainTransforms' import { RailroadTrackGenerator } from '../../lib/RailroadTrackGenerator' import { getRouteTheme } from '../../lib/routeThemes' import { GameHUD } from './GameHUD' import { RailroadTrackPath } from './RailroadTrackPath' import { TrainAndCars } from './TrainAndCars' import { TrainTerrainBackground } from './TrainTerrainBackground' import { GhostTrain } from './GhostTrain' const BoardingPassengerAnimation = memo(({ animation }: { animation: BoardingAnimation }) => { const spring = useSpring({ from: { x: animation.fromX, y: animation.fromY, opacity: 1 }, to: { x: animation.toX, y: animation.toY, opacity: 1 }, config: { tension: 120, friction: 14 }, }) return ( <animated.text x={spring.x} y={spring.y} textAnchor="middle" opacity={spring.opacity} style={{ fontSize: '55px', pointerEvents: 'none', filter: animation.passenger.isUrgent ? 'drop-shadow(0 0 8px rgba(245, 158, 11, 0.8))' : 'drop-shadow(0 2px 4px rgba(0, 0, 0, 0.3))', }} > {animation.passenger.avatar} </animated.text> ) }) BoardingPassengerAnimation.displayName = 'BoardingPassengerAnimation' const DisembarkingPassengerAnimation = memo( ({ animation }: { animation: DisembarkingAnimation }) => { const spring = useSpring({ from: { x: animation.fromX, y: animation.fromY, opacity: 1 }, to: { x: animation.toX, y: animation.toY, opacity: 1 }, config: { tension: 120, friction: 14 }, }) return ( <animated.text x={spring.x} y={spring.y} textAnchor="middle" opacity={spring.opacity} style={{ fontSize: '55px', pointerEvents: 'none', filter: 'drop-shadow(0 0 12px rgba(16, 185, 129, 0.8))', }} > {animation.passenger.avatar} </animated.text> ) } ) DisembarkingPassengerAnimation.displayName = 'DisembarkingPassengerAnimation' interface SteamTrainJourneyProps { momentum: number trainPosition: number pressure: number elapsedTime: number currentQuestion: ComplementQuestion | null currentInput: string } export function SteamTrainJourney({ momentum, trainPosition, pressure, elapsedTime, currentQuestion, currentInput, }: SteamTrainJourneyProps) { const { state, multiplayerState, localPlayerId } = useComplementRace() const { getSkyGradient, getTimeOfDayPeriod } = useSteamJourney() const _skyGradient = getSkyGradient() const period = getTimeOfDayPeriod() const { players } = useGameMode() const { profile: _profile } = useUserProfile() // Get the LOCAL player's emoji (not just the first player!) const activePlayers = Array.from(players.values()).filter((p) => p.isActive) const localPlayer = activePlayers.find((p) => p.isLocal) const playerEmoji = localPlayer?.emoji ?? '👤' const svgRef = useRef<SVGSVGElement>(null) const pathRef = useRef<SVGPathElement>(null) const [trackGenerator] = useState(() => new RailroadTrackGenerator(800, 600)) // Use server's authoritative maxConcurrentPassengers calculation // This ensures visual display matches game logic and console logs const maxCars = Math.max(1, state.maxConcurrentPassengers || 3) const carSpacing = 7 // Distance between cars (in % of track) // Train transforms (extracted to hook) const { trainTransform, trainCars, locomotiveOpacity } = useTrainTransforms({ trainPosition, trackGenerator, pathRef, maxCars, carSpacing, }) // Track management (extracted to hook) const { trackData, tiesAndRails, stationPositions, landmarks, landmarkPositions, displayPassengers, } = useTrackManagement({ currentRoute: state.currentRoute, trainPosition, trackGenerator, pathRef, stations: state.stations, passengers: state.passengers, maxCars, carSpacing, }) // Passenger animations (extracted to hook) const { boardingAnimations, disembarkingAnimations } = usePassengerAnimations({ passengers: state.passengers, stations: state.stations, stationPositions, trainPosition, trackGenerator, pathRef, }) // Time remaining (60 seconds total) const timeRemaining = Math.max(0, 60 - Math.floor(elapsedTime / 1000)) // Period names for display const periodNames = ['Dawn', 'Morning', 'Midday', 'Afternoon', 'Dusk', 'Night'] // Get current route theme const routeTheme = getRouteTheme(state.currentRoute) // Memoize filtered passenger lists to avoid recalculating on every render // Arcade room multiplayer uses claimedBy/deliveredBy instead of isBoarded/isDelivered // Only show passengers claimed by the LOCAL player const boardedPassengers = useMemo( () => displayPassengers.filter( (p) => p.claimedBy === localPlayer?.id && p.claimedBy !== null && p.deliveredBy === null ), [displayPassengers, localPlayer?.id] ) const nonDeliveredPassengers = useMemo( () => displayPassengers.filter((p) => p.deliveredBy === null), [displayPassengers] ) // Memoize ground texture circles to avoid recreating on every render const groundTextureCircles = useMemo( () => Array.from({ length: 30 }).map((_, i) => ({ key: `ground-texture-${i}`, cx: -30 + i * 28 + (i % 3) * 10, cy: 140 + (i % 5) * 60, r: 2 + (i % 3), })), [] ) // Calculate local train car positions for ghost train overlap detection // Array includes locomotive + all cars: [locomotive, car1, car2, car3] const localTrainCarPositions = useMemo(() => { const positions = [trainPosition] // Locomotive at front for (let i = 0; i < maxCars; i++) { positions.push(Math.max(0, trainPosition - (i + 1) * carSpacing)) } return positions }, [trainPosition, maxCars, carSpacing]) // Get other players for ghost trains (filter out local player) const otherPlayers = useMemo(() => { if (!multiplayerState?.players || !localPlayerId) { return [] } const filtered = Object.entries(multiplayerState.players) .filter(([playerId, player]) => playerId !== localPlayerId && player.isActive) .map(([_, player]) => player) return filtered }, [multiplayerState?.players, localPlayerId]) if (!trackData) return null return ( <div data-component="steam-train-journey" style={{ position: 'relative', width: '100%', height: '100%', background: 'transparent', overflow: 'visible', display: 'flex', alignItems: 'center', justifyContent: 'stretch', }} > {/* Game HUD - overlays and UI elements */} <GameHUD routeTheme={routeTheme} currentRoute={state.currentRoute} periodName={periodNames[period]} timeRemaining={timeRemaining} pressure={pressure} nonDeliveredPassengers={nonDeliveredPassengers} stations={state.stations} currentQuestion={currentQuestion} currentInput={currentInput} /> {/* Railroad track SVG */} <svg data-component="railroad-track" ref={svgRef} viewBox="-50 -50 900 700" style={{ width: '100%', height: 'auto', aspectRatio: '800 / 600', overflow: 'visible', }} > {/* Terrain background - ground, mountains, and tunnels */} <TrainTerrainBackground ballastPath={trackData.ballastPath} groundTextureCircles={groundTextureCircles} /> {/* Railroad track, landmarks, and stations */} <RailroadTrackPath tiesAndRails={tiesAndRails} referencePath={trackData.referencePath} pathRef={pathRef} landmarkPositions={landmarkPositions} landmarks={landmarks} stationPositions={stationPositions} stations={state.stations} passengers={displayPassengers} boardingAnimations={boardingAnimations} disembarkingAnimations={disembarkingAnimations} /> {/* Ghost trains - other players in multiplayer */} {otherPlayers.map((player) => ( <GhostTrain key={player.id} player={player} trainPosition={player.position} // Use each player's individual position localTrainCarPositions={localTrainCarPositions} // For per-car overlap detection maxCars={maxCars} carSpacing={carSpacing} trackGenerator={trackGenerator} pathRef={pathRef} /> ))} {/* Train, cars, and passenger animations - local player */} <TrainAndCars boardingAnimations={boardingAnimations} disembarkingAnimations={disembarkingAnimations} BoardingPassengerAnimation={BoardingPassengerAnimation} DisembarkingPassengerAnimation={DisembarkingPassengerAnimation} trainCars={trainCars} boardedPassengers={boardedPassengers} trainTransform={trainTransform} locomotiveOpacity={locomotiveOpacity} playerEmoji={playerEmoji} momentum={momentum} /> </svg> {/* CSS animations */} <style>{` @keyframes steamPuffSVG { 0% { opacity: 0.8; transform: scale(0.5) translate(0, 0); } 50% { opacity: 0.4; transform: scale(1.5) translate(15px, -30px); } 100% { opacity: 0; transform: scale(2) translate(25px, -60px); } } @keyframes coalFallingSVG { 0% { opacity: 1; transform: translate(0, 0) scale(1); } 50% { opacity: 0.7; transform: translate(5px, 15px) scale(0.8); } 100% { opacity: 0; transform: translate(8px, 30px) scale(0.5); } } @keyframes celebrateDelivery { 0% { opacity: 1; transform: scale(1) translateY(0); } 20% { transform: scale(1.3) translateY(-10px); } 40% { transform: scale(1.2) translateY(-5px); } 100% { opacity: 0; transform: scale(0.8) translateY(-20px); } } `}</style> </div> ) } |