miaoda-game-beam-core
v0.3.2
Published
Engine-agnostic laser / beam core: a telegraph->fire->fade lifecycle state machine, beam geometry (origin + angle + length + width), and along-beam hit testing. No rendering, no engine dependency. Backs shoot-em-up boss lasers, tower-defense laser towers,
Downloads
943
Maintainers
Readme
miaoda-game-beam-core
Use this package for lasers and other line-shaped attacks whose geometry is fixed or swept over time. It handles the warning, active damage, fade, segment geometry, and circular-target hit test without a rendering or engine dependency.
Choose this instead of miaoda-game-shooter-core when the attack is a beam rather than a moving bullet. Angles use degrees with 0 pointing along +X.
Install
pnpm add miaoda-game-beam-coreCreate and update a beam
import { Beam, sweepAngle } from 'miaoda-game-beam-core';
const beam = new Beam({
origin: bossPosition,
angleDeg: -60,
length: 900,
width: 40,
telegraph: 1, // warning seconds
fire: 2, // damage seconds
fade: 0.4, // visual tail seconds
});
beam.tick(dtSeconds);
const fireProgress = (beam.age - 1) / 2;
beam.aim(bossPosition, sweepAngle(-60, 60, fireProgress));
if (beam.hits(playerPosition, playerRadius)) {
damagePlayer();
}
drawLine(beam.segment(), beam.widthNow, beam.progress);The lifecycle is telegraph -> firing -> fading -> dead:
telegraphis visible but never damages.firingis the only phase in whichhitscan returntrue.fadingremains visible but is damage-free.deadis ready to remove or recycle.
All durations and tick values are in seconds. Configuration coordinates, angle, geometry, and durations must be finite; tick deltas must also be non-negative. Negative finite phase durations clamp to zero. progress and widthNow are presentation values; the core does not draw anything.
Manage many beams
import { BeamSet } from 'miaoda-game-beam-core';
const beams = new BeamSet();
beams.spawn(config);
const { alive, removed } = beams.tick(dtSeconds);
const hits = beams.beamsHitting(playerPosition, playerRadius);Use BeamSet when a boss, turret group, or spell system owns multiple active beams. Remove or recycle the entries in removed; alive contains the still-active beams for rendering and tracking.
Geometry helpers
pointToSegmentDistance(point, start, end)measures distance to a beam segment.sweepAngle(fromDeg, toDeg, t)interpolates the shortest angular route fortfrom0to1.directionFromAngle(angleDeg)converts the shared degree convention to a unit vector.
The core reports hits only. Damage, invulnerability frames, hit de-duplication, and collision response remain game-owned.
