Skip to content

5. Show enemy health ​

A health bar over the enemy: it floats above its head, always faces you, hides behind pillars like the enemy does, and empties as you hit it.

The bar ​

WorldLabel is a small card that lives in the 3D scene, with a canvas on its face to draw on. Made a billboard, it turns to face the camera whenever its owner calls faceCamera. The bar is one of those, with the card as its dark track and a coloured fill drawn over it:

ts
// src/health-bar.ts
import * as THREE from 'three';
import { WorldLabel } from 'marble3/world-label';

export class HealthBar {
  private label: WorldLabel;

  constructor(width: number, height: number) {
    // A 128×22 px canvas on a card `width` units wide, `height` units above its owner's centre.
    this.label = new WorldLabel([128, 22], [width, width * 0.17], { billboard: true });
    this.label.mesh.position.set(0, height, 0);
    this.set(1);
  }

  get mesh(): THREE.Object3D {
    return this.label.mesh;
  }

  // Redraw the fill for a health fraction from 0 to 1: green, then yellow, then red.
  set(fraction: number) {
    const { ctx, w, h } = this.label;
    ctx.clearRect(0, 0, w, h);
    ctx.fillStyle = fraction > 0.5 ? '#4be04b' : fraction > 0.25 ? '#e0c93a' : '#e04b4b';
    ctx.fillRect(3, 3, (w - 6) * Math.max(0, fraction), h - 6);
    this.label.flush();
  }

  faceCamera(camera: THREE.Camera) {
    this.label.faceCamera(camera);
  }

  dispose() {
    this.label.dispose();
  }
}

flush() sends the canvas to the GPU; call it after every redraw. The bar is drawn into the scene like any mesh, so a pillar between you and the enemy hides it: no bar shows through walls.

On the enemy ​

The enemy carries the bar as a child of its three group, so it moves with the body. Three changes: add the bar in the constructor, redraw it in hit(), turn it to the camera in update(); and free its canvas texture in dispose().

ts
// 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';

import { HealthBar } from './health-bar'; 
const MAX_HP = 3; 
export class Enemy extends CharacterEntity {
  hp = 3; 
  hp = MAX_HP; 
  private dir = new THREE.Vector3();
  private bar = new HealthBar(1.6, 1.5); 

  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
    });
    this.three.add(this.bar.mesh); 
  }

  get dead() {
    return this.hp <= 0;
  }

  hit() {
    this.hp -= 1;
    this.bar.set(this.hp / MAX_HP); 
    const material = this.mesh?.material as THREE.MeshStandardMaterial;
    material.emissiveIntensity = 0.85;
    setTimeout(() => (material.emissiveIntensity = 0.35), 60);
  }

  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);
    this.bar.faceCamera(this.target.camera); 
  }
  dispose() { 
    if (this.disposed) return; 
    this.bar.dispose(); 
    super.dispose(); 
  } 
}

faceCamera runs after super.update, once the body has moved this frame, so the bar is turned from where the enemy is now. dispose frees what the bar owns; the entity's own dispose already frees the geometry of everything in its three group.

What you should see: a green bar floats over the enemy's head, square on to you from any side. Hit the enemy once and the fill drops to two thirds; twice, to a third, and it turns yellow. The third hit removes enemy and bar together. Walk behind a pillar and the bar disappears with the enemy.

In the full example the bar is the same class (health-bar.ts), sized from each kind's radius so a boss carries a bigger bar; with 150 hit points, the boss's goes through red too. One enemy is not much of a fight; chapter 6 sends them in waves.