From e08b6160e74e4265d36674c53fbd95b194d2fc76 Mon Sep 17 00:00:00 2001 From: Ori Pomerantz Date: Fri, 22 Mar 2024 06:28:28 -0500 Subject: [PATCH] feat(templates/threejs): easier to use template (#2473) Co-authored-by: Fraser Scott --- .../threejs/packages/client/package.json | 2 +- templates/threejs/packages/client/src/App.tsx | 164 ++++++++++++++++-- .../client/src/useKeyboardMovement.ts | 12 +- 3 files changed, 155 insertions(+), 23 deletions(-) diff --git a/templates/threejs/packages/client/package.json b/templates/threejs/packages/client/package.json index 272a46de64..9fa322566e 100644 --- a/templates/threejs/packages/client/package.json +++ b/templates/threejs/packages/client/package.json @@ -20,7 +20,7 @@ "@latticexyz/store-sync": "link:../../../../packages/store-sync", "@latticexyz/utils": "link:../../../../packages/utils", "@latticexyz/world": "link:../../../../packages/world", - "@react-three/fiber": "^8.12.0", + "@react-three/fiber": "^8.15.19", "contracts": "workspace:*", "react": "^18.2.0", "react-dom": "^18.2.0", diff --git a/templates/threejs/packages/client/src/App.tsx b/templates/threejs/packages/client/src/App.tsx index 6f9645d851..f553d1f975 100644 --- a/templates/threejs/packages/client/src/App.tsx +++ b/templates/threejs/packages/client/src/App.tsx @@ -1,27 +1,81 @@ -import React from "react"; +/* eslint-disable react/no-unknown-property */ +// Workaround react-three-fiber types by disabling unknown properties: +// https://github.com/pmndrs/react-three-fiber/discussions/2487 + import { Canvas, Color, ThreeElements, useThree } from "@react-three/fiber"; import { useComponentValue, useEntityQuery } from "@latticexyz/react"; import { getComponentValueStrict, Has } from "@latticexyz/recs"; import { useMUD } from "./MUDContext"; import { useKeyboardMovement } from "./useKeyboardMovement"; -const Plane = (props: ThreeElements["mesh"]) => { +const Plane = () => { return ( - - {/* eslint-disable-next-line react/no-unknown-property */} - - - + <> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + ); }; const Player = (props: ThreeElements["mesh"] & { color: Color }) => { return ( - - {/* eslint-disable-next-line react/no-unknown-property */} - - - + <> + + + + + + + + + ); }; @@ -58,7 +112,7 @@ const Scene = () => { {/* eslint-disable-next-line react/no-unknown-property */} - + {players.map((p, i) => ( { const styles = { height: "100vh" }; +const Directions = () => { + return ( + <> +

+ You are the rectangular prism, moving around the scene. To move use W, A, S, and D. + You can also move up (T) and down (G). +

+ + ); +}; + +const PlayerInfo = () => { + const { + components: { Position }, + network: { playerEntity }, + } = useMUD(); + + const playerPosition = useComponentValue(Position, playerEntity); + + if (!playerPosition) { + return ( +
+ + + + + + + +
+

Reading player position

+
+ +
+
+ ); + } + + return ( +
+ + + + + + + +
+ + + + + + + + + + + + + + + + + + + +
CoordinateValue
x{playerPosition.x}
y{playerPosition.y}
z{playerPosition.z}
+
+ +
+
+ ); +}; + export const App = () => { return ( - - - + <> + + + + + ); }; diff --git a/templates/threejs/packages/client/src/useKeyboardMovement.ts b/templates/threejs/packages/client/src/useKeyboardMovement.ts index 78fd2ef190..2c8f5d5cb3 100644 --- a/templates/threejs/packages/client/src/useKeyboardMovement.ts +++ b/templates/threejs/packages/client/src/useKeyboardMovement.ts @@ -8,22 +8,22 @@ export const useKeyboardMovement = () => { useEffect(() => { const listener = (e: KeyboardEvent) => { - if (e.key === "ArrowUp") { + if (e.key === "w") { moveBy(1, 0, 0); } - if (e.key === "ArrowDown") { + if (e.key === "s") { moveBy(-1, 0, 0); } - if (e.key === "ArrowLeft") { + if (e.key === "a") { moveBy(0, 0, -1); } - if (e.key === "ArrowRight") { + if (e.key === "d") { moveBy(0, 0, 1); } - if (e.key === " ") { + if (e.key === "t") { moveBy(0, 1, 0); } - if (e.ctrlKey) { + if (e.key === "g") { moveBy(0, -1, 0); } };