Skip to content

A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.

3.4 KB
Raw Blame
import {
    defineSystem, Query, Mut, Res, Time, Transform, Physics2D, Commands,
    EventWriter, EventReader, GetWorld,
} from 'esengine';
import { Facing, Vitality, Swing, Player } from '../components';
import { DamageDealt, Died } from '../events';
import { POINT_BLANK } from '../config';

const DEG = Math.PI / 180;

/**
 * The one place a swing becomes damage, for the player and for every enemy. The
 * overlap query is the physics world's; `hits` decides who a swing can reach,
 * which is the only difference between Lyra's sword and a wisp's touch.
 */
export const meleeResolveSystem = defineSystem(
    [
        Query(Transform, Facing, Mut(Swing)),
        Res(Time), Res(Physics2D), GetWorld(), EventWriter(DamageDealt),
    ],
    (attackers, time, physics, world, damage) => {
        for (const [attacker, transform, facing, attack] of attackers) {
            if (attack.cooldownLeft > 0) attack.cooldownLeft -= time.delta;
            if (!attack.pending) continue;
            attack.pending = false;
            if (attack.cooldownLeft > 0) continue;
            attack.cooldownLeft = attack.cooldown;

            const origin = transform.position;
            const minDot = Math.cos((attack.arcDegrees / 2) * DEG);
            for (const target of physics.overlapCircle(origin, attack.reach, attack.hits)) {
                if (target === attacker || !world.has(target, Transform)) continue;
                const p = world.get(target, Transform).position;
                const dx = p.x - origin.x;
                const dy = p.y - origin.y;
                const dist = Math.hypot(dx, dy);
                if (dist > POINT_BLANK && (dx * facing.x + dy * facing.y) / dist < minDot) continue;
                damage.send({ target, amount: attack.damage, fromX: origin.x, fromY: origin.y });
            }
        }
    },
    { name: 'MeleeResolveSystem' },
);

/** Applies damage, honours invulnerability frames, and announces deaths. */
export const damageSystem = defineSystem(
    [EventReader(DamageDealt), GetWorld(), EventWriter(Died)],
    (blows, world, died) => {
        for (const blow of blows) {
            if (!world.has(blow.target, Vitality)) continue;
            const health = world.get(blow.target, Vitality);
            if (health.invulnerable > 0 || health.current <= 0) continue;
            let killed = false;
            world.update(blow.target, Vitality, (h) => {
                h.current -= blow.amount;
                h.invulnerable = h.invulnerability;
                if (h.current <= 0) {
                    h.current = 0;
                    killed = true;
                }
            });
            if (killed) died.send({ entity: blow.target, isPlayer: world.has(blow.target, Player) });
        }
    },
    { name: 'DamageSystem' },
);

export const invulnerabilitySystem = defineSystem(
    [Query(Mut(Vitality)), Res(Time)],
    (bodies, time) => {
        for (const [, health] of bodies) {
            if (health.invulnerable > 0) health.invulnerable -= time.delta;
        }
    },
    { name: 'InvulnerabilitySystem' },
);

/** Enemies leave the world; Lyra's death is the game's business, not a despawn. */
export const deathSystem = defineSystem(
    [EventReader(Died), Commands()],
    (deaths, commands) => {
        for (const death of deaths) {
            if (!death.isPlayer) commands.despawn(death.entity);
        }
    },
    { name: 'DeathSystem' },
);