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.
TypeScript 2d-game-engine3d3d-game-engineandroidcppecsgame-editorgame-enginegamedeviostypescriptwebassemblywebglwebgpuwechat-minigame
import {
defineSystem, Query, Mut, Res, Time, Commands,
Sprite, Light, ShadowCaster2D,
} from 'esengine';
import { Fading } from '../components';
import { PLACED_LIGHT_INTENSITY, FADE_DURATION } from '../config';
// Dims placed entities to nothing over FADE_DURATION, then despawns them. The
// two queries are disjoint: a placed light has Light (no ShadowCaster2D), an
// obstacle has ShadowCaster2D (no Light), so each entity is ticked exactly
// once. The torch never gets a Fading component, so it is never matched here.
export const fadeSystem = defineSystem(
[
Query(Mut(Fading), Mut(Sprite), Mut(Light)),
Query(Mut(Fading), Mut(Sprite), ShadowCaster2D),
Res(Time),
Commands(),
],
(fadingLights, fadingObstacles, time, cmds) => {
for (const [entity, fading, sprite, light] of fadingLights) {
fading.remaining -= time.delta;
const t = Math.max(0, fading.remaining / FADE_DURATION);
light.intensity = PLACED_LIGHT_INTENSITY * t;
sprite.color.a = t;
if (fading.remaining <= 0) cmds.despawn(entity);
}
for (const [entity, fading, sprite] of fadingObstacles) {
fading.remaining -= time.delta;
const t = Math.max(0, fading.remaining / FADE_DURATION);
sprite.color.a = t;
if (fading.remaining <= 0) cmds.despawn(entity);
}
},
{ name: 'FadeSystem' },
);