Gleam - v1.0.2
    Preparing search index...

    Class Projectile<T>

    A self-propelled sprite — update(dt) advances pos along vel * speed, accumulates lifetime, and flips alive to false once lifetime >= maxLifetime. The image is pre-baked to a rotated canvas matching the velocity direction; call rebuildRotation after re-aiming.

    The T generic types the optional payload so callers can attach typed metadata (damage, owner, etc.) without losing inference.

    Type Parameters

    • T = unknown
    Index

    Constructors

    Properties

    _rect: Rect

    Backing storage for the rect getter. Subclasses can read it; mutate via pos/rebuildRotation instead of touching it directly.

    image: HTMLCanvasElement

    Current pre-rotated sprite. Re-baked by rebuildRotation from the un-rotated originalImage.

    lifetime: number = 0

    Accumulated time (seconds). Drives the alive check against maxLifetime.

    maxLifetime: number = Infinity

    Seconds after which alive flips to false. Defaults to Infinity — no natural expiry.

    payload?: T

    Caller-supplied data. Typed via the class generic so consumers can read projectile.payload without casting.

    pos: Vec2

    Top-left position. Cloned from the constructor arg so the caller's Vec2 isn't aliased.

    rotation: number = 0

    Current sprite rotation in radians, kept in sync with vel by rebuildRotation.

    speed: number = 1200

    Magnitude multiplier applied to vel each update: pos += vel * speed * dt. Pass a unit-length vel to make this read as "pixels per second".

    vel: Vec2

    Velocity direction vector. Multiplied by speed each update — pass a unit vector for speed-as-px-per-second semantics. Cloned from the constructor arg.

    Accessors

    Methods

    • Blit the pre-rotated image at pos + offset. offset is useful for shifting by a camera/world transform without mutating pos.

      Parameters

      • context: CanvasRenderingContext2D
      • offset: Vec2 = ...

      Returns void

    • Re-bake the sprite to match the current vel direction (rotation = atan2(vel.y, vel.x)) and update rect to the new bounds. Allocates a fresh rotated canvas every call — no internal cache, so heavy re-aiming (homing/seeking) is a candidate for adding quantized caching.

      Returns void

    • Force alive to false immediately (sets lifetime past maxLifetime). Use when the projectile should die on collision/impact, not from natural expiry.

      Returns void

    • Integrate motion and advance lifetime. Doesn't re-bake the rotation — call rebuildRotation after mutating vel.

      Parameters

      • dt: number

      Returns void