Skip to content
  • Android
  • iOS
  • Web

Game Runtime

Create a game world and render it with GameView using the focused games entry point.

Examples

Quick start

nayan-ui/games
import { GameView, useWorld } from 'nayan-ui/games';

export function Scene() {
  const world = useWorld(32, (world) => {
    world.spawn({ mesh: 'cube', color: '#699cff' });
  });
  return world ? <GameView source={world} style={{ width: '100%', height: 240 }} /> : null;
}

Apply an impulse to a physics body

nayan-ui/games
import { useRef } from 'react';
import { GameView, useWorld, type Entity } from 'nayan-ui/games';

export function PhysicsScene() {
  const body = useRef<Entity | null>(null);
  const world = useWorld(32, (scene) => {
    body.current = scene.spawn({ mesh: 'sphere', physics: 'dynamic', position: [0, 1, 0] });
    scene.spawn({ mesh: 'cube', physics: 'fixed', scale: [8, 0.25, 8] });
  });
  return world && (
    <GameView source={world} style={{ height: 250 }} onTap={() => {
      if (body.current !== null) world.impulse(body.current, [0, 4, 0]);
    }} />
  );
}

Track a score and countdown

nayan-ui/games
import type { World } from 'nayan-ui/games';

export function startRound(world: World) {
  world.scoreboard.reset(3);
  world.scoreboard.startCountdown(60);
  return () => world.scoreboard.add(10);
}

Load a texture and spawn a textured entity

nayan-ui/games
import { loadTexture, type World } from 'nayan-ui/games';

export async function addCrate(world: World, uri: string) {
  const texture = await loadTexture(uri);
  return world.spawn({ mesh: 'cube', texture, position: [0, 1, 0] });
}
Related guide

Tags