ecspresso 0.12.7 → 0.13.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/README.md +11 -0
- package/dist/index.js +2 -2
- package/dist/index.js.map +5 -5
- package/dist/plugin.d.ts +89 -22
- package/dist/plugins/audio.d.ts +2 -3
- package/dist/plugins/audio.js +2 -2
- package/dist/plugins/audio.js.map +3 -3
- package/dist/plugins/bounds.d.ts +2 -3
- package/dist/plugins/bounds.js +2 -2
- package/dist/plugins/bounds.js.map +3 -3
- package/dist/plugins/camera.d.ts +1 -2
- package/dist/plugins/camera.js +2 -2
- package/dist/plugins/camera.js.map +3 -3
- package/dist/plugins/collision.d.ts +9 -8
- package/dist/plugins/collision.js +2 -2
- package/dist/plugins/collision.js.map +4 -4
- package/dist/plugins/coroutine.d.ts +2 -3
- package/dist/plugins/coroutine.js +2 -2
- package/dist/plugins/coroutine.js.map +3 -3
- package/dist/plugins/diagnostics.d.ts +1 -3
- package/dist/plugins/diagnostics.js +2 -2
- package/dist/plugins/diagnostics.js.map +3 -3
- package/dist/plugins/input.d.ts +11 -3
- package/dist/plugins/input.js +2 -2
- package/dist/plugins/input.js.map +3 -3
- package/dist/plugins/particles.d.ts +2 -2
- package/dist/plugins/particles.js +2 -2
- package/dist/plugins/particles.js.map +3 -3
- package/dist/plugins/physics2D.d.ts +8 -5
- package/dist/plugins/physics2D.js +2 -2
- package/dist/plugins/physics2D.js.map +4 -4
- package/dist/plugins/renderers/renderer2D.d.ts +36 -9
- package/dist/plugins/renderers/renderer2D.js +2 -2
- package/dist/plugins/renderers/renderer2D.js.map +3 -3
- package/dist/plugins/spatial-index.d.ts +1 -4
- package/dist/plugins/spatial-index.js +2 -2
- package/dist/plugins/spatial-index.js.map +4 -4
- package/dist/plugins/sprite-animation.d.ts +2 -3
- package/dist/plugins/sprite-animation.js +2 -2
- package/dist/plugins/sprite-animation.js.map +3 -3
- package/dist/plugins/state-machine.d.ts +2 -3
- package/dist/plugins/state-machine.js +2 -2
- package/dist/plugins/state-machine.js.map +3 -3
- package/dist/plugins/timers.d.ts +2 -3
- package/dist/plugins/timers.js +2 -2
- package/dist/plugins/timers.js.map +3 -3
- package/dist/plugins/transform.d.ts +3 -3
- package/dist/plugins/transform.js +2 -2
- package/dist/plugins/transform.js.map +3 -3
- package/dist/plugins/tween.d.ts +2 -3
- package/dist/plugins/tween.js +2 -2
- package/dist/plugins/tween.js.map +3 -3
- package/dist/utils/narrowphase.d.ts +60 -19
- package/dist/utils/spatial-hash.d.ts +11 -1
- package/package.json +6 -3
package/dist/plugin.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type ECSpresso from './ecspresso';
|
|
2
2
|
import type { SystemPhase } from './types';
|
|
3
|
-
import type { WorldConfig, EmptyConfig, MergeConfigs,
|
|
3
|
+
import type { WorldConfig, EmptyConfig, MergeConfigs, WithComponents, WithEvents, WithResources, WithAssets, WithScreens } from './type-utils';
|
|
4
4
|
/**
|
|
5
5
|
* Plugin interface for ECSpresso. A plugin is a plain object with an `install`
|
|
6
6
|
* function that configures a world directly, plus phantom properties for
|
|
@@ -32,30 +32,97 @@ export interface BasePluginOptions<G extends string = string> {
|
|
|
32
32
|
phase?: SystemPhase;
|
|
33
33
|
}
|
|
34
34
|
/**
|
|
35
|
-
*
|
|
36
|
-
*
|
|
35
|
+
* Fluent builder for defining plugins. Mirrors `ECSpressoBuilder`'s
|
|
36
|
+
* type-accumulator pattern: each `.withXxx<T>()` call threads `T` into the
|
|
37
|
+
* appropriate WorldConfig slot at the type level, with no runtime cost.
|
|
38
|
+
*
|
|
39
|
+
* Terminal call is `.install(fn)` which returns the finalized `Plugin<...>`.
|
|
37
40
|
*
|
|
38
41
|
* @example
|
|
39
42
|
* ```typescript
|
|
40
|
-
*
|
|
41
|
-
*
|
|
42
|
-
*
|
|
43
|
-
*
|
|
44
|
-
*
|
|
43
|
+
* const myPlugin = definePlugin('my-plugin')
|
|
44
|
+
* .withComponentTypes<MyComponents>()
|
|
45
|
+
* .withEventTypes<MyEvents>()
|
|
46
|
+
* .withResourceTypes<MyResources>()
|
|
47
|
+
* .install((world) => {
|
|
48
|
+
* world.addSystem('foo').setProcess(() => {});
|
|
49
|
+
* });
|
|
50
|
+
* ```
|
|
51
|
+
*/
|
|
52
|
+
export declare class PluginBuilder<Cfg extends WorldConfig = EmptyConfig, Requires extends WorldConfig = EmptyConfig, Labels extends string = never, Groups extends string = never, AssetGroupNames extends string = never, ReactiveQueryNames extends string = never> {
|
|
53
|
+
private readonly _id;
|
|
54
|
+
constructor(_id: string);
|
|
55
|
+
/**
|
|
56
|
+
* Declare component types this plugin provides.
|
|
57
|
+
* Pure type-level operation with no runtime cost.
|
|
58
|
+
*/
|
|
59
|
+
withComponentTypes<T extends Record<string, any>>(): PluginBuilder<WithComponents<Cfg, T>, Requires, Labels, Groups, AssetGroupNames, ReactiveQueryNames>;
|
|
60
|
+
/**
|
|
61
|
+
* Declare event types this plugin provides.
|
|
62
|
+
* Pure type-level operation with no runtime cost.
|
|
63
|
+
*/
|
|
64
|
+
withEventTypes<T extends Record<string, any>>(): PluginBuilder<WithEvents<Cfg, T>, Requires, Labels, Groups, AssetGroupNames, ReactiveQueryNames>;
|
|
65
|
+
/**
|
|
66
|
+
* Declare resource types this plugin provides.
|
|
67
|
+
* Pure type-level operation with no runtime cost.
|
|
68
|
+
*/
|
|
69
|
+
withResourceTypes<T extends Record<string, any>>(): PluginBuilder<WithResources<Cfg, T>, Requires, Labels, Groups, AssetGroupNames, ReactiveQueryNames>;
|
|
70
|
+
/**
|
|
71
|
+
* Declare asset types this plugin provides.
|
|
72
|
+
* Pure type-level operation with no runtime cost.
|
|
73
|
+
*/
|
|
74
|
+
withAssetTypes<T extends Record<string, unknown>>(): PluginBuilder<WithAssets<Cfg, T>, Requires, Labels, Groups, AssetGroupNames, ReactiveQueryNames>;
|
|
75
|
+
/**
|
|
76
|
+
* Declare screen types this plugin provides.
|
|
77
|
+
* Pure type-level operation with no runtime cost.
|
|
78
|
+
*/
|
|
79
|
+
withScreenTypes<T extends Record<string, any>>(): PluginBuilder<WithScreens<Cfg, T>, Requires, Labels, Groups, AssetGroupNames, ReactiveQueryNames>;
|
|
80
|
+
/**
|
|
81
|
+
* Declare system labels this plugin registers.
|
|
82
|
+
* Pure type-level operation with no runtime cost.
|
|
83
|
+
*/
|
|
84
|
+
withLabels<L extends string>(): PluginBuilder<Cfg, Requires, Labels | L, Groups, AssetGroupNames, ReactiveQueryNames>;
|
|
85
|
+
/**
|
|
86
|
+
* Declare system groups this plugin uses.
|
|
87
|
+
* Pure type-level operation with no runtime cost.
|
|
88
|
+
*/
|
|
89
|
+
withGroups<G extends string>(): PluginBuilder<Cfg, Requires, Labels, Groups | G, AssetGroupNames, ReactiveQueryNames>;
|
|
90
|
+
/**
|
|
91
|
+
* Declare asset group names this plugin uses.
|
|
92
|
+
* Pure type-level operation with no runtime cost.
|
|
93
|
+
*/
|
|
94
|
+
withAssetGroupNames<N extends string>(): PluginBuilder<Cfg, Requires, Labels, Groups, AssetGroupNames | N, ReactiveQueryNames>;
|
|
95
|
+
/**
|
|
96
|
+
* Declare reactive query names this plugin registers.
|
|
97
|
+
* Pure type-level operation with no runtime cost.
|
|
98
|
+
*/
|
|
99
|
+
withReactiveQueryNames<N extends string>(): PluginBuilder<Cfg, Requires, Labels, Groups, AssetGroupNames, ReactiveQueryNames | N>;
|
|
100
|
+
/**
|
|
101
|
+
* Declare dependencies this plugin requires from other plugins.
|
|
102
|
+
* Accepts a pre-built `WorldConfig` type (typically a named alias like
|
|
103
|
+
* `TransformWorldConfig`). The install callback will see these types
|
|
104
|
+
* merged into its world parameter.
|
|
105
|
+
* Pure type-level operation with no runtime cost.
|
|
106
|
+
*/
|
|
107
|
+
requires<R extends WorldConfig>(): PluginBuilder<Cfg, R, Labels, Groups, AssetGroupNames, ReactiveQueryNames>;
|
|
108
|
+
/**
|
|
109
|
+
* Terminal method. Provide the install function and receive the finalized
|
|
110
|
+
* `Plugin<...>` object. The install function receives a world typed as
|
|
111
|
+
* `ECSpresso<MergeConfigs<Cfg, Requires>>` — meaning it can use both the
|
|
112
|
+
* types this plugin provides and the types it declared via `.requires<>()`.
|
|
113
|
+
*/
|
|
114
|
+
install(install: (world: ECSpresso<MergeConfigs<Cfg, Requires>>) => void): Plugin<Cfg, Requires, Labels, Groups, AssetGroupNames, ReactiveQueryNames>;
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* Entry point for the fluent plugin builder. Pass the plugin id and chain
|
|
118
|
+
* type-accumulator methods, terminating with `.install(fn)`.
|
|
45
119
|
*
|
|
46
|
-
*
|
|
47
|
-
*
|
|
48
|
-
* const myPlugin = definePlugin
|
|
49
|
-
*
|
|
50
|
-
*
|
|
51
|
-
* });
|
|
120
|
+
* @example
|
|
121
|
+
* ```typescript
|
|
122
|
+
* const myPlugin = definePlugin('my-plugin')
|
|
123
|
+
* .withComponentTypes<MyComponents>()
|
|
124
|
+
* .withResourceTypes<MyResources>()
|
|
125
|
+
* .install((world) => { ... });
|
|
52
126
|
* ```
|
|
53
127
|
*/
|
|
54
|
-
export declare function definePlugin
|
|
55
|
-
id: string;
|
|
56
|
-
install: (world: W) => void;
|
|
57
|
-
}): Plugin<ConfigOf<W>, Requires, Labels, Groups, AssetGroupNames, ReactiveQueryNames>;
|
|
58
|
-
export declare function definePlugin<Cfg extends WorldConfig = EmptyConfig, Requires extends WorldConfig = EmptyConfig, Labels extends string = never, Groups extends string = never, AssetGroupNames extends string = never, ReactiveQueryNames extends string = never>(config: {
|
|
59
|
-
id: string;
|
|
60
|
-
install: (world: ECSpresso<MergeConfigs<Cfg, Requires>>) => void;
|
|
61
|
-
}): Plugin<Cfg, Requires, Labels, Groups, AssetGroupNames, ReactiveQueryNames>;
|
|
128
|
+
export declare function definePlugin(id: string): PluginBuilder;
|
package/dist/plugins/audio.d.ts
CHANGED
|
@@ -5,9 +5,8 @@
|
|
|
5
5
|
* User-defined channels with type-safe volume control, hybrid resource + component API,
|
|
6
6
|
* and asset manager integration.
|
|
7
7
|
*/
|
|
8
|
-
import { type
|
|
8
|
+
import { type BasePluginOptions } from 'ecspresso';
|
|
9
9
|
import type { AssetsOfWorld, AnyECSpresso, ChannelOfWorld } from 'ecspresso';
|
|
10
|
-
import type { WorldConfigFrom, EmptyConfig } from '../type-utils';
|
|
11
10
|
import type { Howl } from 'howler';
|
|
12
11
|
/**
|
|
13
12
|
* Configuration for a single audio channel.
|
|
@@ -243,7 +242,7 @@ export declare function loadSound(src: string | string[], options?: {
|
|
|
243
242
|
* audio.play('explosion', { channel: 'sfx' });
|
|
244
243
|
* ```
|
|
245
244
|
*/
|
|
246
|
-
export declare function createAudioPlugin<Ch extends string, G extends string = 'audio'>(options: AudioPluginOptions<Ch, G>): Plugin<
|
|
245
|
+
export declare function createAudioPlugin<Ch extends string, G extends string = 'audio'>(options: AudioPluginOptions<Ch, G>): import("ecspresso").Plugin<import("ecspresso").WithResources<import("ecspresso").WithEvents<import("ecspresso").WithComponents<import("ecspresso").EmptyConfig, AudioComponentTypes<Ch>>, AudioEventTypes<Ch>>, AudioResourceTypes<Ch>>, import("ecspresso").EmptyConfig, "audio-sync", G, never, "audio-sources">;
|
|
247
246
|
/**
|
|
248
247
|
* Typed helpers for the audio plugin.
|
|
249
248
|
* Creates helpers that validate sound keys and channel names against the world type W.
|
package/dist/plugins/audio.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
var A=((Q)=>typeof require<"u"?require:typeof Proxy<"u"?new Proxy(Q,{get:($,Y)=>(typeof require<"u"?require:$)[Y]}):Q)(function(Q){if(typeof require<"u")return require.apply(this,arguments);throw Error('Dynamic require of "'+Q+'" is not supported')});import{definePlugin as B}from"ecspresso";function I(Q){return Object.freeze(Q)}function S(Q,$,Y){return{audioSource:{sound:Q,channel:$,volume:Y?.volume??1,loop:Y?.loop??!1,autoRemove:Y?.autoRemove??!1,playing:!1,_soundId:-1}}}function f(Q,$){return()=>import("howler").then(({Howl:Y})=>new Promise((T,H)=>{let Z,K=!1;if(Z=new Y({src:Array.isArray(Q)?Q:[Q],html5:$?.html5??!1,preload:$?.preload??!0,onload:()=>{K=!0,T(Z)},onloaderror:(U,G)=>H(G instanceof Error?G:Error(String(G)))}),!K&&Z.state?.()==="loaded")T(Z)}))}function w(Q){let{channels:$,systemGroup:Y="audio",priority:T=0,phase:H="update"}=Q,Z=new Map,K=new Map,U=new Map,G=1,j=!1,N=[];for(let[z,q]of Object.entries($))Z.set(z,q.volume),N.push(z);let P=N[0];function E(z,q){if(j)return 0;let F=Z.get(q)??1;return z*F*G}function R(z){for(let F of K.values()){if(F.channel!==z)continue;F.howl.volume(E(F.individualVolume,z),F.soundId)}let q=U.get(z);if(q)q.howl.volume(E(q.individualVolume,z),q.soundId)}function W(){for(let z of N)R(z)}function b(z){let q=K.get(z);if(!q)return;q.howl.stop(z),K.delete(z)}let k=null,D=null,O={play(z,q){if(!D)return-1;let F=q?.channel??P,X=q?.volume??1,L=q?.loop??!1,M=D(z);M.volume(E(X,F)),M.loop(L);let J=M.play(),_={howl:M,soundId:J,channel:F,individualVolume:X,assetKey:z,entityId:-1};return K.set(J,_),M.once("end",()=>{K.delete(J),k?.publish("soundEnded",{entityId:-1,soundId:J,sound:z})},J),J},stop(z){b(z)},playMusic(z,q){if(!D)return;let F=q?.channel??P,X=q?.volume??1,L=q?.loop??!0,M=U.get(F);if(M)M.howl.stop(M.soundId),K.delete(M.soundId);let J=D(z);J.volume(E(X,F)),J.loop(L);let _=J.play(),x={howl:J,soundId:_,channel:F,individualVolume:X,assetKey:z};U.set(F,x),K.set(_,{...x,entityId:-1}),J.once("end",()=>{if(K.delete(_),U.get(F)?.soundId===_)U.delete(F)},_)},stopMusic(z){if(z!==void 0){let q=U.get(z);if(q)q.howl.stop(q.soundId),K.delete(q.soundId),U.delete(z)}else for(let[q,F]of U)F.howl.stop(F.soundId),K.delete(F.soundId),U.delete(q)},pauseMusic(z){if(z!==void 0){let q=U.get(z);if(q)q.howl.pause(q.soundId)}else for(let q of U.values())q.howl.pause(q.soundId)},resumeMusic(z){if(z!==void 0){let q=U.get(z);if(q)q.howl.play(q.soundId)}else for(let q of U.values())q.howl.play(q.soundId)},setChannelVolume(z,q){Z.set(z,q),R(z)},getChannelVolume(z){return Z.get(z)??1},setMasterVolume(z){G=z,W()},getMasterVolume(){return G},mute(){j=!0,W()},unmute(){j=!1,W()},toggleMute(){j=!j,W()},isMuted(){return j}};return B(
|
|
1
|
+
var A=((Q)=>typeof require<"u"?require:typeof Proxy<"u"?new Proxy(Q,{get:($,Y)=>(typeof require<"u"?require:$)[Y]}):Q)(function(Q){if(typeof require<"u")return require.apply(this,arguments);throw Error('Dynamic require of "'+Q+'" is not supported')});import{definePlugin as B}from"ecspresso";function I(Q){return Object.freeze(Q)}function S(Q,$,Y){return{audioSource:{sound:Q,channel:$,volume:Y?.volume??1,loop:Y?.loop??!1,autoRemove:Y?.autoRemove??!1,playing:!1,_soundId:-1}}}function f(Q,$){return()=>import("howler").then(({Howl:Y})=>new Promise((T,H)=>{let Z,K=!1;if(Z=new Y({src:Array.isArray(Q)?Q:[Q],html5:$?.html5??!1,preload:$?.preload??!0,onload:()=>{K=!0,T(Z)},onloaderror:(U,G)=>H(G instanceof Error?G:Error(String(G)))}),!K&&Z.state?.()==="loaded")T(Z)}))}function w(Q){let{channels:$,systemGroup:Y="audio",priority:T=0,phase:H="update"}=Q,Z=new Map,K=new Map,U=new Map,G=1,j=!1,N=[];for(let[z,q]of Object.entries($))Z.set(z,q.volume),N.push(z);let P=N[0];function E(z,q){if(j)return 0;let F=Z.get(q)??1;return z*F*G}function R(z){for(let F of K.values()){if(F.channel!==z)continue;F.howl.volume(E(F.individualVolume,z),F.soundId)}let q=U.get(z);if(q)q.howl.volume(E(q.individualVolume,z),q.soundId)}function W(){for(let z of N)R(z)}function b(z){let q=K.get(z);if(!q)return;q.howl.stop(z),K.delete(z)}let k=null,D=null,O={play(z,q){if(!D)return-1;let F=q?.channel??P,X=q?.volume??1,L=q?.loop??!1,M=D(z);M.volume(E(X,F)),M.loop(L);let J=M.play(),_={howl:M,soundId:J,channel:F,individualVolume:X,assetKey:z,entityId:-1};return K.set(J,_),M.once("end",()=>{K.delete(J),k?.publish("soundEnded",{entityId:-1,soundId:J,sound:z})},J),J},stop(z){b(z)},playMusic(z,q){if(!D)return;let F=q?.channel??P,X=q?.volume??1,L=q?.loop??!0,M=U.get(F);if(M)M.howl.stop(M.soundId),K.delete(M.soundId);let J=D(z);J.volume(E(X,F)),J.loop(L);let _=J.play(),x={howl:J,soundId:_,channel:F,individualVolume:X,assetKey:z};U.set(F,x),K.set(_,{...x,entityId:-1}),J.once("end",()=>{if(K.delete(_),U.get(F)?.soundId===_)U.delete(F)},_)},stopMusic(z){if(z!==void 0){let q=U.get(z);if(q)q.howl.stop(q.soundId),K.delete(q.soundId),U.delete(z)}else for(let[q,F]of U)F.howl.stop(F.soundId),K.delete(F.soundId),U.delete(q)},pauseMusic(z){if(z!==void 0){let q=U.get(z);if(q)q.howl.pause(q.soundId)}else for(let q of U.values())q.howl.pause(q.soundId)},resumeMusic(z){if(z!==void 0){let q=U.get(z);if(q)q.howl.play(q.soundId)}else for(let q of U.values())q.howl.play(q.soundId)},setChannelVolume(z,q){Z.set(z,q),R(z)},getChannelVolume(z){return Z.get(z)??1},setMasterVolume(z){G=z,W()},getMasterVolume(){return G},mute(){j=!0,W()},unmute(){j=!1,W()},toggleMute(){j=!j,W()},isMuted(){return j}};return B("audio").withComponentTypes().withEventTypes().withResourceTypes().withLabels().withGroups().withReactiveQueryNames().install((z)=>{z.addResource("audioState",O),z.registerDispose("audioSource",({value:q})=>{if(q._soundId!==-1)b(q._soundId)}),z.addSystem("audio-sync").setPriority(T).inPhase(H).inGroup(Y).setOnInitialize((q)=>{k=q.eventBus;let F=q.tryGetResource("$assets");if(F)D=(X)=>F.get(X);q.addReactiveQuery("audio-sources",{with:["audioSource"],onEnter:(X)=>{let L=X.components.audioSource;if(!D)return;if(L._soundId!==-1)return;let M=D(L.sound);M.volume(E(L.volume,L.channel)),M.loop(L.loop);let J=M.play();L._soundId=J,L.playing=!0;let _={howl:M,soundId:J,channel:L.channel,individualVolume:L.volume,assetKey:L.sound,entityId:X.id};K.set(J,_),M.once("end",()=>{if(K.delete(J),L.playing=!1,k?.publish("soundEnded",{entityId:X.id,soundId:J,sound:L.sound}),L.autoRemove)q.commands.removeEntity(X.id)},J)},onExit:(X)=>{}})}).setEventHandlers({playSound({data:q,ecs:F}){F.getResource("audioState").play(q.sound,{channel:q.channel,volume:q.volume,loop:q.loop})},stopMusic({data:q,ecs:F}){F.getResource("audioState").stopMusic(q.channel)}}).setOnDetach(()=>{for(let q of K.values())q.howl.stop(q.soundId);K.clear(),U.clear(),k=null,D=null})})}function v(Q){return{createAudioSource:S}}export{f as loadSound,I as defineAudioChannels,S as createAudioSource,w as createAudioPlugin,v as createAudioHelpers};
|
|
2
2
|
|
|
3
|
-
//# debugId=
|
|
3
|
+
//# debugId=3DA90E7DCE062E0E64756E2164756E21
|
|
4
4
|
//# sourceMappingURL=audio.js.map
|
|
@@ -2,9 +2,9 @@
|
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../src/plugins/audio.ts"],
|
|
4
4
|
"sourcesContent": [
|
|
5
|
-
"/**\n * Audio Plugin for ECSpresso\n *\n * Web Audio API integration via Howler.js for sound effects and music playback.\n * User-defined channels with type-safe volume control, hybrid resource + component API,\n * and asset manager integration.\n */\n\nimport { definePlugin, type Plugin, type BasePluginOptions } from 'ecspresso';\nimport type { AssetsOfWorld, AnyECSpresso, ChannelOfWorld } from 'ecspresso';\nimport type { WorldConfigFrom, EmptyConfig } from '../type-utils';\nimport type { Howl } from 'howler';\n\n// ==================== Channel Definition ====================\n\n/**\n * Configuration for a single audio channel.\n */\nexport interface AudioChannelConfig {\n\treadonly volume: number;\n}\n\n/**\n * Define audio channels with type-safe names and initial volumes.\n * Mirrors `defineCollisionLayers` pattern.\n *\n * @param channels Object mapping channel names to their configuration\n * @returns Frozen channel configuration with inferred channel name union\n *\n * @example\n * ```typescript\n * const channels = defineAudioChannels({\n * sfx: { volume: 1 },\n * music: { volume: 0.7 },\n * ui: { volume: 0.8 },\n * });\n * type Ch = ChannelsOf<typeof channels>; // 'sfx' | 'music' | 'ui'\n * ```\n */\nexport function defineAudioChannels<const T extends Record<string, AudioChannelConfig>>(\n\tchannels: T\n): Readonly<T> {\n\treturn Object.freeze(channels);\n}\n\n/**\n * Extract channel name union from a `defineAudioChannels` result.\n */\nexport type ChannelsOf<T> = T extends Record<infer K extends string, AudioChannelConfig> ? K : never;\n\n// ==================== Component Types ====================\n\n/**\n * Audio source component attached to entities for positional/entity-bound audio.\n */\nexport interface AudioSource<Ch extends string = string> {\n\t/** Asset key for the sound */\n\treadonly sound: string;\n\t/** Channel this sound plays on */\n\treadonly channel: Ch;\n\t/** Individual volume (0-1) */\n\tvolume: number;\n\t/** Whether sound loops */\n\tloop: boolean;\n\t/** Remove entity when sound ends (like timer autoRemove) */\n\tautoRemove: boolean;\n\t/** Whether sound is currently playing (system-managed) */\n\tplaying: boolean;\n\t/** Howler sound ID (system-managed, -1 = not started) */\n\t_soundId: number;\n}\n\n/**\n * Component types provided by the audio plugin.\n */\nexport interface AudioComponentTypes<Ch extends string = string> {\n\taudioSource: AudioSource<Ch>;\n}\n\n// ==================== Event Types ====================\n\n/**\n * Event to trigger fire-and-forget sound playback from any system.\n */\nexport interface PlaySoundEvent<Ch extends string = string> {\n\t/** Asset key for the sound */\n\tsound: string;\n\t/** Channel to play on */\n\tchannel?: Ch;\n\t/** Individual volume (0-1) */\n\tvolume?: number;\n\t/** Whether sound loops */\n\tloop?: boolean;\n}\n\n/**\n * Event to stop music on a channel.\n */\nexport interface StopMusicEvent<Ch extends string = string> {\n\t/** Channel to stop music on. If omitted, stops all music. */\n\tchannel?: Ch;\n}\n\n/**\n * Event published when a sound finishes playing.\n */\nexport interface SoundEndedEvent {\n\t/** Entity ID if sound was entity-attached, -1 for fire-and-forget */\n\tentityId: number;\n\t/** Howler sound ID */\n\tsoundId: number;\n\t/** Asset key of the sound */\n\tsound: string;\n}\n\n/**\n * Event types provided by the audio plugin.\n */\nexport interface AudioEventTypes<Ch extends string = string> {\n\tplaySound: PlaySoundEvent<Ch>;\n\tstopMusic: StopMusicEvent<Ch>;\n\tsoundEnded: SoundEndedEvent;\n}\n\n// ==================== Resource Types ====================\n\n/**\n * Play options for fire-and-forget sound effects.\n */\nexport interface PlayOptions<Ch extends string = string> {\n\t/** Channel to play on (uses first defined channel if omitted) */\n\tchannel?: Ch;\n\t/** Individual volume (0-1, default: 1) */\n\tvolume?: number;\n\t/** Whether to loop (default: false) */\n\tloop?: boolean;\n}\n\n/**\n * Music playback options.\n */\nexport interface MusicOptions<Ch extends string = string> {\n\t/** Channel to play music on (uses first defined channel if omitted) */\n\tchannel?: Ch;\n\t/** Volume (0-1, default: 1) */\n\tvolume?: number;\n\t/** Whether to loop (default: true) */\n\tloop?: boolean;\n}\n\n/**\n * Audio state resource providing fire-and-forget SFX and music control.\n * Effective volume = individual * channel * master.\n */\nexport interface AudioState<Ch extends string = string> {\n\t/** Play a fire-and-forget sound effect. Returns the Howler sound ID. */\n\tplay(sound: string, options?: PlayOptions<Ch>): number;\n\t/** Stop a specific sound by its Howler sound ID. */\n\tstop(soundId: number): void;\n\n\t/** Play music on a channel. Stops any existing music on that channel first. */\n\tplayMusic(sound: string, options?: MusicOptions<Ch>): void;\n\t/** Stop music on a channel. If omitted, stops all music. */\n\tstopMusic(channel?: Ch): void;\n\t/** Pause music on a channel. If omitted, pauses all music. */\n\tpauseMusic(channel?: Ch): void;\n\t/** Resume music on a channel. If omitted, resumes all music. */\n\tresumeMusic(channel?: Ch): void;\n\n\t/** Set volume for a channel (0-1). */\n\tsetChannelVolume(channel: Ch, volume: number): void;\n\t/** Get current volume for a channel. */\n\tgetChannelVolume(channel: Ch): number;\n\t/** Set master volume (0-1). */\n\tsetMasterVolume(volume: number): void;\n\t/** Get current master volume. */\n\tgetMasterVolume(): number;\n\t/** Mute all audio. */\n\tmute(): void;\n\t/** Unmute all audio. */\n\tunmute(): void;\n\t/** Toggle mute state. */\n\ttoggleMute(): void;\n\t/** Check if audio is muted. */\n\tisMuted(): boolean;\n}\n\n/**\n * Resource types provided by the audio plugin.\n */\nexport interface AudioResourceTypes<Ch extends string = string> {\n\taudioState: AudioState<Ch>;\n}\n\n// ==================== Plugin Options ====================\n\n/**\n * Configuration options for the audio plugin.\n */\nexport interface AudioPluginOptions<Ch extends string, G extends string = 'audio'> extends BasePluginOptions<G> {\n\t/** Channel definitions from defineAudioChannels */\n\tchannels: Readonly<Record<Ch, AudioChannelConfig>>;\n}\n\n// ==================== Helper Functions ====================\n\n/**\n * Create an audioSource component for entity-attached audio.\n *\n * @param sound Asset key for the sound\n * @param channel Channel to play on\n * @param options Optional configuration\n * @returns Component object suitable for spreading into spawn()\n *\n * @example\n * ```typescript\n * ecs.spawn({\n * ...createAudioSource('explosion', 'sfx'),\n * ...createTransform(100, 200),\n * });\n * ```\n */\nexport function createAudioSource<Ch extends string>(\n\tsound: string,\n\tchannel: Ch,\n\toptions?: { volume?: number; loop?: boolean; autoRemove?: boolean }\n): Pick<AudioComponentTypes<Ch>, 'audioSource'> {\n\treturn {\n\t\taudioSource: {\n\t\t\tsound,\n\t\t\tchannel,\n\t\t\tvolume: options?.volume ?? 1,\n\t\t\tloop: options?.loop ?? false,\n\t\t\tautoRemove: options?.autoRemove ?? false,\n\t\t\tplaying: false,\n\t\t\t_soundId: -1,\n\t\t},\n\t};\n}\n\n/**\n * Create a loader function for use with the asset manager.\n * Returns a factory function that loads a Howl when called.\n *\n * @param src URL(s) for the sound file\n * @param options Optional Howl configuration\n * @returns Factory function compatible with asset manager's loader parameter\n *\n * @example\n * ```typescript\n * const ecs = ECSpresso.create()\n * .withAssets(a => a\n * .add('explosion', loadSound('/sounds/explosion.mp3'))\n * .add('bgm', loadSound(['/sounds/bgm.webm', '/sounds/bgm.mp3']))\n * )\n * .build();\n * ```\n */\nexport function loadSound(\n\tsrc: string | string[],\n\toptions?: { html5?: boolean; preload?: boolean }\n): () => Promise<Howl> {\n\treturn () => import('howler').then(({ Howl: HowlClass }) =>\n\t\tnew Promise<Howl>((resolve, reject) => {\n\t\t\tlet howl: Howl;\n\t\t\tlet resolved = false;\n\t\t\thowl = new HowlClass({\n\t\t\t\tsrc: Array.isArray(src) ? src : [src],\n\t\t\t\thtml5: options?.html5 ?? false,\n\t\t\t\tpreload: options?.preload ?? true,\n\t\t\t\tonload: () => {\n\t\t\t\t\tresolved = true;\n\t\t\t\t\tresolve(howl);\n\t\t\t\t},\n\t\t\t\tonloaderror: (_id: number, err: unknown) => reject(\n\t\t\t\t\terr instanceof Error ? err : new Error(String(err))\n\t\t\t\t),\n\t\t\t});\n\t\t\t// If onload fired synchronously during construction (e.g. cached),\n\t\t\t// howl is now assigned and the promise is already resolved.\n\t\t\tif (!resolved && howl.state?.() === 'loaded') {\n\t\t\t\tresolve(howl);\n\t\t\t}\n\t\t})\n\t);\n}\n\n// ==================== Internal Types ====================\n\ninterface ActiveSound<Ch extends string> {\n\thowl: Howl;\n\tsoundId: number;\n\tchannel: Ch;\n\tindividualVolume: number;\n\tassetKey: string;\n\tentityId: number;\n}\n\ninterface MusicEntry<Ch extends string> {\n\thowl: Howl;\n\tsoundId: number;\n\tchannel: Ch;\n\tindividualVolume: number;\n\tassetKey: string;\n}\n\n// ==================== Plugin Factory ====================\n\n/**\n * Create an audio plugin for ECSpresso.\n *\n * Provides:\n * - `audioState` resource for fire-and-forget SFX and music\n * - `audioSource` component for entity-attached sounds\n * - Volume hierarchy: individual * channel * master\n * - `playSound` / `stopMusic` event handlers\n * - `soundEnded` event on completion\n * - Automatic cleanup on entity removal (dispose callback)\n *\n * Sounds must be preloaded through the asset pipeline (`loadSound` helper).\n *\n * @example\n * ```typescript\n * const channels = defineAudioChannels({\n * sfx: { volume: 1 },\n * music: { volume: 0.7 },\n * });\n *\n * const ecs = ECSpresso.create()\n * .withAssets(a => a.add('explosion', loadSound('/sfx/boom.mp3')))\n * .withPlugin(createAudioPlugin({ channels }))\n * .build();\n *\n * await ecs.initialize();\n * const audio = ecs.getResource('audioState');\n * audio.play('explosion', { channel: 'sfx' });\n * ```\n */\nexport function createAudioPlugin<Ch extends string, G extends string = 'audio'>(\n\toptions: AudioPluginOptions<Ch, G>\n): Plugin<WorldConfigFrom<AudioComponentTypes<Ch>, AudioEventTypes<Ch>, AudioResourceTypes<Ch>>, EmptyConfig, 'audio-sync', G, never, 'audio-sources'> {\n\tconst {\n\t\tchannels: channelDefs,\n\t\tsystemGroup = 'audio',\n\t\tpriority = 0,\n\t\tphase = 'update',\n\t} = options;\n\n\t// Closure state\n\tconst channelVolumes = new Map<Ch, number>();\n\tconst activeSounds = new Map<number, ActiveSound<Ch>>();\n\tconst musicByChannel = new Map<Ch, MusicEntry<Ch>>();\n\tlet masterVolume = 1;\n\tlet muted = false;\n\n\t// Initialize channel volumes from definitions\n\tconst channelNames: Ch[] = [];\n\tfor (const [name, config] of Object.entries(channelDefs) as Array<[Ch, AudioChannelConfig]>) {\n\t\tchannelVolumes.set(name, config.volume);\n\t\tchannelNames.push(name);\n\t}\n\n\tconst defaultChannel = channelNames[0] as Ch;\n\n\t// Volume computation\n\tfunction effectiveVolume(individualVol: number, channel: Ch): number {\n\t\tif (muted) return 0;\n\t\tconst chanVol = channelVolumes.get(channel) ?? 1;\n\t\treturn individualVol * chanVol * masterVolume;\n\t}\n\n\t// Propagate volume changes to all active sounds on a channel\n\tfunction propagateChannelVolume(channel: Ch): void {\n\t\tfor (const sound of activeSounds.values()) {\n\t\t\tif (sound.channel !== channel) continue;\n\t\t\tsound.howl.volume(effectiveVolume(sound.individualVolume, channel), sound.soundId);\n\t\t}\n\t\tconst music = musicByChannel.get(channel);\n\t\tif (music) {\n\t\t\tmusic.howl.volume(effectiveVolume(music.individualVolume, channel), music.soundId);\n\t\t}\n\t}\n\n\t// Propagate volume to all sounds across all channels\n\tfunction propagateAllVolumes(): void {\n\t\tfor (const ch of channelNames) {\n\t\t\tpropagateChannelVolume(ch);\n\t\t}\n\t}\n\n\t// Stop a sound by its Howler sound ID\n\tfunction stopSoundById(soundId: number): void {\n\t\tconst entry = activeSounds.get(soundId);\n\t\tif (!entry) return;\n\t\tentry.howl.stop(soundId);\n\t\tactiveSounds.delete(soundId);\n\t}\n\n\t// Event bus reference, set during initialization\n\tlet eventBusRef: { publish(event: string, data: unknown): void } | null = null;\n\n\t// Resolve Howl from asset key\n\tlet getAsset: ((key: string) => Howl) | null = null;\n\n\t// AudioState resource implementation\n\tconst audioState: AudioState<Ch> = {\n\t\tplay(sound, playOpts) {\n\t\t\tif (!getAsset) return -1;\n\t\t\tconst channel = playOpts?.channel ?? defaultChannel;\n\t\t\tconst individualVol = playOpts?.volume ?? 1;\n\t\t\tconst loop = playOpts?.loop ?? false;\n\n\t\t\tconst howl = getAsset(sound);\n\t\t\thowl.volume(effectiveVolume(individualVol, channel));\n\t\t\thowl.loop(loop);\n\t\t\tconst soundId = howl.play();\n\n\t\t\tconst entry: ActiveSound<Ch> = {\n\t\t\t\thowl,\n\t\t\t\tsoundId,\n\t\t\t\tchannel,\n\t\t\t\tindividualVolume: individualVol,\n\t\t\t\tassetKey: sound,\n\t\t\t\tentityId: -1,\n\t\t\t};\n\t\t\tactiveSounds.set(soundId, entry);\n\n\t\t\thowl.once('end', () => {\n\t\t\t\tactiveSounds.delete(soundId);\n\t\t\t\teventBusRef?.publish('soundEnded', {\n\t\t\t\t\tentityId: -1,\n\t\t\t\t\tsoundId,\n\t\t\t\t\tsound,\n\t\t\t\t} satisfies SoundEndedEvent);\n\t\t\t}, soundId);\n\n\t\t\treturn soundId;\n\t\t},\n\n\t\tstop(soundId) {\n\t\t\tstopSoundById(soundId);\n\t\t},\n\n\t\tplayMusic(sound, musicOpts) {\n\t\t\tif (!getAsset) return;\n\t\t\tconst channel = musicOpts?.channel ?? defaultChannel;\n\t\t\tconst individualVol = musicOpts?.volume ?? 1;\n\t\t\tconst loop = musicOpts?.loop ?? true;\n\n\t\t\t// Stop existing music on this channel\n\t\t\tconst existing = musicByChannel.get(channel);\n\t\t\tif (existing) {\n\t\t\t\texisting.howl.stop(existing.soundId);\n\t\t\t\tactiveSounds.delete(existing.soundId);\n\t\t\t}\n\n\t\t\tconst howl = getAsset(sound);\n\t\t\thowl.volume(effectiveVolume(individualVol, channel));\n\t\t\thowl.loop(loop);\n\t\t\tconst soundId = howl.play();\n\n\t\t\tconst entry: MusicEntry<Ch> = {\n\t\t\t\thowl,\n\t\t\t\tsoundId,\n\t\t\t\tchannel,\n\t\t\t\tindividualVolume: individualVol,\n\t\t\t\tassetKey: sound,\n\t\t\t};\n\t\t\tmusicByChannel.set(channel, entry);\n\t\t\tactiveSounds.set(soundId, {\n\t\t\t\t...entry,\n\t\t\t\tentityId: -1,\n\t\t\t});\n\n\t\t\thowl.once('end', () => {\n\t\t\t\tactiveSounds.delete(soundId);\n\t\t\t\tconst current = musicByChannel.get(channel);\n\t\t\t\tif (current?.soundId === soundId) {\n\t\t\t\t\tmusicByChannel.delete(channel);\n\t\t\t\t}\n\t\t\t}, soundId);\n\t\t},\n\n\t\tstopMusic(channel) {\n\t\t\tif (channel !== undefined) {\n\t\t\t\tconst entry = musicByChannel.get(channel);\n\t\t\t\tif (entry) {\n\t\t\t\t\tentry.howl.stop(entry.soundId);\n\t\t\t\t\tactiveSounds.delete(entry.soundId);\n\t\t\t\t\tmusicByChannel.delete(channel);\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\tfor (const [ch, entry] of musicByChannel) {\n\t\t\t\t\tentry.howl.stop(entry.soundId);\n\t\t\t\t\tactiveSounds.delete(entry.soundId);\n\t\t\t\t\tmusicByChannel.delete(ch);\n\t\t\t\t}\n\t\t\t}\n\t\t},\n\n\t\tpauseMusic(channel) {\n\t\t\tif (channel !== undefined) {\n\t\t\t\tconst entry = musicByChannel.get(channel);\n\t\t\t\tif (entry) entry.howl.pause(entry.soundId);\n\t\t\t} else {\n\t\t\t\tfor (const entry of musicByChannel.values()) {\n\t\t\t\t\tentry.howl.pause(entry.soundId);\n\t\t\t\t}\n\t\t\t}\n\t\t},\n\n\t\tresumeMusic(channel) {\n\t\t\tif (channel !== undefined) {\n\t\t\t\tconst entry = musicByChannel.get(channel);\n\t\t\t\tif (entry) entry.howl.play(entry.soundId);\n\t\t\t} else {\n\t\t\t\tfor (const entry of musicByChannel.values()) {\n\t\t\t\t\tentry.howl.play(entry.soundId);\n\t\t\t\t}\n\t\t\t}\n\t\t},\n\n\t\tsetChannelVolume(channel, volume) {\n\t\t\tchannelVolumes.set(channel, volume);\n\t\t\tpropagateChannelVolume(channel);\n\t\t},\n\n\t\tgetChannelVolume(channel) {\n\t\t\treturn channelVolumes.get(channel) ?? 1;\n\t\t},\n\n\t\tsetMasterVolume(volume) {\n\t\t\tmasterVolume = volume;\n\t\t\tpropagateAllVolumes();\n\t\t},\n\n\t\tgetMasterVolume() {\n\t\t\treturn masterVolume;\n\t\t},\n\n\t\tmute() {\n\t\t\tmuted = true;\n\t\t\tpropagateAllVolumes();\n\t\t},\n\n\t\tunmute() {\n\t\t\tmuted = false;\n\t\t\tpropagateAllVolumes();\n\t\t},\n\n\t\ttoggleMute() {\n\t\t\tmuted = !muted;\n\t\t\tpropagateAllVolumes();\n\t\t},\n\n\t\tisMuted() {\n\t\t\treturn muted;\n\t\t},\n\t};\n\n\treturn definePlugin<WorldConfigFrom<AudioComponentTypes<Ch>, AudioEventTypes<Ch>, AudioResourceTypes<Ch>>, EmptyConfig, 'audio-sync', G, never, 'audio-sources'>({\n\t\tid: 'audio',\n\t\tinstall(world) {\n\t\t\tworld.addResource('audioState', audioState);\n\n\t\t\t// Dispose callback: stop sounds when audioSource component is removed\n\t\t\tworld.registerDispose('audioSource', ({ value: source }: { value: AudioSource<Ch>; entityId: number }) => {\n\t\t\t\tif (source._soundId !== -1) {\n\t\t\t\t\tstopSoundById(source._soundId);\n\t\t\t\t}\n\t\t\t});\n\n\t\t\tworld\n\t\t\t\t.addSystem('audio-sync')\n\t\t\t\t.setPriority(priority)\n\t\t\t\t.inPhase(phase)\n\t\t\t\t.inGroup(systemGroup)\n\t\t\t\t.setOnInitialize((ecs) => {\n\t\t\t\t\teventBusRef = ecs.eventBus;\n\n\t\t\t\t\t// Resolve asset getter - works with $assets resource if available\n\t\t\t\t\tconst assets = ecs.tryGetResource<{ get(k: string): unknown }>('$assets');\n\t\t\t\t\tif (assets) {\n\t\t\t\t\t\tgetAsset = (key: string) => assets.get(key) as Howl;\n\t\t\t\t\t}\n\n\t\t\t\t\t// Register reactive query for audioSource components\n\t\t\t\t\tecs.addReactiveQuery('audio-sources', {\n\t\t\t\t\t\twith: ['audioSource'],\n\t\t\t\t\t\tonEnter: (entity) => {\n\t\t\t\t\t\t\tconst source = entity.components.audioSource;\n\t\t\t\t\t\t\tif (!getAsset) return;\n\t\t\t\t\t\t\tif (source._soundId !== -1) return; // Already started\n\n\t\t\t\t\t\t\tconst howl = getAsset(source.sound);\n\t\t\t\t\t\t\thowl.volume(effectiveVolume(source.volume, source.channel));\n\t\t\t\t\t\t\thowl.loop(source.loop);\n\t\t\t\t\t\t\tconst soundId = howl.play();\n\n\t\t\t\t\t\t\tsource._soundId = soundId;\n\t\t\t\t\t\t\tsource.playing = true;\n\n\t\t\t\t\t\t\tconst entry: ActiveSound<Ch> = {\n\t\t\t\t\t\t\t\thowl,\n\t\t\t\t\t\t\t\tsoundId,\n\t\t\t\t\t\t\t\tchannel: source.channel,\n\t\t\t\t\t\t\t\tindividualVolume: source.volume,\n\t\t\t\t\t\t\t\tassetKey: source.sound,\n\t\t\t\t\t\t\t\tentityId: entity.id,\n\t\t\t\t\t\t\t};\n\t\t\t\t\t\t\tactiveSounds.set(soundId, entry);\n\n\t\t\t\t\t\t\thowl.once('end', () => {\n\t\t\t\t\t\t\t\tactiveSounds.delete(soundId);\n\t\t\t\t\t\t\t\tsource.playing = false;\n\n\t\t\t\t\t\t\t\teventBusRef?.publish('soundEnded', {\n\t\t\t\t\t\t\t\t\tentityId: entity.id,\n\t\t\t\t\t\t\t\t\tsoundId,\n\t\t\t\t\t\t\t\t\tsound: source.sound,\n\t\t\t\t\t\t\t\t} satisfies SoundEndedEvent);\n\n\t\t\t\t\t\t\t\tif (source.autoRemove) {\n\t\t\t\t\t\t\t\t\tecs.commands.removeEntity(entity.id);\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t}, soundId);\n\t\t\t\t\t\t},\n\t\t\t\t\t\tonExit: (_entityId) => {\n\t\t\t\t\t\t\t// Cleanup handled by dispose callback\n\t\t\t\t\t\t},\n\t\t\t\t\t});\n\t\t\t\t})\n\t\t\t\t.setEventHandlers({\n\t\t\t\t\tplaySound({ data, ecs }) {\n\t\t\t\t\t\tconst audio = ecs.getResource('audioState');\n\t\t\t\t\t\taudio.play(data.sound, {\n\t\t\t\t\t\t\tchannel: data.channel,\n\t\t\t\t\t\t\tvolume: data.volume,\n\t\t\t\t\t\t\tloop: data.loop,\n\t\t\t\t\t\t});\n\t\t\t\t\t},\n\t\t\t\t\tstopMusic({ data, ecs }) {\n\t\t\t\t\t\tconst audio = ecs.getResource('audioState');\n\t\t\t\t\t\taudio.stopMusic(data.channel);\n\t\t\t\t\t},\n\t\t\t\t})\n\t\t\t\t.setOnDetach(() => {\n\t\t\t\t\t// Stop all active sounds\n\t\t\t\t\tfor (const entry of activeSounds.values()) {\n\t\t\t\t\t\tentry.howl.stop(entry.soundId);\n\t\t\t\t\t}\n\t\t\t\t\tactiveSounds.clear();\n\t\t\t\t\tmusicByChannel.clear();\n\t\t\t\t\teventBusRef = null;\n\t\t\t\t\tgetAsset = null;\n\t\t\t\t});\n\t\t},\n\t});\n}\n\n// ==================== Post-Build Helpers ====================\n\n/**\n * Typed helpers for the audio plugin.\n * Creates helpers that validate sound keys and channel names against the world type W.\n * Call after .build() using typeof ecs.\n *\n * @template W - Concrete ECS world type (e.g. `typeof ecs`)\n *\n * @example\n * ```typescript\n * const ecs = ECSpresso.create()\n * .withPlugin(createAudioPlugin({ channels }))\n * .withAssets(a => a.add('boom', loadSound('/sfx/boom.mp3')))\n * .build();\n *\n * const { createAudioSource } = createAudioHelpers<typeof ecs>();\n * // Type-safe: 'boom' must be a registered asset, 'sfx' a valid channel\n * createAudioSource('boom', 'sfx');\n * ```\n */\nexport interface AudioHelpers<W extends AnyECSpresso> {\n\tcreateAudioSource: (\n\t\tsound: keyof AssetsOfWorld<W> & string,\n\t\tchannel: ChannelOfWorld<W>,\n\t\toptions?: { volume?: number; loop?: boolean; autoRemove?: boolean },\n\t) => Pick<AudioComponentTypes<ChannelOfWorld<W>>, 'audioSource'>;\n}\n\nexport function createAudioHelpers<W extends AnyECSpresso>(_world?: W): AudioHelpers<W> {\n\treturn {\n\t\tcreateAudioSource: createAudioSource as AudioHelpers<W>['createAudioSource'],\n\t};\n}\n"
|
|
5
|
+
"/**\n * Audio Plugin for ECSpresso\n *\n * Web Audio API integration via Howler.js for sound effects and music playback.\n * User-defined channels with type-safe volume control, hybrid resource + component API,\n * and asset manager integration.\n */\n\nimport { definePlugin, type BasePluginOptions } from 'ecspresso';\nimport type { AssetsOfWorld, AnyECSpresso, ChannelOfWorld } from 'ecspresso';\nimport type { Howl } from 'howler';\n\n// ==================== Channel Definition ====================\n\n/**\n * Configuration for a single audio channel.\n */\nexport interface AudioChannelConfig {\n\treadonly volume: number;\n}\n\n/**\n * Define audio channels with type-safe names and initial volumes.\n * Mirrors `defineCollisionLayers` pattern.\n *\n * @param channels Object mapping channel names to their configuration\n * @returns Frozen channel configuration with inferred channel name union\n *\n * @example\n * ```typescript\n * const channels = defineAudioChannels({\n * sfx: { volume: 1 },\n * music: { volume: 0.7 },\n * ui: { volume: 0.8 },\n * });\n * type Ch = ChannelsOf<typeof channels>; // 'sfx' | 'music' | 'ui'\n * ```\n */\nexport function defineAudioChannels<const T extends Record<string, AudioChannelConfig>>(\n\tchannels: T\n): Readonly<T> {\n\treturn Object.freeze(channels);\n}\n\n/**\n * Extract channel name union from a `defineAudioChannels` result.\n */\nexport type ChannelsOf<T> = T extends Record<infer K extends string, AudioChannelConfig> ? K : never;\n\n// ==================== Component Types ====================\n\n/**\n * Audio source component attached to entities for positional/entity-bound audio.\n */\nexport interface AudioSource<Ch extends string = string> {\n\t/** Asset key for the sound */\n\treadonly sound: string;\n\t/** Channel this sound plays on */\n\treadonly channel: Ch;\n\t/** Individual volume (0-1) */\n\tvolume: number;\n\t/** Whether sound loops */\n\tloop: boolean;\n\t/** Remove entity when sound ends (like timer autoRemove) */\n\tautoRemove: boolean;\n\t/** Whether sound is currently playing (system-managed) */\n\tplaying: boolean;\n\t/** Howler sound ID (system-managed, -1 = not started) */\n\t_soundId: number;\n}\n\n/**\n * Component types provided by the audio plugin.\n */\nexport interface AudioComponentTypes<Ch extends string = string> {\n\taudioSource: AudioSource<Ch>;\n}\n\n// ==================== Event Types ====================\n\n/**\n * Event to trigger fire-and-forget sound playback from any system.\n */\nexport interface PlaySoundEvent<Ch extends string = string> {\n\t/** Asset key for the sound */\n\tsound: string;\n\t/** Channel to play on */\n\tchannel?: Ch;\n\t/** Individual volume (0-1) */\n\tvolume?: number;\n\t/** Whether sound loops */\n\tloop?: boolean;\n}\n\n/**\n * Event to stop music on a channel.\n */\nexport interface StopMusicEvent<Ch extends string = string> {\n\t/** Channel to stop music on. If omitted, stops all music. */\n\tchannel?: Ch;\n}\n\n/**\n * Event published when a sound finishes playing.\n */\nexport interface SoundEndedEvent {\n\t/** Entity ID if sound was entity-attached, -1 for fire-and-forget */\n\tentityId: number;\n\t/** Howler sound ID */\n\tsoundId: number;\n\t/** Asset key of the sound */\n\tsound: string;\n}\n\n/**\n * Event types provided by the audio plugin.\n */\nexport interface AudioEventTypes<Ch extends string = string> {\n\tplaySound: PlaySoundEvent<Ch>;\n\tstopMusic: StopMusicEvent<Ch>;\n\tsoundEnded: SoundEndedEvent;\n}\n\n// ==================== Resource Types ====================\n\n/**\n * Play options for fire-and-forget sound effects.\n */\nexport interface PlayOptions<Ch extends string = string> {\n\t/** Channel to play on (uses first defined channel if omitted) */\n\tchannel?: Ch;\n\t/** Individual volume (0-1, default: 1) */\n\tvolume?: number;\n\t/** Whether to loop (default: false) */\n\tloop?: boolean;\n}\n\n/**\n * Music playback options.\n */\nexport interface MusicOptions<Ch extends string = string> {\n\t/** Channel to play music on (uses first defined channel if omitted) */\n\tchannel?: Ch;\n\t/** Volume (0-1, default: 1) */\n\tvolume?: number;\n\t/** Whether to loop (default: true) */\n\tloop?: boolean;\n}\n\n/**\n * Audio state resource providing fire-and-forget SFX and music control.\n * Effective volume = individual * channel * master.\n */\nexport interface AudioState<Ch extends string = string> {\n\t/** Play a fire-and-forget sound effect. Returns the Howler sound ID. */\n\tplay(sound: string, options?: PlayOptions<Ch>): number;\n\t/** Stop a specific sound by its Howler sound ID. */\n\tstop(soundId: number): void;\n\n\t/** Play music on a channel. Stops any existing music on that channel first. */\n\tplayMusic(sound: string, options?: MusicOptions<Ch>): void;\n\t/** Stop music on a channel. If omitted, stops all music. */\n\tstopMusic(channel?: Ch): void;\n\t/** Pause music on a channel. If omitted, pauses all music. */\n\tpauseMusic(channel?: Ch): void;\n\t/** Resume music on a channel. If omitted, resumes all music. */\n\tresumeMusic(channel?: Ch): void;\n\n\t/** Set volume for a channel (0-1). */\n\tsetChannelVolume(channel: Ch, volume: number): void;\n\t/** Get current volume for a channel. */\n\tgetChannelVolume(channel: Ch): number;\n\t/** Set master volume (0-1). */\n\tsetMasterVolume(volume: number): void;\n\t/** Get current master volume. */\n\tgetMasterVolume(): number;\n\t/** Mute all audio. */\n\tmute(): void;\n\t/** Unmute all audio. */\n\tunmute(): void;\n\t/** Toggle mute state. */\n\ttoggleMute(): void;\n\t/** Check if audio is muted. */\n\tisMuted(): boolean;\n}\n\n/**\n * Resource types provided by the audio plugin.\n */\nexport interface AudioResourceTypes<Ch extends string = string> {\n\taudioState: AudioState<Ch>;\n}\n\n// ==================== Plugin Options ====================\n\n/**\n * Configuration options for the audio plugin.\n */\nexport interface AudioPluginOptions<Ch extends string, G extends string = 'audio'> extends BasePluginOptions<G> {\n\t/** Channel definitions from defineAudioChannels */\n\tchannels: Readonly<Record<Ch, AudioChannelConfig>>;\n}\n\n// ==================== Helper Functions ====================\n\n/**\n * Create an audioSource component for entity-attached audio.\n *\n * @param sound Asset key for the sound\n * @param channel Channel to play on\n * @param options Optional configuration\n * @returns Component object suitable for spreading into spawn()\n *\n * @example\n * ```typescript\n * ecs.spawn({\n * ...createAudioSource('explosion', 'sfx'),\n * ...createTransform(100, 200),\n * });\n * ```\n */\nexport function createAudioSource<Ch extends string>(\n\tsound: string,\n\tchannel: Ch,\n\toptions?: { volume?: number; loop?: boolean; autoRemove?: boolean }\n): Pick<AudioComponentTypes<Ch>, 'audioSource'> {\n\treturn {\n\t\taudioSource: {\n\t\t\tsound,\n\t\t\tchannel,\n\t\t\tvolume: options?.volume ?? 1,\n\t\t\tloop: options?.loop ?? false,\n\t\t\tautoRemove: options?.autoRemove ?? false,\n\t\t\tplaying: false,\n\t\t\t_soundId: -1,\n\t\t},\n\t};\n}\n\n/**\n * Create a loader function for use with the asset manager.\n * Returns a factory function that loads a Howl when called.\n *\n * @param src URL(s) for the sound file\n * @param options Optional Howl configuration\n * @returns Factory function compatible with asset manager's loader parameter\n *\n * @example\n * ```typescript\n * const ecs = ECSpresso.create()\n * .withAssets(a => a\n * .add('explosion', loadSound('/sounds/explosion.mp3'))\n * .add('bgm', loadSound(['/sounds/bgm.webm', '/sounds/bgm.mp3']))\n * )\n * .build();\n * ```\n */\nexport function loadSound(\n\tsrc: string | string[],\n\toptions?: { html5?: boolean; preload?: boolean }\n): () => Promise<Howl> {\n\treturn () => import('howler').then(({ Howl: HowlClass }) =>\n\t\tnew Promise<Howl>((resolve, reject) => {\n\t\t\tlet howl: Howl;\n\t\t\tlet resolved = false;\n\t\t\thowl = new HowlClass({\n\t\t\t\tsrc: Array.isArray(src) ? src : [src],\n\t\t\t\thtml5: options?.html5 ?? false,\n\t\t\t\tpreload: options?.preload ?? true,\n\t\t\t\tonload: () => {\n\t\t\t\t\tresolved = true;\n\t\t\t\t\tresolve(howl);\n\t\t\t\t},\n\t\t\t\tonloaderror: (_id: number, err: unknown) => reject(\n\t\t\t\t\terr instanceof Error ? err : new Error(String(err))\n\t\t\t\t),\n\t\t\t});\n\t\t\t// If onload fired synchronously during construction (e.g. cached),\n\t\t\t// howl is now assigned and the promise is already resolved.\n\t\t\tif (!resolved && howl.state?.() === 'loaded') {\n\t\t\t\tresolve(howl);\n\t\t\t}\n\t\t})\n\t);\n}\n\n// ==================== Internal Types ====================\n\ninterface ActiveSound<Ch extends string> {\n\thowl: Howl;\n\tsoundId: number;\n\tchannel: Ch;\n\tindividualVolume: number;\n\tassetKey: string;\n\tentityId: number;\n}\n\ninterface MusicEntry<Ch extends string> {\n\thowl: Howl;\n\tsoundId: number;\n\tchannel: Ch;\n\tindividualVolume: number;\n\tassetKey: string;\n}\n\n// ==================== Plugin Factory ====================\n\n/**\n * Create an audio plugin for ECSpresso.\n *\n * Provides:\n * - `audioState` resource for fire-and-forget SFX and music\n * - `audioSource` component for entity-attached sounds\n * - Volume hierarchy: individual * channel * master\n * - `playSound` / `stopMusic` event handlers\n * - `soundEnded` event on completion\n * - Automatic cleanup on entity removal (dispose callback)\n *\n * Sounds must be preloaded through the asset pipeline (`loadSound` helper).\n *\n * @example\n * ```typescript\n * const channels = defineAudioChannels({\n * sfx: { volume: 1 },\n * music: { volume: 0.7 },\n * });\n *\n * const ecs = ECSpresso.create()\n * .withAssets(a => a.add('explosion', loadSound('/sfx/boom.mp3')))\n * .withPlugin(createAudioPlugin({ channels }))\n * .build();\n *\n * await ecs.initialize();\n * const audio = ecs.getResource('audioState');\n * audio.play('explosion', { channel: 'sfx' });\n * ```\n */\nexport function createAudioPlugin<Ch extends string, G extends string = 'audio'>(\n\toptions: AudioPluginOptions<Ch, G>\n) {\n\tconst {\n\t\tchannels: channelDefs,\n\t\tsystemGroup = 'audio',\n\t\tpriority = 0,\n\t\tphase = 'update',\n\t} = options;\n\n\t// Closure state\n\tconst channelVolumes = new Map<Ch, number>();\n\tconst activeSounds = new Map<number, ActiveSound<Ch>>();\n\tconst musicByChannel = new Map<Ch, MusicEntry<Ch>>();\n\tlet masterVolume = 1;\n\tlet muted = false;\n\n\t// Initialize channel volumes from definitions\n\tconst channelNames: Ch[] = [];\n\tfor (const [name, config] of Object.entries(channelDefs) as Array<[Ch, AudioChannelConfig]>) {\n\t\tchannelVolumes.set(name, config.volume);\n\t\tchannelNames.push(name);\n\t}\n\n\tconst defaultChannel = channelNames[0] as Ch;\n\n\t// Volume computation\n\tfunction effectiveVolume(individualVol: number, channel: Ch): number {\n\t\tif (muted) return 0;\n\t\tconst chanVol = channelVolumes.get(channel) ?? 1;\n\t\treturn individualVol * chanVol * masterVolume;\n\t}\n\n\t// Propagate volume changes to all active sounds on a channel\n\tfunction propagateChannelVolume(channel: Ch): void {\n\t\tfor (const sound of activeSounds.values()) {\n\t\t\tif (sound.channel !== channel) continue;\n\t\t\tsound.howl.volume(effectiveVolume(sound.individualVolume, channel), sound.soundId);\n\t\t}\n\t\tconst music = musicByChannel.get(channel);\n\t\tif (music) {\n\t\t\tmusic.howl.volume(effectiveVolume(music.individualVolume, channel), music.soundId);\n\t\t}\n\t}\n\n\t// Propagate volume to all sounds across all channels\n\tfunction propagateAllVolumes(): void {\n\t\tfor (const ch of channelNames) {\n\t\t\tpropagateChannelVolume(ch);\n\t\t}\n\t}\n\n\t// Stop a sound by its Howler sound ID\n\tfunction stopSoundById(soundId: number): void {\n\t\tconst entry = activeSounds.get(soundId);\n\t\tif (!entry) return;\n\t\tentry.howl.stop(soundId);\n\t\tactiveSounds.delete(soundId);\n\t}\n\n\t// Event bus reference, set during initialization\n\tlet eventBusRef: { publish(event: string, data: unknown): void } | null = null;\n\n\t// Resolve Howl from asset key\n\tlet getAsset: ((key: string) => Howl) | null = null;\n\n\t// AudioState resource implementation\n\tconst audioState: AudioState<Ch> = {\n\t\tplay(sound, playOpts) {\n\t\t\tif (!getAsset) return -1;\n\t\t\tconst channel = playOpts?.channel ?? defaultChannel;\n\t\t\tconst individualVol = playOpts?.volume ?? 1;\n\t\t\tconst loop = playOpts?.loop ?? false;\n\n\t\t\tconst howl = getAsset(sound);\n\t\t\thowl.volume(effectiveVolume(individualVol, channel));\n\t\t\thowl.loop(loop);\n\t\t\tconst soundId = howl.play();\n\n\t\t\tconst entry: ActiveSound<Ch> = {\n\t\t\t\thowl,\n\t\t\t\tsoundId,\n\t\t\t\tchannel,\n\t\t\t\tindividualVolume: individualVol,\n\t\t\t\tassetKey: sound,\n\t\t\t\tentityId: -1,\n\t\t\t};\n\t\t\tactiveSounds.set(soundId, entry);\n\n\t\t\thowl.once('end', () => {\n\t\t\t\tactiveSounds.delete(soundId);\n\t\t\t\teventBusRef?.publish('soundEnded', {\n\t\t\t\t\tentityId: -1,\n\t\t\t\t\tsoundId,\n\t\t\t\t\tsound,\n\t\t\t\t} satisfies SoundEndedEvent);\n\t\t\t}, soundId);\n\n\t\t\treturn soundId;\n\t\t},\n\n\t\tstop(soundId) {\n\t\t\tstopSoundById(soundId);\n\t\t},\n\n\t\tplayMusic(sound, musicOpts) {\n\t\t\tif (!getAsset) return;\n\t\t\tconst channel = musicOpts?.channel ?? defaultChannel;\n\t\t\tconst individualVol = musicOpts?.volume ?? 1;\n\t\t\tconst loop = musicOpts?.loop ?? true;\n\n\t\t\t// Stop existing music on this channel\n\t\t\tconst existing = musicByChannel.get(channel);\n\t\t\tif (existing) {\n\t\t\t\texisting.howl.stop(existing.soundId);\n\t\t\t\tactiveSounds.delete(existing.soundId);\n\t\t\t}\n\n\t\t\tconst howl = getAsset(sound);\n\t\t\thowl.volume(effectiveVolume(individualVol, channel));\n\t\t\thowl.loop(loop);\n\t\t\tconst soundId = howl.play();\n\n\t\t\tconst entry: MusicEntry<Ch> = {\n\t\t\t\thowl,\n\t\t\t\tsoundId,\n\t\t\t\tchannel,\n\t\t\t\tindividualVolume: individualVol,\n\t\t\t\tassetKey: sound,\n\t\t\t};\n\t\t\tmusicByChannel.set(channel, entry);\n\t\t\tactiveSounds.set(soundId, {\n\t\t\t\t...entry,\n\t\t\t\tentityId: -1,\n\t\t\t});\n\n\t\t\thowl.once('end', () => {\n\t\t\t\tactiveSounds.delete(soundId);\n\t\t\t\tconst current = musicByChannel.get(channel);\n\t\t\t\tif (current?.soundId === soundId) {\n\t\t\t\t\tmusicByChannel.delete(channel);\n\t\t\t\t}\n\t\t\t}, soundId);\n\t\t},\n\n\t\tstopMusic(channel) {\n\t\t\tif (channel !== undefined) {\n\t\t\t\tconst entry = musicByChannel.get(channel);\n\t\t\t\tif (entry) {\n\t\t\t\t\tentry.howl.stop(entry.soundId);\n\t\t\t\t\tactiveSounds.delete(entry.soundId);\n\t\t\t\t\tmusicByChannel.delete(channel);\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\tfor (const [ch, entry] of musicByChannel) {\n\t\t\t\t\tentry.howl.stop(entry.soundId);\n\t\t\t\t\tactiveSounds.delete(entry.soundId);\n\t\t\t\t\tmusicByChannel.delete(ch);\n\t\t\t\t}\n\t\t\t}\n\t\t},\n\n\t\tpauseMusic(channel) {\n\t\t\tif (channel !== undefined) {\n\t\t\t\tconst entry = musicByChannel.get(channel);\n\t\t\t\tif (entry) entry.howl.pause(entry.soundId);\n\t\t\t} else {\n\t\t\t\tfor (const entry of musicByChannel.values()) {\n\t\t\t\t\tentry.howl.pause(entry.soundId);\n\t\t\t\t}\n\t\t\t}\n\t\t},\n\n\t\tresumeMusic(channel) {\n\t\t\tif (channel !== undefined) {\n\t\t\t\tconst entry = musicByChannel.get(channel);\n\t\t\t\tif (entry) entry.howl.play(entry.soundId);\n\t\t\t} else {\n\t\t\t\tfor (const entry of musicByChannel.values()) {\n\t\t\t\t\tentry.howl.play(entry.soundId);\n\t\t\t\t}\n\t\t\t}\n\t\t},\n\n\t\tsetChannelVolume(channel, volume) {\n\t\t\tchannelVolumes.set(channel, volume);\n\t\t\tpropagateChannelVolume(channel);\n\t\t},\n\n\t\tgetChannelVolume(channel) {\n\t\t\treturn channelVolumes.get(channel) ?? 1;\n\t\t},\n\n\t\tsetMasterVolume(volume) {\n\t\t\tmasterVolume = volume;\n\t\t\tpropagateAllVolumes();\n\t\t},\n\n\t\tgetMasterVolume() {\n\t\t\treturn masterVolume;\n\t\t},\n\n\t\tmute() {\n\t\t\tmuted = true;\n\t\t\tpropagateAllVolumes();\n\t\t},\n\n\t\tunmute() {\n\t\t\tmuted = false;\n\t\t\tpropagateAllVolumes();\n\t\t},\n\n\t\ttoggleMute() {\n\t\t\tmuted = !muted;\n\t\t\tpropagateAllVolumes();\n\t\t},\n\n\t\tisMuted() {\n\t\t\treturn muted;\n\t\t},\n\t};\n\n\treturn definePlugin('audio')\n\t\t.withComponentTypes<AudioComponentTypes<Ch>>()\n\t\t.withEventTypes<AudioEventTypes<Ch>>()\n\t\t.withResourceTypes<AudioResourceTypes<Ch>>()\n\t\t.withLabels<'audio-sync'>()\n\t\t.withGroups<G>()\n\t\t.withReactiveQueryNames<'audio-sources'>()\n\t\t.install((world) => {\n\t\t\tworld.addResource('audioState', audioState);\n\n\t\t\t// Dispose callback: stop sounds when audioSource component is removed\n\t\t\tworld.registerDispose('audioSource', ({ value: source }: { value: AudioSource<Ch>; entityId: number }) => {\n\t\t\t\tif (source._soundId !== -1) {\n\t\t\t\t\tstopSoundById(source._soundId);\n\t\t\t\t}\n\t\t\t});\n\n\t\t\tworld\n\t\t\t\t.addSystem('audio-sync')\n\t\t\t\t.setPriority(priority)\n\t\t\t\t.inPhase(phase)\n\t\t\t\t.inGroup(systemGroup)\n\t\t\t\t.setOnInitialize((ecs) => {\n\t\t\t\t\teventBusRef = ecs.eventBus;\n\n\t\t\t\t\t// Resolve asset getter - works with $assets resource if available\n\t\t\t\t\tconst assets = ecs.tryGetResource<{ get(k: string): unknown }>('$assets');\n\t\t\t\t\tif (assets) {\n\t\t\t\t\t\tgetAsset = (key: string) => assets.get(key) as Howl;\n\t\t\t\t\t}\n\n\t\t\t\t\t// Register reactive query for audioSource components\n\t\t\t\t\tecs.addReactiveQuery('audio-sources', {\n\t\t\t\t\t\twith: ['audioSource'],\n\t\t\t\t\t\tonEnter: (entity) => {\n\t\t\t\t\t\t\tconst source = entity.components.audioSource;\n\t\t\t\t\t\t\tif (!getAsset) return;\n\t\t\t\t\t\t\tif (source._soundId !== -1) return; // Already started\n\n\t\t\t\t\t\t\tconst howl = getAsset(source.sound);\n\t\t\t\t\t\t\thowl.volume(effectiveVolume(source.volume, source.channel));\n\t\t\t\t\t\t\thowl.loop(source.loop);\n\t\t\t\t\t\t\tconst soundId = howl.play();\n\n\t\t\t\t\t\t\tsource._soundId = soundId;\n\t\t\t\t\t\t\tsource.playing = true;\n\n\t\t\t\t\t\t\tconst entry: ActiveSound<Ch> = {\n\t\t\t\t\t\t\t\thowl,\n\t\t\t\t\t\t\t\tsoundId,\n\t\t\t\t\t\t\t\tchannel: source.channel,\n\t\t\t\t\t\t\t\tindividualVolume: source.volume,\n\t\t\t\t\t\t\t\tassetKey: source.sound,\n\t\t\t\t\t\t\t\tentityId: entity.id,\n\t\t\t\t\t\t\t};\n\t\t\t\t\t\t\tactiveSounds.set(soundId, entry);\n\n\t\t\t\t\t\t\thowl.once('end', () => {\n\t\t\t\t\t\t\t\tactiveSounds.delete(soundId);\n\t\t\t\t\t\t\t\tsource.playing = false;\n\n\t\t\t\t\t\t\t\teventBusRef?.publish('soundEnded', {\n\t\t\t\t\t\t\t\t\tentityId: entity.id,\n\t\t\t\t\t\t\t\t\tsoundId,\n\t\t\t\t\t\t\t\t\tsound: source.sound,\n\t\t\t\t\t\t\t\t} satisfies SoundEndedEvent);\n\n\t\t\t\t\t\t\t\tif (source.autoRemove) {\n\t\t\t\t\t\t\t\t\tecs.commands.removeEntity(entity.id);\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t}, soundId);\n\t\t\t\t\t\t},\n\t\t\t\t\t\tonExit: (_entityId) => {\n\t\t\t\t\t\t\t// Cleanup handled by dispose callback\n\t\t\t\t\t\t},\n\t\t\t\t\t});\n\t\t\t\t})\n\t\t\t\t.setEventHandlers({\n\t\t\t\t\tplaySound({ data, ecs }) {\n\t\t\t\t\t\tconst audio = ecs.getResource('audioState');\n\t\t\t\t\t\taudio.play(data.sound, {\n\t\t\t\t\t\t\tchannel: data.channel,\n\t\t\t\t\t\t\tvolume: data.volume,\n\t\t\t\t\t\t\tloop: data.loop,\n\t\t\t\t\t\t});\n\t\t\t\t\t},\n\t\t\t\t\tstopMusic({ data, ecs }) {\n\t\t\t\t\t\tconst audio = ecs.getResource('audioState');\n\t\t\t\t\t\taudio.stopMusic(data.channel);\n\t\t\t\t\t},\n\t\t\t\t})\n\t\t\t\t.setOnDetach(() => {\n\t\t\t\t\t// Stop all active sounds\n\t\t\t\t\tfor (const entry of activeSounds.values()) {\n\t\t\t\t\t\tentry.howl.stop(entry.soundId);\n\t\t\t\t\t}\n\t\t\t\t\tactiveSounds.clear();\n\t\t\t\t\tmusicByChannel.clear();\n\t\t\t\t\teventBusRef = null;\n\t\t\t\t\tgetAsset = null;\n\t\t\t\t});\n\t\t});\n}\n\n// ==================== Post-Build Helpers ====================\n\n/**\n * Typed helpers for the audio plugin.\n * Creates helpers that validate sound keys and channel names against the world type W.\n * Call after .build() using typeof ecs.\n *\n * @template W - Concrete ECS world type (e.g. `typeof ecs`)\n *\n * @example\n * ```typescript\n * const ecs = ECSpresso.create()\n * .withPlugin(createAudioPlugin({ channels }))\n * .withAssets(a => a.add('boom', loadSound('/sfx/boom.mp3')))\n * .build();\n *\n * const { createAudioSource } = createAudioHelpers<typeof ecs>();\n * // Type-safe: 'boom' must be a registered asset, 'sfx' a valid channel\n * createAudioSource('boom', 'sfx');\n * ```\n */\nexport interface AudioHelpers<W extends AnyECSpresso> {\n\tcreateAudioSource: (\n\t\tsound: keyof AssetsOfWorld<W> & string,\n\t\tchannel: ChannelOfWorld<W>,\n\t\toptions?: { volume?: number; loop?: boolean; autoRemove?: boolean },\n\t) => Pick<AudioComponentTypes<ChannelOfWorld<W>>, 'audioSource'>;\n}\n\nexport function createAudioHelpers<W extends AnyECSpresso>(_world?: W): AudioHelpers<W> {\n\treturn {\n\t\tcreateAudioSource: createAudioSource as AudioHelpers<W>['createAudioSource'],\n\t};\n}\n"
|
|
6
6
|
],
|
|
7
|
-
"mappings": "2PAQA,uBAAS,
|
|
8
|
-
"debugId": "
|
|
7
|
+
"mappings": "2PAQA,uBAAS,kBA8BF,SAAS,CAAuE,CACtF,EACc,CACd,OAAO,OAAO,OAAO,CAAQ,EAoLvB,SAAS,CAAoC,CACnD,EACA,EACA,EAC+C,CAC/C,MAAO,CACN,YAAa,CACZ,QACA,UACA,OAAQ,GAAS,QAAU,EAC3B,KAAM,GAAS,MAAQ,GACvB,WAAY,GAAS,YAAc,GACnC,QAAS,GACT,SAAU,EACX,CACD,EAqBM,SAAS,CAAS,CACxB,EACA,EACsB,CACtB,MAAO,IAAa,iBAAU,KAAK,EAAG,KAAM,KAC3C,IAAI,QAAc,CAAC,EAAS,IAAW,CACtC,IAAI,EACA,EAAW,GAef,GAdA,EAAO,IAAI,EAAU,CACpB,IAAK,MAAM,QAAQ,CAAG,EAAI,EAAM,CAAC,CAAG,EACpC,MAAO,GAAS,OAAS,GACzB,QAAS,GAAS,SAAW,GAC7B,OAAQ,IAAM,CACb,EAAW,GACX,EAAQ,CAAI,GAEb,YAAa,CAAC,EAAa,IAAiB,EAC3C,aAAe,MAAQ,EAAU,MAAM,OAAO,CAAG,CAAC,CACnD,CACD,CAAC,EAGG,CAAC,GAAY,EAAK,QAAQ,IAAM,SACnC,EAAQ,CAAI,EAEb,CACF,EAsDM,SAAS,CAAgE,CAC/E,EACC,CACD,IACC,SAAU,EACV,cAAc,QACd,WAAW,EACX,QAAQ,UACL,EAGE,EAAiB,IAAI,IACrB,EAAe,IAAI,IACnB,EAAiB,IAAI,IACvB,EAAe,EACf,EAAQ,GAGN,EAAqB,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bounds.d.ts
CHANGED
|
@@ -5,8 +5,7 @@
|
|
|
5
5
|
* Reads worldTransform for position checking; modifies localTransform for corrections.
|
|
6
6
|
* Supports destroy, clamp, and wrap behaviors.
|
|
7
7
|
*/
|
|
8
|
-
import { type
|
|
9
|
-
import type { WorldConfigFrom } from '../type-utils';
|
|
8
|
+
import { type BasePluginOptions } from 'ecspresso';
|
|
10
9
|
import type { TransformWorldConfig } from './transform';
|
|
11
10
|
/**
|
|
12
11
|
* Component that marks an entity for destruction when outside bounds.
|
|
@@ -180,4 +179,4 @@ export declare function createWrapAtBounds(padding?: number): Pick<BoundsCompone
|
|
|
180
179
|
* });
|
|
181
180
|
* ```
|
|
182
181
|
*/
|
|
183
|
-
export declare function createBoundsPlugin<ResourceTypes extends BoundsResourceTypes = BoundsResourceTypes, G extends string = 'physics'>(options?: BoundsPluginOptions<G>): Plugin<
|
|
182
|
+
export declare function createBoundsPlugin<ResourceTypes extends BoundsResourceTypes = BoundsResourceTypes, G extends string = 'physics'>(options?: BoundsPluginOptions<G>): import("ecspresso").Plugin<import("ecspresso").WithResources<import("ecspresso").WithEvents<import("ecspresso").WithComponents<import("ecspresso").EmptyConfig, BoundsComponentTypes>, BoundsEventTypes>, ResourceTypes>, TransformWorldConfig, "bounds-destroy" | "bounds-clamp" | "bounds-wrap", G, never, never>;
|
package/dist/plugins/bounds.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
var R=((j)=>typeof require<"u"?require:typeof Proxy<"u"?new Proxy(j,{get:(I,F)=>(typeof require<"u"?require:I)[F]}):j)(function(j){if(typeof require<"u")return require.apply(this,arguments);throw Error('Dynamic require of "'+j+'" is not supported')});import{definePlugin as K}from"ecspresso";function h(j,I,F,S){let V={width:j,height:I};if(F!==void 0)V.x=F;if(S!==void 0)V.y=S;return V}function T(j){return{destroyOutOfBounds:j!==void 0?{padding:j}:{}}}function b(j){return{clampToBounds:j!==void 0?{margin:j}:{}}}function w(j){return{wrapAtBounds:j!==void 0?{padding:j}:{}}}function y(j){let{systemGroup:I="physics",priority:F=50,boundsResourceKey:S="bounds",autoRemove:V=!0,phase:U="postUpdate"}=j??{};return K(
|
|
1
|
+
var R=((j)=>typeof require<"u"?require:typeof Proxy<"u"?new Proxy(j,{get:(I,F)=>(typeof require<"u"?require:I)[F]}):j)(function(j){if(typeof require<"u")return require.apply(this,arguments);throw Error('Dynamic require of "'+j+'" is not supported')});import{definePlugin as K}from"ecspresso";function h(j,I,F,S){let V={width:j,height:I};if(F!==void 0)V.x=F;if(S!==void 0)V.y=S;return V}function T(j){return{destroyOutOfBounds:j!==void 0?{padding:j}:{}}}function b(j){return{clampToBounds:j!==void 0?{margin:j}:{}}}function w(j){return{wrapAtBounds:j!==void 0?{padding:j}:{}}}function y(j){let{systemGroup:I="physics",priority:F=50,boundsResourceKey:S="bounds",autoRemove:V=!0,phase:U="postUpdate"}=j??{};return K("bounds").withComponentTypes().withEventTypes().withResourceTypes().withLabels().withGroups().requires().install((G)=>{G.addSystem("bounds-destroy").setPriority(F).inPhase(U).inGroup(I).addQuery("entities",{with:["worldTransform","destroyOutOfBounds"]}).setProcess(({queries:D,ecs:J})=>{let C=J.getResource(S),L=C.x??0,N=C.y??0,v=L+C.width,A=N+C.height;for(let Q of D.entities){let{worldTransform:Z,destroyOutOfBounds:z}=Q.components,P=z.padding??0,k=E(Z,L,N,v,A,P);if(!k)continue;if(J.eventBus.publish("entityOutOfBounds",{entityId:Q.id,exitEdge:k}),V)J.commands.removeEntity(Q.id)}}),G.addSystem("bounds-clamp").setPriority(F-1).inPhase(U).inGroup(I).addQuery("entities",{with:["localTransform","worldTransform","clampToBounds"]}).setProcess(({queries:D,ecs:J})=>{let C=J.getResource(S),L=C.x??0,N=C.y??0,v=L+C.width,A=N+C.height;for(let Q of D.entities){let{localTransform:Z,worldTransform:z,clampToBounds:P}=Q.components,k=P.margin??0,_=L+k,$=N+k,H=v-k,O=A-k,W=0,B=0;if(z.x<_)W=_-z.x;if(z.x>H)W=H-z.x;if(z.y<$)B=$-z.y;if(z.y>O)B=O-z.y;if(W!==0||B!==0)Z.x+=W,Z.y+=B,J.markChanged(Q.id,"localTransform")}}),G.addSystem("bounds-wrap").setPriority(F-2).inPhase(U).inGroup(I).addQuery("entities",{with:["localTransform","worldTransform","wrapAtBounds"]}).setProcess(({queries:D,ecs:J})=>{let C=J.getResource(S),L=C.x??0,N=C.y??0,v=L+C.width,A=N+C.height;for(let Q of D.entities){let{localTransform:Z,worldTransform:z,wrapAtBounds:P}=Q.components,k=P.padding??0,_=0,$=0,H=v-L,O=A-N;if(z.x>v+k)_=-(H+2*k);else if(z.x<L-k)_=H+2*k;if(z.y>A+k)$=-(O+2*k);else if(z.y<N-k)$=O+2*k;if(_!==0||$!==0)Z.x+=_,Z.y+=$,J.markChanged(Q.id,"localTransform")}})})}function E(j,I,F,S,V,U){if(j.x>S+U)return"right";if(j.x<I-U)return"left";if(j.y>V+U)return"bottom";if(j.y<F-U)return"top";return null}export{w as createWrapAtBounds,T as createDestroyOutOfBounds,b as createClampToBounds,y as createBoundsPlugin,h as createBounds};
|
|
2
2
|
|
|
3
|
-
//# debugId=
|
|
3
|
+
//# debugId=A9A3B2D9F55EA97664756E2164756E21
|
|
4
4
|
//# sourceMappingURL=bounds.js.map
|
|
@@ -2,9 +2,9 @@
|
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../src/plugins/bounds.ts"],
|
|
4
4
|
"sourcesContent": [
|
|
5
|
-
"/**\n * Bounds Plugin for ECSpresso\n *\n * Provides screen bounds enforcement for entities with transforms.\n * Reads worldTransform for position checking; modifies localTransform for corrections.\n * Supports destroy, clamp, and wrap behaviors.\n */\n\nimport { definePlugin, type Plugin, type BasePluginOptions } from 'ecspresso';\nimport type { WorldConfigFrom } from '../type-utils';\nimport type { TransformWorldConfig } from './transform';\n\n// ==================== Component Types ====================\n\n/**\n * Component that marks an entity for destruction when outside bounds.\n */\nexport interface DestroyOutOfBounds {\n\t/** Extra padding beyond bounds before destruction (default: 0) */\n\tpadding?: number;\n}\n\n/**\n * Component that clamps an entity's position to stay within bounds.\n */\nexport interface ClampToBounds {\n\t/** Margin to shrink the valid area (default: 0) */\n\tmargin?: number;\n}\n\n/**\n * Component that wraps an entity's position to the opposite edge.\n */\nexport interface WrapAtBounds {\n\t/** Padding beyond bounds before wrapping (default: 0) */\n\tpadding?: number;\n}\n\n/**\n * Component types provided by the bounds plugin.\n * Included automatically via `.withPlugin(createBoundsPlugin())`.\n *\n * @example\n * ```typescript\n * const ecs = ECSpresso.create()\n * .withPlugin(createTransformPlugin())\n * .withPlugin(createBoundsPlugin({ width: 800, height: 600 }))\n * .withComponentTypes<{ sprite: Sprite }>()\n * .build();\n * ```\n */\nexport interface BoundsComponentTypes {\n\tdestroyOutOfBounds: DestroyOutOfBounds;\n\tclampToBounds: ClampToBounds;\n\twrapAtBounds: WrapAtBounds;\n}\n\n// ==================== Resource Types ====================\n\n/**\n * Bounds rectangle definition.\n */\nexport interface BoundsRect {\n\t/** Left edge x coordinate (default: 0) */\n\tx?: number;\n\t/** Top edge y coordinate (default: 0) */\n\ty?: number;\n\t/** Width of the bounds area */\n\twidth: number;\n\t/** Height of the bounds area */\n\theight: number;\n}\n\n/**\n * Resource types provided by the bounds plugin.\n */\nexport interface BoundsResourceTypes {\n\tbounds: BoundsRect;\n}\n\n// ==================== Event Types ====================\n\n/**\n * Event fired when an entity exits bounds.\n */\nexport interface EntityOutOfBoundsEvent {\n\t/** The entity that exited bounds */\n\tentityId: number;\n\t/** The edge the entity exited through */\n\texitEdge: 'top' | 'bottom' | 'left' | 'right';\n}\n\n/**\n * Event types provided by the bounds plugin.\n */\nexport interface BoundsEventTypes {\n\tentityOutOfBounds: EntityOutOfBoundsEvent;\n}\n\n// ==================== Plugin Options ====================\n\n/**\n * Configuration options for the bounds plugin.\n */\nexport interface BoundsPluginOptions<G extends string = 'physics'> extends BasePluginOptions<G> {\n\t/** Resource key for bounds rectangle (default: 'bounds') */\n\tboundsResourceKey?: string;\n\t/** Whether to auto-remove entities when out of bounds (default: true) */\n\tautoRemove?: boolean;\n}\n\n// ==================== Helper Functions ====================\n\n/**\n * Create a bounds rectangle resource.\n *\n * @param width The width of the bounds area\n * @param height The height of the bounds area\n * @param x The left edge x coordinate (default: 0)\n * @param y The top edge y coordinate (default: 0)\n * @returns Bounds rectangle suitable for use as a resource\n *\n * @example\n * ```typescript\n * ECSpresso.create()\n * .withResource('bounds', createBounds(800, 600))\n * .build();\n * ```\n */\nexport function createBounds(width: number, height: number, x?: number, y?: number): BoundsRect {\n\tconst bounds: BoundsRect = { width, height };\n\tif (x !== undefined) bounds.x = x;\n\tif (y !== undefined) bounds.y = y;\n\treturn bounds;\n}\n\n/**\n * Create a destroyOutOfBounds component.\n *\n * @param padding Extra padding beyond bounds before destruction\n * @returns Component object suitable for spreading into spawn()\n *\n * @example\n * ```typescript\n * ecs.spawn({\n * ...createTransform(100, 200),\n * ...createDestroyOutOfBounds(20),\n * });\n * ```\n */\nexport function createDestroyOutOfBounds(padding?: number): Pick<BoundsComponentTypes, 'destroyOutOfBounds'> {\n\treturn {\n\t\tdestroyOutOfBounds: padding !== undefined ? { padding } : {},\n\t};\n}\n\n/**\n * Create a clampToBounds component.\n *\n * @param margin Margin to shrink the valid area\n * @returns Component object suitable for spreading into spawn()\n *\n * @example\n * ```typescript\n * ecs.spawn({\n * ...createTransform(100, 200),\n * ...createClampToBounds(30),\n * });\n * ```\n */\nexport function createClampToBounds(margin?: number): Pick<BoundsComponentTypes, 'clampToBounds'> {\n\treturn {\n\t\tclampToBounds: margin !== undefined ? { margin } : {},\n\t};\n}\n\n/**\n * Create a wrapAtBounds component.\n *\n * @param padding Padding beyond bounds before wrapping\n * @returns Component object suitable for spreading into spawn()\n *\n * @example\n * ```typescript\n * ecs.spawn({\n * ...createTransform(100, 200),\n * ...createWrapAtBounds(10),\n * });\n * ```\n */\nexport function createWrapAtBounds(padding?: number): Pick<BoundsComponentTypes, 'wrapAtBounds'> {\n\treturn {\n\t\twrapAtBounds: padding !== undefined ? { padding } : {},\n\t};\n}\n\n// ==================== Dependency Types ====================\n\n// ==================== Plugin Factory ====================\n\n/**\n * Create a bounds plugin for ECSpresso.\n *\n * This plugin provides:\n * - Destroy out of bounds system - removes entities that exit bounds\n * - Clamp to bounds system - constrains entities within bounds\n * - Wrap at bounds system - wraps entities to opposite edge\n *\n * Uses worldTransform for position checking (world-space) and modifies\n * localTransform for corrections. Works best with entities that don't\n * have parent transforms (orphan entities).\n *\n * @example\n * ```typescript\n * const ecs = ECSpresso\n * .create<Components, Events, Resources>()\n * .withResource('bounds', createBounds(800, 600))\n * .withPlugin(createTransformPlugin())\n * .withPlugin(createBoundsPlugin())\n * .build();\n *\n * // Entity that gets destroyed when leaving screen\n * ecs.spawn({\n * ...createTransform(100, 200),\n * ...createDestroyOutOfBounds(),\n * });\n * ```\n */\nexport function createBoundsPlugin<ResourceTypes extends BoundsResourceTypes = BoundsResourceTypes, G extends string = 'physics'>(\n\toptions?: BoundsPluginOptions<G>\n): Plugin<WorldConfigFrom<BoundsComponentTypes, BoundsEventTypes, ResourceTypes>, TransformWorldConfig, 'bounds-destroy' | 'bounds-clamp' | 'bounds-wrap', G> {\n\tconst {\n\t\tsystemGroup = 'physics',\n\t\tpriority = 50,\n\t\tboundsResourceKey = 'bounds',\n\t\tautoRemove = true,\n\t\tphase = 'postUpdate',\n\t} = options ?? {};\n\n\treturn definePlugin<WorldConfigFrom<BoundsComponentTypes, BoundsEventTypes, ResourceTypes>, TransformWorldConfig, 'bounds-destroy' | 'bounds-clamp' | 'bounds-wrap', G>({\n\t\tid: 'bounds',\n\t\tinstall(world) {\n\t\t\t// Destroy out of bounds system\n\t\t\tworld\n\t\t\t\t.addSystem('bounds-destroy')\n\t\t\t\t.setPriority(priority)\n\t\t\t\t.inPhase(phase)\n\t\t\t\t.inGroup(systemGroup)\n\t\t\t\t.addQuery('entities', {\n\t\t\t\t\twith: ['worldTransform', 'destroyOutOfBounds'],\n\t\t\t\t})\n\t\t\t\t.setProcess(({ queries, ecs }) => {\n\t\t\t\t\tconst bounds = ecs.getResource(boundsResourceKey as keyof ResourceTypes) as BoundsRect;\n\t\t\t\t\tconst minX = bounds.x ?? 0;\n\t\t\t\t\tconst minY = bounds.y ?? 0;\n\t\t\t\t\tconst maxX = minX + bounds.width;\n\t\t\t\t\tconst maxY = minY + bounds.height;\n\n\t\t\t\t\tfor (const entity of queries.entities) {\n\t\t\t\t\t\tconst { worldTransform, destroyOutOfBounds } = entity.components;\n\t\t\t\t\t\tconst padding = destroyOutOfBounds.padding ?? 0;\n\n\t\t\t\t\t\tconst exitEdge = getExitEdge(worldTransform, minX, minY, maxX, maxY, padding);\n\t\t\t\t\t\tif (!exitEdge) continue;\n\n\t\t\t\t\t\tecs.eventBus.publish('entityOutOfBounds', {\n\t\t\t\t\t\t\tentityId: entity.id,\n\t\t\t\t\t\t\texitEdge,\n\t\t\t\t\t\t});\n\n\t\t\t\t\t\tif (autoRemove) {\n\t\t\t\t\t\t\tecs.commands.removeEntity(entity.id);\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t});\n\n\t\t\t// Clamp to bounds system\n\t\t\tworld\n\t\t\t\t.addSystem('bounds-clamp')\n\t\t\t\t.setPriority(priority - 1)\n\t\t\t\t.inPhase(phase)\n\t\t\t\t.inGroup(systemGroup)\n\t\t\t\t.addQuery('entities', {\n\t\t\t\t\twith: ['localTransform', 'worldTransform', 'clampToBounds'],\n\t\t\t\t})\n\t\t\t\t.setProcess(({ queries, ecs }) => {\n\t\t\t\t\tconst bounds = ecs.getResource(boundsResourceKey as keyof ResourceTypes) as BoundsRect;\n\t\t\t\t\tconst minX = bounds.x ?? 0;\n\t\t\t\t\tconst minY = bounds.y ?? 0;\n\t\t\t\t\tconst maxX = minX + bounds.width;\n\t\t\t\t\tconst maxY = minY + bounds.height;\n\n\t\t\t\t\tfor (const entity of queries.entities) {\n\t\t\t\t\t\tconst { localTransform, worldTransform, clampToBounds } = entity.components;\n\t\t\t\t\t\tconst margin = clampToBounds.margin ?? 0;\n\n\t\t\t\t\t\tconst clampedMinX = minX + margin;\n\t\t\t\t\t\tconst clampedMinY = minY + margin;\n\t\t\t\t\t\tconst clampedMaxX = maxX - margin;\n\t\t\t\t\t\tconst clampedMaxY = maxY - margin;\n\n\t\t\t\t\t\t// Calculate world-space correction and apply to local transform\n\t\t\t\t\t\t// For entities without parents, this is equivalent to direct position clamping\n\t\t\t\t\t\tlet deltaX = 0;\n\t\t\t\t\t\tlet deltaY = 0;\n\n\t\t\t\t\t\tif (worldTransform.x < clampedMinX) deltaX = clampedMinX - worldTransform.x;\n\t\t\t\t\t\tif (worldTransform.x > clampedMaxX) deltaX = clampedMaxX - worldTransform.x;\n\t\t\t\t\t\tif (worldTransform.y < clampedMinY) deltaY = clampedMinY - worldTransform.y;\n\t\t\t\t\t\tif (worldTransform.y > clampedMaxY) deltaY = clampedMaxY - worldTransform.y;\n\n\t\t\t\t\t\tif (deltaX !== 0 || deltaY !== 0) {\n\t\t\t\t\t\t\tlocalTransform.x += deltaX;\n\t\t\t\t\t\t\tlocalTransform.y += deltaY;\n\t\t\t\t\t\t\tecs.markChanged(entity.id, 'localTransform');\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t});\n\n\t\t\t// Wrap at bounds system\n\t\t\tworld\n\t\t\t\t.addSystem('bounds-wrap')\n\t\t\t\t.setPriority(priority - 2)\n\t\t\t\t.inPhase(phase)\n\t\t\t\t.inGroup(systemGroup)\n\t\t\t\t.addQuery('entities', {\n\t\t\t\t\twith: ['localTransform', 'worldTransform', 'wrapAtBounds'],\n\t\t\t\t})\n\t\t\t\t.setProcess(({ queries, ecs }) => {\n\t\t\t\t\tconst bounds = ecs.getResource(boundsResourceKey as keyof ResourceTypes) as BoundsRect;\n\t\t\t\t\tconst minX = bounds.x ?? 0;\n\t\t\t\t\tconst minY = bounds.y ?? 0;\n\t\t\t\t\tconst maxX = minX + bounds.width;\n\t\t\t\t\tconst maxY = minY + bounds.height;\n\n\t\t\t\t\tfor (const entity of queries.entities) {\n\t\t\t\t\t\tconst { localTransform, worldTransform, wrapAtBounds } = entity.components;\n\t\t\t\t\t\tconst padding = wrapAtBounds.padding ?? 0;\n\n\t\t\t\t\t\tlet deltaX = 0;\n\t\t\t\t\t\tlet deltaY = 0;\n\t\t\t\t\t\tconst boundsWidth = maxX - minX;\n\t\t\t\t\t\tconst boundsHeight = maxY - minY;\n\n\t\t\t\t\t\t// Wrap horizontally\n\t\t\t\t\t\tif (worldTransform.x > maxX + padding) {\n\t\t\t\t\t\t\tdeltaX = -(boundsWidth + 2 * padding);\n\t\t\t\t\t\t} else if (worldTransform.x < minX - padding) {\n\t\t\t\t\t\t\tdeltaX = boundsWidth + 2 * padding;\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\t// Wrap vertically\n\t\t\t\t\t\tif (worldTransform.y > maxY + padding) {\n\t\t\t\t\t\t\tdeltaY = -(boundsHeight + 2 * padding);\n\t\t\t\t\t\t} else if (worldTransform.y < minY - padding) {\n\t\t\t\t\t\t\tdeltaY = boundsHeight + 2 * padding;\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tif (deltaX !== 0 || deltaY !== 0) {\n\t\t\t\t\t\t\tlocalTransform.x += deltaX;\n\t\t\t\t\t\t\tlocalTransform.y += deltaY;\n\t\t\t\t\t\t\tecs.markChanged(entity.id, 'localTransform');\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t});\n\t\t},\n\t});\n}\n\n/**\n * Determine which edge an entity has exited through, if any.\n */\nfunction getExitEdge(\n\ttransform: { x: number; y: number },\n\tminX: number,\n\tminY: number,\n\tmaxX: number,\n\tmaxY: number,\n\tpadding: number\n): 'top' | 'bottom' | 'left' | 'right' | null {\n\tif (transform.x > maxX + padding) return 'right';\n\tif (transform.x < minX - padding) return 'left';\n\tif (transform.y > maxY + padding) return 'bottom';\n\tif (transform.y < minY - padding) return 'top';\n\treturn null;\n}\n"
|
|
5
|
+
"/**\n * Bounds Plugin for ECSpresso\n *\n * Provides screen bounds enforcement for entities with transforms.\n * Reads worldTransform for position checking; modifies localTransform for corrections.\n * Supports destroy, clamp, and wrap behaviors.\n */\n\nimport { definePlugin, type BasePluginOptions } from 'ecspresso';\nimport type { TransformWorldConfig } from './transform';\n\n// ==================== Component Types ====================\n\n/**\n * Component that marks an entity for destruction when outside bounds.\n */\nexport interface DestroyOutOfBounds {\n\t/** Extra padding beyond bounds before destruction (default: 0) */\n\tpadding?: number;\n}\n\n/**\n * Component that clamps an entity's position to stay within bounds.\n */\nexport interface ClampToBounds {\n\t/** Margin to shrink the valid area (default: 0) */\n\tmargin?: number;\n}\n\n/**\n * Component that wraps an entity's position to the opposite edge.\n */\nexport interface WrapAtBounds {\n\t/** Padding beyond bounds before wrapping (default: 0) */\n\tpadding?: number;\n}\n\n/**\n * Component types provided by the bounds plugin.\n * Included automatically via `.withPlugin(createBoundsPlugin())`.\n *\n * @example\n * ```typescript\n * const ecs = ECSpresso.create()\n * .withPlugin(createTransformPlugin())\n * .withPlugin(createBoundsPlugin({ width: 800, height: 600 }))\n * .withComponentTypes<{ sprite: Sprite }>()\n * .build();\n * ```\n */\nexport interface BoundsComponentTypes {\n\tdestroyOutOfBounds: DestroyOutOfBounds;\n\tclampToBounds: ClampToBounds;\n\twrapAtBounds: WrapAtBounds;\n}\n\n// ==================== Resource Types ====================\n\n/**\n * Bounds rectangle definition.\n */\nexport interface BoundsRect {\n\t/** Left edge x coordinate (default: 0) */\n\tx?: number;\n\t/** Top edge y coordinate (default: 0) */\n\ty?: number;\n\t/** Width of the bounds area */\n\twidth: number;\n\t/** Height of the bounds area */\n\theight: number;\n}\n\n/**\n * Resource types provided by the bounds plugin.\n */\nexport interface BoundsResourceTypes {\n\tbounds: BoundsRect;\n}\n\n// ==================== Event Types ====================\n\n/**\n * Event fired when an entity exits bounds.\n */\nexport interface EntityOutOfBoundsEvent {\n\t/** The entity that exited bounds */\n\tentityId: number;\n\t/** The edge the entity exited through */\n\texitEdge: 'top' | 'bottom' | 'left' | 'right';\n}\n\n/**\n * Event types provided by the bounds plugin.\n */\nexport interface BoundsEventTypes {\n\tentityOutOfBounds: EntityOutOfBoundsEvent;\n}\n\n// ==================== Plugin Options ====================\n\n/**\n * Configuration options for the bounds plugin.\n */\nexport interface BoundsPluginOptions<G extends string = 'physics'> extends BasePluginOptions<G> {\n\t/** Resource key for bounds rectangle (default: 'bounds') */\n\tboundsResourceKey?: string;\n\t/** Whether to auto-remove entities when out of bounds (default: true) */\n\tautoRemove?: boolean;\n}\n\n// ==================== Helper Functions ====================\n\n/**\n * Create a bounds rectangle resource.\n *\n * @param width The width of the bounds area\n * @param height The height of the bounds area\n * @param x The left edge x coordinate (default: 0)\n * @param y The top edge y coordinate (default: 0)\n * @returns Bounds rectangle suitable for use as a resource\n *\n * @example\n * ```typescript\n * ECSpresso.create()\n * .withResource('bounds', createBounds(800, 600))\n * .build();\n * ```\n */\nexport function createBounds(width: number, height: number, x?: number, y?: number): BoundsRect {\n\tconst bounds: BoundsRect = { width, height };\n\tif (x !== undefined) bounds.x = x;\n\tif (y !== undefined) bounds.y = y;\n\treturn bounds;\n}\n\n/**\n * Create a destroyOutOfBounds component.\n *\n * @param padding Extra padding beyond bounds before destruction\n * @returns Component object suitable for spreading into spawn()\n *\n * @example\n * ```typescript\n * ecs.spawn({\n * ...createTransform(100, 200),\n * ...createDestroyOutOfBounds(20),\n * });\n * ```\n */\nexport function createDestroyOutOfBounds(padding?: number): Pick<BoundsComponentTypes, 'destroyOutOfBounds'> {\n\treturn {\n\t\tdestroyOutOfBounds: padding !== undefined ? { padding } : {},\n\t};\n}\n\n/**\n * Create a clampToBounds component.\n *\n * @param margin Margin to shrink the valid area\n * @returns Component object suitable for spreading into spawn()\n *\n * @example\n * ```typescript\n * ecs.spawn({\n * ...createTransform(100, 200),\n * ...createClampToBounds(30),\n * });\n * ```\n */\nexport function createClampToBounds(margin?: number): Pick<BoundsComponentTypes, 'clampToBounds'> {\n\treturn {\n\t\tclampToBounds: margin !== undefined ? { margin } : {},\n\t};\n}\n\n/**\n * Create a wrapAtBounds component.\n *\n * @param padding Padding beyond bounds before wrapping\n * @returns Component object suitable for spreading into spawn()\n *\n * @example\n * ```typescript\n * ecs.spawn({\n * ...createTransform(100, 200),\n * ...createWrapAtBounds(10),\n * });\n * ```\n */\nexport function createWrapAtBounds(padding?: number): Pick<BoundsComponentTypes, 'wrapAtBounds'> {\n\treturn {\n\t\twrapAtBounds: padding !== undefined ? { padding } : {},\n\t};\n}\n\n// ==================== Dependency Types ====================\n\n// ==================== Plugin Factory ====================\n\n/**\n * Create a bounds plugin for ECSpresso.\n *\n * This plugin provides:\n * - Destroy out of bounds system - removes entities that exit bounds\n * - Clamp to bounds system - constrains entities within bounds\n * - Wrap at bounds system - wraps entities to opposite edge\n *\n * Uses worldTransform for position checking (world-space) and modifies\n * localTransform for corrections. Works best with entities that don't\n * have parent transforms (orphan entities).\n *\n * @example\n * ```typescript\n * const ecs = ECSpresso\n * .create<Components, Events, Resources>()\n * .withResource('bounds', createBounds(800, 600))\n * .withPlugin(createTransformPlugin())\n * .withPlugin(createBoundsPlugin())\n * .build();\n *\n * // Entity that gets destroyed when leaving screen\n * ecs.spawn({\n * ...createTransform(100, 200),\n * ...createDestroyOutOfBounds(),\n * });\n * ```\n */\nexport function createBoundsPlugin<ResourceTypes extends BoundsResourceTypes = BoundsResourceTypes, G extends string = 'physics'>(\n\toptions?: BoundsPluginOptions<G>\n) {\n\tconst {\n\t\tsystemGroup = 'physics',\n\t\tpriority = 50,\n\t\tboundsResourceKey = 'bounds',\n\t\tautoRemove = true,\n\t\tphase = 'postUpdate',\n\t} = options ?? {};\n\n\treturn definePlugin('bounds')\n\t\t.withComponentTypes<BoundsComponentTypes>()\n\t\t.withEventTypes<BoundsEventTypes>()\n\t\t.withResourceTypes<ResourceTypes>()\n\t\t.withLabels<'bounds-destroy' | 'bounds-clamp' | 'bounds-wrap'>()\n\t\t.withGroups<G>()\n\t\t.requires<TransformWorldConfig>()\n\t\t.install((world) => {\n\t\t\t// Destroy out of bounds system\n\t\t\tworld\n\t\t\t\t.addSystem('bounds-destroy')\n\t\t\t\t.setPriority(priority)\n\t\t\t\t.inPhase(phase)\n\t\t\t\t.inGroup(systemGroup)\n\t\t\t\t.addQuery('entities', {\n\t\t\t\t\twith: ['worldTransform', 'destroyOutOfBounds'],\n\t\t\t\t})\n\t\t\t\t.setProcess(({ queries, ecs }) => {\n\t\t\t\t\tconst bounds = ecs.getResource(boundsResourceKey as keyof ResourceTypes) as BoundsRect;\n\t\t\t\t\tconst minX = bounds.x ?? 0;\n\t\t\t\t\tconst minY = bounds.y ?? 0;\n\t\t\t\t\tconst maxX = minX + bounds.width;\n\t\t\t\t\tconst maxY = minY + bounds.height;\n\n\t\t\t\t\tfor (const entity of queries.entities) {\n\t\t\t\t\t\tconst { worldTransform, destroyOutOfBounds } = entity.components;\n\t\t\t\t\t\tconst padding = destroyOutOfBounds.padding ?? 0;\n\n\t\t\t\t\t\tconst exitEdge = getExitEdge(worldTransform, minX, minY, maxX, maxY, padding);\n\t\t\t\t\t\tif (!exitEdge) continue;\n\n\t\t\t\t\t\tecs.eventBus.publish('entityOutOfBounds', {\n\t\t\t\t\t\t\tentityId: entity.id,\n\t\t\t\t\t\t\texitEdge,\n\t\t\t\t\t\t});\n\n\t\t\t\t\t\tif (autoRemove) {\n\t\t\t\t\t\t\tecs.commands.removeEntity(entity.id);\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t});\n\n\t\t\t// Clamp to bounds system\n\t\t\tworld\n\t\t\t\t.addSystem('bounds-clamp')\n\t\t\t\t.setPriority(priority - 1)\n\t\t\t\t.inPhase(phase)\n\t\t\t\t.inGroup(systemGroup)\n\t\t\t\t.addQuery('entities', {\n\t\t\t\t\twith: ['localTransform', 'worldTransform', 'clampToBounds'],\n\t\t\t\t})\n\t\t\t\t.setProcess(({ queries, ecs }) => {\n\t\t\t\t\tconst bounds = ecs.getResource(boundsResourceKey as keyof ResourceTypes) as BoundsRect;\n\t\t\t\t\tconst minX = bounds.x ?? 0;\n\t\t\t\t\tconst minY = bounds.y ?? 0;\n\t\t\t\t\tconst maxX = minX + bounds.width;\n\t\t\t\t\tconst maxY = minY + bounds.height;\n\n\t\t\t\t\tfor (const entity of queries.entities) {\n\t\t\t\t\t\tconst { localTransform, worldTransform, clampToBounds } = entity.components;\n\t\t\t\t\t\tconst margin = clampToBounds.margin ?? 0;\n\n\t\t\t\t\t\tconst clampedMinX = minX + margin;\n\t\t\t\t\t\tconst clampedMinY = minY + margin;\n\t\t\t\t\t\tconst clampedMaxX = maxX - margin;\n\t\t\t\t\t\tconst clampedMaxY = maxY - margin;\n\n\t\t\t\t\t\t// Calculate world-space correction and apply to local transform\n\t\t\t\t\t\t// For entities without parents, this is equivalent to direct position clamping\n\t\t\t\t\t\tlet deltaX = 0;\n\t\t\t\t\t\tlet deltaY = 0;\n\n\t\t\t\t\t\tif (worldTransform.x < clampedMinX) deltaX = clampedMinX - worldTransform.x;\n\t\t\t\t\t\tif (worldTransform.x > clampedMaxX) deltaX = clampedMaxX - worldTransform.x;\n\t\t\t\t\t\tif (worldTransform.y < clampedMinY) deltaY = clampedMinY - worldTransform.y;\n\t\t\t\t\t\tif (worldTransform.y > clampedMaxY) deltaY = clampedMaxY - worldTransform.y;\n\n\t\t\t\t\t\tif (deltaX !== 0 || deltaY !== 0) {\n\t\t\t\t\t\t\tlocalTransform.x += deltaX;\n\t\t\t\t\t\t\tlocalTransform.y += deltaY;\n\t\t\t\t\t\t\tecs.markChanged(entity.id, 'localTransform');\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t});\n\n\t\t\t// Wrap at bounds system\n\t\t\tworld\n\t\t\t\t.addSystem('bounds-wrap')\n\t\t\t\t.setPriority(priority - 2)\n\t\t\t\t.inPhase(phase)\n\t\t\t\t.inGroup(systemGroup)\n\t\t\t\t.addQuery('entities', {\n\t\t\t\t\twith: ['localTransform', 'worldTransform', 'wrapAtBounds'],\n\t\t\t\t})\n\t\t\t\t.setProcess(({ queries, ecs }) => {\n\t\t\t\t\tconst bounds = ecs.getResource(boundsResourceKey as keyof ResourceTypes) as BoundsRect;\n\t\t\t\t\tconst minX = bounds.x ?? 0;\n\t\t\t\t\tconst minY = bounds.y ?? 0;\n\t\t\t\t\tconst maxX = minX + bounds.width;\n\t\t\t\t\tconst maxY = minY + bounds.height;\n\n\t\t\t\t\tfor (const entity of queries.entities) {\n\t\t\t\t\t\tconst { localTransform, worldTransform, wrapAtBounds } = entity.components;\n\t\t\t\t\t\tconst padding = wrapAtBounds.padding ?? 0;\n\n\t\t\t\t\t\tlet deltaX = 0;\n\t\t\t\t\t\tlet deltaY = 0;\n\t\t\t\t\t\tconst boundsWidth = maxX - minX;\n\t\t\t\t\t\tconst boundsHeight = maxY - minY;\n\n\t\t\t\t\t\t// Wrap horizontally\n\t\t\t\t\t\tif (worldTransform.x > maxX + padding) {\n\t\t\t\t\t\t\tdeltaX = -(boundsWidth + 2 * padding);\n\t\t\t\t\t\t} else if (worldTransform.x < minX - padding) {\n\t\t\t\t\t\t\tdeltaX = boundsWidth + 2 * padding;\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\t// Wrap vertically\n\t\t\t\t\t\tif (worldTransform.y > maxY + padding) {\n\t\t\t\t\t\t\tdeltaY = -(boundsHeight + 2 * padding);\n\t\t\t\t\t\t} else if (worldTransform.y < minY - padding) {\n\t\t\t\t\t\t\tdeltaY = boundsHeight + 2 * padding;\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tif (deltaX !== 0 || deltaY !== 0) {\n\t\t\t\t\t\t\tlocalTransform.x += deltaX;\n\t\t\t\t\t\t\tlocalTransform.y += deltaY;\n\t\t\t\t\t\t\tecs.markChanged(entity.id, 'localTransform');\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t});\n\t\t});\n}\n\n/**\n * Determine which edge an entity has exited through, if any.\n */\nfunction getExitEdge(\n\ttransform: { x: number; y: number },\n\tminX: number,\n\tminY: number,\n\tmaxX: number,\n\tmaxY: number,\n\tpadding: number\n): 'top' | 'bottom' | 'left' | 'right' | null {\n\tif (transform.x > maxX + padding) return 'right';\n\tif (transform.x < minX - padding) return 'left';\n\tif (transform.y > maxY + padding) return 'bottom';\n\tif (transform.y < minY - padding) return 'top';\n\treturn null;\n}\n"
|
|
6
6
|
],
|
|
7
|
-
"mappings": "2PAQA,uBAAS,
|
|
8
|
-
"debugId": "
|
|
7
|
+
"mappings": "2PAQA,uBAAS,kBAwHF,SAAS,CAAY,CAAC,EAAe,EAAgB,EAAY,EAAwB,CAC/F,IAAM,EAAqB,CAAE,QAAO,QAAO,EAC3C,GAAI,IAAM,OAAW,EAAO,EAAI,EAChC,GAAI,IAAM,OAAW,EAAO,EAAI,EAChC,OAAO,EAiBD,SAAS,CAAwB,CAAC,EAAoE,CAC5G,MAAO,CACN,mBAAoB,IAAY,OAAY,CAAE,SAAQ,EAAI,CAAC,CAC5D,EAiBM,SAAS,CAAmB,CAAC,EAA8D,CACjG,MAAO,CACN,cAAe,IAAW,OAAY,CAAE,QAAO,EAAI,CAAC,CACrD,EAiBM,SAAS,CAAkB,CAAC,EAA8D,CAChG,MAAO,CACN,aAAc,IAAY,OAAY,CAAE,SAAQ,EAAI,CAAC,CACtD,EAmCM,SAAS,CAAiH,CAChI,EACC,CACD,IACC,cAAc,UACd,WAAW,GACX,oBAAoB,SACpB,aAAa,GACb,QAAQ,cACL,GAAW,CAAC,EAEhB,OAAO,EAAa,QAAQ,EAC1B,mBAAyC,EACzC,eAAiC,EACjC,kBAAiC,EACjC,WAA8D,EAC9D,WAAc,EACd,SAA+B,EAC/B,QAAQ,CAAC,IAAU,CAEnB,EACE,UAAU,gBAAgB,EAC1B,YAAY,CAAQ,EACpB,QAAQ,CAAK,EACb,QAAQ,CAAW,EACnB,SAAS,WAAY,CACrB,KAAM,CAAC,iBAAkB,oBAAoB,CAC9C,CAAC,EACA,WAAW,EAAG,UAAS,S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
|
|
|
11
11
|
* It reads the target entity's worldTransform for follow, but doesn't participate
|
|
12
12
|
* in the transform hierarchy itself.
|
|
13
13
|
*/
|
|
14
|
-
import { type Plugin } from 'ecspresso';
|
|
15
14
|
import type { SystemPhase } from 'ecspresso';
|
|
16
15
|
import type ECSpresso from 'ecspresso';
|
|
17
16
|
import type { WorldConfigFrom } from '../type-utils';
|
|
@@ -85,4 +84,4 @@ export declare function screenToWorld(screenX: number, screenY: number, state: C
|
|
|
85
84
|
x: number;
|
|
86
85
|
y: number;
|
|
87
86
|
};
|
|
88
|
-
export declare function createCameraPlugin<G extends string = 'camera'>(options?: CameraPluginOptions<G>): Plugin<
|
|
87
|
+
export declare function createCameraPlugin<G extends string = 'camera'>(options?: CameraPluginOptions<G>): import("ecspresso").Plugin<import("ecspresso").WithResources<import("ecspresso").WithComponents<import("ecspresso").EmptyConfig, CameraComponentTypes>, CameraResourceTypes>, TransformWorldConfig, "camera-follow" | "camera-shake-update" | "camera-bounds" | "camera-state-sync", G, never, never>;
|
package/dist/plugins/camera.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
var q=((C)=>typeof require<"u"?require:typeof Proxy<"u"?new Proxy(C,{get:(J,j)=>(typeof require<"u"?require:J)[j]}):C)(function(C){if(typeof require<"u")return require.apply(this,arguments);throw Error('Dynamic require of "'+C+'" is not supported')});import{definePlugin as G}from"ecspresso";var W={x:0,y:0,zoom:1,rotation:0},v={x:0,y:0,zoom:1,rotation:0,shakeOffsetX:0,shakeOffsetY:0,shakeRotation:0,viewportWidth:800,viewportHeight:600};function F(C=0,J=0,j=1,K=0){return{camera:{x:C,y:J,zoom:j,rotation:K}}}function k(C,J){return{cameraFollow:{target:C,smoothing:J?.smoothing??5,deadzoneX:J?.deadzoneX??0,deadzoneY:J?.deadzoneY??0,offsetX:J?.offsetX??0,offsetY:J?.offsetY??0}}}function X(C){return{cameraShake:{trauma:C?.trauma??0,traumaDecay:C?.traumaDecay??1,maxOffsetX:C?.maxOffsetX??10,maxOffsetY:C?.maxOffsetY??10,maxRotation:C?.maxRotation??0.05}}}function Y(C,J,j,K){return{cameraBounds:{minX:C,minY:J,maxX:j,maxY:K}}}function g(C,J,j){let K=C.getComponent(J,"cameraShake");if(!K)return;K.trauma=Math.min(1,Math.max(0,K.trauma+j))}function u(C,J,j){let K=C-(j.x+j.shakeOffsetX),U=J-(j.y+j.shakeOffsetY),V=-(j.rotation+j.shakeRotation),R=Math.cos(V),N=Math.sin(V),A=K*R-U*N,T=K*N+U*R;return{x:A*j.zoom+j.viewportWidth/2,y:T*j.zoom+j.viewportHeight/2}}function x(C,J,j){let K=(C-j.viewportWidth/2)/j.zoom,U=(J-j.viewportHeight/2)/j.zoom,V=j.rotation+j.shakeRotation,R=Math.cos(V),N=Math.sin(V),A=K*R-U*N,T=K*N+U*R;return{x:A+j.x+j.shakeOffsetX,y:T+j.y+j.shakeOffsetY}}function h(C){let{viewportWidth:J=800,viewportHeight:j=600,systemGroup:K="camera",phase:U="postUpdate",randomFn:V=Math.random}=C??{};return G(
|
|
1
|
+
var q=((C)=>typeof require<"u"?require:typeof Proxy<"u"?new Proxy(C,{get:(J,j)=>(typeof require<"u"?require:J)[j]}):C)(function(C){if(typeof require<"u")return require.apply(this,arguments);throw Error('Dynamic require of "'+C+'" is not supported')});import{definePlugin as G}from"ecspresso";var W={x:0,y:0,zoom:1,rotation:0},v={x:0,y:0,zoom:1,rotation:0,shakeOffsetX:0,shakeOffsetY:0,shakeRotation:0,viewportWidth:800,viewportHeight:600};function F(C=0,J=0,j=1,K=0){return{camera:{x:C,y:J,zoom:j,rotation:K}}}function k(C,J){return{cameraFollow:{target:C,smoothing:J?.smoothing??5,deadzoneX:J?.deadzoneX??0,deadzoneY:J?.deadzoneY??0,offsetX:J?.offsetX??0,offsetY:J?.offsetY??0}}}function X(C){return{cameraShake:{trauma:C?.trauma??0,traumaDecay:C?.traumaDecay??1,maxOffsetX:C?.maxOffsetX??10,maxOffsetY:C?.maxOffsetY??10,maxRotation:C?.maxRotation??0.05}}}function Y(C,J,j,K){return{cameraBounds:{minX:C,minY:J,maxX:j,maxY:K}}}function g(C,J,j){let K=C.getComponent(J,"cameraShake");if(!K)return;K.trauma=Math.min(1,Math.max(0,K.trauma+j))}function u(C,J,j){let K=C-(j.x+j.shakeOffsetX),U=J-(j.y+j.shakeOffsetY),V=-(j.rotation+j.shakeRotation),R=Math.cos(V),N=Math.sin(V),A=K*R-U*N,T=K*N+U*R;return{x:A*j.zoom+j.viewportWidth/2,y:T*j.zoom+j.viewportHeight/2}}function x(C,J,j){let K=(C-j.viewportWidth/2)/j.zoom,U=(J-j.viewportHeight/2)/j.zoom,V=j.rotation+j.shakeRotation,R=Math.cos(V),N=Math.sin(V),A=K*R-U*N,T=K*N+U*R;return{x:A+j.x+j.shakeOffsetX,y:T+j.y+j.shakeOffsetY}}function h(C){let{viewportWidth:J=800,viewportHeight:j=600,systemGroup:K="camera",phase:U="postUpdate",randomFn:V=Math.random}=C??{};return G("camera").withComponentTypes().withResourceTypes().withLabels().withGroups().requires().install((R)=>{R.addResource("cameraState",{x:0,y:0,zoom:1,rotation:0,shakeOffsetX:0,shakeOffsetY:0,shakeRotation:0,viewportWidth:J,viewportHeight:j}),R.addSystem("camera-follow").setPriority(400).inPhase(U).inGroup(K).addQuery("cameras",{with:["camera","cameraFollow"]}).setProcess(({queries:N,dt:A,ecs:T})=>{let Q=Math.min(1,A);for(let O of N.cameras){let{camera:E,cameraFollow:L}=O.components,Z;try{Z=T.getComponent(L.target,"worldTransform")}catch{continue}if(!Z)continue;if(!Z)continue;let P=Z.x+L.offsetX,b=Z.y+L.offsetY,_=P-E.x,$=b-E.y,H=Math.abs(_),S=Math.abs($);if(H>L.deadzoneX){let z=_>0?1:-1,D=_-z*L.deadzoneX,I=Math.min(1,L.smoothing*Q);E.x+=D*I}if(S>L.deadzoneY){let z=$>0?1:-1,D=$-z*L.deadzoneY,I=Math.min(1,L.smoothing*Q);E.y+=D*I}}}),R.addSystem("camera-shake-update").setPriority(390).inPhase(U).inGroup(K).addQuery("shakeCameras",{with:["camera","cameraShake"]}).setProcess(({queries:N,dt:A})=>{for(let T of N.shakeCameras){let{cameraShake:Q}=T.components;Q.trauma=Math.max(0,Q.trauma-Q.traumaDecay*A)}}),R.addSystem("camera-bounds").setPriority(380).inPhase(U).inGroup(K).addQuery("boundedCameras",{with:["camera","cameraBounds"]}).setProcess(({queries:N,ecs:A})=>{let T=A.getResource("cameraState");for(let Q of N.boundedCameras){let{camera:O,cameraBounds:E}=Q.components,L=T.viewportWidth/(2*O.zoom),Z=T.viewportHeight/(2*O.zoom),P=E.minX+L,b=E.maxX-L,_=E.minY+Z,$=E.maxY-Z;if(P>b)O.x=(E.minX+E.maxX)/2;else O.x=Math.max(P,Math.min(b,O.x));if(_>$)O.y=(E.minY+E.maxY)/2;else O.y=Math.max(_,Math.min($,O.y))}}),R.addSystem("camera-state-sync").setPriority(370).inPhase(U).inGroup(K).setProcess(({ecs:N})=>{let A=N.getResource("cameraState"),Q=N.getEntitiesWithQuery(["camera"])[0];if(!Q){A.x=0,A.y=0,A.zoom=1,A.rotation=0,A.shakeOffsetX=0,A.shakeOffsetY=0,A.shakeRotation=0;return}let O=Q.components.camera;A.x=O.x,A.y=O.y,A.zoom=O.zoom,A.rotation=O.rotation;let E=N.getComponent(Q.id,"cameraShake");if(E&&E.trauma>0){let L=E.trauma*E.trauma;A.shakeOffsetX=E.maxOffsetX*L*(V()*2-1),A.shakeOffsetY=E.maxOffsetY*L*(V()*2-1),A.shakeRotation=E.maxRotation*L*(V()*2-1)}else A.shakeOffsetX=0,A.shakeOffsetY=0,A.shakeRotation=0})})}export{u as worldToScreen,x as screenToWorld,X as createCameraShake,h as createCameraPlugin,k as createCameraFollow,Y as createCameraBounds,F as createCamera,g as addTrauma,v as DEFAULT_CAMERA_STATE,W as DEFAULT_CAMERA};
|
|
2
2
|
|
|
3
|
-
//# debugId=
|
|
3
|
+
//# debugId=C5CDFAC2DE81350164756E2164756E21
|
|
4
4
|
//# sourceMappingURL=camera.js.map
|
|
@@ -2,9 +2,9 @@
|
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../src/plugins/camera.ts"],
|
|
4
4
|
"sourcesContent": [
|
|
5
|
-
"/**\n * Camera / Viewport Plugin for ECSpresso\n *\n * Provides a camera entity with world/screen coordinate conversion, smooth follow,\n * trauma-based shake, bounds clamping, and logical viewport dimensions.\n *\n * This plugin is renderer-agnostic. PixiJS or other renderer integration (applying\n * cameraState to a container/stage transform) is the consumer's responsibility.\n *\n * Camera uses its own x/y/zoom/rotation rather than localTransform/worldTransform.\n * It reads the target entity's worldTransform for follow, but doesn't participate\n * in the transform hierarchy itself.\n */\n\nimport { definePlugin, type Plugin } from 'ecspresso';\nimport type { SystemPhase } from 'ecspresso';\nimport type ECSpresso from 'ecspresso';\nimport type { WorldConfigFrom } from '../type-utils';\nimport type { TransformWorldConfig } from './transform';\n\n// ==================== Component Types ====================\n\nexport interface Camera {\n\tx: number;\n\ty: number;\n\tzoom: number;\n\trotation: number;\n}\n\nexport interface CameraFollow {\n\ttarget: number;\n\tsmoothing: number;\n\tdeadzoneX: number;\n\tdeadzoneY: number;\n\toffsetX: number;\n\toffsetY: number;\n}\n\nexport interface CameraShake {\n\ttrauma: number;\n\ttraumaDecay: number;\n\tmaxOffsetX: number;\n\tmaxOffsetY: number;\n\tmaxRotation: number;\n}\n\nexport interface CameraBounds {\n\tminX: number;\n\tminY: number;\n\tmaxX: number;\n\tmaxY: number;\n}\n\nexport interface CameraComponentTypes {\n\tcamera: Camera;\n\tcameraFollow: CameraFollow;\n\tcameraShake: CameraShake;\n\tcameraBounds: CameraBounds;\n}\n\n\n// ==================== Resource Types ====================\n\nexport interface CameraState {\n\tx: number;\n\ty: number;\n\tzoom: number;\n\trotation: number;\n\tshakeOffsetX: number;\n\tshakeOffsetY: number;\n\tshakeRotation: number;\n\tviewportWidth: number;\n\tviewportHeight: number;\n}\n\nexport interface CameraResourceTypes {\n\tcameraState: CameraState;\n}\n\n// ==================== Plugin Options ====================\n\nexport interface CameraPluginOptions<G extends string = 'camera'> {\n\tviewportWidth?: number;\n\tviewportHeight?: number;\n\tsystemGroup?: G;\n\tphase?: SystemPhase;\n\trandomFn?: () => number;\n}\n\n// ==================== Default Values ====================\n\nexport const DEFAULT_CAMERA: Readonly<Camera> = {\n\tx: 0,\n\ty: 0,\n\tzoom: 1,\n\trotation: 0,\n};\n\nexport const DEFAULT_CAMERA_STATE: Readonly<CameraState> = {\n\tx: 0,\n\ty: 0,\n\tzoom: 1,\n\trotation: 0,\n\tshakeOffsetX: 0,\n\tshakeOffsetY: 0,\n\tshakeRotation: 0,\n\tviewportWidth: 800,\n\tviewportHeight: 600,\n};\n\n// ==================== Helper Functions ====================\n\nexport function createCamera(\n\tx = 0,\n\ty = 0,\n\tzoom = 1,\n\trotation = 0,\n): Pick<CameraComponentTypes, 'camera'> {\n\treturn {\n\t\tcamera: { x, y, zoom, rotation },\n\t};\n}\n\nexport function createCameraFollow(\n\ttarget: number,\n\toptions?: Partial<Omit<CameraFollow, 'target'>>,\n): Pick<CameraComponentTypes, 'cameraFollow'> {\n\treturn {\n\t\tcameraFollow: {\n\t\t\ttarget,\n\t\t\tsmoothing: options?.smoothing ?? 5,\n\t\t\tdeadzoneX: options?.deadzoneX ?? 0,\n\t\t\tdeadzoneY: options?.deadzoneY ?? 0,\n\t\t\toffsetX: options?.offsetX ?? 0,\n\t\t\toffsetY: options?.offsetY ?? 0,\n\t\t},\n\t};\n}\n\nexport function createCameraShake(\n\toptions?: Partial<CameraShake>,\n): Pick<CameraComponentTypes, 'cameraShake'> {\n\treturn {\n\t\tcameraShake: {\n\t\t\ttrauma: options?.trauma ?? 0,\n\t\t\ttraumaDecay: options?.traumaDecay ?? 1,\n\t\t\tmaxOffsetX: options?.maxOffsetX ?? 10,\n\t\t\tmaxOffsetY: options?.maxOffsetY ?? 10,\n\t\t\tmaxRotation: options?.maxRotation ?? 0.05,\n\t\t},\n\t};\n}\n\nexport function createCameraBounds(\n\tminX: number,\n\tminY: number,\n\tmaxX: number,\n\tmaxY: number,\n): Pick<CameraComponentTypes, 'cameraBounds'> {\n\treturn {\n\t\tcameraBounds: { minX, minY, maxX, maxY },\n\t};\n}\n\nexport function addTrauma<\n\tCfg extends WorldConfigFrom<CameraComponentTypes, {}, CameraResourceTypes>,\n>(\n\tecs: ECSpresso<Cfg>,\n\tentityId: number,\n\tamount: number,\n): void {\n\tconst shake = ecs.getComponent(entityId, 'cameraShake');\n\tif (!shake) return;\n\tshake.trauma = Math.min(1, Math.max(0, shake.trauma + amount));\n}\n\n// ==================== Coordinate Conversion ====================\n\nexport function worldToScreen(\n\tworldX: number,\n\tworldY: number,\n\tstate: CameraState,\n): { x: number; y: number } {\n\tconst dx = worldX - (state.x + state.shakeOffsetX);\n\tconst dy = worldY - (state.y + state.shakeOffsetY);\n\n\tconst angle = -(state.rotation + state.shakeRotation);\n\tconst cos = Math.cos(angle);\n\tconst sin = Math.sin(angle);\n\tconst rx = dx * cos - dy * sin;\n\tconst ry = dx * sin + dy * cos;\n\n\treturn {\n\t\tx: rx * state.zoom + state.viewportWidth / 2,\n\t\ty: ry * state.zoom + state.viewportHeight / 2,\n\t};\n}\n\nexport function screenToWorld(\n\tscreenX: number,\n\tscreenY: number,\n\tstate: CameraState,\n): { x: number; y: number } {\n\tconst cx = (screenX - state.viewportWidth / 2) / state.zoom;\n\tconst cy = (screenY - state.viewportHeight / 2) / state.zoom;\n\n\tconst angle = state.rotation + state.shakeRotation;\n\tconst cos = Math.cos(angle);\n\tconst sin = Math.sin(angle);\n\tconst rx = cx * cos - cy * sin;\n\tconst ry = cx * sin + cy * cos;\n\n\treturn {\n\t\tx: rx + state.x + state.shakeOffsetX,\n\t\ty: ry + state.y + state.shakeOffsetY,\n\t};\n}\n\n// ==================== Plugin Factory ====================\n\nexport function createCameraPlugin<G extends string = 'camera'>(\n\toptions?: CameraPluginOptions<G>,\n): Plugin<WorldConfigFrom<CameraComponentTypes, {}, CameraResourceTypes>, TransformWorldConfig, 'camera-follow' | 'camera-shake-update' | 'camera-bounds' | 'camera-state-sync', G> {\n\tconst {\n\t\tviewportWidth = 800,\n\t\tviewportHeight = 600,\n\t\tsystemGroup = 'camera',\n\t\tphase = 'postUpdate',\n\t\trandomFn = Math.random,\n\t} = options ?? {};\n\n\treturn definePlugin<WorldConfigFrom<CameraComponentTypes, {}, CameraResourceTypes>, TransformWorldConfig, 'camera-follow' | 'camera-shake-update' | 'camera-bounds' | 'camera-state-sync', G>({\n\t\tid: 'camera',\n\t\tinstall(world) {\n\t\t\tworld.addResource('cameraState', {\n\t\t\t\tx: 0,\n\t\t\t\ty: 0,\n\t\t\t\tzoom: 1,\n\t\t\t\trotation: 0,\n\t\t\t\tshakeOffsetX: 0,\n\t\t\t\tshakeOffsetY: 0,\n\t\t\t\tshakeRotation: 0,\n\t\t\t\tviewportWidth,\n\t\t\t\tviewportHeight,\n\t\t\t});\n\n\t\t\t// camera-follow: priority 400 (after transform propagation at 500)\n\t\t\tworld\n\t\t\t\t.addSystem('camera-follow')\n\t\t\t\t.setPriority(400)\n\t\t\t\t.inPhase(phase)\n\t\t\t\t.inGroup(systemGroup)\n\t\t\t\t.addQuery('cameras', {\n\t\t\t\t\twith: ['camera', 'cameraFollow'],\n\t\t\t\t})\n\t\t\t\t.setProcess(({ queries, dt, ecs }) => {\n\t\t\t\t\tconst t = Math.min(1, dt);\n\t\t\t\t\tfor (const entity of queries.cameras) {\n\t\t\t\t\t\tconst { camera, cameraFollow } = entity.components;\n\t\t\t\t\t\tlet targetWorld;\n\t\t\t\t\t\ttry {\n\t\t\t\t\t\t\ttargetWorld = ecs.getComponent(cameraFollow.target, 'worldTransform');\n\t\t\t\t\t\t} catch {\n\t\t\t\t\t\t\tcontinue;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tif (!targetWorld) continue;\n\t\t\t\t\t\tif (!targetWorld) continue;\n\n\t\t\t\t\t\tconst goalX = targetWorld.x + cameraFollow.offsetX;\n\t\t\t\t\t\tconst goalY = targetWorld.y + cameraFollow.offsetY;\n\t\t\t\t\t\tconst dx = goalX - camera.x;\n\t\t\t\t\t\tconst dy = goalY - camera.y;\n\n\t\t\t\t\t\tconst absDx = Math.abs(dx);\n\t\t\t\t\t\tconst absDy = Math.abs(dy);\n\n\t\t\t\t\t\tif (absDx > cameraFollow.deadzoneX) {\n\t\t\t\t\t\t\tconst sign = dx > 0 ? 1 : -1;\n\t\t\t\t\t\t\tconst excessX = dx - sign * cameraFollow.deadzoneX;\n\t\t\t\t\t\t\tconst factor = Math.min(1, cameraFollow.smoothing * t);\n\t\t\t\t\t\t\tcamera.x += excessX * factor;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tif (absDy > cameraFollow.deadzoneY) {\n\t\t\t\t\t\t\tconst sign = dy > 0 ? 1 : -1;\n\t\t\t\t\t\t\tconst excessY = dy - sign * cameraFollow.deadzoneY;\n\t\t\t\t\t\t\tconst factor = Math.min(1, cameraFollow.smoothing * t);\n\t\t\t\t\t\t\tcamera.y += excessY * factor;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t});\n\n\t\t\t// camera-shake-update: priority 390\n\t\t\tworld\n\t\t\t\t.addSystem('camera-shake-update')\n\t\t\t\t.setPriority(390)\n\t\t\t\t.inPhase(phase)\n\t\t\t\t.inGroup(systemGroup)\n\t\t\t\t.addQuery('shakeCameras', {\n\t\t\t\t\twith: ['camera', 'cameraShake'],\n\t\t\t\t})\n\t\t\t\t.setProcess(({ queries, dt }) => {\n\t\t\t\t\tfor (const entity of queries.shakeCameras) {\n\t\t\t\t\t\tconst { cameraShake } = entity.components;\n\t\t\t\t\t\tcameraShake.trauma = Math.max(0, cameraShake.trauma - cameraShake.traumaDecay * dt);\n\t\t\t\t\t}\n\t\t\t\t});\n\n\t\t\t// camera-bounds: priority 380\n\t\t\tworld\n\t\t\t\t.addSystem('camera-bounds')\n\t\t\t\t.setPriority(380)\n\t\t\t\t.inPhase(phase)\n\t\t\t\t.inGroup(systemGroup)\n\t\t\t\t.addQuery('boundedCameras', {\n\t\t\t\t\twith: ['camera', 'cameraBounds'],\n\t\t\t\t})\n\t\t\t\t.setProcess(({ queries, ecs }) => {\n\t\t\t\t\tconst state = ecs.getResource('cameraState');\n\t\t\t\t\tfor (const entity of queries.boundedCameras) {\n\t\t\t\t\t\tconst { camera, cameraBounds } = entity.components;\n\t\t\t\t\t\tconst halfW = state.viewportWidth / (2 * camera.zoom);\n\t\t\t\t\t\tconst halfH = state.viewportHeight / (2 * camera.zoom);\n\n\t\t\t\t\t\tconst effectiveMinX = cameraBounds.minX + halfW;\n\t\t\t\t\t\tconst effectiveMaxX = cameraBounds.maxX - halfW;\n\t\t\t\t\t\tconst effectiveMinY = cameraBounds.minY + halfH;\n\t\t\t\t\t\tconst effectiveMaxY = cameraBounds.maxY - halfH;\n\n\t\t\t\t\t\tif (effectiveMinX > effectiveMaxX) {\n\t\t\t\t\t\t\tcamera.x = (cameraBounds.minX + cameraBounds.maxX) / 2;\n\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\tcamera.x = Math.max(effectiveMinX, Math.min(effectiveMaxX, camera.x));\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tif (effectiveMinY > effectiveMaxY) {\n\t\t\t\t\t\t\tcamera.y = (cameraBounds.minY + cameraBounds.maxY) / 2;\n\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\tcamera.y = Math.max(effectiveMinY, Math.min(effectiveMaxY, camera.y));\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t});\n\n\t\t\t// camera-state-sync: priority 370\n\t\t\tworld\n\t\t\t\t.addSystem('camera-state-sync')\n\t\t\t\t.setPriority(370)\n\t\t\t\t.inPhase(phase)\n\t\t\t\t.inGroup(systemGroup)\n\t\t\t\t.setProcess(({ ecs }) => {\n\t\t\t\t\tconst state = ecs.getResource('cameraState');\n\t\t\t\t\tconst cameras = ecs.getEntitiesWithQuery(['camera']);\n\t\t\t\t\tconst first = cameras[0];\n\n\t\t\t\t\tif (!first) {\n\t\t\t\t\t\tstate.x = 0;\n\t\t\t\t\t\tstate.y = 0;\n\t\t\t\t\t\tstate.zoom = 1;\n\t\t\t\t\t\tstate.rotation = 0;\n\t\t\t\t\t\tstate.shakeOffsetX = 0;\n\t\t\t\t\t\tstate.shakeOffsetY = 0;\n\t\t\t\t\t\tstate.shakeRotation = 0;\n\t\t\t\t\t\treturn;\n\t\t\t\t\t}\n\n\t\t\t\t\tconst camera = first.components.camera;\n\t\t\t\t\tstate.x = camera.x;\n\t\t\t\t\tstate.y = camera.y;\n\t\t\t\t\tstate.zoom = camera.zoom;\n\t\t\t\t\tstate.rotation = camera.rotation;\n\n\t\t\t\t\tconst shake = ecs.getComponent(first.id, 'cameraShake');\n\t\t\t\t\tif (shake && shake.trauma > 0) {\n\t\t\t\t\t\tconst intensity = shake.trauma * shake.trauma;\n\t\t\t\t\t\tstate.shakeOffsetX = shake.maxOffsetX * intensity * (randomFn() * 2 - 1);\n\t\t\t\t\t\tstate.shakeOffsetY = shake.maxOffsetY * intensity * (randomFn() * 2 - 1);\n\t\t\t\t\t\tstate.shakeRotation = shake.maxRotation * intensity * (randomFn() * 2 - 1);\n\t\t\t\t\t} else {\n\t\t\t\t\t\tstate.shakeOffsetX = 0;\n\t\t\t\t\t\tstate.shakeOffsetY = 0;\n\t\t\t\t\t\tstate.shakeRotation = 0;\n\t\t\t\t\t}\n\t\t\t\t});\n\t\t},\n\t});\n}\n"
|
|
5
|
+
"/**\n * Camera / Viewport Plugin for ECSpresso\n *\n * Provides a camera entity with world/screen coordinate conversion, smooth follow,\n * trauma-based shake, bounds clamping, and logical viewport dimensions.\n *\n * This plugin is renderer-agnostic. PixiJS or other renderer integration (applying\n * cameraState to a container/stage transform) is the consumer's responsibility.\n *\n * Camera uses its own x/y/zoom/rotation rather than localTransform/worldTransform.\n * It reads the target entity's worldTransform for follow, but doesn't participate\n * in the transform hierarchy itself.\n */\n\nimport { definePlugin } from 'ecspresso';\nimport type { SystemPhase } from 'ecspresso';\nimport type ECSpresso from 'ecspresso';\nimport type { WorldConfigFrom } from '../type-utils';\nimport type { TransformWorldConfig } from './transform';\n\n// ==================== Component Types ====================\n\nexport interface Camera {\n\tx: number;\n\ty: number;\n\tzoom: number;\n\trotation: number;\n}\n\nexport interface CameraFollow {\n\ttarget: number;\n\tsmoothing: number;\n\tdeadzoneX: number;\n\tdeadzoneY: number;\n\toffsetX: number;\n\toffsetY: number;\n}\n\nexport interface CameraShake {\n\ttrauma: number;\n\ttraumaDecay: number;\n\tmaxOffsetX: number;\n\tmaxOffsetY: number;\n\tmaxRotation: number;\n}\n\nexport interface CameraBounds {\n\tminX: number;\n\tminY: number;\n\tmaxX: number;\n\tmaxY: number;\n}\n\nexport interface CameraComponentTypes {\n\tcamera: Camera;\n\tcameraFollow: CameraFollow;\n\tcameraShake: CameraShake;\n\tcameraBounds: CameraBounds;\n}\n\n\n// ==================== Resource Types ====================\n\nexport interface CameraState {\n\tx: number;\n\ty: number;\n\tzoom: number;\n\trotation: number;\n\tshakeOffsetX: number;\n\tshakeOffsetY: number;\n\tshakeRotation: number;\n\tviewportWidth: number;\n\tviewportHeight: number;\n}\n\nexport interface CameraResourceTypes {\n\tcameraState: CameraState;\n}\n\n// ==================== Plugin Options ====================\n\nexport interface CameraPluginOptions<G extends string = 'camera'> {\n\tviewportWidth?: number;\n\tviewportHeight?: number;\n\tsystemGroup?: G;\n\tphase?: SystemPhase;\n\trandomFn?: () => number;\n}\n\n// ==================== Default Values ====================\n\nexport const DEFAULT_CAMERA: Readonly<Camera> = {\n\tx: 0,\n\ty: 0,\n\tzoom: 1,\n\trotation: 0,\n};\n\nexport const DEFAULT_CAMERA_STATE: Readonly<CameraState> = {\n\tx: 0,\n\ty: 0,\n\tzoom: 1,\n\trotation: 0,\n\tshakeOffsetX: 0,\n\tshakeOffsetY: 0,\n\tshakeRotation: 0,\n\tviewportWidth: 800,\n\tviewportHeight: 600,\n};\n\n// ==================== Helper Functions ====================\n\nexport function createCamera(\n\tx = 0,\n\ty = 0,\n\tzoom = 1,\n\trotation = 0,\n): Pick<CameraComponentTypes, 'camera'> {\n\treturn {\n\t\tcamera: { x, y, zoom, rotation },\n\t};\n}\n\nexport function createCameraFollow(\n\ttarget: number,\n\toptions?: Partial<Omit<CameraFollow, 'target'>>,\n): Pick<CameraComponentTypes, 'cameraFollow'> {\n\treturn {\n\t\tcameraFollow: {\n\t\t\ttarget,\n\t\t\tsmoothing: options?.smoothing ?? 5,\n\t\t\tdeadzoneX: options?.deadzoneX ?? 0,\n\t\t\tdeadzoneY: options?.deadzoneY ?? 0,\n\t\t\toffsetX: options?.offsetX ?? 0,\n\t\t\toffsetY: options?.offsetY ?? 0,\n\t\t},\n\t};\n}\n\nexport function createCameraShake(\n\toptions?: Partial<CameraShake>,\n): Pick<CameraComponentTypes, 'cameraShake'> {\n\treturn {\n\t\tcameraShake: {\n\t\t\ttrauma: options?.trauma ?? 0,\n\t\t\ttraumaDecay: options?.traumaDecay ?? 1,\n\t\t\tmaxOffsetX: options?.maxOffsetX ?? 10,\n\t\t\tmaxOffsetY: options?.maxOffsetY ?? 10,\n\t\t\tmaxRotation: options?.maxRotation ?? 0.05,\n\t\t},\n\t};\n}\n\nexport function createCameraBounds(\n\tminX: number,\n\tminY: number,\n\tmaxX: number,\n\tmaxY: number,\n): Pick<CameraComponentTypes, 'cameraBounds'> {\n\treturn {\n\t\tcameraBounds: { minX, minY, maxX, maxY },\n\t};\n}\n\nexport function addTrauma<\n\tCfg extends WorldConfigFrom<CameraComponentTypes, {}, CameraResourceTypes>,\n>(\n\tecs: ECSpresso<Cfg>,\n\tentityId: number,\n\tamount: number,\n): void {\n\tconst shake = ecs.getComponent(entityId, 'cameraShake');\n\tif (!shake) return;\n\tshake.trauma = Math.min(1, Math.max(0, shake.trauma + amount));\n}\n\n// ==================== Coordinate Conversion ====================\n\nexport function worldToScreen(\n\tworldX: number,\n\tworldY: number,\n\tstate: CameraState,\n): { x: number; y: number } {\n\tconst dx = worldX - (state.x + state.shakeOffsetX);\n\tconst dy = worldY - (state.y + state.shakeOffsetY);\n\n\tconst angle = -(state.rotation + state.shakeRotation);\n\tconst cos = Math.cos(angle);\n\tconst sin = Math.sin(angle);\n\tconst rx = dx * cos - dy * sin;\n\tconst ry = dx * sin + dy * cos;\n\n\treturn {\n\t\tx: rx * state.zoom + state.viewportWidth / 2,\n\t\ty: ry * state.zoom + state.viewportHeight / 2,\n\t};\n}\n\nexport function screenToWorld(\n\tscreenX: number,\n\tscreenY: number,\n\tstate: CameraState,\n): { x: number; y: number } {\n\tconst cx = (screenX - state.viewportWidth / 2) / state.zoom;\n\tconst cy = (screenY - state.viewportHeight / 2) / state.zoom;\n\n\tconst angle = state.rotation + state.shakeRotation;\n\tconst cos = Math.cos(angle);\n\tconst sin = Math.sin(angle);\n\tconst rx = cx * cos - cy * sin;\n\tconst ry = cx * sin + cy * cos;\n\n\treturn {\n\t\tx: rx + state.x + state.shakeOffsetX,\n\t\ty: ry + state.y + state.shakeOffsetY,\n\t};\n}\n\n// ==================== Plugin Factory ====================\n\nexport function createCameraPlugin<G extends string = 'camera'>(\n\toptions?: CameraPluginOptions<G>,\n) {\n\tconst {\n\t\tviewportWidth = 800,\n\t\tviewportHeight = 600,\n\t\tsystemGroup = 'camera',\n\t\tphase = 'postUpdate',\n\t\trandomFn = Math.random,\n\t} = options ?? {};\n\n\treturn definePlugin('camera')\n\t\t.withComponentTypes<CameraComponentTypes>()\n\t\t.withResourceTypes<CameraResourceTypes>()\n\t\t.withLabels<'camera-follow' | 'camera-shake-update' | 'camera-bounds' | 'camera-state-sync'>()\n\t\t.withGroups<G>()\n\t\t.requires<TransformWorldConfig>()\n\t\t.install((world) => {\n\t\t\tworld.addResource('cameraState', {\n\t\t\t\tx: 0,\n\t\t\t\ty: 0,\n\t\t\t\tzoom: 1,\n\t\t\t\trotation: 0,\n\t\t\t\tshakeOffsetX: 0,\n\t\t\t\tshakeOffsetY: 0,\n\t\t\t\tshakeRotation: 0,\n\t\t\t\tviewportWidth,\n\t\t\t\tviewportHeight,\n\t\t\t});\n\n\t\t\t// camera-follow: priority 400 (after transform propagation at 500)\n\t\t\tworld\n\t\t\t\t.addSystem('camera-follow')\n\t\t\t\t.setPriority(400)\n\t\t\t\t.inPhase(phase)\n\t\t\t\t.inGroup(systemGroup)\n\t\t\t\t.addQuery('cameras', {\n\t\t\t\t\twith: ['camera', 'cameraFollow'],\n\t\t\t\t})\n\t\t\t\t.setProcess(({ queries, dt, ecs }) => {\n\t\t\t\t\tconst t = Math.min(1, dt);\n\t\t\t\t\tfor (const entity of queries.cameras) {\n\t\t\t\t\t\tconst { camera, cameraFollow } = entity.components;\n\t\t\t\t\t\tlet targetWorld;\n\t\t\t\t\t\ttry {\n\t\t\t\t\t\t\ttargetWorld = ecs.getComponent(cameraFollow.target, 'worldTransform');\n\t\t\t\t\t\t} catch {\n\t\t\t\t\t\t\tcontinue;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tif (!targetWorld) continue;\n\t\t\t\t\t\tif (!targetWorld) continue;\n\n\t\t\t\t\t\tconst goalX = targetWorld.x + cameraFollow.offsetX;\n\t\t\t\t\t\tconst goalY = targetWorld.y + cameraFollow.offsetY;\n\t\t\t\t\t\tconst dx = goalX - camera.x;\n\t\t\t\t\t\tconst dy = goalY - camera.y;\n\n\t\t\t\t\t\tconst absDx = Math.abs(dx);\n\t\t\t\t\t\tconst absDy = Math.abs(dy);\n\n\t\t\t\t\t\tif (absDx > cameraFollow.deadzoneX) {\n\t\t\t\t\t\t\tconst sign = dx > 0 ? 1 : -1;\n\t\t\t\t\t\t\tconst excessX = dx - sign * cameraFollow.deadzoneX;\n\t\t\t\t\t\t\tconst factor = Math.min(1, cameraFollow.smoothing * t);\n\t\t\t\t\t\t\tcamera.x += excessX * factor;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tif (absDy > cameraFollow.deadzoneY) {\n\t\t\t\t\t\t\tconst sign = dy > 0 ? 1 : -1;\n\t\t\t\t\t\t\tconst excessY = dy - sign * cameraFollow.deadzoneY;\n\t\t\t\t\t\t\tconst factor = Math.min(1, cameraFollow.smoothing * t);\n\t\t\t\t\t\t\tcamera.y += excessY * factor;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t});\n\n\t\t\t// camera-shake-update: priority 390\n\t\t\tworld\n\t\t\t\t.addSystem('camera-shake-update')\n\t\t\t\t.setPriority(390)\n\t\t\t\t.inPhase(phase)\n\t\t\t\t.inGroup(systemGroup)\n\t\t\t\t.addQuery('shakeCameras', {\n\t\t\t\t\twith: ['camera', 'cameraShake'],\n\t\t\t\t})\n\t\t\t\t.setProcess(({ queries, dt }) => {\n\t\t\t\t\tfor (const entity of queries.shakeCameras) {\n\t\t\t\t\t\tconst { cameraShake } = entity.components;\n\t\t\t\t\t\tcameraShake.trauma = Math.max(0, cameraShake.trauma - cameraShake.traumaDecay * dt);\n\t\t\t\t\t}\n\t\t\t\t});\n\n\t\t\t// camera-bounds: priority 380\n\t\t\tworld\n\t\t\t\t.addSystem('camera-bounds')\n\t\t\t\t.setPriority(380)\n\t\t\t\t.inPhase(phase)\n\t\t\t\t.inGroup(systemGroup)\n\t\t\t\t.addQuery('boundedCameras', {\n\t\t\t\t\twith: ['camera', 'cameraBounds'],\n\t\t\t\t})\n\t\t\t\t.setProcess(({ queries, ecs }) => {\n\t\t\t\t\tconst state = ecs.getResource('cameraState');\n\t\t\t\t\tfor (const entity of queries.boundedCameras) {\n\t\t\t\t\t\tconst { camera, cameraBounds } = entity.components;\n\t\t\t\t\t\tconst halfW = state.viewportWidth / (2 * camera.zoom);\n\t\t\t\t\t\tconst halfH = state.viewportHeight / (2 * camera.zoom);\n\n\t\t\t\t\t\tconst effectiveMinX = cameraBounds.minX + halfW;\n\t\t\t\t\t\tconst effectiveMaxX = cameraBounds.maxX - halfW;\n\t\t\t\t\t\tconst effectiveMinY = cameraBounds.minY + halfH;\n\t\t\t\t\t\tconst effectiveMaxY = cameraBounds.maxY - halfH;\n\n\t\t\t\t\t\tif (effectiveMinX > effectiveMaxX) {\n\t\t\t\t\t\t\tcamera.x = (cameraBounds.minX + cameraBounds.maxX) / 2;\n\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\tcamera.x = Math.max(effectiveMinX, Math.min(effectiveMaxX, camera.x));\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tif (effectiveMinY > effectiveMaxY) {\n\t\t\t\t\t\t\tcamera.y = (cameraBounds.minY + cameraBounds.maxY) / 2;\n\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\tcamera.y = Math.max(effectiveMinY, Math.min(effectiveMaxY, camera.y));\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t});\n\n\t\t\t// camera-state-sync: priority 370\n\t\t\tworld\n\t\t\t\t.addSystem('camera-state-sync')\n\t\t\t\t.setPriority(370)\n\t\t\t\t.inPhase(phase)\n\t\t\t\t.inGroup(systemGroup)\n\t\t\t\t.setProcess(({ ecs }) => {\n\t\t\t\t\tconst state = ecs.getResource('cameraState');\n\t\t\t\t\tconst cameras = ecs.getEntitiesWithQuery(['camera']);\n\t\t\t\t\tconst first = cameras[0];\n\n\t\t\t\t\tif (!first) {\n\t\t\t\t\t\tstate.x = 0;\n\t\t\t\t\t\tstate.y = 0;\n\t\t\t\t\t\tstate.zoom = 1;\n\t\t\t\t\t\tstate.rotation = 0;\n\t\t\t\t\t\tstate.shakeOffsetX = 0;\n\t\t\t\t\t\tstate.shakeOffsetY = 0;\n\t\t\t\t\t\tstate.shakeRotation = 0;\n\t\t\t\t\t\treturn;\n\t\t\t\t\t}\n\n\t\t\t\t\tconst camera = first.components.camera;\n\t\t\t\t\tstate.x = camera.x;\n\t\t\t\t\tstate.y = camera.y;\n\t\t\t\t\tstate.zoom = camera.zoom;\n\t\t\t\t\tstate.rotation = camera.rotation;\n\n\t\t\t\t\tconst shake = ecs.getComponent(first.id, 'cameraShake');\n\t\t\t\t\tif (shake && shake.trauma > 0) {\n\t\t\t\t\t\tconst intensity = shake.trauma * shake.trauma;\n\t\t\t\t\t\tstate.shakeOffsetX = shake.maxOffsetX * intensity * (randomFn() * 2 - 1);\n\t\t\t\t\t\tstate.shakeOffsetY = shake.maxOffsetY * intensity * (randomFn() * 2 - 1);\n\t\t\t\t\t\tstate.shakeRotation = shake.maxRotation * intensity * (randomFn() * 2 - 1);\n\t\t\t\t\t} else {\n\t\t\t\t\t\tstate.shakeOffsetX = 0;\n\t\t\t\t\t\tstate.shakeOffsetY = 0;\n\t\t\t\t\t\tstate.shakeRotation = 0;\n\t\t\t\t\t}\n\t\t\t\t});\n\t\t});\n}\n"
|
|
6
6
|
],
|
|
7
|
-
"mappings": "2PAcA,uBAAS,kBA6EF,IAAM,EAAmC,CAC/C,EAAG,EACH,EAAG,EACH,KAAM,EACN,SAAU,CACX,EAEa,EAA8C,CAC1D,EAAG,EACH,EAAG,EACH,KAAM,EACN,SAAU,EACV,aAAc,EACd,aAAc,EACd,cAAe,EACf,cAAe,IACf,eAAgB,GACjB,EAIO,SAAS,CAAY,CAC3B,EAAI,EACJ,EAAI,EACJ,EAAO,EACP,EAAW,EAC4B,CACvC,MAAO,CACN,OAAQ,CAAE,IAAG,IAAG,OAAM,UAAS,CAChC,EAGM,SAAS,CAAkB,CACjC,EACA,EAC6C,CAC7C,MAAO,CACN,aAAc,CACb,SACA,UAAW,GAAS,WAAa,EACjC,UAAW,GAAS,WAAa,EACjC,UAAW,GAAS,WAAa,EACjC,QAAS,GAAS,SAAW,EAC7B,QAAS,GAAS,SAAW,CAC9B,CACD,EAGM,SAAS,CAAiB,CAChC,EAC4C,CAC5C,MAAO,CACN,YAAa,CACZ,OAAQ,GAAS,QAAU,EAC3B,YAAa,GAAS,aAAe,EACrC,WAAY,GAAS,YAAc,GACnC,WAAY,GAAS,YAAc,GACnC,YAAa,GAAS,aAAe,IACtC,CACD,EAGM,SAAS,CAAkB,CACjC,EACA,EACA,EACA,EAC6C,CAC7C,MAAO,CACN,aAAc,C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