Skip to main content

Shooter 2D

Shooter2DBehavior is a stage-scoped firing helper: given a source entity, aim target, and stage, it spawns projectiles from your factory, applies initial velocity, and respects per-source cooldown state. It does not read gamepad input by itself—wire fireShooter2DIfRequested or a coordinator to connect input.

Minimal example​

website/snippets/behaviors/catalog/shooter-2d.ts:

import { Shooter2DBehavior, fireShooter2DIfRequested } from '@zylem/game-lib/behavior';

const shooter = gunner.use(Shooter2DBehavior, {
projectileFactory: () => createSphere({ name: 'bullet' }),
projectileSpeed: 30,
cooldownMs: 250,
});

gunner.onUpdate(({ inputs, me }) => {
fireShooter2DIfRequested(me, shooter, stage, {
aimX: inputs.p1.axes.SecondaryHorizontal.value,
aimY: inputs.p1.axes.SecondaryVertical.value,
shootPressed: inputs.p1.buttons.A.pressed,
shootHeld: inputs.p1.buttons.A.held > 0,
});
});

Options​

OptionRole
projectileFactoryRequired; returns entity (sync or async).
projectileSpeedInitial speed along aim vector.
cooldownMsPer-source refractory period.
spawnOffset, rotateProjectileSpawn tweak and align sprite rotation.

Handle​

  • isReady(source), fire({ source, stage, target }).

Aim resolution uses resolveAim2D and entity facing from top-down state or 2D rotation helpers.

Pitfalls​

  • stage.add must accept spawned projectile entities.
  • Factory entities should set up velocity in prependSetup if you need custom collision groups.
  • Cooldown is per source entity state (shooter2dState), not global.

API reference​