@flare-engine/scene 0.2.1 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -21
- package/NOTICE +2 -0
- package/README.md +16 -6
- package/dist/index.d.mts +91 -87
- package/dist/index.d.mts.map +1 -0
- package/dist/index.d.ts +91 -87
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +291 -275
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +287 -245
- package/dist/index.mjs.map +1 -1
- package/package.json +18 -10
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts","../src/transition.ts","../src/scene-manager.ts"],"sourcesContent":["export type { Scene, SceneFactory } from './scene.js';\n\nexport {\n type SlideDirection,\n type Transition,\n type TransitionState,\n noneTransition,\n fadeTransition,\n slideTransition,\n} from './transition.js';\n\nexport { SceneManager } from './scene-manager.js';\n","import type { EasingFn } from '@flare-engine/math';\n\nexport type { EasingFn };\n\n/** Slide direction — where the outgoing scene moves off-screen to. */\nexport type SlideDirection = 'left' | 'right' | 'up' | 'down';\n\n/** Transition state during a scene switch. */\nexport interface TransitionState {\n /** Progress 0 → 1. */\n progress: number;\n /** Whether the transition is complete. */\n done: boolean;\n /** Phase: 'out' = leaving scene fading out, 'in' = entering scene fading in. */\n phase: 'out' | 'in';\n}\n\n/** A transition controls the visual blending between two scenes. */\nexport interface Transition {\n /** Discriminator for consumers that apply a visual effect (alpha, translate, etc.). */\n readonly kind: 'none' | 'fade' | 'slide';\n\n /** Duration in seconds for each phase (out + in). */\n readonly duration: number;\n\n /** For slide transitions, the direction the outgoing scene slides toward. */\n readonly direction?: SlideDirection;\n\n /** Start the transition. */\n start(phase: 'out' | 'in'): TransitionState;\n\n /** Update the transition by dt seconds. Returns the updated state. */\n update(state: TransitionState, dt: number): TransitionState;\n}\n\nconst identity: EasingFn = (t) => t;\n\n/**\n * Returns a stateful advance function that tracks raw (linear) progress in its\n * own closure so the easing function is applied without accumulating drift.\n * Call `reset()` when starting a new transition phase.\n */\nfunction makeAdvancer(duration: number, easing: EasingFn) {\n let raw = 0;\n return {\n reset() {\n raw = 0;\n },\n advance(state: TransitionState, dt: number): TransitionState {\n if (state.done) return state;\n raw += dt / duration;\n if (raw >= 1) {\n raw = 1;\n state.progress = 1;\n state.done = true;\n } else {\n state.progress = easing(raw);\n }\n return state;\n },\n };\n}\n\n/** No transition — instant scene switch. */\nexport function noneTransition(): Transition {\n return {\n kind: 'none',\n duration: 0,\n start(phase) {\n return { progress: 1, done: true, phase };\n },\n update(state) {\n state.progress = 1;\n state.done = true;\n return state;\n },\n };\n}\n\n/** Fade transition — consumer interprets `progress` as alpha. */\nexport function fadeTransition(duration = 0.3, easing: EasingFn = identity): Transition {\n const advancer = makeAdvancer(duration, easing);\n return {\n kind: 'fade',\n duration,\n start(phase) {\n advancer.reset();\n return { progress: 0, done: false, phase };\n },\n update(state, dt) {\n return advancer.advance(state, dt);\n },\n };\n}\n\n/**\n * Slide transition — consumer interprets `progress` as a 0→1 offset along `direction`.\n * Default direction is 'left' (outgoing scene slides off to the left, incoming enters from the right).\n */\nexport function slideTransition(\n duration = 0.3,\n direction: SlideDirection = 'left',\n easing: EasingFn = identity,\n): Transition {\n const advancer = makeAdvancer(duration, easing);\n return {\n kind: 'slide',\n duration,\n direction,\n start(phase) {\n advancer.reset();\n return { progress: 0, done: false, phase };\n },\n update(state, dt) {\n return advancer.advance(state, dt);\n },\n };\n}\n","import { World } from '@flare-engine/ecs';\nimport { Camera2D } from '@flare-engine/render';\nimport type { Scene, SceneFactory } from './scene.js';\nimport type { Transition, TransitionState } from './transition.js';\nimport { noneTransition } from './transition.js';\n\n/**\n * SceneManager — manages scene registration, navigation, and a scene stack.\n *\n * - `register(name, factory)` — register a scene factory\n * - `go(name, data?)` — switch to a scene (clears the stack)\n * - `push(name, data?)` — push a scene onto the stack (pauses current)\n * - `pop()` — pop the top scene and resume the one below\n * - `update(dt)` — update the active scene and any active transition\n */\nexport class SceneManager {\n private _factories = new Map<string, SceneFactory>();\n private _scenes = new Map<string, Scene>();\n private _initialized = new Set<string>();\n private _stack: Scene[] = [];\n private _transition: Transition = noneTransition();\n private _transitionState: TransitionState | null = null;\n private _pendingScene: Scene | null = null;\n private _pendingData: unknown = undefined;\n\n /** The currently active scene (top of stack). */\n get current(): Scene | undefined {\n return this._stack.length > 0 ? this._stack[this._stack.length - 1] : undefined;\n }\n\n /** Number of scenes on the stack. */\n get stackSize(): number {\n return this._stack.length;\n }\n\n /** Whether a transition is currently in progress. */\n get isTransitioning(): boolean {\n return this._transitionState !== null;\n }\n\n /** Set the default transition for scene switches. */\n setTransition(transition: Transition): void {\n this._transition = transition;\n }\n\n /** Register a scene factory by name. */\n register(name: string, factory: SceneFactory): void {\n this._factories.set(name, factory);\n }\n\n /**\n * Navigate to a scene by name. Clears the entire stack.\n * Calls onExit on the current scene, onDestroy on all stacked scenes,\n * then onPreload → onInit → onEnter on the new scene.\n */\n async go(name: string, data?: unknown): Promise<void> {\n const scene = this._getOrCreate(name);\n if (!scene) return;\n\n if (this._transition.duration > 0 && this.current) {\n // Animated transition\n this._pendingScene = scene;\n this._pendingData = data;\n this._transitionState = this._transition.start('out');\n } else {\n // Instant transition\n this._exitAndDestroyCurrent();\n await this._activateScene(scene, data);\n }\n }\n\n /**\n * Push a scene onto the stack. The current scene gets onExit.\n * The new scene gets onPreload → onInit → onEnter.\n */\n async push(name: string, data?: unknown): Promise<void> {\n const scene = this._getOrCreate(name);\n if (!scene) return;\n\n this.current?.onExit?.();\n await this._initIfNeeded(scene);\n this._stack.push(scene);\n scene.onEnter?.(data);\n }\n\n /** Pop the top scene. Calls onExit + onDestroy on it, onEnter on the one below. */\n pop(): void {\n if (this._stack.length <= 1) return;\n\n const top = this._stack.pop()!;\n top.onExit?.();\n top.onDestroy?.();\n this._scenes.delete(top.name);\n this._initialized.delete(top.name);\n\n this.current?.onEnter?.();\n }\n\n /** Update the active scene and any active transition. */\n update(dt: number): void {\n if (this._transitionState) {\n this._updateTransition(dt);\n return;\n }\n\n const scene = this.current;\n if (scene) {\n scene.onUpdate?.(dt);\n scene.onDraw?.();\n }\n }\n\n /** Destroy all scenes and clear state. */\n destroy(): void {\n for (let i = this._stack.length - 1; i >= 0; i--) {\n const scene = this._stack[i]!;\n scene.onExit?.();\n scene.onDestroy?.();\n }\n this._stack.length = 0;\n this._scenes.clear();\n this._initialized.clear();\n this._transitionState = null;\n this._pendingScene = null;\n }\n\n /** Get the transition progress (0–1) for rendering. 0 during 'out', 1 when done. */\n get transitionProgress(): number {\n if (!this._transitionState) return 0;\n return this._transitionState.progress;\n }\n\n /** Get the current transition phase. */\n get transitionPhase(): 'out' | 'in' | null {\n return this._transitionState?.phase ?? null;\n }\n\n private _getOrCreate(name: string): Scene | null {\n let scene = this._scenes.get(name);\n if (scene) return scene;\n\n const factory = this._factories.get(name);\n if (!factory) return null;\n\n scene = factory();\n // Inject World and Camera\n scene.world = new World();\n scene.camera = new Camera2D();\n this._scenes.set(name, scene);\n return scene;\n }\n\n private async _initIfNeeded(scene: Scene): Promise<void> {\n if (this._initialized.has(scene.name)) return;\n this._initialized.add(scene.name);\n await scene.onPreload?.();\n scene.onInit?.();\n }\n\n private async _activateScene(scene: Scene, data?: unknown): Promise<void> {\n // Destroy all stacked scenes\n for (let i = this._stack.length - 1; i >= 0; i--) {\n const s = this._stack[i]!;\n s.onExit?.();\n s.onDestroy?.();\n this._scenes.delete(s.name);\n this._initialized.delete(s.name);\n }\n this._stack.length = 0;\n\n await this._initIfNeeded(scene);\n this._stack.push(scene);\n scene.onEnter?.(data);\n }\n\n private _exitAndDestroyCurrent(): void {\n for (let i = this._stack.length - 1; i >= 0; i--) {\n const s = this._stack[i]!;\n s.onExit?.();\n s.onDestroy?.();\n this._scenes.delete(s.name);\n this._initialized.delete(s.name);\n }\n this._stack.length = 0;\n }\n\n private _updateTransition(dt: number): void {\n if (!this._transitionState) return;\n\n this._transition.update(this._transitionState, dt);\n\n if (this._transitionState.done) {\n if (this._transitionState.phase === 'out') {\n // Phase 'out' done — switch scenes, start 'in' phase\n const pending = this._pendingScene;\n const data = this._pendingData;\n this._pendingScene = null;\n this._pendingData = undefined;\n\n if (pending) {\n // Destroy current stack\n for (let i = this._stack.length - 1; i >= 0; i--) {\n const s = this._stack[i]!;\n s.onExit?.();\n s.onDestroy?.();\n this._scenes.delete(s.name);\n this._initialized.delete(s.name);\n }\n this._stack.length = 0;\n\n // Init and activate pending scene synchronously for transitions\n this._scenes.set(pending.name, pending);\n if (!this._initialized.has(pending.name)) {\n this._initialized.add(pending.name);\n pending.onPreload?.();\n pending.onInit?.();\n }\n this._stack.push(pending);\n pending.onEnter?.(data);\n }\n\n this._transitionState = this._transition.start('in');\n } else {\n // Phase 'in' done — transition complete\n this._transitionState = null;\n }\n }\n\n // Still update the current scene during transition\n this.current?.onUpdate?.(dt);\n this.current?.onDraw?.();\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACmCA,IAAM,WAAqB,CAAC,MAAM;AAOlC,SAAS,aAAa,UAAkB,QAAkB;AACxD,MAAI,MAAM;AACV,SAAO;AAAA,IACL,QAAQ;AACN,YAAM;AAAA,IACR;AAAA,IACA,QAAQ,OAAwB,IAA6B;AAC3D,UAAI,MAAM,KAAM,QAAO;AACvB,aAAO,KAAK;AACZ,UAAI,OAAO,GAAG;AACZ,cAAM;AACN,cAAM,WAAW;AACjB,cAAM,OAAO;AAAA,MACf,OAAO;AACL,cAAM,WAAW,OAAO,GAAG;AAAA,MAC7B;AACA,aAAO;AAAA,IACT;AAAA,EACF;AACF;AAGO,SAAS,iBAA6B;AAC3C,SAAO;AAAA,IACL,MAAM;AAAA,IACN,UAAU;AAAA,IACV,MAAM,OAAO;AACX,aAAO,EAAE,UAAU,GAAG,MAAM,MAAM,MAAM;AAAA,IAC1C;AAAA,IACA,OAAO,OAAO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MACrD,OAAO;AAEL,aAAK,mBAAmB;AAAA,MAC1B;AAAA,IACF;AAGA,SAAK,SAAS,WAAW,EAAE;AAC3B,SAAK,SAAS,SAAS;AAAA,EACzB;AACF;","names":[]}
|
|
1
|
+
{"version":3,"file":"index.js","names":["World","Camera2D"],"sources":["../src/transition.ts","../src/scene-manager.ts"],"sourcesContent":["import type { EasingFn } from '@flare-engine/math';\n\nexport type { EasingFn };\n\n/** Slide direction — where the outgoing scene moves off-screen to. */\nexport type SlideDirection = 'left' | 'right' | 'up' | 'down';\n\n/** Transition state during a scene switch. */\nexport interface TransitionState {\n /** Progress 0 → 1. */\n progress: number;\n /** Whether the transition is complete. */\n done: boolean;\n /** Phase: 'out' = leaving scene fading out, 'in' = entering scene fading in. */\n phase: 'out' | 'in';\n}\n\n/** A transition controls the visual blending between two scenes. */\nexport interface Transition {\n /** Discriminator for consumers that apply a visual effect (alpha, translate, etc.). */\n readonly kind: 'none' | 'fade' | 'slide';\n\n /** Duration in seconds for each phase (out + in). */\n readonly duration: number;\n\n /** For slide transitions, the direction the outgoing scene slides toward. */\n readonly direction?: SlideDirection;\n\n /** Start the transition. */\n start(phase: 'out' | 'in'): TransitionState;\n\n /** Update the transition by dt seconds. Returns the updated state. */\n update(state: TransitionState, dt: number): TransitionState;\n}\n\nconst identity: EasingFn = (t) => t;\n\n/**\n * Returns a stateful advance function that tracks raw (linear) progress in its\n * own closure so the easing function is applied without accumulating drift.\n * Call `reset()` when starting a new transition phase.\n */\nfunction makeAdvancer(duration: number, easing: EasingFn) {\n let raw = 0;\n return {\n reset() {\n raw = 0;\n },\n advance(state: TransitionState, dt: number): TransitionState {\n if (state.done) return state;\n raw += dt / duration;\n if (raw >= 1) {\n raw = 1;\n state.progress = 1;\n state.done = true;\n } else {\n state.progress = easing(raw);\n }\n return state;\n },\n };\n}\n\n/** No transition — instant scene switch. */\nexport function noneTransition(): Transition {\n return {\n kind: 'none',\n duration: 0,\n start(phase) {\n return { progress: 1, done: true, phase };\n },\n update(state) {\n state.progress = 1;\n state.done = true;\n return state;\n },\n };\n}\n\n/** Fade transition — consumer interprets `progress` as alpha. */\nexport function fadeTransition(duration = 0.3, easing: EasingFn = identity): Transition {\n const advancer = makeAdvancer(duration, easing);\n return {\n kind: 'fade',\n duration,\n start(phase) {\n advancer.reset();\n return { progress: 0, done: false, phase };\n },\n update(state, dt) {\n return advancer.advance(state, dt);\n },\n };\n}\n\n/**\n * Slide transition — consumer interprets `progress` as a 0→1 offset along `direction`.\n * Default direction is 'left' (outgoing scene slides off to the left, incoming enters from the right).\n */\nexport function slideTransition(\n duration = 0.3,\n direction: SlideDirection = 'left',\n easing: EasingFn = identity,\n): Transition {\n const advancer = makeAdvancer(duration, easing);\n return {\n kind: 'slide',\n duration,\n direction,\n start(phase) {\n advancer.reset();\n return { progress: 0, done: false, phase };\n },\n update(state, dt) {\n return advancer.advance(state, dt);\n },\n };\n}\n","import { World } from '@flare-engine/ecs';\nimport { Camera2D } from '@flare-engine/render';\nimport type { Scene, SceneFactory } from './scene.js';\nimport type { Transition, TransitionState } from './transition.js';\nimport { noneTransition } from './transition.js';\n\ndeclare const __DEV__: boolean | undefined;\n\n/**\n * SceneManager — manages scene registration, navigation, and a scene stack.\n *\n * - `register(name, factory)` — register a scene factory\n * - `go(name, data?)` — switch to a scene (clears the stack)\n * - `push(name, data?)` — push a scene onto the stack (pauses current)\n * - `pop()` — pop the top scene and resume the one below\n * - `update(dt)` — update the active scene and any active transition\n */\nexport class SceneManager {\n private _factories = new Map<string, SceneFactory>();\n private _scenes = new Map<string, Scene>();\n private _initialized = new Set<string>();\n private _stack: Scene[] = [];\n private _transition: Transition = noneTransition();\n private _transitionState: TransitionState | null = null;\n private _pendingScene: Scene | null = null;\n private _pendingData: unknown = undefined;\n private _preloadResolved = true;\n\n /** The currently active scene (top of stack). */\n get current(): Scene | undefined {\n return this._stack.length > 0 ? this._stack[this._stack.length - 1] : undefined;\n }\n\n /** Number of scenes on the stack. */\n get stackSize(): number {\n return this._stack.length;\n }\n\n /** Whether a transition is currently in progress. */\n get isTransitioning(): boolean {\n return this._transitionState !== null;\n }\n\n /** Set the default transition for scene switches. */\n setTransition(transition: Transition): void {\n this._transition = transition;\n }\n\n /** Register a scene factory by name. */\n register(name: string, factory: SceneFactory): void {\n this._factories.set(name, factory);\n }\n\n /**\n * Navigate to a scene by name. Clears the entire stack.\n * Calls onExit on the current scene, onDestroy on all stacked scenes,\n * then onPreload → onInit → onEnter on the new scene.\n */\n async go(name: string, data?: unknown): Promise<void> {\n if (!this._factories.has(name)) {\n if (typeof __DEV__ !== 'undefined' && __DEV__) {\n console.warn(\n `[SceneManager] go('${name}'): scene not registered. Registered: ${[...this._factories.keys()].join(', ')}`,\n );\n }\n return;\n }\n\n if (this._transition.duration > 0 && this.current) {\n // Animated transition — evict target from cache first so restart\n // builds a fresh instance with a fresh World/Camera2D.\n this._scenes.delete(name);\n this._initialized.delete(name);\n const scene = this._getOrCreate(name)!;\n this._pendingScene = scene;\n this._pendingData = data;\n\n // Start preload concurrently with the fade-out.\n this._initialized.add(scene.name);\n this._preloadResolved = false;\n const preload = scene.onPreload?.();\n if (preload == null) {\n this._preloadResolved = true;\n } else {\n void preload.then(\n () => {\n this._preloadResolved = true;\n },\n (err) => {\n this._preloadResolved = true;\n if (typeof __DEV__ !== 'undefined' && __DEV__) {\n console.error(\n `[SceneManager] onPreload('${scene.name}') rejected; entering scene anyway`,\n err,\n );\n }\n },\n );\n }\n\n this._transitionState = this._transition.start('out');\n } else {\n // Instant transition — destroy first so _getOrCreate builds fresh.\n this._exitAndDestroyCurrent();\n const scene = this._getOrCreate(name)!;\n await this._activateScene(scene, data);\n }\n }\n\n /**\n * Push a scene onto the stack. The current scene gets onExit.\n * The new scene gets onPreload → onInit → onEnter.\n */\n async push(name: string, data?: unknown): Promise<void> {\n const scene = this._getOrCreate(name);\n if (!scene) return;\n\n this.current?.onExit?.();\n await this._initIfNeeded(scene);\n this._stack.push(scene);\n scene.onEnter?.(data);\n }\n\n /** Pop the top scene. Calls onExit + onDestroy on it, onEnter on the one below. */\n pop(): void {\n if (this._stack.length <= 1) return;\n\n const top = this._stack.pop()!;\n top.onExit?.();\n top.onDestroy?.();\n this._scenes.delete(top.name);\n this._initialized.delete(top.name);\n\n this.current?.onEnter?.();\n }\n\n /** Update the active scene and any active transition. */\n update(dt: number): void {\n if (this._transitionState) {\n this._updateTransition(dt);\n return;\n }\n\n const scene = this.current;\n if (scene) {\n scene.onUpdate?.(dt);\n scene.onDraw?.();\n }\n }\n\n /** Destroy all scenes and clear state. */\n destroy(): void {\n for (let i = this._stack.length - 1; i >= 0; i--) {\n const scene = this._stack[i]!;\n scene.onExit?.();\n scene.onDestroy?.();\n }\n this._stack.length = 0;\n this._scenes.clear();\n this._initialized.clear();\n this._transitionState = null;\n this._pendingScene = null;\n }\n\n /** Get the transition progress (0–1) for rendering. 0 during 'out', 1 when done. */\n get transitionProgress(): number {\n if (!this._transitionState) return 0;\n return this._transitionState.progress;\n }\n\n /** Get the current transition phase. */\n get transitionPhase(): 'out' | 'in' | null {\n return this._transitionState?.phase ?? null;\n }\n\n private _getOrCreate(name: string): Scene | null {\n let scene = this._scenes.get(name);\n if (scene) return scene;\n\n const factory = this._factories.get(name);\n if (!factory) return null;\n\n scene = factory();\n // Inject World and Camera\n scene.world = new World();\n scene.camera = new Camera2D();\n this._scenes.set(name, scene);\n return scene;\n }\n\n private async _initIfNeeded(scene: Scene): Promise<void> {\n if (this._initialized.has(scene.name)) return;\n this._initialized.add(scene.name);\n await scene.onPreload?.();\n scene.onInit?.();\n }\n\n private async _activateScene(scene: Scene, data?: unknown): Promise<void> {\n // Destroy all stacked scenes\n for (let i = this._stack.length - 1; i >= 0; i--) {\n const s = this._stack[i]!;\n s.onExit?.();\n s.onDestroy?.();\n this._scenes.delete(s.name);\n this._initialized.delete(s.name);\n }\n this._stack.length = 0;\n\n await this._initIfNeeded(scene);\n this._stack.push(scene);\n scene.onEnter?.(data);\n }\n\n private _exitAndDestroyCurrent(): void {\n for (let i = this._stack.length - 1; i >= 0; i--) {\n const s = this._stack[i]!;\n s.onExit?.();\n s.onDestroy?.();\n this._scenes.delete(s.name);\n this._initialized.delete(s.name);\n }\n this._stack.length = 0;\n }\n\n private _updateTransition(dt: number): void {\n if (!this._transitionState) return;\n\n this._transition.update(this._transitionState, dt);\n\n if (this._transitionState.done) {\n if (this._transitionState.phase === 'out') {\n // Hold until preload (started concurrently in go()) resolves.\n if (!this._preloadResolved) {\n this.current?.onUpdate?.(dt);\n this.current?.onDraw?.();\n return;\n }\n\n const pending = this._pendingScene;\n const data = this._pendingData;\n this._pendingScene = null;\n this._pendingData = undefined;\n\n if (pending) {\n // Destroy current stack\n for (let i = this._stack.length - 1; i >= 0; i--) {\n const s = this._stack[i]!;\n s.onExit?.();\n s.onDestroy?.();\n this._scenes.delete(s.name);\n this._initialized.delete(s.name);\n }\n this._stack.length = 0;\n\n // Preload already awaited in go(); call onInit then enter.\n this._scenes.set(pending.name, pending);\n this._initialized.add(pending.name);\n pending.onInit?.();\n this._stack.push(pending);\n pending.onEnter?.(data);\n }\n\n this._transitionState = this._transition.start('in');\n } else {\n // Phase 'in' done — transition complete\n this._transitionState = null;\n }\n }\n\n // Still update the current scene during transition\n this.current?.onUpdate?.(dt);\n this.current?.onDraw?.();\n }\n}\n"],"mappings":";;;;AAmCA,MAAM,YAAsB,MAAM;;;;;;AAOlC,SAAS,aAAa,UAAkB,QAAkB;CACxD,IAAI,MAAM;CACV,OAAO;EACL,QAAQ;GACN,MAAM;EACR;EACA,QAAQ,OAAwB,IAA6B;GAC3D,IAAI,MAAM,MAAM,OAAO;GACvB,OAAO,KAAK;GACZ,IAAI,OAAO,GAAG;IACZ,MAAM;IACN,MAAM,WAAW;IACjB,MAAM,OAAO;GACf,OACE,MAAM,WAAW,OAAO,GAAG;GAE7B,OAAO;EACT;CACF;AACF;;AAGA,SAAgB,iBAA6B;CAC3C,OAAO;EACL,MAAM;EACN,UAAU;EACV,MAAM,OAAO;G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;IACtC,KAAK,aAAa,IAAI,QAAQ,IAAI;IAClC,QAAQ,SAAS;IACjB,KAAK,OAAO,KAAK,OAAO;IACxB,QAAQ,UAAU,IAAI;GACxB;GAEA,KAAK,mBAAmB,KAAK,YAAY,MAAM,IAAI;EACrD,OAEE,KAAK,mBAAmB;EAK5B,KAAK,SAAS,WAAW,EAAE;EAC3B,KAAK,SAAS,SAAS;CACzB;AACF"}
|
package/dist/index.mjs
CHANGED
|
@@ -1,254 +1,296 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
import { World } from "@flare-engine/ecs";
|
|
2
|
+
import { Camera2D } from "@flare-engine/render";
|
|
3
|
+
//#region src/transition.ts
|
|
4
|
+
const identity = (t) => t;
|
|
5
|
+
/**
|
|
6
|
+
* Returns a stateful advance function that tracks raw (linear) progress in its
|
|
7
|
+
* own closure so the easing function is applied without accumulating drift.
|
|
8
|
+
* Call `reset()` when starting a new transition phase.
|
|
9
|
+
*/
|
|
3
10
|
function makeAdvancer(duration, easing) {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
}
|
|
21
|
-
};
|
|
11
|
+
let raw = 0;
|
|
12
|
+
return {
|
|
13
|
+
reset() {
|
|
14
|
+
raw = 0;
|
|
15
|
+
},
|
|
16
|
+
advance(state, dt) {
|
|
17
|
+
if (state.done) return state;
|
|
18
|
+
raw += dt / duration;
|
|
19
|
+
if (raw >= 1) {
|
|
20
|
+
raw = 1;
|
|
21
|
+
state.progress = 1;
|
|
22
|
+
state.done = true;
|
|
23
|
+
} else state.progress = easing(raw);
|
|
24
|
+
return state;
|
|
25
|
+
}
|
|
26
|
+
};
|
|
22
27
|
}
|
|
28
|
+
/** No transition — instant scene switch. */
|
|
23
29
|
function noneTransition() {
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
30
|
+
return {
|
|
31
|
+
kind: "none",
|
|
32
|
+
duration: 0,
|
|
33
|
+
start(phase) {
|
|
34
|
+
return {
|
|
35
|
+
progress: 1,
|
|
36
|
+
done: true,
|
|
37
|
+
phase
|
|
38
|
+
};
|
|
39
|
+
},
|
|
40
|
+
update(state) {
|
|
41
|
+
state.progress = 1;
|
|
42
|
+
state.done = true;
|
|
43
|
+
return state;
|
|
44
|
+
}
|
|
45
|
+
};
|
|
36
46
|
}
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
47
|
+
/** Fade transition — consumer interprets `progress` as alpha. */
|
|
48
|
+
function fadeTransition(duration = .3, easing = identity) {
|
|
49
|
+
const advancer = makeAdvancer(duration, easing);
|
|
50
|
+
return {
|
|
51
|
+
kind: "fade",
|
|
52
|
+
duration,
|
|
53
|
+
start(phase) {
|
|
54
|
+
advancer.reset();
|
|
55
|
+
return {
|
|
56
|
+
progress: 0,
|
|
57
|
+
done: false,
|
|
58
|
+
phase
|
|
59
|
+
};
|
|
60
|
+
},
|
|
61
|
+
update(state, dt) {
|
|
62
|
+
return advancer.advance(state, dt);
|
|
63
|
+
}
|
|
64
|
+
};
|
|
50
65
|
}
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
66
|
+
/**
|
|
67
|
+
* Slide transition — consumer interprets `progress` as a 0→1 offset along `direction`.
|
|
68
|
+
* Default direction is 'left' (outgoing scene slides off to the left, incoming enters from the right).
|
|
69
|
+
*/
|
|
70
|
+
function slideTransition(duration = .3, direction = "left", easing = identity) {
|
|
71
|
+
const advancer = makeAdvancer(duration, easing);
|
|
72
|
+
return {
|
|
73
|
+
kind: "slide",
|
|
74
|
+
duration,
|
|
75
|
+
direction,
|
|
76
|
+
start(phase) {
|
|
77
|
+
advancer.reset();
|
|
78
|
+
return {
|
|
79
|
+
progress: 0,
|
|
80
|
+
done: false,
|
|
81
|
+
phase
|
|
82
|
+
};
|
|
83
|
+
},
|
|
84
|
+
update(state, dt) {
|
|
85
|
+
return advancer.advance(state, dt);
|
|
86
|
+
}
|
|
87
|
+
};
|
|
65
88
|
}
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
89
|
+
//#endregion
|
|
90
|
+
//#region src/scene-manager.ts
|
|
91
|
+
/**
|
|
92
|
+
* SceneManager — manages scene registration, navigation, and a scene stack.
|
|
93
|
+
*
|
|
94
|
+
* - `register(name, factory)` — register a scene factory
|
|
95
|
+
* - `go(name, data?)` — switch to a scene (clears the stack)
|
|
96
|
+
* - `push(name, data?)` — push a scene onto the stack (pauses current)
|
|
97
|
+
* - `pop()` — pop the top scene and resume the one below
|
|
98
|
+
* - `update(dt)` — update the active scene and any active transition
|
|
99
|
+
*/
|
|
70
100
|
var SceneManager = class {
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
101
|
+
_factories = /* @__PURE__ */ new Map();
|
|
102
|
+
_scenes = /* @__PURE__ */ new Map();
|
|
103
|
+
_initialized = /* @__PURE__ */ new Set();
|
|
104
|
+
_stack = [];
|
|
105
|
+
_transition = noneTransition();
|
|
106
|
+
_transitionState = null;
|
|
107
|
+
_pendingScene = null;
|
|
108
|
+
_pendingData = void 0;
|
|
109
|
+
_preloadResolved = true;
|
|
110
|
+
/** The currently active scene (top of stack). */
|
|
111
|
+
get current() {
|
|
112
|
+
return this._stack.length > 0 ? this._stack[this._stack.length - 1] : void 0;
|
|
113
|
+
}
|
|
114
|
+
/** Number of scenes on the stack. */
|
|
115
|
+
get stackSize() {
|
|
116
|
+
return this._stack.length;
|
|
117
|
+
}
|
|
118
|
+
/** Whether a transition is currently in progress. */
|
|
119
|
+
get isTransitioning() {
|
|
120
|
+
return this._transitionState !== null;
|
|
121
|
+
}
|
|
122
|
+
/** Set the default transition for scene switches. */
|
|
123
|
+
setTransition(transition) {
|
|
124
|
+
this._transition = transition;
|
|
125
|
+
}
|
|
126
|
+
/** Register a scene factory by name. */
|
|
127
|
+
register(name, factory) {
|
|
128
|
+
this._factories.set(name, factory);
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Navigate to a scene by name. Clears the entire stack.
|
|
132
|
+
* Calls onExit on the current scene, onDestroy on all stacked scenes,
|
|
133
|
+
* then onPreload → onInit → onEnter on the new scene.
|
|
134
|
+
*/
|
|
135
|
+
async go(name, data) {
|
|
136
|
+
if (!this._factories.has(name)) {
|
|
137
|
+
if (typeof __DEV__ !== "undefined" && __DEV__) console.warn(`[SceneManager] go('${name}'): scene not registered. Registered: ${[...this._factories.keys()].join(", ")}`);
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
if (this._transition.duration > 0 && this.current) {
|
|
141
|
+
this._scenes.delete(name);
|
|
142
|
+
this._initialized.delete(name);
|
|
143
|
+
const scene = this._getOrCreate(name);
|
|
144
|
+
this._pendingScene = scene;
|
|
145
|
+
this._pendingData = data;
|
|
146
|
+
this._initialized.add(scene.name);
|
|
147
|
+
this._preloadResolved = false;
|
|
148
|
+
const preload = scene.onPreload?.();
|
|
149
|
+
if (preload == null) this._preloadResolved = true;
|
|
150
|
+
else preload.then(() => {
|
|
151
|
+
this._preloadResolved = true;
|
|
152
|
+
}, (err) => {
|
|
153
|
+
this._preloadResolved = true;
|
|
154
|
+
if (typeof __DEV__ !== "undefined" && __DEV__) console.error(`[SceneManager] onPreload('${scene.name}') rejected; entering scene anyway`, err);
|
|
155
|
+
});
|
|
156
|
+
this._transitionState = this._transition.start("out");
|
|
157
|
+
} else {
|
|
158
|
+
this._exitAndDestroyCurrent();
|
|
159
|
+
const scene = this._getOrCreate(name);
|
|
160
|
+
await this._activateScene(scene, data);
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* Push a scene onto the stack. The current scene gets onExit.
|
|
165
|
+
* The new scene gets onPreload → onInit → onEnter.
|
|
166
|
+
*/
|
|
167
|
+
async push(name, data) {
|
|
168
|
+
const scene = this._getOrCreate(name);
|
|
169
|
+
if (!scene) return;
|
|
170
|
+
this.current?.onExit?.();
|
|
171
|
+
await this._initIfNeeded(scene);
|
|
172
|
+
this._stack.push(scene);
|
|
173
|
+
scene.onEnter?.(data);
|
|
174
|
+
}
|
|
175
|
+
/** Pop the top scene. Calls onExit + onDestroy on it, onEnter on the one below. */
|
|
176
|
+
pop() {
|
|
177
|
+
if (this._stack.length <= 1) return;
|
|
178
|
+
const top = this._stack.pop();
|
|
179
|
+
top.onExit?.();
|
|
180
|
+
top.onDestroy?.();
|
|
181
|
+
this._scenes.delete(top.name);
|
|
182
|
+
this._initialized.delete(top.name);
|
|
183
|
+
this.current?.onEnter?.();
|
|
184
|
+
}
|
|
185
|
+
/** Update the active scene and any active transition. */
|
|
186
|
+
update(dt) {
|
|
187
|
+
if (this._transitionState) {
|
|
188
|
+
this._updateTransition(dt);
|
|
189
|
+
return;
|
|
190
|
+
}
|
|
191
|
+
const scene = this.current;
|
|
192
|
+
if (scene) {
|
|
193
|
+
scene.onUpdate?.(dt);
|
|
194
|
+
scene.onDraw?.();
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
/** Destroy all scenes and clear state. */
|
|
198
|
+
destroy() {
|
|
199
|
+
for (let i = this._stack.length - 1; i >= 0; i--) {
|
|
200
|
+
const scene = this._stack[i];
|
|
201
|
+
scene.onExit?.();
|
|
202
|
+
scene.onDestroy?.();
|
|
203
|
+
}
|
|
204
|
+
this._stack.length = 0;
|
|
205
|
+
this._scenes.clear();
|
|
206
|
+
this._initialized.clear();
|
|
207
|
+
this._transitionState = null;
|
|
208
|
+
this._pendingScene = null;
|
|
209
|
+
}
|
|
210
|
+
/** Get the transition progress (0–1) for rendering. 0 during 'out', 1 when done. */
|
|
211
|
+
get transitionProgress() {
|
|
212
|
+
if (!this._transitionState) return 0;
|
|
213
|
+
return this._transitionState.progress;
|
|
214
|
+
}
|
|
215
|
+
/** Get the current transition phase. */
|
|
216
|
+
get transitionPhase() {
|
|
217
|
+
return this._transitionState?.phase ?? null;
|
|
218
|
+
}
|
|
219
|
+
_getOrCreate(name) {
|
|
220
|
+
let scene = this._scenes.get(name);
|
|
221
|
+
if (scene) return scene;
|
|
222
|
+
const factory = this._factories.get(name);
|
|
223
|
+
if (!factory) return null;
|
|
224
|
+
scene = factory();
|
|
225
|
+
scene.world = new World();
|
|
226
|
+
scene.camera = new Camera2D();
|
|
227
|
+
this._scenes.set(name, scene);
|
|
228
|
+
return scene;
|
|
229
|
+
}
|
|
230
|
+
async _initIfNeeded(scene) {
|
|
231
|
+
if (this._initialized.has(scene.name)) return;
|
|
232
|
+
this._initialized.add(scene.name);
|
|
233
|
+
await scene.onPreload?.();
|
|
234
|
+
scene.onInit?.();
|
|
235
|
+
}
|
|
236
|
+
async _activateScene(scene, data) {
|
|
237
|
+
for (let i = this._stack.length - 1; i >= 0; i--) {
|
|
238
|
+
const s = this._stack[i];
|
|
239
|
+
s.onExit?.();
|
|
240
|
+
s.onDestroy?.();
|
|
241
|
+
this._scenes.delete(s.name);
|
|
242
|
+
this._initialized.delete(s.name);
|
|
243
|
+
}
|
|
244
|
+
this._stack.length = 0;
|
|
245
|
+
await this._initIfNeeded(scene);
|
|
246
|
+
this._stack.push(scene);
|
|
247
|
+
scene.onEnter?.(data);
|
|
248
|
+
}
|
|
249
|
+
_exitAndDestroyCurrent() {
|
|
250
|
+
for (let i = this._stack.length - 1; i >= 0; i--) {
|
|
251
|
+
const s = this._stack[i];
|
|
252
|
+
s.onExit?.();
|
|
253
|
+
s.onDestroy?.();
|
|
254
|
+
this._scenes.delete(s.name);
|
|
255
|
+
this._initialized.delete(s.name);
|
|
256
|
+
}
|
|
257
|
+
this._stack.length = 0;
|
|
258
|
+
}
|
|
259
|
+
_updateTransition(dt) {
|
|
260
|
+
if (!this._transitionState) return;
|
|
261
|
+
this._transition.update(this._transitionState, dt);
|
|
262
|
+
if (this._transitionState.done) if (this._transitionState.phase === "out") {
|
|
263
|
+
if (!this._preloadResolved) {
|
|
264
|
+
this.current?.onUpdate?.(dt);
|
|
265
|
+
this.current?.onDraw?.();
|
|
266
|
+
return;
|
|
267
|
+
}
|
|
268
|
+
const pending = this._pendingScene;
|
|
269
|
+
const data = this._pendingData;
|
|
270
|
+
this._pendingScene = null;
|
|
271
|
+
this._pendingData = void 0;
|
|
272
|
+
if (pending) {
|
|
273
|
+
for (let i = this._stack.length - 1; i >= 0; i--) {
|
|
274
|
+
const s = this._stack[i];
|
|
275
|
+
s.onExit?.();
|
|
276
|
+
s.onDestroy?.();
|
|
277
|
+
this._scenes.delete(s.name);
|
|
278
|
+
this._initialized.delete(s.name);
|
|
279
|
+
}
|
|
280
|
+
this._stack.length = 0;
|
|
281
|
+
this._scenes.set(pending.name, pending);
|
|
282
|
+
this._initialized.add(pending.name);
|
|
283
|
+
pending.onInit?.();
|
|
284
|
+
this._stack.push(pending);
|
|
285
|
+
pending.onEnter?.(data);
|
|
286
|
+
}
|
|
287
|
+
this._transitionState = this._transition.start("in");
|
|
288
|
+
} else this._transitionState = null;
|
|
289
|
+
this.current?.onUpdate?.(dt);
|
|
290
|
+
this.current?.onDraw?.();
|
|
291
|
+
}
|
|
253
292
|
};
|
|
293
|
+
//#endregion
|
|
294
|
+
export { SceneManager, fadeTransition, noneTransition, slideTransition };
|
|
295
|
+
|
|
254
296
|
//# sourceMappingURL=index.mjs.map
|