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
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
import { getComponent, SpineAnimation } from '../ecs/component';
import { SpriteAnimator } from '../animation/SpriteAnimator';
// Straight from the generated enum module (the C++ ES_ENUM is the single source), not
// via the UI barrel: this file is core runtime and must not pull the UI module in.
import { UIDisplay } from '../wasm/wasm.generated';
import type { ParticleAPI } from '../particle/ParticleAPI';
import type { AudioAPI } from '../audio/Audio';
import type { Entity } from '../types';
import type { World } from '../ecs/world';
/**
* Shared timeline runtime helpers. The timeline is a
* pure-TS runtime now (see TimelineDrive); this module holds the pieces shared by
* the drive and the editor: child-entity resolution, nested-path writes, and the
* event dispatcher.
*/
export const TimelineEventType = {
SpinePlay: 0,
SpineStop: 1,
SpriteAnimPlay: 2,
AudioPlay: 3,
ActivationSet: 4,
ParticleAction: 5,
} as const;
export { resolveChildEntity } from '../ecs/childPath';
export function setNestedProperty(obj: Record<string, any>, path: string, value: number): boolean {
const parts = path.split('.');
let target = obj;
for (let i = 0; i < parts.length - 1; i++) {
target = target[parts[i]];
if (target == null || typeof target !== 'object') return false;
}
const lastKey = parts[parts.length - 1];
if (!(lastKey in target)) return false;
target[lastKey] = typeof target[lastKey] === 'boolean' ? value !== 0 : value;
return true;
}
export function applyTimelineEvent(
world: any, audio: AudioAPI | null,
type: number, entity: Entity,
intParam: number, floatParam: number, str: string, particle?: Pick<ParticleAPI, 'play' | 'stop' | 'reset'> | null,
): void {
switch (type) {
case TimelineEventType.ParticleAction: {
if (str === 'play') { particle?.reset(entity); particle?.play(entity); }
else if (str === 'stop') particle?.stop(entity);
else if (str === 'reset') particle?.reset(entity);
break;
}
case TimelineEventType.SpinePlay: {
if (world.has(entity, SpineAnimation)) {
const current = world.get(entity, SpineAnimation);
current.animation = str;
current.playing = true;
current.loop = intParam !== 0;
world.set(entity, SpineAnimation, current);
}
break;
}
case TimelineEventType.SpineStop: {
if (world.has(entity, SpineAnimation)) {
const current = world.get(entity, SpineAnimation);
current.playing = false;
world.set(entity, SpineAnimation, current);
}
break;
}
case TimelineEventType.SpriteAnimPlay: {
if (world.has(entity, SpriteAnimator)) {
const current = world.get(entity, SpriteAnimator);
world.insert(entity, SpriteAnimator, { ...current, clip: str, playing: true });
}
break;
}
case TimelineEventType.AudioPlay: {
if (str && audio) audio.playSFX(str, { volume: floatParam });
break;
}
case TimelineEventType.ActivationSet: {
// ActivationSet fires every frame the track is evaluated; only WRITE a
// component when its `enabled` actually flips, or change-detection is
// dirtied (and the reconciler re-projects) 60×/s for a steady value.
const active = intParam !== 0;
if (world.has(entity, SpineAnimation)) {
const current = world.get(entity, SpineAnimation);
if (current.enabled !== active) {
current.enabled = active;
world.set(entity, SpineAnimation, current);
}
}
if (world.has(entity, SpriteAnimator)) {
const current = world.get(entity, SpriteAnimator);
if (current.enabled !== active) {
world.insert(entity, SpriteAnimator, { ...current, enabled: active });
}
}
const Sprite = getComponent('Sprite');
if (Sprite && world.has(entity, Sprite)) {
const current = world.get(entity, Sprite);
if (current.enabled !== active) {
world.set(entity, Sprite, { ...current, enabled: active });
}
}
// A UI node has no `enabled`: `display` is its show/hide, and the only one
// that takes the subtree with it — which is what an activation track means
// when it switches a whole widget off (a closed chest, an idle badge).
const UINode = getComponent('UINode');
if (UINode && world.has(entity, UINode)) {
const current = world.get(entity, UINode);
const display = active ? UIDisplay.Flex : UIDisplay.None;
if (current.display !== display) {
world.insert(entity, UINode, { ...current, display });
}
}
break;
}
}
}