Files
Space-Game/apps/game/src/ui/useKeyboardShortcuts.ts
francy51 7c93b8a1ae Add game UI panels, keyboard shortcuts, docs narrative overhaul, and unified dev script
- Add MiniStarMap, NpcMarketPanel, ShipStatusPanel, useKeyboardShortcuts
- Add progress bars for approach/mining operations and cargo fill indicator
- Rewrite docs from spreadsheet-first to exploration-first open-world RPG
- Replace dev:db + dev:standalone with unified dev script (scripts/dev.sh)
- Add Vite chunk splitting for three.js and spacetimedb
- Fix displayName dependency in useSpacetimeConnection
- Remove stale usePlayerSession.ts
- Add AGENTS.md files across all packages
2026-06-02 17:32:15 -04:00

69 lines
2.0 KiB
TypeScript

import { useEffect } from "react";
export type ShipActions = {
onUndock: () => void;
onStartApproach: () => void;
onDock: () => void;
onStartMining: () => void;
onSelectTarget?: (index: number) => void;
};
export function useKeyboardShortcuts(
ship: {
dockedStationId: string;
flightMode: string;
selectedPoiId: string;
currentPoiId: string;
} | undefined,
pois: Array<{ poiId: string; poiType: string }>,
operation: { operationType: string } | undefined,
actions: ShipActions,
) {
useEffect(() => {
function handleKeyDown(event: KeyboardEvent) {
if (event.target instanceof HTMLInputElement || event.target instanceof HTMLTextAreaElement) return;
const key = event.key.toLowerCase();
if (key === "u" && !operation) {
if (ship && ship.dockedStationId.length > 0) {
event.preventDefault();
actions.onUndock();
}
}
if (key === "d" && !operation) {
if (ship && ship.dockedStationId.length === 0 && ship.flightMode === "flight") {
event.preventDefault();
actions.onDock();
}
}
if (key === "a" && !operation) {
if (ship && ship.dockedStationId.length === 0 && ship.selectedPoiId.length > 0 && ship.selectedPoiId !== ship.currentPoiId) {
event.preventDefault();
actions.onStartApproach();
}
}
if (key === "m" && !operation) {
if (ship && ship.dockedStationId.length === 0 && ship.flightMode === "flight") {
event.preventDefault();
actions.onStartMining();
}
}
if (key >= "1" && key <= "9") {
const index = parseInt(key) - 1;
if (index < pois.length && actions.onSelectTarget) {
event.preventDefault();
actions.onSelectTarget(index);
}
}
}
window.addEventListener("keydown", handleKeyDown);
return () => window.removeEventListener("keydown", handleKeyDown);
}, [ship, pois, operation, actions]);
}