All files / web/src/arcade-games/know-your-world/features/animations useAnimationController.ts

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

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                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       
/**
 * Animation Controller Hook
 *
 * Consolidates animation state for give-up, hint, and celebration animations.
 * Uses the underlying usePulsingAnimation hook for consistent animation behavior.
 */

import { useCallback, useMemo, useRef, useState } from 'react'

import type { CelebrationType } from '../../Provider'
import { CELEBRATION_TIMING } from '../../utils/celebration'
import { usePulsingAnimation } from './usePulsingAnimation'

// ============================================================================
// Types
// ============================================================================

export interface AnimationControllerState {
  // Give-up reveal
  giveUpProgress: number
  isGiveUpAnimating: boolean

  // Hint
  hintProgress: number
  isHintAnimating: boolean

  // Celebration
  celebrationProgress: number
  /** Celebration doesn't have a separate boolean - it's derived from whether celebration is active */
}

export interface AnimationControllerActions {
  /**
   * Start the give-up reveal animation.
   * @param onComplete Called after animation and cooldown complete
   */
  startGiveUp: (onComplete: () => void) => void

  /**
   * Cancel the give-up animation and reset state.
   */
  cancelGiveUp: () => void

  /**
   * Start the hint animation.
   * @param onComplete Called when animation completes
   */
  startHint: (onComplete: () => void) => void

  /**
   * Cancel the hint animation and reset state.
   */
  cancelHint: () => void

  /**
   * Start the celebration animation.
   * @param celebrationType The type of celebration ('lightning' | 'standard' | 'hard-earned')
   */
  startCelebration: (celebrationType: CelebrationType) => void

  /**
   * Cancel the celebration animation and reset state.
   */
  cancelCelebration: () => void

  /**
   * Cancel all animations and reset all state.
   */
  cancelAll: () => void
}

export type AnimationController = AnimationControllerState & AnimationControllerActions

// ============================================================================
// Constants
// ============================================================================

const GIVE_UP_ANIMATION = {
  duration: 2000,
  pulses: 3,
  cooldownMs: 500, // Wait before cleanup
}

const HINT_ANIMATION = {
  duration: 1500,
  pulses: 2,
}

// ============================================================================
// Hook
// ============================================================================

/**
 * Hook that consolidates animation state for give-up, hint, and celebration.
 *
 * Uses usePulsingAnimation internally for consistent pulsing behavior.
 * All animation state is managed in one place, making it easier to coordinate
 * and debug animations.
 *
 * @example
 * ```tsx
 * const animations = useAnimationController()
 *
 * // Start give-up animation
 * animations.startGiveUp(() => {
 *   // Clear reveal state after animation
 *   setGiveUpReveal(null)
 * })
 *
 * // Use progress values in render
 * <path opacity={animations.giveUpProgress} />
 * ```
 */
export function useAnimationController(): AnimationController {
  // ----- Give-up state -----
  const [giveUpProgress, setGiveUpProgress] = useState(0)
  const [isGiveUpAnimating, setIsGiveUpAnimating] = useState(false)
  const giveUpPulsing = usePulsingAnimation()
  const giveUpTimeoutRef = useRef<ReturnType<typeof setTimeout> | null>(null)
  const giveUpIsCancelledRef = useRef(false)

  // ----- Hint state -----
  const [hintProgress, setHintProgress] = useState(0)
  const [isHintAnimating, setIsHintAnimating] = useState(false)
  const hintPulsing = usePulsingAnimation()

  // ----- Celebration state -----
  const [celebrationProgress, setCelebrationProgress] = useState(0)
  const celebrationPulsing = usePulsingAnimation()

  // ----- Give-up actions -----
  const startGiveUp = useCallback(
    (onComplete: () => void) => {
      giveUpIsCancelledRef.current = false
      setIsGiveUpAnimating(true)

      giveUpPulsing.start({
        duration: GIVE_UP_ANIMATION.duration,
        pulses: GIVE_UP_ANIMATION.pulses,
        onProgress: (pulseProgress) => {
          if (!giveUpIsCancelledRef.current) {
            setGiveUpProgress(pulseProgress)
          }
        },
        onComplete: () => {
          // Wait for cooldown before cleanup
          giveUpTimeoutRef.current = setTimeout(() => {
            if (!giveUpIsCancelledRef.current) {
              setGiveUpProgress(0)
              setIsGiveUpAnimating(false)
              onComplete()
            }
          }, GIVE_UP_ANIMATION.cooldownMs)
        },
      })
    },
    [giveUpPulsing]
  )

  const cancelGiveUp = useCallback(() => {
    giveUpIsCancelledRef.current = true
    giveUpPulsing.cancel()
    if (giveUpTimeoutRef.current) {
      clearTimeout(giveUpTimeoutRef.current)
      giveUpTimeoutRef.current = null
    }
    setGiveUpProgress(0)
    setIsGiveUpAnimating(false)
  }, [giveUpPulsing])

  // ----- Hint actions -----
  const startHint = useCallback(
    (onComplete: () => void) => {
      setIsHintAnimating(true)

      hintPulsing.start({
        duration: HINT_ANIMATION.duration,
        pulses: HINT_ANIMATION.pulses,
        onProgress: setHintProgress,
        onComplete: () => {
          setHintProgress(0)
          setIsHintAnimating(false)
          onComplete()
        },
      })
    },
    [hintPulsing]
  )

  const cancelHint = useCallback(() => {
    hintPulsing.cancel()
    setHintProgress(0)
    setIsHintAnimating(false)
  }, [hintPulsing])

  // ----- Celebration actions -----
  const startCelebration = useCallback(
    (celebrationType: CelebrationType) => {
      const timing = CELEBRATION_TIMING[celebrationType]

      // Calculate pulses based on celebration type (matching MapRenderer behavior)
      const pulses = celebrationType === 'lightning' ? 2 : celebrationType === 'standard' ? 3 : 4

      celebrationPulsing.start({
        duration: timing.totalDuration,
        pulses,
        onProgress: setCelebrationProgress,
        // No onComplete - animation runs until cancelled
      })
    },
    [celebrationPulsing]
  )

  const cancelCelebration = useCallback(() => {
    celebrationPulsing.cancel()
    setCelebrationProgress(0)
  }, [celebrationPulsing])

  // ----- Cancel all -----
  const cancelAll = useCallback(() => {
    cancelGiveUp()
    cancelHint()
    cancelCelebration()
  }, [cancelGiveUp, cancelHint, cancelCelebration])

  // ----- Return memoized controller -----
  return useMemo(
    () => ({
      // State
      giveUpProgress,
      isGiveUpAnimating,
      hintProgress,
      isHintAnimating,
      celebrationProgress,
      // Actions
      startGiveUp,
      cancelGiveUp,
      startHint,
      cancelHint,
      startCelebration,
      cancelCelebration,
      cancelAll,
    }),
    [
      giveUpProgress,
      isGiveUpAnimating,
      hintProgress,
      isHintAnimating,
      celebrationProgress,
      startGiveUp,
      cancelGiveUp,
      startHint,
      cancelHint,
      startCelebration,
      cancelCelebration,
      cancelAll,
    ]
  )
}