6. Spawn waves
Instead of one enemy, a wave of them; clear it and the next one has one more. A line at the top of the screen keeps count.
Everything happens in Combat:
nextWavecounts the wave up and spawns2 + waveenemies: three, then four, then five;spawnPointpicks a random spot on the floor away from the pillars and from you;updatecounts the kills as it removes the dead, and callsnextWaveonce none are left;showwrites the HUD line.
// src/combat.ts
import * as THREE from 'three';
import type { FPS } from 'marble3';
import { GraphicEntity } from 'marble3/entity';
import { raycastFromCamera } from 'marble3/physics';
import type { Player } from 'marble3/player';
import { Enemy } from './enemy';
import { loadGun } from './gun';
export class Combat extends GraphicEntity {
private enemies: Enemy[] = [];
private wave = 0;
private kills = 0;
constructor(private fps: FPS, private player: Player) {
super();
loadGun(player.camera);
document.addEventListener('mousedown', this.shoot);
this.spawn(new THREE.Vector3(10, 3, -30));
this.nextWave();
}
private nextWave() {
this.wave++;
for (let i = 0; i < 2 + this.wave; i++) this.spawn(this.spawnPoint());
this.show();
}
// A random spot on the floor, clear of the pillars and at least 18 units from the player.
private spawnPoint() {
const p = this.player.position;
for (;;) {
const x = (Math.random() - 0.5) * 90;
const z = (Math.random() - 0.5) * 90;
const nearPillar = Math.hypot(x - Math.round(x / 20) * 20, z - Math.round(z / 20) * 20) < 6;
if (!nearPillar && Math.hypot(x - p.x, z - p.z) > 18) return new THREE.Vector3(x, 3, z);
}
}
private spawn(at: THREE.Vector3) {
const enemy = new Enemy(this.player);
enemy.position = at;
this.fps.add(enemy);
this.enemies.push(enemy);
}
private show() {
this.fps.hud.text('status', `Wave ${this.wave} · ${this.enemies.length} left · ${this.kills} kills`, 'top');
}
private shoot = (e: MouseEvent) => {
if (e.button !== 0) return;
const { collision } = raycastFromCamera(this.player.camera, this.fps.world);
const hit = collision?.entityId ? this.fps.entities[collision.entityId] : undefined;
if (hit instanceof Enemy) hit.hit();
};
update() {
// The dead leave the world here, once their last hit has landed.
for (const e of this.enemies) if (e.dead) this.fps.remove(e);
this.enemies = this.enemies.filter((e) => !e.dead);
const dead = this.enemies.filter((e) => e.dead);
if (!dead.length) return;
for (const e of dead) this.fps.remove(e);
this.kills += dead.length;
this.enemies = this.enemies.filter((e) => !e.dead);
// An empty arena brings the next wave.
if (this.enemies.length) this.show();
else this.nextWave();
}
dispose() {
document.removeEventListener('mousedown', this.shoot);
super.dispose();
}
}The pillars stand on every multiple of 20, so Math.round(x / 20) * 20 is the nearest pillar's row; a spot within 6 units of a pillar's centre would start the enemy inside or against it. The floor is 100 units wide, and the spots stay inside 90 of it.
hud.text writes a line of text in one of the HUD's slots, top here. The first call with an id creates the line; every later call with the same id rewrites it, so there is no element to keep a reference to.
What you should see: Wave 1 · 3 left · 0 kills at the top of the screen, and three enemies dropping onto the floor around the arena, each with its bar. Every kill counts down left and counts up kills. Kill the third and four more drop in at once: Wave 2 · 4 left · 3 kills.
The full example composes its waves (combat.ts, composeWave): flyers from wave 3, the odd pink brute from wave 4, a green boss on wave 5 and on every tenth, and enemies a little faster each wave. Nothing here can hurt you yet; chapter 7 makes the enemies bite.