MCPcopy Create free account
hub / github.com/code100x/plinkoo / Simulate

Function Simulate

frontend/src/components/Simulate.tsx:8–68  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

6import { WIDTH } from "../game/constants";
7
8export const Simulate = () => {
9 const navigate = useNavigate();
10
11 const canvasRef = useRef<any>();
12 let [outputs, setOutputs] = useState<{ [key: number]: number[] }>({
13 0: [],
14 1: [],
15 2: [],
16 3: [],
17 4: [],
18 5: [],
19 6: [],
20 7: [],
21 8: [],
22 9: [],
23 10: [],
24 11: [],
25 12: [],
26 13: [],
27 14: [],
28 15: [],
29 16: [],
30 17: [],
31 });
32
33 async function simulate(ballManager: BallManager) {
34 let i = 0;
35 while (1) {
36 i++;
37 ballManager.addBall(pad(WIDTH / 2 + 20 * (Math.random() - 0.5)));
38 await new Promise((resolve) => setTimeout(resolve, 1000));
39 }
40 }
41
42 useEffect(() => {
43 if (canvasRef.current) {
44 const ballManager = new BallManager(
45 canvasRef.current as unknown as HTMLCanvasElement,
46 (index: number, startX?: number) => {
47 setOutputs((outputs: any) => {
48 return {
49 ...outputs,
50 [index]: [...(outputs[index] as number[]), startX],
51 };
52 });
53 }
54 );
55 simulate(ballManager);
56
57 return () => {
58 ballManager.stop();
59 };
60 }
61 }, [canvasRef]);
62
63 return (
64 <div className="flex flex-col items-center justify-center">
65 <canvas ref={canvasRef} width="800" height="800"></canvas>

Callers

nothing calls this directly

Calls 2

stopMethod · 0.95
simulateFunction · 0.70

Tested by

no test coverage detected