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.2 KB
Raw Blame
import {
    defineSystem, Res, Input, Commands, UICameraInfo,
    Transform, Sprite, Light, LightType, ShadowCaster2D,
} from 'esengine';
import type { UICameraData } from 'esengine';
import { Fading } from '../components';
import {
    LIGHT_COLORS, PLACED_LIGHT_INTENSITY, PLACED_LIGHT_RADIUS, PLACED_LIGHT_SOFTNESS,
    OBSTACLE_SIZE, MAX_LIGHTS, MAX_OBSTACLES, FADE_DURATION,
} from '../config';

// FIFO records of what the player has placed. When a list exceeds its cap the
// oldest entity is tagged Fading (the fade system dims and despawns it).
const lights: number[] = [];
const obstacles: number[] = [];
let nextColor = 0;

// Left-click → colored Point light, right-click → box obstacle, C → clear.
// Light markers stay unlit (no `lit` flag) — an emissive dot that reads as the
// source itself. Obstacles are lit AND cast shadows: an occluder never shadows
// its own interior, so a caster's sprite takes light like any other surface.
export const placeSystem = defineSystem(
    [Res(Input), Res(UICameraInfo), Commands()],
    (input, camera: UICameraData, cmds) => {
        if (camera.valid) {
            const x = camera.worldMouseX;
            const y = camera.worldMouseY;

            if (input.isMouseButtonPressed(0)) {
                const color = LIGHT_COLORS[nextColor % LIGHT_COLORS.length];
                nextColor++;
                const entity = cmds.spawn()
                    .insert(Transform, { position: { x, y, z: 0 } })
                    .insert(Light, {
                        type: LightType.Point,
                        color,
                        intensity: PLACED_LIGHT_INTENSITY,
                        radius: PLACED_LIGHT_RADIUS,
                        shadowSoftness: PLACED_LIGHT_SOFTNESS,
                        enabled: true,
                    })
                    .insert(Sprite, { size: { x: 18, y: 18 }, color, layer: 5 })
                    .id();
                lights.push(entity);
                if (lights.length > MAX_LIGHTS) {
                    cmds.entity(lights.shift()!).insert(Fading, { remaining: FADE_DURATION });
                }
            }

            if (input.isMouseButtonPressed(2)) {
                const entity = cmds.spawn()
                    .insert(Transform, { position: { x, y, z: 0 } })
                    .insert(Sprite, {
                        size: OBSTACLE_SIZE,
                        color: { r: 0.22, g: 0.24, b: 0.30, a: 1 },
                        layer: 4,
                        lit: true,
                    })
                    .insert(ShadowCaster2D, { size: OBSTACLE_SIZE, enabled: true })
                    .id();
                obstacles.push(entity);
                if (obstacles.length > MAX_OBSTACLES) {
                    cmds.entity(obstacles.shift()!).insert(Fading, { remaining: FADE_DURATION });
                }
            }
        }

        if (input.isKeyPressed('KeyC')) {
            for (const entity of lights) cmds.entity(entity).insert(Fading, { remaining: FADE_DURATION });
            for (const entity of obstacles) cmds.entity(entity).insert(Fading, { remaining: FADE_DURATION });
            lights.length = 0;
            obstacles.length = 0;
        }
    },
    { name: 'PlaceSystem' },
);