From 8d60c7f93407988ee0232ea90980028f299cb0f3 Mon Sep 17 00:00:00 2001 From: srdusr Date: Fri, 26 Sep 2025 13:39:28 +0200 Subject: Initial Commit --- web/src/hooks/useCoreGame.ts | 80 ++++++++++++++++++++++++++++++++++++++++++++ web/src/hooks/useTheme.ts | 21 ++++++++++++ 2 files changed, 101 insertions(+) create mode 100644 web/src/hooks/useCoreGame.ts create mode 100644 web/src/hooks/useTheme.ts (limited to 'web/src/hooks') diff --git a/web/src/hooks/useCoreGame.ts b/web/src/hooks/useCoreGame.ts new file mode 100644 index 0000000..3aa69ec --- /dev/null +++ b/web/src/hooks/useCoreGame.ts @@ -0,0 +1,80 @@ +import { useEffect, useRef, useState } from 'react'; +import init, { TyperPunkGame as Game } from '@typerpunk/wasm'; + +export function useCoreGame() { + const [isLoading, setIsLoading] = useState(true); + const [error, setError] = useState(null); + const gameRef = useRef(null); + + useEffect(() => { + let mounted = true; + + const initGame = async () => { + try { + await init(); + if (!mounted) return; + + const game = new Game(); + gameRef.current = game; + setIsLoading(false); + } catch (err) { + console.error('Failed to initialize game:', err); + if (mounted) { + setError('Failed to initialize game'); + setIsLoading(false); + } + } + }; + + initGame(); + + return () => { + mounted = false; + if (gameRef.current) { + try { + gameRef.current.free(); + } catch (err) { + console.error('Error cleaning up game:', err); + } + gameRef.current = null; + } + }; + }, []); + + const resetGame = async () => { + if (gameRef.current) { + try { + gameRef.current.free(); + } catch (err) { + console.error('Error freeing old game:', err); + } + } + + try { + const game = new Game(); + gameRef.current = game; + } catch (err) { + console.error('Error resetting game:', err); + setError('Failed to reset game'); + } + }; + + const cleanupGame = () => { + if (gameRef.current) { + try { + gameRef.current.free(); + } catch (err) { + console.error('Error cleaning up game:', err); + } + gameRef.current = null; + } + }; + + return { + game: gameRef.current, + isLoading, + error, + resetGame, + cleanupGame + }; +} \ No newline at end of file diff --git a/web/src/hooks/useTheme.ts b/web/src/hooks/useTheme.ts new file mode 100644 index 0000000..a5457b6 --- /dev/null +++ b/web/src/hooks/useTheme.ts @@ -0,0 +1,21 @@ +import { useState, useEffect } from 'react'; +import { Theme } from '../types'; + +export const useTheme = () => { + const [theme, setTheme] = useState(() => { + const savedTheme = localStorage.getItem('theme'); + return (savedTheme as Theme) || Theme.Light; + }); + + useEffect(() => { + localStorage.setItem('theme', theme); + document.documentElement.classList.remove(Theme.Light, Theme.Dark); + document.documentElement.classList.add(theme); + }, [theme]); + + const toggleTheme = () => { + setTheme(prev => prev === Theme.Light ? Theme.Dark : Theme.Light); + }; + + return { theme, toggleTheme }; +}; \ No newline at end of file -- cgit v1.2.3