|
1 | | -import { useComponentValue } from "@latticexyz/react"; |
2 | | -import { useMUD } from "./MUDContext"; |
3 | | -import { singletonEntity } from "@latticexyz/store-sync/recs"; |
| 1 | +import { AccountButton } from "@latticexyz/entrykit/internal"; |
| 2 | +import { Direction, Entity } from "./common"; |
| 3 | +import mudConfig from "contracts/mud.config"; |
| 4 | +import { useMemo } from "react"; |
| 5 | +import { GameMap } from "./game/GameMap"; |
| 6 | +import { useWorldContract } from "./mud/useWorldContract"; |
| 7 | +import { Synced } from "./mud/Synced"; |
| 8 | +import { useSync } from "@latticexyz/store-sync/react"; |
| 9 | +import { components } from "./mud/recs"; |
| 10 | +import { useEntityQuery } from "@latticexyz/react"; |
| 11 | +import { Has, getComponentValueStrict } from "@latticexyz/recs"; |
| 12 | +import { Address } from "viem"; |
4 | 13 |
|
5 | | -export const App = () => { |
6 | | - const { |
7 | | - components: { Counter }, |
8 | | - systemCalls: { increment }, |
9 | | - } = useMUD(); |
| 14 | +export function App() { |
| 15 | + const playerEntities = useEntityQuery([Has(components.Owner), Has(components.Position)]); |
| 16 | + const players = useMemo( |
| 17 | + () => |
| 18 | + playerEntities.map((entity) => { |
| 19 | + const owner = getComponentValueStrict(components.Owner, entity); |
| 20 | + const position = getComponentValueStrict(components.Position, entity); |
| 21 | + return { |
| 22 | + entity: entity as Entity, |
| 23 | + owner: owner.owner as Address, |
| 24 | + x: position.x, |
| 25 | + y: position.y, |
| 26 | + }; |
| 27 | + }), |
| 28 | + [playerEntities], |
| 29 | + ); |
| 30 | + |
| 31 | + const sync = useSync(); |
| 32 | + const worldContract = useWorldContract(); |
| 33 | + |
| 34 | + const onMove = useMemo( |
| 35 | + () => |
| 36 | + sync.data && worldContract |
| 37 | + ? async (entity: Entity, direction: Direction) => { |
| 38 | + const tx = await worldContract.write.app__move([entity, mudConfig.enums.Direction.indexOf(direction)]); |
| 39 | + await sync.data.waitForTransaction(tx); |
| 40 | + } |
| 41 | + : undefined, |
| 42 | + [sync.data, worldContract], |
| 43 | + ); |
10 | 44 |
|
11 | | - const counter = useComponentValue(Counter, singletonEntity); |
| 45 | + const onSpawn = useMemo( |
| 46 | + () => |
| 47 | + sync.data && worldContract |
| 48 | + ? async () => { |
| 49 | + const tx = await worldContract.write.app__spawn(); |
| 50 | + await sync.data.waitForTransaction(tx); |
| 51 | + } |
| 52 | + : undefined, |
| 53 | + [sync.data, worldContract], |
| 54 | + ); |
12 | 55 |
|
13 | 56 | return ( |
14 | 57 | <> |
15 | | - <div> |
16 | | - Counter: <span>{counter?.value ?? "??"}</span> |
| 58 | + <div className="fixed inset-0 grid place-items-center p-4"> |
| 59 | + <Synced |
| 60 | + fallback={({ message, percentage }) => ( |
| 61 | + <div className="tabular-nums"> |
| 62 | + {message} ({percentage.toFixed(1)}%)… |
| 63 | + </div> |
| 64 | + )} |
| 65 | + > |
| 66 | + <GameMap players={players} onMove={onMove} onSpawn={onSpawn} /> |
| 67 | + </Synced> |
| 68 | + </div> |
| 69 | + <div className="fixed top-2 right-2"> |
| 70 | + <AccountButton /> |
17 | 71 | </div> |
18 | | - <button |
19 | | - type="button" |
20 | | - onClick={async (event) => { |
21 | | - event.preventDefault(); |
22 | | - console.log("new counter value:", await increment()); |
23 | | - }} |
24 | | - > |
25 | | - Increment |
26 | | - </button> |
27 | 72 | </> |
28 | 73 | ); |
29 | | -}; |
| 74 | +} |
0 commit comments