All files / web/src/app/arcade/complement-race/components/RaceTrack TrainAndCars.tsx

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

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                                                                                                                                                                                                                                                                                                                                                                                                                 
'use client'

import { memo } from 'react'
import type { BoardingAnimation, DisembarkingAnimation } from '../../hooks/usePassengerAnimations'
import type { Passenger } from '@/arcade-games/complement-race/types'

interface TrainCarTransform {
  x: number
  y: number
  rotation: number
  position: number
  opacity: number
}

interface TrainTransform {
  x: number
  y: number
  rotation: number
}

interface TrainAndCarsProps {
  boardingAnimations: Map<string, BoardingAnimation>
  disembarkingAnimations: Map<string, DisembarkingAnimation>
  BoardingPassengerAnimation: React.ComponentType<{
    animation: BoardingAnimation
  }>
  DisembarkingPassengerAnimation: React.ComponentType<{
    animation: DisembarkingAnimation
  }>
  trainCars: TrainCarTransform[]
  boardedPassengers: Passenger[]
  trainTransform: TrainTransform
  locomotiveOpacity: number
  playerEmoji: string
  momentum: number
}

export const TrainAndCars = memo(
  ({
    boardingAnimations,
    disembarkingAnimations,
    BoardingPassengerAnimation,
    DisembarkingPassengerAnimation,
    trainCars,
    boardedPassengers,
    trainTransform,
    locomotiveOpacity,
    playerEmoji,
    momentum,
  }: TrainAndCarsProps) => {
    return (
      <>
        {/* Boarding animations - passengers moving from station to train car */}
        {Array.from(boardingAnimations.values()).map((animation) => (
          <BoardingPassengerAnimation
            key={`boarding-${animation.passenger.id}`}
            animation={animation}
          />
        ))}

        {/* Disembarking animations - passengers moving from train car to station */}
        {Array.from(disembarkingAnimations.values()).map((animation) => (
          <DisembarkingPassengerAnimation
            key={`disembarking-${animation.passenger.id}`}
            animation={animation}
          />
        ))}

        {/* Train cars - render in reverse order so locomotive appears on top */}
        {trainCars.map((carTransform, carIndex) => {
          // Assign passenger to this car (if one exists for this car index)
          const passenger = boardedPassengers[carIndex]

          return (
            <g
              key={`train-car-${carIndex}`}
              data-component="train-car"
              transform={`translate(${carTransform.x}, ${carTransform.y}) rotate(${carTransform.rotation}) scale(-1, 1)`}
              opacity={carTransform.opacity}
              style={{
                transition: 'opacity 0.5s ease-in',
              }}
            >
              {/* Train car */}
              <text
                data-element="train-car-body"
                x={0}
                y={0}
                textAnchor="middle"
                style={{
                  fontSize: '65px',
                  filter: 'drop-shadow(0 2px 4px rgba(0, 0, 0, 0.3))',
                  pointerEvents: 'none',
                }}
              >
                🚃
              </text>

              {/* Passenger inside this car (hide if currently boarding) */}
              {passenger && !boardingAnimations.has(passenger.id) && (
                <text
                  data-element="car-passenger"
                  x={0}
                  y={0}
                  textAnchor="middle"
                  style={{
                    fontSize: '42px',
                    filter: passenger.isUrgent
                      ? 'drop-shadow(0 0 6px rgba(245, 158, 11, 0.8))'
                      : 'drop-shadow(0 2px 4px rgba(0, 0, 0, 0.3))',
                    pointerEvents: 'none',
                  }}
                >
                  {passenger.avatar}
                </text>
              )}
            </g>
          )
        })}

        {/* Locomotive - rendered last so it appears on top */}
        <g
          data-component="locomotive-group"
          transform={`translate(${trainTransform.x}, ${trainTransform.y}) rotate(${trainTransform.rotation}) scale(-1, 1)`}
          opacity={locomotiveOpacity}
          style={{
            transition: 'opacity 0.5s ease-in',
          }}
        >
          {/* Train locomotive */}
          <text
            data-element="train-locomotive"
            x={0}
            y={0}
            textAnchor="middle"
            style={{
              fontSize: '100px',
              filter: 'drop-shadow(0 2px 4px rgba(0, 0, 0, 0.3))',
              pointerEvents: 'none',
            }}
          >
            🚂
          </text>

          {/* Player engineer - layered over the train */}
          <text
            data-element="player-engineer"
            x={45}
            y={0}
            textAnchor="middle"
            style={{
              fontSize: '70px',
              filter: 'drop-shadow(0 2px 4px rgba(0, 0, 0, 0.3))',
              pointerEvents: 'none',
            }}
          >
            {playerEmoji}
          </text>

          {/* Steam puffs - positioned at smokestack, layered over train */}
          {momentum > 10 &&
            [0, 0.6, 1.2].map((delay, i) => (
              <circle
                key={`steam-${i}`}
                cx={-35}
                cy={-35}
                r="10"
                fill="rgba(255, 255, 255, 0.6)"
                style={{
                  filter: 'blur(4px)',
                  animation: `steamPuffSVG 2s ease-out infinite`,
                  animationDelay: `${delay}s`,
                  pointerEvents: 'none',
                }}
              />
            ))}

          {/* Coal particles - animated when shoveling */}
          {momentum > 60 &&
            [0, 0.3, 0.6].map((delay, i) => (
              <circle
                key={`coal-${i}`}
                cx={25}
                cy={0}
                r="3"
                fill="#2c2c2c"
                style={{
                  animation: 'coalFallingSVG 1.2s ease-out infinite',
                  animationDelay: `${delay}s`,
                  pointerEvents: 'none',
                }}
              />
            ))}
        </g>
      </>
    )
  }
)

TrainAndCars.displayName = 'TrainAndCars'