jetlag2d
    Preparing search index...

    Class HealthBehavior

    HealthBehavior tracks hit points for anything that can be hurt / defeated. It also provides invincibility.

    Hierarchy (View Summary)

    Index
    • Construct a HealthBehavior

      Parameters

      • cfg: {
            hitPoints?: number;
            onDefeated?: (a: Actor, defeatedBy?: Actor<unknown>) => void;
            onHitPointsChange?: (a: Actor) => void;
            team?: number;
        } = {}
        • OptionalhitPoints?: number

          Starting hit points (default 1)

        • OptionalonDefeated?: (a: Actor, defeatedBy?: Actor<unknown>) => void

          Code to run when this actor is defeated

        • OptionalonHitPointsChange?: (a: Actor) => void

          Code to run on any change to hit points

        • Optionalteam?: number

          The team this actor belongs to (default 0)

      Returns HealthBehavior

    _actor?: Actor<unknown>

    The actor associated with this Behavior

    onDefeated?: (a: Actor, defeatedBy?: Actor<unknown>) => void

    Code to run when this actor's health drops to 0 and it is defeated

    onHitPointsChange?: (a: Actor) => void

    Code to run when this actor's hit points change

    team: number = 0

    Which "team" this Behavior belongs to. A team is just a number, and for the most part it's only when user code consults teams that teams have meaning. The only exception is that HazardBehavior uses team to prevent team attack damage.

    • get invincibleRemaining(): number

      Time (seconds) until invincibility runs out

      Returns number

    • set invincibleRemaining(amountSeconds: number): void

      Setting a positive value fires an INV_Y state-change event, so an AnimatedSprite observing this actor can switch to an invincibility animation. Setting it back to 0 (e.g. to cancel invincibility early, rather than letting it decay naturally) fires INV_N, so that animation is cleared.

      Parameters

      • amountSeconds: number

      Returns void

    • Apply damage. While invincible, damage is ignored (unless ignoreInvincibility is set). If hit points drop to 0 or below, the actor is defeated (removed, and onDefeated runs).

      Parameters

      • amount: number

        How much damage to apply

      • OptionaldefeatedBy: Actor<unknown>

        The actor responsible for the damage, if any

      • ignoreInvincibility: boolean = false

        Apply the damage even if the actor is currently invincible

      Returns void

    • Turn off physical collisions for actor. Used by Behaviors that are only ever passed through, not physically collided with, like coins.

      Parameters

      • actor: Actor<unknown> | undefined

        The actor being attached (may be undefined, if this Behavior is being detached)

      Returns void

    • If actor's body isn't dynamic, warn and upgrade it to dynamic. Used by every Behavior whose whole point requires a dynamic body (e.g. setVelocity/forces actually doing something).

      Parameters

      • actor: Actor<unknown> | undefined

        The actor being attached (may be undefined, if this Behavior is being detached)

      • behaviorName: string

        The name to use in the warning message

      Returns void