|
1 | | -import React, { useEffect, useRef } from "react"; |
2 | | -import { Card } from "antd"; |
3 | | -import { Game } from "../logic/game/game"; |
4 | | -import { keyCaptureStart, keyCaptureStop } from "../core/keycapture"; |
5 | | -import { gameRender } from "../logic/game/render"; |
| 1 | +import React, { useEffect, useRef, useState } from "react"; |
| 2 | +import { Button, Card } from "antd"; |
6 | 3 | import { GameAiEval } from "../logic/gameAi/GameAiEval"; |
7 | | -import { range } from "../core/common"; |
8 | | -import { GameAI } from "../logic/gameAi/GameAi"; |
9 | | -import { IASelectionFunctionType } from "../logic/ai/ga/gaProcesGenerationFunction"; |
| 4 | +import { Render, Events } from "matter-js"; |
| 5 | +import { renderPoint, renderLine } from "../utils/rendererUtils"; |
| 6 | +import { Game } from "../logic/game/game"; |
| 7 | +import { NeuralNet } from "../_old/logic/ai/nn/nn"; |
| 8 | + |
10 | 9 |
|
11 | 10 | const debug = false; |
12 | 11 |
|
13 | 12 | type TRunnerProps = { |
14 | 13 | capture?: boolean, |
| 14 | + snapshot: NeuralNet[] | undefined, |
15 | 15 | }; |
16 | 16 |
|
17 | | -type TRendererProps = { |
18 | | - width: number, |
19 | | - height: number, |
20 | | -}; |
| 17 | +const height = 300; |
| 18 | +const width = 300; |
21 | 19 |
|
22 | | -const Renderer: React.FC<TRendererProps> = ({ height, width }) => { |
| 20 | +export const PlayPage: React.FC<TRunnerProps> = ({ capture, snapshot }) => { |
| 21 | + const runningState = useState(false); |
| 22 | + const [evaler, setEvaler] = useState<GameAiEval | undefined>(undefined); |
23 | 23 | const ref = useRef<HTMLDivElement>(undefined as any); |
| 24 | + const [renderer, setRenderer] = useState<Render | undefined>(undefined); |
| 25 | + const setRunning = runningState[1]; |
| 26 | + |
| 27 | + const canStart = !(runningState[0] || !snapshot); |
24 | 28 |
|
25 | | - const init = () => { |
26 | | - setTimeout(() => { |
27 | | - // _init(); |
28 | | - }, (2000)); |
| 29 | + const start = () => { |
| 30 | + if (!canStart) return; |
| 31 | + setRunning(true); |
| 32 | + setEvaler(new GameAiEval(snapshot!, Game.SETTINGS_DEFAULT)); |
29 | 33 | } |
30 | 34 |
|
31 | | - const _init = async () => { |
32 | | - if (!ref.current) |
33 | | - return; |
34 | | - // const game = new Game(); |
| 35 | + const stop = () => { |
| 36 | + setRunning(false); |
| 37 | + } |
| 38 | + |
| 39 | + useEffect(() => { |
| 40 | + if (runningState[0]) return; |
| 41 | + if (renderer) |
| 42 | + Render.stop(renderer) |
| 43 | + setRenderer(undefined); |
| 44 | + if (ref.current) ref.current.innerHTML = ""; |
| 45 | + }, [renderer, runningState[0]]) |
| 46 | + |
| 47 | + useEffect(() => { |
| 48 | + if (!ref.current || !evaler || !runningState[0]) return; |
| 49 | + // if (!ref.current) return; |
35 | 50 | const element = ref.current; |
36 | 51 |
|
37 | | - const gameAi = new GameAI({ |
38 | | - aiParams: { |
39 | | - sensors: Game.SETTINGS_DEFAULT.ai.sensorSidesArrayAngle, |
40 | | - }, |
41 | | - gaInit: { |
42 | | - popSize: 5, |
43 | | - proccessFunction: { |
44 | | - breedingParents: 1, |
45 | | - mutationRate: .01, |
46 | | - selection: { |
47 | | - type: IASelectionFunctionType.percent, |
48 | | - value: 5, |
49 | | - } |
50 | | - } |
51 | | - }, |
52 | | - games: 2, |
53 | | - gameSettings: Game.SETTINGS_DEFAULT, |
54 | | - nnInit: { hiddenLayers: [5], }, |
55 | | - }, () => { |
56 | | - console.log("game ended"); |
| 52 | + const game = evaler.game; |
| 53 | + // const game = new Game(); |
| 54 | + const engine = game.engine; |
| 55 | + |
| 56 | + // create renderer |
| 57 | + const render = Render.create({ |
| 58 | + element, |
| 59 | + engine, |
| 60 | + options: { |
| 61 | + width, |
| 62 | + height, |
| 63 | + |
| 64 | + background: "#cccccc", |
| 65 | + wireframeBackground: "#0f0f13", |
| 66 | + wireframes: false, |
| 67 | + |
| 68 | + ...(debug |
| 69 | + ? { |
| 70 | + wireframes: true, |
| 71 | + showVelocity: true, |
| 72 | + showCollisions: true, |
| 73 | + showSeparations: true, |
| 74 | + showAngleIndicator: true, |
| 75 | + } : {}), |
| 76 | + } as any, |
57 | 77 | }); |
58 | 78 |
|
59 | | - range(0).forEach(x => { |
60 | | - console.log(`generation ${x} best: ${gameAi.gann.ga.population[0].fitness}`); |
61 | | - gameAi.next(() => { |
62 | | - console.log("game ended"); |
| 79 | + setRenderer(render); |
| 80 | + |
| 81 | + // Render.run(render); |
| 82 | + |
| 83 | + // fit the render viewport to the scene |
| 84 | + (Render as any).lookAt(render, { |
| 85 | + min: { x: 0, y: 0 }, |
| 86 | + max: { x: game.settings.map.size, y: game.settings.map.size }, |
| 87 | + }); |
| 88 | + |
| 89 | + Events.on(render, "afterRender", () => { |
| 90 | + game.gameState.players.forEach((_p, pi) => { |
| 91 | + const res = game.sensor(pi); |
| 92 | + const playerPos = game.gameState.players[pi].body.position; |
| 93 | + |
| 94 | + res.forEach(res => { |
| 95 | + renderPoint(render, res.point); |
| 96 | + renderLine(render, playerPos, res.point); |
| 97 | + }); |
63 | 98 | }); |
64 | | - }) |
| 99 | + }); |
65 | 100 |
|
66 | | - // const botsNNs = range(2).map(() => GameAiEval.initializeRandomBot( |
67 | | - // { hiddens: [8, 5, 3], sensors: GameAI.defaultInitParams.aiParams.sensors } |
68 | | - // )) |
| 101 | + const targetDelta = game.settings.simulation.delta; |
69 | 102 |
|
70 | | - const pop = gameAi.gann.ga.population; |
71 | | - const botsNNs = [pop[0].dna, pop[1].dna]; |
| 103 | + let last = Date.now(); |
| 104 | + const step = () => { |
| 105 | + const now = Date.now(); |
| 106 | + const delta = now - last; |
| 107 | + if (delta < targetDelta) return; |
72 | 108 |
|
73 | | - const gameAiEval = new GameAiEval(botsNNs, Game.SETTINGS_DEFAULT); |
| 109 | + last += delta; |
| 110 | + // game.next(); |
| 111 | + evaler.next(); |
| 112 | + step(); |
| 113 | + }; |
74 | 114 |
|
75 | | - const aiInput = () => gameAiEval.calculateBotResponse(); |
| 115 | + const rafLoop = () => { |
| 116 | + step(); |
| 117 | + if (!game.isGameOver) { |
| 118 | + // if ([runningState[0]]) |
| 119 | + requestAnimationFrame(rafLoop); |
| 120 | + } else { |
76 | 121 |
|
77 | | - // keyCaptureStart(); |
78 | | - // const userInput = () => { |
79 | | - // const walk = capturedKeys.has("ArrowUp"); |
80 | | - // const rotate = capturedKeys.has("ArrowLeft") ? -1 |
81 | | - // : capturedKeys.has("ArrowRight") ? 1 |
82 | | - // : 0 |
83 | | - // ; |
84 | | - // const use = capturedKeys.has(" "); |
85 | | - // return { players: [{ walk, rotate, use } as any] }; |
86 | | - // }; |
| 122 | + // todo: print winner on canvas |
| 123 | + console.log(`winner is ${game.gameState.winner}`); |
| 124 | + } |
| 125 | + }; |
| 126 | + requestAnimationFrame(rafLoop); |
87 | 127 |
|
88 | | - gameRender({ |
89 | | - element, game: gameAiEval.game, height, width, debug, input: aiInput, |
90 | | - }); |
91 | | - }; |
92 | | - useEffect(init, []); |
| 128 | + Render.run(render); |
| 129 | + }, [evaler]) |
93 | 130 |
|
94 | | - return <> |
95 | | - <div style={{ justifyContent: "center", display: "flex" }}> |
96 | | - <div ref={ref} /> |
97 | | - </div> |
98 | | - </>; |
99 | | -}; |
| 131 | + const button = runningState[0] |
| 132 | + ? <Button onClick={stop}>Stop</Button> |
| 133 | + : <Button onClick={start} disabled={!canStart}>Play</Button> |
| 134 | + ; |
100 | 135 |
|
101 | | -export const PlayPage: React.FC<TRunnerProps> = ({ capture }) => { |
102 | | - if (capture) |
103 | | - keyCaptureStart(); |
104 | | - else |
105 | | - keyCaptureStop(); |
106 | 136 |
|
107 | 137 | return <> |
108 | | - <Card title="Play"> |
109 | | - <Renderer {...{ width: 500, height: 500 }} /> |
| 138 | + <Card title="Play" extra={button}> |
| 139 | + <div style={{ justifyContent: "center", display: "flex" }}> |
| 140 | + <div ref={ref} /> |
| 141 | + </div> |
110 | 142 | </Card> |
111 | 143 | </>; |
112 | 144 | }; |
0 commit comments