All files / web/src/app/arcade/complement-race/hooks usePassengerAnimations.ts

93.97% Statements 156/166
81.48% Branches 22/27
100% Functions 1/1
93.97% Lines 156/166

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 1671x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 11x 11x 11x 11x 11x 11x 13x 13x 11x 11x 11x 11x 13x 13x 11x 11x 11x 11x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x           3x 11x 11x 11x 11x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x           1x 11x 11x 11x 11x 15x 15x 15x 15x 15x 15x 15x  
import { useEffect, useRef, useState } from 'react'
import type { Passenger, Station } from '../lib/gameTypes'
import type { RailroadTrackGenerator } from '../lib/RailroadTrackGenerator'
 
export interface BoardingAnimation {
  passenger: Passenger
  fromX: number
  fromY: number
  toX: number
  toY: number
  carIndex: number
  startTime: number
}
 
export interface DisembarkingAnimation {
  passenger: Passenger
  fromX: number
  fromY: number
  toX: number
  toY: number
  startTime: number
}
 
interface UsePassengerAnimationsParams {
  passengers: Passenger[]
  stations: Station[]
  stationPositions: Array<{ x: number; y: number }>
  trainPosition: number
  trackGenerator: RailroadTrackGenerator
  pathRef: React.RefObject<SVGPathElement>
}
 
export function usePassengerAnimations({
  passengers,
  stations,
  stationPositions,
  trainPosition,
  trackGenerator,
  pathRef,
}: UsePassengerAnimationsParams) {
  const [boardingAnimations, setBoardingAnimations] = useState<Map<string, BoardingAnimation>>(
    new Map()
  )
  const [disembarkingAnimations, setDisembarkingAnimations] = useState<
    Map<string, DisembarkingAnimation>
  >(new Map())
  const previousPassengersRef = useRef<Passenger[]>(passengers)
 
  // Detect passengers boarding/disembarking and start animations
  useEffect(() => {
    if (!pathRef.current || stationPositions.length === 0) return
 
    const previousPassengers = previousPassengersRef.current
    const currentPassengers = passengers
 
    // Find newly boarded passengers
    const newlyBoarded = currentPassengers.filter((curr) => {
      const prev = previousPassengers.find((p) => p.id === curr.id)
      return curr.isBoarded && prev && !prev.isBoarded
    })
 
    // Find newly delivered passengers
    const newlyDelivered = currentPassengers.filter((curr) => {
      const prev = previousPassengers.find((p) => p.id === curr.id)
      return curr.isDelivered && prev && !prev.isDelivered
    })
 
    // Start animation for each newly boarded passenger
    newlyBoarded.forEach((passenger) => {
      // Find origin station
      const originStation = stations.find((s) => s.id === passenger.originStationId)
      if (!originStation) return
 
      const stationIndex = stations.indexOf(originStation)
      const stationPos = stationPositions[stationIndex]
      if (!stationPos) return
 
      // Find which car this passenger will be in
      const boardedPassengers = currentPassengers.filter((p) => p.isBoarded && !p.isDelivered)
      const carIndex = boardedPassengers.indexOf(passenger)
 
      // Calculate train car position
      const carPosition = Math.max(0, trainPosition - (carIndex + 1) * 7) // 7% spacing
      const carTransform = trackGenerator.getTrainTransform(pathRef.current!, carPosition)
 
      // Create boarding animation
      const animation: BoardingAnimation = {
        passenger,
        fromX: stationPos.x,
        fromY: stationPos.y - 30,
        toX: carTransform.x,
        toY: carTransform.y,
        carIndex,
        startTime: Date.now(),
      }
 
      setBoardingAnimations((prev) => {
        const next = new Map(prev)
        next.set(passenger.id, animation)
        return next
      })
 
      // Remove animation after 800ms
      setTimeout(() => {
        setBoardingAnimations((prev) => {
          const next = new Map(prev)
          next.delete(passenger.id)
          return next
        })
      }, 800)
    })
 
    // Start animation for each newly delivered passenger
    newlyDelivered.forEach((passenger) => {
      // Find destination station
      const destinationStation = stations.find((s) => s.id === passenger.destinationStationId)
      if (!destinationStation) return
 
      const stationIndex = stations.indexOf(destinationStation)
      const stationPos = stationPositions[stationIndex]
      if (!stationPos) return
 
      // Find which car this passenger was in (before delivery)
      const prevBoardedPassengers = previousPassengers.filter((p) => p.isBoarded && !p.isDelivered)
      const carIndex = prevBoardedPassengers.findIndex((p) => p.id === passenger.id)
      if (carIndex === -1) return
 
      // Calculate train car position at time of disembarking
      const carPosition = Math.max(0, trainPosition - (carIndex + 1) * 7) // 7% spacing
      const carTransform = trackGenerator.getTrainTransform(pathRef.current!, carPosition)
 
      // Create disembarking animation (from car to station)
      const animation: DisembarkingAnimation = {
        passenger,
        fromX: carTransform.x,
        fromY: carTransform.y,
        toX: stationPos.x,
        toY: stationPos.y - 30,
        startTime: Date.now(),
      }
 
      setDisembarkingAnimations((prev) => {
        const next = new Map(prev)
        next.set(passenger.id, animation)
        return next
      })
 
      // Remove animation after 800ms
      setTimeout(() => {
        setDisembarkingAnimations((prev) => {
          const next = new Map(prev)
          next.delete(passenger.id)
          return next
        })
      }, 800)
    })
 
    // Update ref
    previousPassengersRef.current = currentPassengers
  }, [passengers, stations, stationPositions, trainPosition, trackGenerator, pathRef])
 
  return {
    boardingAnimations,
    disembarkingAnimations,
  }
}