import { useState, useEffect, useRef } from "react"; import { TheAbstractionEngine, Game } from "../engine"; import { Miscellaneous } from "../engine/config"; import { Title } from "./Title"; export interface GameCanvasProps { width: number; height: number; } export const GameCanvas = ({ width, height }: GameCanvasProps) => { const canvasRef = useRef(null); const [game, setGame] = useState(); const [ready, setReady] = useState(true); // false); const [loading, setLoading] = useState(true); useEffect(() => { if (canvasRef.current && !game) { const canvas = canvasRef.current; const ctx = canvas.getContext("2d"); if (ctx) { const game = new Game(); const theAbstractionEngine = new TheAbstractionEngine(game, ctx); theAbstractionEngine.init().then(() => { theAbstractionEngine.play(); canvas.focus(); setGame(theAbstractionEngine); setLoading(false); }); return () => theAbstractionEngine.stop(); } } }, [canvasRef, ready]); if (ready) { return (
{loading && Loading...}
); } return (
</div> ); };