|
| 1 | +import { useCallback, useEffect, useRef } from 'react' |
| 2 | +import { IgnitionEnvTFJS } from '@ignitionai/backend-tfjs' |
| 3 | +import { DroneEnv } from './drone-env' |
| 4 | +import { Scene3D } from './Scene3D' |
| 5 | +import { HUD } from './HUD' |
| 6 | +import { Controls } from './Controls' |
| 7 | +import { useDemoStore } from './store' |
| 8 | + |
| 9 | +export default function App() { |
| 10 | + const envRef = useRef<DroneEnv>(new DroneEnv()) |
| 11 | + const trainerRef = useRef<IgnitionEnvTFJS | null>(null) |
| 12 | + |
| 13 | + const setMode = useDemoStore((s) => s.setMode) |
| 14 | + const setStats = useDemoStore((s) => s.setStats) |
| 15 | + const incrementEpisode = useDemoStore((s) => s.incrementEpisode) |
| 16 | + const resetStore = useDemoStore((s) => s.reset) |
| 17 | + |
| 18 | + // Sync store stats from the env on a timer so React isn't reading |
| 19 | + // the mutable env state on every render. |
| 20 | + useEffect(() => { |
| 21 | + const id = setInterval(() => { |
| 22 | + const env = envRef.current |
| 23 | + setStats({ |
| 24 | + totalSteps: trainerRef.current?.stepCount ?? 0, |
| 25 | + captures: env.captures, |
| 26 | + lastReward: env.reward(), |
| 27 | + }) |
| 28 | + }, 200) |
| 29 | + return () => clearInterval(id) |
| 30 | + }, [setStats]) |
| 31 | + |
| 32 | + // Wrap DroneEnv.reset so we can count episodes through the store |
| 33 | + useEffect(() => { |
| 34 | + const env = envRef.current |
| 35 | + const originalReset = env.reset.bind(env) |
| 36 | + env.reset = () => { |
| 37 | + originalReset() |
| 38 | + incrementEpisode() |
| 39 | + } |
| 40 | + return () => { |
| 41 | + env.reset = originalReset |
| 42 | + } |
| 43 | + }, [incrementEpisode]) |
| 44 | + |
| 45 | + const startTrainer = useCallback(() => { |
| 46 | + if (!trainerRef.current) { |
| 47 | + trainerRef.current = new IgnitionEnvTFJS(envRef.current) |
| 48 | + } |
| 49 | + return trainerRef.current |
| 50 | + }, []) |
| 51 | + |
| 52 | + const handleTrain = useCallback(() => { |
| 53 | + const trainer = startTrainer() |
| 54 | + trainer.train('dqn') |
| 55 | + trainer.setSpeed(25) |
| 56 | + setMode('training') |
| 57 | + }, [setMode, startTrainer]) |
| 58 | + |
| 59 | + const handleInfer = useCallback(() => { |
| 60 | + const trainer = trainerRef.current |
| 61 | + if (!trainer) { |
| 62 | + // No trained agent yet — kick off training first so the model exists |
| 63 | + handleTrain() |
| 64 | + return |
| 65 | + } |
| 66 | + trainer.infer() |
| 67 | + setMode('inference') |
| 68 | + }, [handleTrain, setMode]) |
| 69 | + |
| 70 | + const handleReset = useCallback(() => { |
| 71 | + trainerRef.current?.stop() |
| 72 | + envRef.current.reset() |
| 73 | + resetStore() |
| 74 | + }, [resetStore]) |
| 75 | + |
| 76 | + const handleSpeedChange = useCallback((speed: number) => { |
| 77 | + trainerRef.current?.setSpeed(speed) |
| 78 | + }, []) |
| 79 | + |
| 80 | + return ( |
| 81 | + <div |
| 82 | + style={{ |
| 83 | + minHeight: '100vh', |
| 84 | + background: '#0a0a1a', |
| 85 | + color: '#e2e8f0', |
| 86 | + fontFamily: 'system-ui, sans-serif', |
| 87 | + }} |
| 88 | + > |
| 89 | + <header |
| 90 | + style={{ |
| 91 | + textAlign: 'center', |
| 92 | + padding: '16px 0 6px', |
| 93 | + position: 'relative', |
| 94 | + }} |
| 95 | + > |
| 96 | + <a |
| 97 | + href="/" |
| 98 | + style={{ |
| 99 | + position: 'absolute', |
| 100 | + left: 24, |
| 101 | + top: 18, |
| 102 | + color: '#94a3b8', |
| 103 | + fontSize: 13, |
| 104 | + textDecoration: 'none', |
| 105 | + padding: '6px 12px', |
| 106 | + border: '1px solid #334155', |
| 107 | + borderRadius: 8, |
| 108 | + background: '#0f172a', |
| 109 | + }} |
| 110 | + aria-label="Back to IgnitionAI landing page" |
| 111 | + > |
| 112 | + ← IgnitionAI |
| 113 | + </a> |
| 114 | + <h1 style={{ margin: 0, fontSize: 24, fontWeight: 800 }}> |
| 115 | + Ignition<span style={{ color: '#6366f1' }}>AI</span> |
| 116 | + <span style={{ fontSize: 14, fontWeight: 400, color: '#888', marginLeft: 12 }}> |
| 117 | + Drone Navigation |
| 118 | + </span> |
| 119 | + </h1> |
| 120 | + <p style={{ margin: '6px 0 0', color: '#888', fontSize: 13 }}> |
| 121 | + Rigid-body physics · 8 thrust combos · DQN learns to fly |
| 122 | + </p> |
| 123 | + </header> |
| 124 | + |
| 125 | + {/* 3D Scene — hero area with HUD overlay */} |
| 126 | + <div |
| 127 | + style={{ |
| 128 | + position: 'relative', |
| 129 | + height: '60vh', |
| 130 | + minHeight: 420, |
| 131 | + maxHeight: 620, |
| 132 | + margin: '0 auto', |
| 133 | + maxWidth: 1100, |
| 134 | + }} |
| 135 | + > |
| 136 | + <Scene3D env={envRef.current} /> |
| 137 | + <HUD /> |
| 138 | + </div> |
| 139 | + |
| 140 | + <Controls |
| 141 | + onTrain={handleTrain} |
| 142 | + onInfer={handleInfer} |
| 143 | + onReset={handleReset} |
| 144 | + onSpeedChange={handleSpeedChange} |
| 145 | + /> |
| 146 | + |
| 147 | + <p |
| 148 | + style={{ |
| 149 | + textAlign: 'center', |
| 150 | + color: '#64748b', |
| 151 | + fontSize: 12, |
| 152 | + padding: '8px 24px 24px', |
| 153 | + maxWidth: 720, |
| 154 | + margin: '0 auto', |
| 155 | + }} |
| 156 | + > |
| 157 | + Press <strong>Train</strong> and wait ~3 minutes at 25× speed. The drone starts tumbling, |
| 158 | + then learns to hover, then chases the pulsing target. Each target captured earns a big |
| 159 | + reward and a new one spawns. Hitting the ground or flying out of the arena crashes the |
| 160 | + episode. Click <strong>Infer</strong> when it looks good to watch the trained policy play. |
| 161 | + </p> |
| 162 | + </div> |
| 163 | + ) |
| 164 | +} |
0 commit comments