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

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

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

import { useEffect, useState } from 'react'
import { useComplementRace } from '@/arcade-games/complement-race/Provider'
import { useSoundEffects } from '../hooks/useSoundEffects'

export function GameCountdown() {
  const { dispatch } = useComplementRace()
  const { playSound } = useSoundEffects()
  const [count, setCount] = useState(3)
  const [showGo, setShowGo] = useState(false)

  useEffect(() => {
    const countdownInterval = setInterval(() => {
      setCount((prevCount) => {
        if (prevCount > 1) {
          // Play countdown beep (volume 0.4)
          playSound('countdown', 0.4)
          return prevCount - 1
        } else if (prevCount === 1) {
          // Show GO!
          setShowGo(true)
          // Play race start fanfare (volume 0.6)
          playSound('race_start', 0.6)
          return 0
        }
        return prevCount
      })
    }, 1000)

    return () => clearInterval(countdownInterval)
  }, [playSound])

  useEffect(() => {
    if (showGo) {
      // Hide countdown and start game after GO animation
      const timer = setTimeout(() => {
        dispatch({ type: 'BEGIN_GAME' })
      }, 1000)

      return () => clearTimeout(timer)
    }
  }, [showGo, dispatch])

  return (
    <div
      style={{
        position: 'fixed',
        top: 0,
        left: 0,
        right: 0,
        bottom: 0,
        display: 'flex',
        flexDirection: 'column',
        alignItems: 'center',
        justifyContent: 'center',
        background: 'rgba(0, 0, 0, 0.9)',
        zIndex: 1000,
      }}
    >
      <div
        style={{
          fontSize: showGo ? '120px' : '160px',
          fontWeight: 'bold',
          color: showGo ? '#10b981' : 'white',
          textShadow: '0 4px 20px rgba(0, 0, 0, 0.5)',
          animation: showGo ? 'scaleUp 1s ease-out' : 'pulse 0.5s ease-in-out',
          transition: 'all 0.3s ease',
        }}
      >
        {showGo ? 'GO!' : count}
      </div>

      {!showGo && (
        <div
          style={{
            marginTop: '32px',
            fontSize: '24px',
            color: 'rgba(255, 255, 255, 0.8)',
            fontWeight: 500,
          }}
        >
          Get Ready!
        </div>
      )}

      <style
        dangerouslySetInnerHTML={{
          __html: `
          @keyframes pulse {
            0%, 100% { transform: scale(1); opacity: 1; }
            50% { transform: scale(1.1); opacity: 0.8; }
          }
          @keyframes scaleUp {
            0% { transform: scale(0.5); opacity: 0; }
            50% { transform: scale(1.2); opacity: 1; }
            100% { transform: scale(1); opacity: 1; }
          }
        `,
        }}
      />
    </div>
  )
}