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.

2.6 KB
Raw Blame
import {
    defineSystem, Query, Mut, Res, Commands,
    Transform, Time,
} from 'esengine';
import { Explosion, Star, Bullet, Enemy } from '../components';
import { HALF_HEIGHT, HALF_WIDTH, DESTROY_Y, SPAWN_Y } from '../resources';

/**
 * Carries every live bullet along its own axis.
 *
 * @compiled
 * The frame this moves is what verify-aot-parity holds against an interpreted
 * one: a twin reading the wrong offset puts the bullets somewhere visible.
 */
export const bulletMoveSystem = defineSystem(
    [Res(Time), Query(Mut(Transform), Bullet)],
    (time, query) => {
        const dt = time.delta;

        for (const [_entity, transform, bullet] of query) {
            const dir = bullet.fromPlayer ? 1 : -1;
            transform.position.y += bullet.speed * dt * dir;
        }
    },
    { name: 'BulletMoveSystem' }
);

export const explosionSystem = defineSystem(
    [Res(Time), Commands(), Query(Mut(Transform), Mut(Explosion))],
    (time, cmds, query) => {
        const dt = time.delta;

        for (const [entity, transform, explosion] of query) {
            explosion.timer -= dt;

            const progress = 1 - explosion.timer / 0.3;
            const scale = 0.5 + progress * 1.0;
            transform.scale = { x: scale, y: scale, z: 1 };

            if (explosion.timer <= 0) {
                cmds.despawn(entity);
            }
        }
    },
    { name: 'ExplosionSystem' }
);

/**
 * Wrap the starfield. The DRIFT is `Velocity` on each star, integrated by the
 * engine — no physics body, no per-frame arithmetic here; all this does is put a
 * star that has left the bottom back at the top.
 */
export const starScrollSystem = defineSystem(
    [Query(Mut(Transform), Star)],
    (query) => {
        for (const [_entity, transform] of query) {
            if (transform.position.y < -HALF_HEIGHT - 10) {
                transform.position.y = HALF_HEIGHT + 10;
                transform.position.x = (Math.random() - 0.5) * HALF_WIDTH * 2;
            }
        }
    },
    { name: 'StarScrollSystem' }
);

export const boundarySystem = defineSystem(
    [Commands(), Query(Transform, Bullet), Query(Transform, Enemy)],
    (cmds, bulletQuery, enemyQuery) => {
        for (const [entity, transform] of bulletQuery) {
            if (transform.position.y > SPAWN_Y || transform.position.y < DESTROY_Y) {
                cmds.despawn(entity);
            }
        }

        for (const [entity, transform] of enemyQuery) {
            if (transform.position.y < DESTROY_Y) {
                cmds.despawn(entity);
            }
        }
    },
    { name: 'BoundarySystem' }
);