logo

Install with the CLI:

bjs download Health

Health

Hit-points with invulnerability windows and optional respawn, decoupled from any one game.

What it does

HealthComponent stores HP for an entity; HealthSystem is purely event-driven. It listens for health.damage and health.heal requests, mutates the entity's HP, and emits health.damaged / health.healed when HP actually moves. Damage taken during a post-hit invulnerability window (set by invulnSeconds) is ignored. When HP reaches 0 it emits health.died; if the entity has a respawnPosition, the system then snaps the host's MeshPrimitive position back to that point, restores full HP, grants a brief grace period, and emits health.respawned.

Use it in a scene

A scene is just data — a list of entities, each with its components. At startup the SceneLoader turns this JSON into a live world; edit the file and reload to rebuild it.

Fastest path

bjs download scene Health

Pulls this exact scene into your project and runs it — no copy-paste. Add --all to grab the components and assets it needs too.

Or build it from scratch with the bjs CLI:

  1. 1Scaffold a project
    npm create @babylonjsmarket/arcade@latest my-game

    World, renderer, and dev server — ready to run.

  2. 2Install dependencies
    cd my-game && npm install
  3. 3Add Health
    bjs download Health

    Copies its source into src/ so the scene resolves.

    First time? Run bjs login once.

  4. 4Paste the scene into src/scenes/arcade-room.ts and run
    npm run dev

    SceneLoader builds the world from the JSON; reload to rebuild.

JSON
{
  "entities": {
    "Player": {
      "tags": ["player"],
      "components": {
        "MeshPrimitive": {
          "primitive": "capsule",
          "radius": 0.5,
          "height": 1.6,
          "position": [0, 0.8, 0]
        },
        "Health": { "hp": 5, "maxHp": 5, "invulnSeconds": 0.6, "respawnPosition": [0, 0.8, 0] }
      }
    }
  }
}

Props

  • hp (number, default 10) — current hit points.
  • maxHp (number, default = hp) — cap that healing and respawn restore to.
  • invulnSeconds (number, default 0) — post-damage grace period in seconds; 0 means no i-frames.
  • respawnPosition ([x, y, z], default none) — if set, the entity teleports here and restores full HP when HP hits 0.

Events

  • listens health.damage { entityId, damage } — applies damage (respecting i-frames; ignored if already dead).
  • listens health.heal { entityId, amount } — heals up to maxHp.
  • emits health.damaged { entityId, hp, maxHp, damage } — after HP drops.
  • emits health.healed { entityId, hp, maxHp, amount } — after HP rises.
  • emits health.died { entityId } — the frame HP first hits 0, before any respawn.
  • emits health.respawned { entityId, hp, maxHp } — after a respawn-configured entity is reset to full HP.

Dependencies

MeshPrimitive — respawn writes the new position into the host's MeshPrimitiveComponent.

Notes

  • Respawn sets the position DATA only; it does not push to the renderer. It assumes another system (e.g. TwinStickShooter for the player) owns and commits the transform each frame. Writing the mesh handle here too would race that owner on the death frame.
  • After respawn the entity gets at least 1 second of invulnerability (max(invulnSeconds, 1.0)) so a clump of enemies can't insta-kill it again.
  • invulnUntil is wall-clock time (performance.now), not simulation time.

More like this

Bullet
ContactDamage
MeshPrimitive
PlayerWalkAnimator
TwinStickEnemy
AiPilot
Animation
ArcCamera
Asteroid
CameraFollow
DirectionalLight
EnemySpawner
EnvironmentTexture
FreighterCar
FreighterChain
FreighterHead
GameConfig
HemisphericLight
Jump
KeyboardInput
KeyboardMover
Lifetime
LineOfSight
MachineGun
Mesh
Missile
MissileLauncher
MissionDirector
MoneyField
MouseInput
Movement
Obstacle
ObstacleField
OilBlob
Parts
Physics
PlayerInput
RespawnTimer
RoundReset
Score
Shadow
ShipLoadout
ShooterCamera
SkeletonAnimator
SmokeScreen
SpaceShooterBullet
SpaceShooterHealth
SpaceShooterScore
TwinStickShooter
WaveDirector
Waypoint
WaypointTrack
WeaponsVan

Was this page helpful?

We read every note — tell us what's working and what isn't.

↑↓ NavigateEnter SelectEsc CloseCtrl+K Open Search