feat: implement pixel-based spaceship animation with 100ms movement duration
Refactor spaceship positioning from column-based to pixel-based coordinates with time-based interpolation, enabling smooth animation to letter positions in exactly 100ms regardless of distance. Fixes coordinate mismatch that prevented spaceship from moving to spawned letters.
This commit is contained in:
+16
-6
@@ -1,4 +1,4 @@
|
||||
import { useState, useEffect, useCallback } from "react";
|
||||
import { useState, useEffect } from "react";
|
||||
import type { GameManager, GameOptions, KeyLogEntry } from "../types";
|
||||
import { useBoard } from "../useBoard";
|
||||
import { useCursor } from "../useCursor";
|
||||
@@ -15,14 +15,24 @@ export function useGame(
|
||||
const cursorManager = useCursor();
|
||||
|
||||
// Wrap renderBoard to include player position
|
||||
const renderBoardWrapped = useCallback((letters: any = []) => {
|
||||
const renderBoardWrapped = (letters: any = []) => {
|
||||
boardManager.renderBoard(cursorManager.position(), letters);
|
||||
}, [boardManager, cursorManager]);
|
||||
};
|
||||
|
||||
// Game loop tick handler
|
||||
const handleGameLoopTick = useCallback((_deltaTime: number, letters: any) => {
|
||||
const handleGameLoopTick = (deltaTime: number, letters: any) => {
|
||||
// If a letter just spawned, move ship to it
|
||||
if (letters.length > 0) {
|
||||
const letter = letters[0];
|
||||
console.log('Setting target to:', letter.x);
|
||||
cursorManager.setTargetX(letter.x);
|
||||
}
|
||||
|
||||
// Move spaceship towards target
|
||||
cursorManager.moveTowardTarget(deltaTime);
|
||||
|
||||
renderBoardWrapped(letters);
|
||||
}, [renderBoardWrapped]);
|
||||
};
|
||||
|
||||
const gameStatusManager = useGameStatus(handleGameLoopTick);
|
||||
const scoreManager = useScore(gameStatusManager);
|
||||
@@ -30,7 +40,7 @@ export function useGame(
|
||||
// Initial render on mount
|
||||
useEffect(() => {
|
||||
renderBoardWrapped();
|
||||
}, [renderBoardWrapped]);
|
||||
}, []);
|
||||
|
||||
const gameManager: GameManager = {
|
||||
containerRef: boardManager.containerRef,
|
||||
|
||||
Reference in New Issue
Block a user