2. Add the player
Swap the fixed camera for someone who can walk, and give them a crosshair.
// src/game.ts
import * as THREE from 'three';
import { FPS } from 'marble3';
import { LookAtCursor } from 'marble3/cursor';
import { defaultLights } from 'marble3/lights';
import { Player } from 'marble3/player';
import { buildArena, sunset } from './arena';
export const createGame = (): HTMLElement => {
const camera = new THREE.PerspectiveCamera(70, innerWidth / innerHeight, 0.1, 4000);
const fps = new FPS();
fps.scene.add(defaultLights());
fps.scene.add(sunset());
fps.renderer.toneMapping = THREE.ACESFilmicToneMapping; // the sky is in physical units
fps.renderer.toneMappingExposure = 0.6;
buildArena(fps);
camera.position.set(0, 30, 60);
camera.lookAt(0, 0, 0);
return fps.start({ camera });
const player = new Player({ camera });
player.position = new THREE.Vector3(0, 0, 0);
fps.add(player);
const el = fps.start(player);
fps.add(new LookAtCursor(player));
return el;
};The player gets the camera: start(player) draws through it, at the player's eye height, and the mouse turns it. The player starts at the middle of the arena, the one spot chapter 1 left without a pillar.
What you get for free:
- a capsule rigid body: the player collides, gets pushed, can't walk through pillars;
- a character controller that hugs floors, refuses steep slopes, and jumps only from the ground;
- the camera at eye height, its orientation owned by the mouse;
- a respawn guard: walk off the arena's edge and fall far enough, and you're back at the start.
The LookAtCursor is the ring in the middle of the screen. It follows whatever the centre of the view is on, and it is where a shot will go in chapter 4.
What you should see: you stand in the middle of the arena, among the pillars. Click the canvas: the mouse is captured, WASD walks, Space jumps, Esc releases. Walk into a pillar and you stop against it. On a phone (or with ?touch in the URL) the same code mounts a stick and a jump button instead.
An arena with nobody in it is a walk in the park. Chapter 3 brings an enemy.