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 261 262 263 264 265 266 267 268 269 270 271 | 2x 2x 96x 96x 96x 96x 96x 3x 3x 3x 3x 3x 3x 3x 96x 14x 14x 14x 14x 96x 47x 47x 96x 82x 49x 25x 25x 2x 2x 2x 23x 1x 1x 1x 22x 22x 13x 13x 1x 1x 1x 1x 12x 12x 4x 8x 8x 8x 8x 8x 8x 9x 9x 9x 9x 2x 2x 2x 2x 1x 1x 1x 1x 4x 4x 1x 1x 1x 1x 1x 1x 1x 1x 49x 49x 96x | /**
* Custom hook for keyboard controls with visual feedback
* Manages keyboard input for trigram stance switching and combat actions
*
* @module hooks/useKeyboardControls
* @category Input System
* @korean 키보드 컨트롤 훅
*/
import { useCallback, useEffect, useRef, useState } from "react";
import { ControlMapper } from "../utils/controlMapping";
/**
* Queued input for input buffer display
*/
export interface QueuedInput {
readonly action: string;
readonly key: string;
readonly timestamp: number;
}
/**
* Props for useKeyboardControls hook
*/
export interface UseKeyboardControlsProps {
/** Callback when stance changes (receives stance index 0-7) */
readonly onStanceChange: (stance: number) => void;
/** Callback for combat actions (attack, block, etc.) */
readonly onAction: (action: string) => void;
/** Whether keyboard input is enabled */
readonly enabled?: boolean;
/** Current stance index (0-7) for validation */
readonly currentStance?: number;
/** Callback when hints toggle is requested */
readonly onToggleHints?: () => void;
/** Play sound effect function */
readonly playSFX?: (soundId: string) => void;
}
/**
* Return type for useKeyboardControls hook
*/
export interface UseKeyboardControlsReturn {
/** Queued inputs for display */
readonly queuedInputs: readonly QueuedInput[];
/** Whether hints are visible */
readonly showHints: boolean;
/** Toggle hints visibility */
readonly toggleHints: () => void;
}
/**
* Maximum number of inputs to keep in queue
*/
const MAX_QUEUE_SIZE = 3;
/**
* Time to keep inputs in queue (milliseconds)
*/
const QUEUE_RETENTION_TIME = 2000;
/**
* Custom hook for handling keyboard controls with visual feedback
*
* Features:
* - Trigram stance switching (1-8 keys or custom bindings)
* - Combat action handling (attack, block, movement)
* - Input queue visualization
* - Keyboard hints toggle (F1)
* - Invalid input prevention
* - Audio feedback integration
*
* @example
* ```typescript
* const { queuedInputs, showHints, toggleHints } = useKeyboardControls({
* onStanceChange: (stance) => handleStanceChange(stance),
* onAction: (action) => handleAction(action),
* enabled: !isPaused,
* currentStance: player.stance,
* playSFX: audio.playSFX,
* });
* ```
*
* @public
* @korean 키보드컨트롤사용
*/
export function useKeyboardControls({
onStanceChange,
onAction,
enabled = true,
currentStance = 0,
onToggleHints,
playSFX,
}: UseKeyboardControlsProps): UseKeyboardControlsReturn {
const [queuedInputs, setQueuedInputs] = useState<readonly QueuedInput[]>([]);
const [showHints, setShowHints] = useState(false);
const controlMapperRef = useRef<ControlMapper>(new ControlMapper());
const lastStanceChangeRef = useRef<number>(0);
/**
* Toggle hints visibility
*/
const toggleHints = useCallback(() => {
setShowHints((prev) => {
const newState = !prev;
Eif (onToggleHints) {
onToggleHints();
}
// Play UI sound
Eif (playSFX) {
playSFX("menu_select");
}
return newState;
});
}, [onToggleHints, playSFX]);
/**
* Add input to queue
*/
const addToQueue = useCallback((action: string, key: string) => {
const newInput: QueuedInput = {
action,
key,
timestamp: Date.now(),
};
setQueuedInputs((prev) => {
const updated = [newInput, ...prev].slice(0, MAX_QUEUE_SIZE);
return updated;
});
}, []);
/**
* Clean up old queued inputs
* Single interval for component lifetime to avoid recreating on every input
*/
useEffect(() => {
const interval = setInterval(() => {
const now = Date.now();
setQueuedInputs((prev) => {
if (prev.length === 0) return prev;
return prev.filter((input) => now - input.timestamp < QUEUE_RETENTION_TIME);
});
}, 500);
return () => clearInterval(interval);
}, []); // Empty deps - single interval for component lifetime
/**
* Handle keyboard input
*/
useEffect(() => {
if (!enabled) return;
const handleKeyDown = (e: KeyboardEvent) => {
const mapper = controlMapperRef.current;
// F1: Toggle hints (prevent default browser help)
if (e.key === "F1") {
e.preventDefault();
toggleHints();
return;
}
// Escape: Close hints if open
if (e.key === "Escape" && showHints) {
e.preventDefault();
setShowHints(false);
return;
}
// Check for stance change (1-8 or custom bindings)
const stanceIndex = mapper.getStanceForKey(e.key);
if (stanceIndex !== null) {
e.preventDefault();
// Prevent switching to the same stance
if (stanceIndex === currentStance) {
// Invalid input feedback (shake animation trigger)
addToQueue(`Stance ${stanceIndex + 1} (already active)`, e.key);
// Play error sound
Eif (playSFX) {
playSFX("menu_error");
}
return;
}
// Throttle stance changes (minimum 8 frames at 60fps = 133ms)
const now = Date.now();
if (now - lastStanceChangeRef.current < 133) {
return;
}
lastStanceChangeRef.current = now;
// Execute stance change
onStanceChange(stanceIndex);
addToQueue(`Stance ${stanceIndex + 1}`, e.key);
// Play stance change sound
Eif (playSFX) {
playSFX("stance_change");
}
return;
}
// Handle other actions
const action = mapper.getActionForKey(e.key);
Eif (action) {
e.preventDefault();
switch (action) {
case "attack":
onAction("attack");
addToQueue("Attack", e.key);
Eif (playSFX) playSFX("attack_light");
break;
case "block":
onAction("block");
addToQueue("Block", e.key);
Eif (playSFX) playSFX("block");
break;
case "move_up":
case "move_down":
case "move_left":
case "move_right":
onAction(action);
break;
case "precision":
onAction("precision");
addToQueue("Precision Mode", e.key);
Eif (playSFX) playSFX("menu_select");
break;
case "quick_switch":
onAction("quick_switch");
addToQueue("Quick Switch", e.key);
Eif (playSFX) playSFX("stance_change");
break;
case "reset":
onAction("reset");
addToQueue("Reset", e.key);
break;
}
}
};
window.addEventListener("keydown", handleKeyDown);
return () => window.removeEventListener("keydown", handleKeyDown);
}, [
enabled,
currentStance,
onStanceChange,
onAction,
addToQueue,
toggleHints,
showHints,
playSFX,
]);
return {
queuedInputs,
showHints,
toggleHints,
};
}
|