@threert/gameplay
v0.1.1
Published
Game logic for ThreeRT: state, transactional handlers, world handlers and per-frame scripts on top of @threert/scene; deterministic and replayable.
Maintainers
Readme
@threert/gameplay
Game logic for ThreeRT, the hardware ray-traced engine with a three.js-style API: state, handlers that change it as transactions, and a frame script that shows it in the scene.
Install
npm install @threert/scene @threert/gameplayA game
defineGame takes the initial state, a setup that builds the scene with @threert/scene, handlers that change the state, and a frame script that derives the scene from the state every frame.
import { Scene, PerspectiveCamera, Mesh, SphereGeometry, MeshStandardMaterial, DirectionalLight, SkyAtmosphere } from "@threert/scene";
import { defineGame } from "@threert/gameplay";
export default defineGame({
state: { lit: {} as Record<string, boolean>, night: false },
setup() {
const scene = new Scene();
const sun = new DirectionalLight(0xffffff, 80000).setSunAngles(32, 35);
scene.add(sun);
scene.environment = new SkyAtmosphere({ sun });
const orb = new Mesh(new SphereGeometry(0.4), new MeshStandardMaterial({ color: 0xffffff, emissive: 0xffc890 }));
orb.id = "orb_0"; // what handlers and the frame script find it by
orb.position.set(0, 1, 0);
scene.add(orb);
const camera = new PerspectiveCamera(40, 16 / 9, 0.1, 200);
camera.position.set(0, 1.4, 5);
scene.add(camera);
return { scene, camera };
},
// Handlers change the state, and only the state.
handlers: {
click(tx, ev) {
tx.decide(ev.target.startsWith("orb_"), `nothing happens when you click ${ev.target}`);
tx.state.lit[ev.target] = !tx.state.lit[ev.target];
},
night(tx) {
tx.state.night = !tx.state.night;
},
},
keys: { L: "night" },
// The frame script derives the scene from the state, every frame.
frame(f) {
const orb = f.object("orb_0") as Mesh;
orb.material.emissiveIntensity = f.state.lit.orb_0 ? 2500 : 0;
orb.position.y = 1 + 0.08 * Math.sin(f.time * 2);
},
});- Handlers are transactions. A click reaches the game as a
clickevent whosetargetis the object under the cursor.tx.deciderefuses an event, and nothing is half-written. - Deterministic and replayable. Events are logged with what they read and wrote;
replayLogandverifyLogrebuild a world from its log, and seeded randomness (seededRandom) gives the same result on every machine. - World handlers (
on) reach the engine's own world: physics, a ray cast into the scene, objects added and removed as an event arrives.
How scene code runs in the engine is in RUNTIME.md.
Running it
The game runs inside a ThreeRT client, which path-traces the scene on the device's GPU: ThreeRT for macOS and iPhone, with threert dev for the edit-and-reload loop (getting started), or threert-render play built from the engine's source. The same game runs as three.js in a browser.
Links
threert.dev · Docs · Showcase · GitHub
three.js is an independent open-source project; ThreeRT is not affiliated with it. MIT licence.
