3. Spawn an enemy
An enemy is a CharacterEntity: the same dynamic body and character controller the player walks with, minus the keyboard. Each frame it points its walk direction at you; the controller does the walking, and the physics keeps it out of the pillars.
The enemy
// src/enemy.ts
import * as THREE from 'three';
import type { FPS } from 'marble3';
import * as bullet from 'marble3/bullet';
import { CharacterEntity } from 'marble3/entity';
import type { Player } from 'marble3/player';
export class Enemy extends CharacterEntity {
private dir = new THREE.Vector3();
constructor(private target: Player) {
super({
shape: new bullet.btSphereShape(0.8),
geometry: new THREE.IcosahedronGeometry(0.8),
material: new THREE.MeshStandardMaterial({ color: 0xff3030, emissive: 0xaa0000, emissiveIntensity: 0.35, flatShading: true }),
mass: 4,
walkSpeed: 3.5, // slower than the player's 6, so you can outrun it
});
}
update(delta = 0, engine?: FPS) {
const p = this.target.position;
const me = this.position;
const dx = p.x - me.x;
const dz = p.z - me.z;
const d = Math.hypot(dx, dz);
this.controller.setWalkDirection(d > 1.2 ? this.dir.set(dx / d, 0, dz / d) : this.dir.set(0, 0, 0));
super.update(delta, engine);
}
}shapeis the body Bullet simulates, a sphere;geometryis what you see, an icosahedron the same size.- A
massof 4 is heavy enough that walking into it barely shoves it. updateruns every frame. It steers on the floor plane only (ystays 0, gravity does the rest) and stops 1.2 units from you, so it presses up against you instead of trying to walk through you.super.updatethen lets the controller move the body and the mesh follow it.
Let it loose
Add one in createGame, after fps.start(player), between two rows of pillars:
// 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';
import { Enemy } from './enemy';
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);
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));
const enemy = new Enemy(player);
enemy.position = new THREE.Vector3(10, 3, -30);
fps.add(enemy);
return el;
};The spot (10, 3, -30) is clear of every pillar (they stand on multiples of 20), and 3 units up, so the enemy drops onto the floor rather than starting inside it.
What you should see: reload and look around for a red stone 30 units away. It drops to the floor and comes for you. It stops when it reaches you and follows wherever you go. It only knows the straight line to you: put a pillar between you and it pushes against the pillar. At 6 units a second you outrun its 3.5. It can't hurt you yet, and you can't hurt it: chapter 4 gives you a gun.
The full example's walkers are this class with stats per kind (ENEMY_KINDS in enemy.ts): the red one here, a pink brute and a green boss, each bigger, slower and tougher.