From 770d4c1d3d22279c70b28a6fefde5d3a046bd362 Mon Sep 17 00:00:00 2001 From: Balaj Marius Date: Fri, 18 Sep 2026 12:01:33 +0300 Subject: [PATCH 1/2] fix: reset game state and timing on restart --- src/context/game.tsx | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/src/context/game.tsx b/src/context/game.tsx index b75a473..c55bf3e 100644 --- a/src/context/game.tsx +++ b/src/context/game.tsx @@ -49,6 +49,7 @@ const GameContext = createContext(null); export const GameProvider = ({ children }: GameProviderProps) => { const frame = useRef(null); const fireEndsAt = useRef(null); + const startedAt = useRef(GAME_TICK_START); const lastBugTick = useRef(GAME_TICK_START); const lastBulletTick = useRef(GAME_TICK_START); @@ -61,10 +62,17 @@ export const GameProvider = ({ children }: GameProviderProps) => { const [fireLane, setFireLane] = useState(null); const handleStart = () => { + fireEndsAt.current = null; + startedAt.current = performance.now(); + lastBugTick.current = GAME_TICK_START; + lastBulletTick.current = GAME_TICK_START; + reset(); + setLane(OCTO_LANE_FIRST); + setFireLane(null); + setLanes(GAME_LANES); setGameState("on"); play("game/keyPress"); - setLanes(GAME_LANES); }; const handleBugs = useEventCallback(() => { @@ -216,14 +224,9 @@ export const GameProvider = ({ children }: GameProviderProps) => { return; } - const startedAt = performance.now(); - - lastBugTick.current = GAME_TICK_START; - lastBulletTick.current = GAME_TICK_START; - const update = (now: number) => { - const bugTick = Math.floor((now - startedAt) / BUG_TICK_MS); - const bulletTick = Math.floor((now - startedAt) / BULLET_TICK_MS); + const bugTick = Math.floor((now - startedAt.current) / BUG_TICK_MS); + const bulletTick = Math.floor((now - startedAt.current) / BULLET_TICK_MS); if (isNotNil(fireEndsAt.current) && now >= fireEndsAt.current) { setFireLane(null); From 131a7ae07ac7ff1936658ed986a10f3697d6eb28 Mon Sep 17 00:00:00 2001 From: Balaj Marius Date: Fri, 18 Sep 2026 12:10:46 +0300 Subject: [PATCH 2/2] refactor: group game handler state updates --- src/context/game.tsx | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/context/game.tsx b/src/context/game.tsx index c55bf3e..cc56d36 100644 --- a/src/context/game.tsx +++ b/src/context/game.tsx @@ -62,17 +62,17 @@ export const GameProvider = ({ children }: GameProviderProps) => { const [fireLane, setFireLane] = useState(null); const handleStart = () => { - fireEndsAt.current = null; - startedAt.current = performance.now(); - lastBugTick.current = GAME_TICK_START; - lastBulletTick.current = GAME_TICK_START; - reset(); setLane(OCTO_LANE_FIRST); setFireLane(null); setLanes(GAME_LANES); setGameState("on"); play("game/keyPress"); + + fireEndsAt.current = null; + startedAt.current = performance.now(); + lastBugTick.current = GAME_TICK_START; + lastBulletTick.current = GAME_TICK_START; }; const handleBugs = useEventCallback(() => { @@ -196,10 +196,11 @@ export const GameProvider = ({ children }: GameProviderProps) => { return; } - fireEndsAt.current = null; setFireLane(null); - play("game/keyPress"); + + fireEndsAt.current = null; + if (event.code === "Space") { handleFire(); }