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 | /** * Map Game Context * * Provides game-specific state and utilities to child components, * avoiding deep prop drilling while maintaining type safety. * * This context consolidates game state that's needed by magnifier, * overlays, and other sub-components: * - Map data and regions * - Game progress (found regions, prompts) * - Celebration/give-up animations * - Hot/cold feedback state * - Debug settings * - Multiplayer state */ 'use client' import { createContext, useContext, useMemo, type ReactNode } from 'react' import type { MapData, MapRegion } from '../../types' import type { BoundingBox } from '../../utils/adaptiveZoomSearch' import type { CrosshairStyle } from '../magnifier/types' // ============================================================================ // Types // ============================================================================ export interface CelebrationState { regionId: string } export interface GiveUpRevealState { regionId: string } export interface MagnifierBorderStyle { border: string glow: string width: number } // ============================================================================ // Context Value Type // ============================================================================ export interface MapGameContextValue { // ------------------------------------------------------------------------- // Map Data // ------------------------------------------------------------------------- /** Full map data including regions and viewBox */ mapData: MapData /** Original viewBox string */ displayViewBox: string // ------------------------------------------------------------------------- // Game Progress // ------------------------------------------------------------------------- /** IDs of regions that have been found */ regionsFound: string[] /** Currently hovered region ID */ hoveredRegion: string | null /** Set hovered region */ setHoveredRegion: (regionId: string | null) => void /** Current prompt (region to find) */ currentPrompt: string | null // ------------------------------------------------------------------------- // Celebration & Give-up Animations // ------------------------------------------------------------------------- /** Current celebration state */ celebration: CelebrationState | null /** Current give-up reveal state */ giveUpReveal: GiveUpRevealState | null /** Whether give-up animation is in progress */ isGiveUpAnimating: boolean /** Celebration flash progress (0-1) */ celebrationFlashProgress: number /** Give-up flash progress (0-1) */ giveUpFlashProgress: number // ------------------------------------------------------------------------- // Hot/Cold Feedback // ------------------------------------------------------------------------- /** Whether hot/cold feedback is enabled */ effectiveHotColdEnabled: boolean /** Current hot/cold feedback type */ hotColdFeedbackType: string | null /** Magnifier border style based on heat */ magnifierBorderStyle: MagnifierBorderStyle /** Crosshair style based on heat */ crosshairHeatStyle: CrosshairStyle // ------------------------------------------------------------------------- // Debug // ------------------------------------------------------------------------- /** Whether to show debug bounding boxes */ effectiveShowDebugBoundingBoxes: boolean /** Whether to show magnifier debug info */ effectiveShowMagnifierDebugInfo: boolean /** Debug bounding boxes for visualization */ debugBoundingBoxes: BoundingBox[] // ------------------------------------------------------------------------- // Multiplayer // ------------------------------------------------------------------------- /** Game mode */ gameMode?: 'cooperative' | 'race' | 'turn-based' /** Current player (for turn-based) */ currentPlayer?: string | null /** Local player ID */ localPlayerId?: string // ------------------------------------------------------------------------- // Callbacks // ------------------------------------------------------------------------- /** Get player who found a region */ getPlayerWhoFoundRegion: (regionId: string) => string | null /** Whether to show outline for a region */ showOutline: (region: MapRegion) => boolean /** Handle region click with celebration */ handleRegionClickWithCelebration: (regionId: string, regionName: string) => void /** Select region at crosshairs (center of magnifier) */ selectRegionAtCrosshairs: () => void /** Request pointer lock for precision mode */ requestPointerLock: () => void } // ============================================================================ // Context Creation // ============================================================================ const MapGameContext = createContext<MapGameContextValue | null>(null) // ============================================================================ // Provider Component // ============================================================================ export interface MapGameProviderProps { children: ReactNode value: MapGameContextValue } /** * Provider for Map Game context. * * This provider should wrap components that need access to game state * without receiving it through props. * * @example * ```tsx * <MapGameProvider value={gameContextValue}> * <MagnifierOverlay /> * <HotColdDebugPanel /> * </MapGameProvider> * ``` */ export function MapGameProvider({ children, value }: MapGameProviderProps) { // Memoize to prevent unnecessary re-renders const memoizedValue = useMemo( () => value, [ // Map Data value.mapData, value.displayViewBox, // Game Progress value.regionsFound, value.hoveredRegion, value.setHoveredRegion, value.currentPrompt, // Animations value.celebration, value.giveUpReveal, value.isGiveUpAnimating, value.celebrationFlashProgress, value.giveUpFlashProgress, // Hot/Cold value.effectiveHotColdEnabled, value.hotColdFeedbackType, value.magnifierBorderStyle, value.crosshairHeatStyle, // Debug value.effectiveShowDebugBoundingBoxes, value.effectiveShowMagnifierDebugInfo, value.debugBoundingBoxes, // Multiplayer value.gameMode, value.currentPlayer, value.localPlayerId, // Callbacks value.getPlayerWhoFoundRegion, value.showOutline, value.handleRegionClickWithCelebration, value.selectRegionAtCrosshairs, value.requestPointerLock, ] ) return <MapGameContext.Provider value={memoizedValue}>{children}</MapGameContext.Provider> } // ============================================================================ // Hooks // ============================================================================ /** * Access Map Game context. * * @throws Error if used outside of MapGameProvider * @returns Map Game context value * * @example * ```tsx * function MagnifierRegions() { * const { mapData, regionsFound, hoveredRegion } = useMapGameContext() * // ... * } * ``` */ export function useMapGameContext(): MapGameContextValue { const context = useContext(MapGameContext) if (!context) { throw new Error('useMapGameContext must be used within a MapGameProvider') } return context } /** * Safely access Map Game context (returns null if not available). * * Useful for components that can optionally use the context. * * @returns Map Game context value or null */ export function useMapGameContextSafe(): MapGameContextValue | null { return useContext(MapGameContext) } |