|
1 | 1 | import React, { useEffect, useMemo, useRef, useState } from "react"; |
2 | 2 | import { createRoot } from "react-dom/client"; |
3 | | -import { CODEX_PET_STATES, type CodexPetManifest, type CodexPetState } from "codex-pet-web"; |
| 3 | +import { |
| 4 | + CODEX_PET_STATES, |
| 5 | + createCodexPetRandomActionRunner, |
| 6 | + type CodexPetManifest, |
| 7 | + type CodexPetSnapshot, |
| 8 | + type CodexPetState |
| 9 | +} from "codex-pet-web"; |
4 | 10 | import { CodexPet, type CodexPetHandle } from "codex-pet-web-react"; |
5 | 11 | import "./styles.css"; |
6 | 12 |
|
@@ -28,7 +34,7 @@ function App() { |
28 | 34 | const [pets, setPets] = useState<DemoPet[]>([]); |
29 | 35 | const [selectedPetId, setSelectedPetId] = useState(""); |
30 | 36 | const [state, setState] = useState<CodexPetState>("idle"); |
31 | | - const [scale, setScale] = useState(0.5); |
| 37 | + const [scale, setScale] = useState(0.45); |
32 | 38 | const [fps, setFps] = useState(8); |
33 | 39 | const [paused, setPaused] = useState(false); |
34 | 40 | const [reducedMotion, setReducedMotion] = useState(false); |
@@ -59,6 +65,47 @@ function App() { |
59 | 65 | }); |
60 | 66 | }, []); |
61 | 67 |
|
| 68 | + useEffect(() => { |
| 69 | + const runner = createCodexPetRandomActionRunner( |
| 70 | + { |
| 71 | + getSnapshot: () => |
| 72 | + petRef.current?.getSnapshot() ?? |
| 73 | + ({ |
| 74 | + id: "preview", |
| 75 | + spritesheetUrl: "", |
| 76 | + state: "idle", |
| 77 | + baseState: "idle", |
| 78 | + frame: 0, |
| 79 | + scale, |
| 80 | + fps, |
| 81 | + position: null, |
| 82 | + hidden: false, |
| 83 | + mounted: false, |
| 84 | + paused, |
| 85 | + removed: false |
| 86 | + } satisfies CodexPetSnapshot), |
| 87 | + play: (nextState, options) => petRef.current?.play(nextState, options) |
| 88 | + }, |
| 89 | + { |
| 90 | + averageIntervalSeconds: 120, |
| 91 | + minIntervalSeconds: 45, |
| 92 | + maxIntervalSeconds: 300, |
| 93 | + actions: [ |
| 94 | + { state: "waving", weight: 3 }, |
| 95 | + { state: "jumping", weight: 2 }, |
| 96 | + { state: "waiting", weight: 1 }, |
| 97 | + { state: "review", weight: 1 } |
| 98 | + ] |
| 99 | + } |
| 100 | + ); |
| 101 | + |
| 102 | + runner.start(); |
| 103 | + |
| 104 | + return () => { |
| 105 | + runner.destroy(); |
| 106 | + }; |
| 107 | + }, []); |
| 108 | + |
62 | 109 | const selectedPet = useMemo( |
63 | 110 | () => pets.find((pet) => pet.id === selectedPetId) ?? pets[0], |
64 | 111 | [pets, selectedPetId] |
|
0 commit comments