organized types

This commit is contained in:
2025-10-19 16:11:38 +01:00
parent 252027dab2
commit a69b7bbb5f
27 changed files with 231 additions and 203 deletions
+15 -161
View File
@@ -1,48 +1,12 @@
import type { RefObject } from 'react';
import { MazeRenderer, MazeGenerator } from "./useBoard";
export * from './useGame';
export * from './useBoard';
export * from './useCursor';
export * from './useScore';
export type GameStatus = 'waiting' | 'started' | 'has-key' | 'paused' | 'game-over' | 'game-won'
export type GameManager = {
containerRef: RefObject<HTMLDivElement | null>;
renderBoard: () => void;
cursor: CursorManager;
hero: HeroManager;
renderer: HeroRenderer;
scoreManager: ScoreManager;
} & GameStatusManager & GameKeyManager;
export type BoardManager = {
containerRef: RefObject<HTMLDivElement | null>
mazeRef: RefObject<MazeCell[][]>
mazeInstanceRef: RefObject<MazeGenerator | null>
rendererRef: RefObject<MazeRenderer | null>
renderBoard: () => void
}
export type PositionTag = "entrance" | "exit" | "key" | "hero";
export type Coord = { row: number, col: number };
export type CellTag = "wall" | "door" | PositionTag;
export type MazeCell = CellTag[];
export type MazeData = {
maze: MazeCell[][];
width: number;
height: number;
cols: number;
rows: number;
totalSteps: number;
}
import type { CursorManager, HeroManager, HeroRenderManager } from './useCursor/types';
import type { GameStatusManager } from './useGameStatus/types';
import type { ScoreManager } from './useScore/types';
export * from './useBoard/types';
export * from './useCursor/types';
export * from './useGameStatus/types';
export * from './useScore/types';
export type GameOptions = {
rows: number;
@@ -57,121 +21,11 @@ export type GameKeyManager = {
getKeyLog: () => KeyLogEntry[];
};
export type GameStatusManager = {
gameStatus: GameStatus;
setGameStatus: (status: GameStatus) => void;
startGame: () => void;
togglePause: (pause?: boolean) => void;
quitGame: () => void;
};
//Cursor Stuff
export type CursorMode = 'normal' | 'insert' | 'replace' | 'visual' | 'visual-line';
export type UseHeroType = {
position: Coord | null;
canMoveTo: (coord: Coord) => boolean;
moveTo: (coord: Coord) => void;
pickupKey: () => void;
reachExit: () => void;
reset: () => void;
heroPos: Coord | null;
setHeroPos: (pos: Coord | null) => void;
moveHero: (dr: number, dc: number, steps: number, gameStatus: GameStatus, setGameStatus: (s: GameStatus) => void) => void;
};
export type CursorManager = {
position: () => Coord;
mode: () => CursorMode;
move: (dCols: number, dRows: number, count: number) => void;
moveLeft: (count?: number) => void;
moveRight: (count?: number) => void;
moveUp: (count?: number) => void;
moveDown: (count?: number) => void;
moveToStart: () => void;
moveToEnd: () => void;
moveToTop: () => void;
moveToBottom: () => void;
repeatLastMotion: () => void;
resetCount: () => void;
getCount: () => string;
hasCount: () => boolean;
setCount: (digit: string) => void;
setLastKey: (key: string) => void;
getLastKey: () => string;
hero?: UseHeroType;
}
export type HeroManager = {
position: Coord | null;
canMoveTo: (coord: Coord) => boolean;
moveTo: (coord: Coord) => void;
pickupKey: () => void;
reachExit: () => void;
reset: () => void;
}
export type HeroRenderer = {
render: () => void;
updatePosition: (coord: Coord) => void;
showCoordinates: (show: boolean) => void;
}
// Legacy type for backward compatibility
export type Cursor = CursorManager;
export type Motion = { dr: number; dc: number; steps: number };
export type VimMotionSystem = {
processKey: (key: string) => { type: 'move'|'anchor'|'repeat'|'count', data: any } | null;
resetCount: () => void;
getCount: () => number;
hasCount: () => boolean;
setLastMotion: (motion: Motion) => void;
repeatLastMotion: () => Motion | null;
};
export type AnimationSystem = {
animateMovement: (from: Coord, to: Coord, steps: number, onStep: (coord: Coord) => void, onComplete: () => void) => void;
cancelAnimation: () => void;
isAnimating: () => boolean;
};
export type MazeNavigator = {
isValidMove: (from: Coord, to: Coord) => boolean;
findAnchorTarget: (from: Coord, direction: 'left'|'right'|'top'|'bottom') => Coord | null;
validatePath: (from: Coord, direction: Coord, steps: number) => boolean;
};
export type HeroRenderManager = {
render: () => void;
updatePosition: (coord: Coord) => void;
showCoordinates: (show: boolean) => void;
};
export type TimerManager = {
timeValue: number;
startTimer: () => void;
stopTimer: () => void;
resetTimer: () => void;
};
export type ScorePathsResult = {
entranceToKey: number;
entranceToExit: number;
heroToKey: number;
heroToExit: number;
};
export type ScoreManager = {
timeValue: number;
startTimer: () => void;
stopTimer: () => void;
resetTimer: () => void;
distToKey: number;
distToExit: number;
keystrokes: number;
optimalSteps: number;
efficiency: number;
finalScore: number | null;
};
export type GameManager = {
containerRef: RefObject<HTMLDivElement | null>;
renderBoard: () => void;
cursor: CursorManager;
hero: HeroManager;
renderer: HeroRenderManager;
scoreManager: ScoreManager;
} & GameStatusManager & GameKeyManager;
+1 -1
View File
@@ -1,4 +1,4 @@
import type { PositionTag, Coord, CellTag, MazeCell, MazeData } from "../types";
import type { PositionTag, Coord, CellTag, MazeCell, MazeData } from "./types";
export class MazeGenerator {
width: number;
+1 -1
View File
@@ -1,4 +1,4 @@
import type { MazeData } from "../types";
import type { MazeData } from "./types";
export class MazeRenderer {
private mazeData: MazeData;
+1
View File
@@ -2,3 +2,4 @@ export * from './useBoard';
export * from './MazeGenerator';
export * from './MazeRenderer';
export * from './useMazeNavigation';
export * from './types';
+32
View File
@@ -0,0 +1,32 @@
import type { RefObject } from 'react';
export type PositionTag = "entrance" | "exit" | "key" | "hero";
export type Coord = { row: number; col: number };
export type CellTag = "wall" | "door" | PositionTag;
export type MazeCell = CellTag[];
export type MazeData = {
maze: MazeCell[][];
width: number;
height: number;
cols: number;
rows: number;
totalSteps: number;
};
export type BoardManager = {
containerRef: RefObject<HTMLDivElement | null>;
mazeRef: RefObject<MazeCell[][]>;
mazeInstanceRef: RefObject<any>;
rendererRef: RefObject<any>;
renderBoard: () => void;
};
export type MazeNavigator = {
isValidMove: (from: Coord, to: Coord) => boolean;
findAnchorTarget: (from: Coord, direction: 'left' | 'right' | 'top' | 'bottom') => Coord | null;
validatePath: (from: Coord, direction: Coord, steps: number) => boolean;
};
+1 -1
View File
@@ -1,7 +1,7 @@
import { useEffect, useRef } from "react";
import { MazeGenerator } from "./MazeGenerator";
import { MazeRenderer } from "./MazeRenderer";
import type { MazeCell } from "../types";
import type { MazeCell } from "./types";
export function useBoard(cols: number, rows: number) {
const containerRef = useRef<HTMLDivElement | null>(null);
+2 -2
View File
@@ -1,6 +1,6 @@
import type { Coord, MazeCell, MazeNavigator } from '../types';
import type { Coord, MazeCell, MazeNavigator } from './types';
export type { MazeNavigator } from '../types';
export type { MazeNavigator } from './types';
export function useMazeNavigation(maze: MazeCell[][]): MazeNavigator {
function isValidMove(_from: Coord, to: Coord): boolean {
+1
View File
@@ -1,3 +1,4 @@
export * from './types';
export * from './useCursor';
export * from './useHero';
export * from './useHeroRender';
+74
View File
@@ -0,0 +1,74 @@
import type { Coord } from '../useBoard/types';
import type { GameStatus } from '../useGameStatus/types';
export type { Coord } from '../useBoard/types';
export type CursorMode = 'normal' | 'insert' | 'replace' | 'visual' | 'visual-line';
export type Motion = { dr: number; dc: number; steps: number };
export type VimMotionSystem = {
processKey: (key: string) => { type: 'move' | 'anchor' | 'repeat' | 'count'; data: any } | null;
resetCount: () => void;
getCount: () => number;
hasCount: () => boolean;
setLastMotion: (motion: Motion) => void;
repeatLastMotion: () => Motion | null;
};
export type AnimationSystem = {
animateMovement: (from: Coord, to: Coord, steps: number, onStep: (coord: Coord) => void, onComplete: () => void) => void;
cancelAnimation: () => void;
isAnimating: () => boolean;
};
export type UseHeroType = {
position: Coord | null;
canMoveTo: (coord: Coord) => boolean;
moveTo: (coord: Coord) => void;
pickupKey: () => void;
reachExit: () => void;
reset: () => void;
heroPos: Coord | null;
setHeroPos: (pos: Coord | null) => void;
moveHero: (dr: number, dc: number, steps: number, gameStatus: GameStatus, setGameStatus: (s: GameStatus) => void) => void;
};
export type CursorManager = {
position: () => Coord;
mode: () => CursorMode;
move: (dCols: number, dRows: number, count: number) => void;
moveLeft: (count?: number) => void;
moveRight: (count?: number) => void;
moveUp: (count?: number) => void;
moveDown: (count?: number) => void;
moveToStart: () => void;
moveToEnd: () => void;
moveToTop: () => void;
moveToBottom: () => void;
repeatLastMotion: () => void;
resetCount: () => void;
getCount: () => string;
hasCount: () => boolean;
setCount: (digit: string) => void;
setLastKey: (key: string) => void;
getLastKey: () => string;
hero?: UseHeroType;
};
export type HeroManager = {
position: Coord | null;
canMoveTo: (coord: Coord) => boolean;
moveTo: (coord: Coord) => void;
pickupKey: () => void;
reachExit: () => void;
reset: () => void;
};
export type HeroRenderManager = {
render: () => void;
updatePosition: (coord: Coord) => void;
showCoordinates: (show: boolean) => void;
};
export type Cursor = CursorManager;
+2 -2
View File
@@ -1,7 +1,7 @@
import { useRef } from 'react';
import type { Coord, AnimationSystem } from '../types';
import type { Coord, AnimationSystem } from './types';
export type { AnimationSystem } from '../types';
export type { AnimationSystem } from './types';
export function useAnimation(): AnimationSystem {
const animatingRef = useRef(false);
+3 -1
View File
@@ -1,5 +1,7 @@
import { useRef, useEffect } from 'react'
import type { CursorMode, Coord, BoardManager, GameStatusManager } from '../types'
import type { CursorMode, Coord } from './types';
import type { BoardManager } from '../useBoard/types';
import type { GameStatusManager } from '../useGameStatus/types';
import { useHero } from './useHero';
import { useGameKeys } from './useGameKeys';
import { useMazeNavigation } from '../useBoard';
+3 -1
View File
@@ -1,4 +1,6 @@
import type { GameStatusManager, KeyLogEntry, Cursor } from "../types";
import type { GameStatusManager } from "../useGameStatus/types";
import type { KeyLogEntry } from "../types";
import type { Cursor } from "./types";
import { useEffect, useRef, useState } from "react";
+3 -1
View File
@@ -1,5 +1,7 @@
import { useState, useEffect } from "react";
import type { BoardManager, GameStatusManager, GameStatus, Coord, UseHeroType } from "../types";
import type { BoardManager } from "../useBoard/types";
import type { GameStatusManager, GameStatus } from "../useGameStatus/types";
import type { UseHeroType, Coord } from "./types";
import { useHeroRender } from "./useHeroRender";
import { useMazeNavigation } from "../useBoard";
import { useAnimation } from "./useAnimation";
+4 -2
View File
@@ -1,7 +1,9 @@
import { useEffect } from "react";
import type { BoardManager, GameStatus, Coord, UseHeroType, HeroRenderManager } from "../types";
import type { BoardManager } from "../useBoard/types";
import type { GameStatus } from "../useGameStatus/types";
import type { UseHeroType, HeroRenderManager, Coord } from "./types";
export type { HeroRenderManager } from "../types";
export type { HeroRenderManager } from "./types";
type UseHeroRenderParams = {
gameStatus: GameStatus;
+3 -3
View File
@@ -1,8 +1,8 @@
import { useRef } from 'react';
import type { VimMotionSystem, Motion } from '../types';
import type { VimMotionSystem, Motion } from './types';
export type { Motion } from '../types';
export type { VimMotionSystem } from '../types';
export type { Motion } from './types';
export type { VimMotionSystem } from './types';
export function useVimMotions(): VimMotionSystem {
const countRef = useRef<string>('');
+1
View File
@@ -1 +1,2 @@
export * from './types';
export * from './useGameStatus';
+9
View File
@@ -0,0 +1,9 @@
export type GameStatus = 'waiting' | 'started' | 'has-key' | 'paused' | 'game-over' | 'game-won';
export type GameStatusManager = {
gameStatus: GameStatus;
setGameStatus: (status: GameStatus) => void;
startGame: () => void;
togglePause: (pause?: boolean) => void;
quitGame: () => void;
};
+2 -1
View File
@@ -1,5 +1,6 @@
import { useState } from 'react';
import type { GameStatus, BoardManager, Coord } from '../types';
import type { GameStatus } from './types';
import type { BoardManager, Coord } from '../useBoard/types';
export function useGameStatus(board?: BoardManager, setHeroPos?: (pos: Coord) => void) {
const [gameStatus, setGameStatus] = useState<GameStatus>('waiting');
+1
View File
@@ -1,3 +1,4 @@
export * from './types';
export * from './useScore';
export * from './useScorePaths';
export * from './useScoreTime';
+26
View File
@@ -0,0 +1,26 @@
export type TimerManager = {
timeValue: number;
startTimer: () => void;
stopTimer: () => void;
resetTimer: () => void;
};
export type ScorePathsResult = {
entranceToKey: number;
entranceToExit: number;
heroToKey: number;
heroToExit: number;
};
export type ScoreManager = {
timeValue: number;
startTimer: () => void;
stopTimer: () => void;
resetTimer: () => void;
distToKey: number;
distToExit: number;
keystrokes: number;
optimalSteps: number;
efficiency: number;
finalScore: number | null;
};
+3 -1
View File
@@ -2,7 +2,9 @@ import { useEffect, useRef, useState } from "react";
import { useTimer } from "./useTimer";
import { useScoreTime } from "./useScoreTime";
import { useScorePaths } from "./useScorePaths";
import type { BoardManager, GameStatusManager, UseHeroType } from "../types";
import type { BoardManager } from "../useBoard/types";
import type { GameStatusManager } from "../useGameStatus/types";
import type { UseHeroType } from "../useCursor/types";
type UseScoreParams = {
board: BoardManager;
+3 -2
View File
@@ -1,7 +1,8 @@
import { useEffect, useState } from "react";
import type { BoardManager, Coord, ScorePathsResult } from "../types";
import type { BoardManager, Coord } from "../useBoard/types";
import type { ScorePathsResult } from "./types";
export type { ScorePathsResult } from "../types";
export type { ScorePathsResult } from "./types";
type UseScorePathsParams = {
board: BoardManager;
+2 -1
View File
@@ -1,5 +1,6 @@
import { useEffect, useRef } from "react";
import type { TimerManager, GameStatus } from "../types";
import type { TimerManager } from "./types";
import type { GameStatus } from "../useGameStatus/types";
type UseScoreTimeParams = {
gameStatus: GameStatus;
+2 -2
View File
@@ -1,7 +1,7 @@
import { useState, useRef, useEffect } from "react";
import type { TimerManager } from '../types';
import type { TimerManager } from './types';
export type { TimerManager } from '../types';
export type { TimerManager } from './types';
export function useTimer(): TimerManager {
const [timeValue, setTimeValue] = useState(0);