ecspresso 0.12.9 → 0.13.1

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.
Files changed (101) hide show
  1. package/dist/index.js +2 -2
  2. package/dist/index.js.map +5 -5
  3. package/dist/plugin.d.ts +89 -22
  4. package/dist/plugins/ai/detection.d.ts +118 -0
  5. package/dist/plugins/ai/detection.js +4 -0
  6. package/dist/plugins/ai/detection.js.map +10 -0
  7. package/dist/plugins/{audio.d.ts → audio/audio.d.ts} +2 -3
  8. package/dist/plugins/{audio.js → audio/audio.js} +2 -2
  9. package/dist/plugins/audio/audio.js.map +10 -0
  10. package/dist/plugins/combat/health.d.ts +98 -0
  11. package/dist/plugins/combat/health.js +4 -0
  12. package/dist/plugins/combat/health.js.map +10 -0
  13. package/dist/plugins/combat/projectile.d.ts +115 -0
  14. package/dist/plugins/combat/projectile.js +4 -0
  15. package/dist/plugins/combat/projectile.js.map +10 -0
  16. package/dist/plugins/{diagnostics.d.ts → debug/diagnostics.d.ts} +1 -3
  17. package/dist/plugins/debug/diagnostics.js +5 -0
  18. package/dist/plugins/debug/diagnostics.js.map +10 -0
  19. package/dist/plugins/{input.d.ts → input/input.d.ts} +11 -3
  20. package/dist/plugins/input/input.js +4 -0
  21. package/dist/plugins/input/input.js.map +10 -0
  22. package/dist/plugins/input/selection.d.ts +114 -0
  23. package/dist/plugins/input/selection.js +4 -0
  24. package/dist/plugins/input/selection.js.map +11 -0
  25. package/dist/plugins/isometric/depth-sort.d.ts +44 -0
  26. package/dist/plugins/isometric/depth-sort.js +4 -0
  27. package/dist/plugins/isometric/depth-sort.js.map +10 -0
  28. package/dist/plugins/isometric/projection.d.ts +83 -0
  29. package/dist/plugins/isometric/projection.js +4 -0
  30. package/dist/plugins/isometric/projection.js.map +10 -0
  31. package/dist/plugins/{collision.d.ts → physics/collision.d.ts} +10 -9
  32. package/dist/plugins/physics/collision.js +4 -0
  33. package/dist/plugins/physics/collision.js.map +11 -0
  34. package/dist/plugins/{physics2D.d.ts → physics/physics2D.d.ts} +9 -6
  35. package/dist/plugins/physics/physics2D.js +4 -0
  36. package/dist/plugins/physics/physics2D.js.map +11 -0
  37. package/dist/plugins/physics/steering.d.ts +102 -0
  38. package/dist/plugins/physics/steering.js +4 -0
  39. package/dist/plugins/physics/steering.js.map +10 -0
  40. package/dist/plugins/{particles.d.ts → rendering/particles.d.ts} +4 -4
  41. package/dist/plugins/{particles.js → rendering/particles.js} +2 -2
  42. package/dist/plugins/rendering/particles.js.map +10 -0
  43. package/dist/plugins/{renderers → rendering}/renderer2D.d.ts +45 -14
  44. package/dist/plugins/rendering/renderer2D.js +4 -0
  45. package/dist/plugins/rendering/renderer2D.js.map +10 -0
  46. package/dist/plugins/{sprite-animation.d.ts → rendering/sprite-animation.d.ts} +2 -3
  47. package/dist/plugins/{sprite-animation.js → rendering/sprite-animation.js} +2 -2
  48. package/dist/plugins/rendering/sprite-animation.js.map +10 -0
  49. package/dist/plugins/{coroutine.d.ts → scripting/coroutine.d.ts} +2 -3
  50. package/dist/plugins/{coroutine.js → scripting/coroutine.js} +2 -2
  51. package/dist/plugins/scripting/coroutine.js.map +10 -0
  52. package/dist/plugins/{state-machine.d.ts → scripting/state-machine.d.ts} +2 -3
  53. package/dist/plugins/{state-machine.js → scripting/state-machine.js} +2 -2
  54. package/dist/plugins/scripting/state-machine.js.map +10 -0
  55. package/dist/plugins/{timers.d.ts → scripting/timers.d.ts} +2 -3
  56. package/dist/plugins/scripting/timers.js +4 -0
  57. package/dist/plugins/scripting/timers.js.map +10 -0
  58. package/dist/plugins/{tween.d.ts → scripting/tween.d.ts} +3 -4
  59. package/dist/plugins/{tween.js → scripting/tween.js} +2 -2
  60. package/dist/plugins/scripting/tween.js.map +11 -0
  61. package/dist/plugins/{bounds.d.ts → spatial/bounds.d.ts} +2 -3
  62. package/dist/plugins/spatial/bounds.js +4 -0
  63. package/dist/plugins/spatial/bounds.js.map +10 -0
  64. package/dist/plugins/{camera.d.ts → spatial/camera.d.ts} +43 -13
  65. package/dist/plugins/spatial/camera.js +4 -0
  66. package/dist/plugins/spatial/camera.js.map +10 -0
  67. package/dist/plugins/{spatial-index.d.ts → spatial/spatial-index.d.ts} +3 -6
  68. package/dist/plugins/spatial/spatial-index.js +4 -0
  69. package/dist/plugins/spatial/spatial-index.js.map +11 -0
  70. package/dist/plugins/{transform.d.ts → spatial/transform.d.ts} +3 -3
  71. package/dist/plugins/spatial/transform.js +4 -0
  72. package/dist/plugins/spatial/transform.js.map +10 -0
  73. package/dist/utils/narrowphase.d.ts +60 -19
  74. package/dist/utils/spatial-hash.d.ts +11 -1
  75. package/package.json +80 -49
  76. package/dist/plugins/audio.js.map +0 -10
  77. package/dist/plugins/bounds.js +0 -4
  78. package/dist/plugins/bounds.js.map +0 -10
  79. package/dist/plugins/camera.js +0 -4
  80. package/dist/plugins/camera.js.map +0 -10
  81. package/dist/plugins/collision.js +0 -4
  82. package/dist/plugins/collision.js.map +0 -11
  83. package/dist/plugins/coroutine.js.map +0 -10
  84. package/dist/plugins/diagnostics.js +0 -5
  85. package/dist/plugins/diagnostics.js.map +0 -10
  86. package/dist/plugins/input.js +0 -4
  87. package/dist/plugins/input.js.map +0 -10
  88. package/dist/plugins/particles.js.map +0 -10
  89. package/dist/plugins/physics2D.js +0 -4
  90. package/dist/plugins/physics2D.js.map +0 -11
  91. package/dist/plugins/renderers/renderer2D.js +0 -4
  92. package/dist/plugins/renderers/renderer2D.js.map +0 -10
  93. package/dist/plugins/spatial-index.js +0 -4
  94. package/dist/plugins/spatial-index.js.map +0 -11
  95. package/dist/plugins/sprite-animation.js.map +0 -10
  96. package/dist/plugins/state-machine.js.map +0 -10
  97. package/dist/plugins/timers.js +0 -4
  98. package/dist/plugins/timers.js.map +0 -10
  99. package/dist/plugins/transform.js +0 -4
  100. package/dist/plugins/transform.js.map +0 -10
  101. package/dist/plugins/tween.js.map +0 -11
@@ -0,0 +1,44 @@
1
+ /**
2
+ * Isometric Depth Sort Plugin for ECSpresso
3
+ *
4
+ * Sets PixiJS `zIndex` on entities based on their world-space position,
5
+ * ensuring correct visual overlap in isometric rendering. Entities with
6
+ * higher world X + Y values render in front.
7
+ *
8
+ * Requires `rootContainer` from the renderer2D plugin.
9
+ * Enables `sortableChildren` on the root container at initialization.
10
+ */
11
+ import type { BasePluginOptions } from 'ecspresso';
12
+ import type { WorldConfigFrom } from '../../type-utils';
13
+ import type { TransformComponentTypes } from '../spatial/transform';
14
+ import type { Renderer2DComponentTypes, Renderer2DResourceTypes } from '../rendering/renderer2D';
15
+ /**
16
+ * Optional component that offsets an entity's depth sort value.
17
+ * Entities with a positive depthOffset render in front of entities
18
+ * at the same world position (e.g., a player on top of a ground tile).
19
+ */
20
+ export interface IsoDepthSortComponentTypes {
21
+ depthOffset: number;
22
+ }
23
+ type IsoDepthSortRequires = WorldConfigFrom<TransformComponentTypes & Pick<Renderer2DComponentTypes, 'sprite' | 'graphics' | 'container'>, {}, Renderer2DResourceTypes>;
24
+ export interface IsoDepthSortPluginOptions<G extends string = 'isometric'> extends BasePluginOptions<G> {
25
+ /** Custom depth function. Receives world-space x/y, returns a sort key.
26
+ * Default: `(x, y) => x + y` */
27
+ depthFn?: (worldX: number, worldY: number) => number;
28
+ }
29
+ /**
30
+ * Create an isometric depth sort plugin.
31
+ *
32
+ * Adds a render-phase system that sets PixiJS `zIndex` based on world-space
33
+ * position, enabling correct front-to-back ordering in isometric views.
34
+ *
35
+ * @example
36
+ * ```typescript
37
+ * const ecs = ECSpresso.create()
38
+ * .withPlugin(createRenderer2DPlugin({ ... }))
39
+ * .withPlugin(createIsoDepthSortPlugin())
40
+ * .build();
41
+ * ```
42
+ */
43
+ export declare function createIsoDepthSortPlugin<G extends string = 'isometric'>(options?: IsoDepthSortPluginOptions<G>): import("ecspresso").Plugin<import("ecspresso").WithComponents<import("ecspresso").EmptyConfig, IsoDepthSortComponentTypes>, IsoDepthSortRequires, never, G, never, never>;
44
+ export {};
@@ -0,0 +1,4 @@
1
+ var I=((b)=>typeof require<"u"?require:typeof Proxy<"u"?new Proxy(b,{get:(k,B)=>(typeof require<"u"?require:k)[B]}):b)(function(b){if(typeof require<"u")return require.apply(this,arguments);throw Error('Dynamic require of "'+b+'" is not supported')});import{definePlugin as E}from"ecspresso";function H(b,k){return b+k}function L(b){let{depthFn:k=H,systemGroup:B="isometric"}=b??{};return E("isometric-depth-sort").withComponentTypes().requires().withGroups().install((C)=>{C.addSystem("isometric-depth-sort-init").inGroup(B).setOnInitialize((x)=>{let v=x.getResource("rootContainer");v.sortableChildren=!0}),C.addSystem("isometric-depth-sort").setPriority(350).inPhase("render").inGroup(B).addQuery("sprites",{with:["sprite","worldTransform"],changed:["worldTransform"],optional:["depthOffset"]}).addQuery("graphics",{with:["graphics","worldTransform"],changed:["worldTransform"],optional:["depthOffset"]}).addQuery("containers",{with:["container","worldTransform"],changed:["worldTransform"],optional:["depthOffset"]}).setProcess(({queries:x})=>{for(let v of x.sprites){let{sprite:z,worldTransform:j,depthOffset:A}=v.components;z.zIndex=k(j.x,j.y)+(A??0)}for(let v of x.graphics){let{graphics:z,worldTransform:j,depthOffset:A}=v.components;z.zIndex=k(j.x,j.y)+(A??0)}for(let v of x.containers){let{container:z,worldTransform:j,depthOffset:A}=v.components;z.zIndex=k(j.x,j.y)+(A??0)}})})}export{L as createIsoDepthSortPlugin};
2
+
3
+ //# debugId=E8C9DCC3EA4239A864756E2164756E21
4
+ //# sourceMappingURL=depth-sort.js.map
@@ -0,0 +1,10 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../src/plugins/isometric/depth-sort.ts"],
4
+ "sourcesContent": [
5
+ "/**\n * Isometric Depth Sort Plugin for ECSpresso\n *\n * Sets PixiJS `zIndex` on entities based on their world-space position,\n * ensuring correct visual overlap in isometric rendering. Entities with\n * higher world X + Y values render in front.\n *\n * Requires `rootContainer` from the renderer2D plugin.\n * Enables `sortableChildren` on the root container at initialization.\n */\n\nimport { definePlugin } from 'ecspresso';\nimport type { BasePluginOptions } from 'ecspresso';\nimport type { WorldConfigFrom } from '../../type-utils';\nimport type { TransformComponentTypes } from '../spatial/transform';\nimport type { Renderer2DComponentTypes, Renderer2DResourceTypes } from '../rendering/renderer2D';\n\n// ==================== Component Types ====================\n\n/**\n * Optional component that offsets an entity's depth sort value.\n * Entities with a positive depthOffset render in front of entities\n * at the same world position (e.g., a player on top of a ground tile).\n */\nexport interface IsoDepthSortComponentTypes {\n\tdepthOffset: number;\n}\n\ntype IsoDepthSortRequires = WorldConfigFrom<\n\tTransformComponentTypes & Pick<Renderer2DComponentTypes, 'sprite' | 'graphics' | 'container'>,\n\t{},\n\tRenderer2DResourceTypes\n>;\n\n// ==================== Plugin Options ====================\n\nexport interface IsoDepthSortPluginOptions<G extends string = 'isometric'> extends BasePluginOptions<G> {\n\t/** Custom depth function. Receives world-space x/y, returns a sort key.\n\t * Default: `(x, y) => x + y` */\n\tdepthFn?: (worldX: number, worldY: number) => number;\n}\n\n// ==================== Default Depth Function ====================\n\nfunction defaultDepthFn(worldX: number, worldY: number): number {\n\treturn worldX + worldY;\n}\n\n// ==================== Plugin Factory ====================\n\n/**\n * Create an isometric depth sort plugin.\n *\n * Adds a render-phase system that sets PixiJS `zIndex` based on world-space\n * position, enabling correct front-to-back ordering in isometric views.\n *\n * @example\n * ```typescript\n * const ecs = ECSpresso.create()\n * .withPlugin(createRenderer2DPlugin({ ... }))\n * .withPlugin(createIsoDepthSortPlugin())\n * .build();\n * ```\n */\nexport function createIsoDepthSortPlugin<G extends string = 'isometric'>(\n\toptions?: IsoDepthSortPluginOptions<G>,\n) {\n\tconst {\n\t\tdepthFn = defaultDepthFn,\n\t\tsystemGroup = 'isometric',\n\t} = options ?? {};\n\n\treturn definePlugin('isometric-depth-sort')\n\t\t.withComponentTypes<IsoDepthSortComponentTypes>()\n\t\t.requires<IsoDepthSortRequires>()\n\t\t.withGroups<G>()\n\t\t.install((world) => {\n\t\t\t// ==================== Init: Enable Sorting ====================\n\n\t\t\tworld\n\t\t\t\t.addSystem('isometric-depth-sort-init')\n\t\t\t\t.inGroup(systemGroup)\n\t\t\t\t.setOnInitialize((ecs) => {\n\t\t\t\t\tconst root = ecs.getResource('rootContainer');\n\t\t\t\t\troot.sortableChildren = true;\n\t\t\t\t});\n\n\t\t\t// ==================== Depth Sort System ====================\n\n\t\t\tworld\n\t\t\t\t.addSystem('isometric-depth-sort')\n\t\t\t\t.setPriority(350)\n\t\t\t\t.inPhase('render')\n\t\t\t\t.inGroup(systemGroup)\n\t\t\t\t.addQuery('sprites', {\n\t\t\t\t\twith: ['sprite', 'worldTransform'],\n\t\t\t\t\tchanged: ['worldTransform'],\n\t\t\t\t\toptional: ['depthOffset'],\n\t\t\t\t})\n\t\t\t\t.addQuery('graphics', {\n\t\t\t\t\twith: ['graphics', 'worldTransform'],\n\t\t\t\t\tchanged: ['worldTransform'],\n\t\t\t\t\toptional: ['depthOffset'],\n\t\t\t\t})\n\t\t\t\t.addQuery('containers', {\n\t\t\t\t\twith: ['container', 'worldTransform'],\n\t\t\t\t\tchanged: ['worldTransform'],\n\t\t\t\t\toptional: ['depthOffset'],\n\t\t\t\t})\n\t\t\t\t.setProcess(({ queries }) => {\n\t\t\t\t\tfor (const entity of queries.sprites) {\n\t\t\t\t\t\tconst { sprite, worldTransform, depthOffset } = entity.components;\n\t\t\t\t\t\tsprite.zIndex = depthFn(worldTransform.x, worldTransform.y) + (depthOffset ?? 0);\n\t\t\t\t\t}\n\n\t\t\t\t\tfor (const entity of queries.graphics) {\n\t\t\t\t\t\tconst { graphics, worldTransform, depthOffset } = entity.components;\n\t\t\t\t\t\tgraphics.zIndex = depthFn(worldTransform.x, worldTransform.y) + (depthOffset ?? 0);\n\t\t\t\t\t}\n\n\t\t\t\t\tfor (const entity of queries.containers) {\n\t\t\t\t\t\tconst { container, worldTransform, depthOffset } = entity.components;\n\t\t\t\t\t\tcontainer.zIndex = depthFn(worldTransform.x, worldTransform.y) + (depthOffset ?? 0);\n\t\t\t\t\t}\n\t\t\t\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Q,EAAe,EAAG,EAAe,CAAC,GAAK,GAAe,IAElF,EACF",
8
+ "debugId": "E8C9DCC3EA4239A864756E2164756E21",
9
+ "names": []
10
+ }
@@ -0,0 +1,83 @@
1
+ /**
2
+ * Isometric Projection Plugin for ECSpresso
3
+ *
4
+ * Converts Cartesian world-space positions to isometric screen positions
5
+ * in the render phase. All ECS-level logic (physics, collision, camera follow)
6
+ * continues to operate in Cartesian world space — only PixiJS display object
7
+ * positions are projected.
8
+ *
9
+ * Optionally provides an isometric-aware camera sync system that projects
10
+ * the camera position before applying it to the root container.
11
+ */
12
+ import type { BasePluginOptions } from 'ecspresso';
13
+ import type { WorldConfigFrom } from '../../type-utils';
14
+ import type { TransformComponentTypes } from '../spatial/transform';
15
+ import type { Renderer2DComponentTypes, Renderer2DResourceTypes } from '../rendering/renderer2D';
16
+ import type { CameraResourceTypes } from '../spatial/camera';
17
+ /**
18
+ * Isometric projection configuration.
19
+ */
20
+ export interface IsoProjectionState {
21
+ readonly tileWidth: number;
22
+ readonly tileHeight: number;
23
+ readonly originX: number;
24
+ readonly originY: number;
25
+ }
26
+ export interface IsoProjectionResourceTypes {
27
+ isoProjection: IsoProjectionState;
28
+ }
29
+ type IsoProjectionRequires = WorldConfigFrom<TransformComponentTypes & Pick<Renderer2DComponentTypes, 'sprite' | 'graphics' | 'container'>, {}, Renderer2DResourceTypes & CameraResourceTypes>;
30
+ export interface IsoProjectionPluginOptions<G extends string = 'isometric'> extends BasePluginOptions<G> {
31
+ /** Tile width in pixels (default: 64) */
32
+ tileWidth?: number;
33
+ /** Tile height in pixels (default: 32) */
34
+ tileHeight?: number;
35
+ /** Screen-space X origin offset (default: 0) */
36
+ originX?: number;
37
+ /** Screen-space Y origin offset (default: 0) */
38
+ originY?: number;
39
+ /** Register an isometric-aware camera sync system (default: false).
40
+ * When true, set `camera: false` on createRenderer2DPlugin to avoid conflicts. */
41
+ camera?: boolean;
42
+ }
43
+ /**
44
+ * Convert Cartesian world coordinates to isometric screen coordinates.
45
+ *
46
+ * @param worldX World-space X coordinate
47
+ * @param worldY World-space Y coordinate
48
+ * @param state Isometric projection state
49
+ * @returns New object with projected { x, y }
50
+ */
51
+ export declare function worldToIso(worldX: number, worldY: number, state: IsoProjectionState): {
52
+ x: number;
53
+ y: number;
54
+ };
55
+ /**
56
+ * Convert isometric screen coordinates back to Cartesian world coordinates.
57
+ *
58
+ * @param isoX Isometric screen-space X coordinate
59
+ * @param isoY Isometric screen-space Y coordinate
60
+ * @param state Isometric projection state
61
+ * @returns New object with world-space { x, y }
62
+ */
63
+ export declare function isoToWorld(isoX: number, isoY: number, state: IsoProjectionState): {
64
+ x: number;
65
+ y: number;
66
+ };
67
+ /**
68
+ * Create an isometric projection plugin.
69
+ *
70
+ * Adds a render-phase system that overwrites PixiJS display object positions
71
+ * with isometric projections of their `worldTransform` coordinates.
72
+ *
73
+ * @example
74
+ * ```typescript
75
+ * const ecs = ECSpresso.create()
76
+ * .withPlugin(createRenderer2DPlugin({ camera: false, ... }))
77
+ * .withPlugin(createCameraPlugin({ ... }))
78
+ * .withPlugin(createIsoProjectionPlugin({ tileWidth: 64, tileHeight: 32, camera: true }))
79
+ * .build();
80
+ * ```
81
+ */
82
+ export declare function createIsoProjectionPlugin<G extends string = 'isometric'>(options?: IsoProjectionPluginOptions<G>): import("ecspresso").Plugin<import("ecspresso").WithResources<import("ecspresso").EmptyConfig, IsoProjectionResourceTypes>, IsoProjectionRequires, never, G, never, never>;
83
+ export {};
@@ -0,0 +1,4 @@
1
+ var V=((k)=>typeof require<"u"?require:typeof Proxy<"u"?new Proxy(k,{get:(x,b)=>(typeof require<"u"?require:x)[b]}):k)(function(k){if(typeof require<"u")return require.apply(this,arguments);throw Error('Dynamic require of "'+k+'" is not supported')});import{definePlugin as U}from"ecspresso";var O={x:0,y:0};function L(k,x,b,z,A,M){return O.x=(k-x)*b+A,O.y=(k+x)*z+M,O}function C(k,x,b){return{x:(k-x)*(b.tileWidth/2)+b.originX,y:(k+x)*(b.tileHeight/2)+b.originY}}function S(k,x,b){let z=k-b.originX,A=x-b.originY;return{x:z/b.tileWidth+A/b.tileHeight,y:-z/b.tileWidth+A/b.tileHeight}}function G(k){let{tileWidth:x=64,tileHeight:b=32,originX:z=0,originY:A=0,camera:M=!1,systemGroup:Q="isometric"}=k??{};return U("isometric-projection").withResourceTypes().requires().withGroups().install((N)=>{let J=x/2,K=b/2;if(N.addResource("isoProjection",{tileWidth:x,tileHeight:b,originX:z,originY:A}),N.addSystem("isometric-projection").setPriority(400).inPhase("render").inGroup(Q).addQuery("sprites",{with:["sprite","worldTransform"],changed:["worldTransform"]}).addQuery("graphics",{with:["graphics","worldTransform"],changed:["worldTransform"]}).addQuery("containers",{with:["container","worldTransform"],changed:["worldTransform"]}).setProcess(({queries:v})=>{for(let E of v.sprites){let{sprite:F,worldTransform:B}=E.components,D=L(B.x,B.y,J,K,z,A);F.position.set(D.x,D.y)}for(let E of v.graphics){let{graphics:F,worldTransform:B}=E.components,D=L(B.x,B.y,J,K,z,A);F.position.set(D.x,D.y)}for(let E of v.containers){let{container:F,worldTransform:B}=E.components,D=L(B.x,B.y,J,K,z,A);F.position.set(D.x,D.y)}}),M)N.addSystem("isometric-camera-sync").setPriority(900).inPhase("render").inGroup(Q).withResources(["cameraState","rootContainer","pixiApp"]).setProcess(({resources:{cameraState:v,rootContainer:E,pixiApp:F}})=>{let B=F.screen.width,D=F.screen.height,R=L(v.x+v.shakeOffsetX,v.y+v.shakeOffsetY,J,K,z,A);E.position.set(B/2-R.x*v.zoom,D/2-R.y*v.zoom),E.scale.set(v.zoom),E.rotation=-(v.rotation+v.shakeRotation)})})}export{C as worldToIso,S as isoToWorld,G as createIsoProjectionPlugin};
2
+
3
+ //# debugId=D8E00FC32D5B431064756E2164756E21
4
+ //# sourceMappingURL=projection.js.map
@@ -0,0 +1,10 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../src/plugins/isometric/projection.ts"],
4
+ "sourcesContent": [
5
+ "/**\n * Isometric Projection Plugin for ECSpresso\n *\n * Converts Cartesian world-space positions to isometric screen positions\n * in the render phase. All ECS-level logic (physics, collision, camera follow)\n * continues to operate in Cartesian world space — only PixiJS display object\n * positions are projected.\n *\n * Optionally provides an isometric-aware camera sync system that projects\n * the camera position before applying it to the root container.\n */\n\nimport { definePlugin } from 'ecspresso';\nimport type { BasePluginOptions } from 'ecspresso';\nimport type { WorldConfigFrom } from '../../type-utils';\nimport type { TransformComponentTypes } from '../spatial/transform';\nimport type { Renderer2DComponentTypes, Renderer2DResourceTypes } from '../rendering/renderer2D';\nimport type { CameraResourceTypes } from '../spatial/camera';\n\n// ==================== Types ====================\n\n/**\n * Isometric projection configuration.\n */\nexport interface IsoProjectionState {\n\treadonly tileWidth: number;\n\treadonly tileHeight: number;\n\treadonly originX: number;\n\treadonly originY: number;\n}\n\nexport interface IsoProjectionResourceTypes {\n\tisoProjection: IsoProjectionState;\n}\n\ntype IsoProjectionRequires = WorldConfigFrom<\n\tTransformComponentTypes & Pick<Renderer2DComponentTypes, 'sprite' | 'graphics' | 'container'>,\n\t{},\n\tRenderer2DResourceTypes & CameraResourceTypes\n>;\n\n// ==================== Plugin Options ====================\n\nexport interface IsoProjectionPluginOptions<G extends string = 'isometric'> extends BasePluginOptions<G> {\n\t/** Tile width in pixels (default: 64) */\n\ttileWidth?: number;\n\t/** Tile height in pixels (default: 32) */\n\ttileHeight?: number;\n\t/** Screen-space X origin offset (default: 0) */\n\toriginX?: number;\n\t/** Screen-space Y origin offset (default: 0) */\n\toriginY?: number;\n\t/** Register an isometric-aware camera sync system (default: false).\n\t * When true, set `camera: false` on createRenderer2DPlugin to avoid conflicts. */\n\tcamera?: boolean;\n}\n\n// ==================== Coordinate Conversion ====================\n\n// Pre-allocated point for hot-path use — returned by reference, consumed immediately by callers\nconst _tempPoint = { x: 0, y: 0 };\n\nfunction worldToIsoInto(\n\tworldX: number,\n\tworldY: number,\n\thalfW: number,\n\thalfH: number,\n\toriginX: number,\n\toriginY: number,\n): { x: number; y: number } {\n\t_tempPoint.x = (worldX - worldY) * halfW + originX;\n\t_tempPoint.y = (worldX + worldY) * halfH + originY;\n\treturn _tempPoint;\n}\n\n/**\n * Convert Cartesian world coordinates to isometric screen coordinates.\n *\n * @param worldX World-space X coordinate\n * @param worldY World-space Y coordinate\n * @param state Isometric projection state\n * @returns New object with projected { x, y }\n */\nexport function worldToIso(\n\tworldX: number,\n\tworldY: number,\n\tstate: IsoProjectionState,\n): { x: number; y: number } {\n\treturn {\n\t\tx: (worldX - worldY) * (state.tileWidth / 2) + state.originX,\n\t\ty: (worldX + worldY) * (state.tileHeight / 2) + state.originY,\n\t};\n}\n\n/**\n * Convert isometric screen coordinates back to Cartesian world coordinates.\n *\n * @param isoX Isometric screen-space X coordinate\n * @param isoY Isometric screen-space Y coordinate\n * @param state Isometric projection state\n * @returns New object with world-space { x, y }\n */\nexport function isoToWorld(\n\tisoX: number,\n\tisoY: number,\n\tstate: IsoProjectionState,\n): { x: number; y: number } {\n\tconst relX = isoX - state.originX;\n\tconst relY = isoY - state.originY;\n\treturn {\n\t\tx: relX / state.tileWidth + relY / state.tileHeight,\n\t\ty: -relX / state.tileWidth + relY / state.tileHeight,\n\t};\n}\n\n// ==================== Plugin Factory ====================\n\n/**\n * Create an isometric projection plugin.\n *\n * Adds a render-phase system that overwrites PixiJS display object positions\n * with isometric projections of their `worldTransform` coordinates.\n *\n * @example\n * ```typescript\n * const ecs = ECSpresso.create()\n * .withPlugin(createRenderer2DPlugin({ camera: false, ... }))\n * .withPlugin(createCameraPlugin({ ... }))\n * .withPlugin(createIsoProjectionPlugin({ tileWidth: 64, tileHeight: 32, camera: true }))\n * .build();\n * ```\n */\nexport function createIsoProjectionPlugin<G extends string = 'isometric'>(\n\toptions?: IsoProjectionPluginOptions<G>,\n) {\n\tconst {\n\t\ttileWidth = 64,\n\t\ttileHeight = 32,\n\t\toriginX = 0,\n\t\toriginY = 0,\n\t\tcamera = false,\n\t\tsystemGroup = 'isometric',\n\t} = options ?? {};\n\n\treturn definePlugin('isometric-projection')\n\t\t.withResourceTypes<IsoProjectionResourceTypes>()\n\t\t.requires<IsoProjectionRequires>()\n\t\t.withGroups<G>()\n\t\t.install((world) => {\n\t\t\tconst halfW = tileWidth / 2;\n\t\t\tconst halfH = tileHeight / 2;\n\n\t\t\tworld.addResource('isoProjection', {\n\t\t\t\ttileWidth,\n\t\t\t\ttileHeight,\n\t\t\t\toriginX,\n\t\t\t\toriginY,\n\t\t\t});\n\n\t\t\t// ==================== Projection System ====================\n\n\t\t\tworld\n\t\t\t\t.addSystem('isometric-projection')\n\t\t\t\t.setPriority(400)\n\t\t\t\t.inPhase('render')\n\t\t\t\t.inGroup(systemGroup)\n\t\t\t\t.addQuery('sprites', {\n\t\t\t\t\twith: ['sprite', 'worldTransform'],\n\t\t\t\t\tchanged: ['worldTransform'],\n\t\t\t\t})\n\t\t\t\t.addQuery('graphics', {\n\t\t\t\t\twith: ['graphics', 'worldTransform'],\n\t\t\t\t\tchanged: ['worldTransform'],\n\t\t\t\t})\n\t\t\t\t.addQuery('containers', {\n\t\t\t\t\twith: ['container', 'worldTransform'],\n\t\t\t\t\tchanged: ['worldTransform'],\n\t\t\t\t})\n\t\t\t\t.setProcess(({ queries }) => {\n\t\t\t\t\tfor (const entity of queries.sprites) {\n\t\t\t\t\t\tconst { sprite, worldTransform } = entity.components;\n\t\t\t\t\t\tconst projected = worldToIsoInto(worldTransform.x, worldTransform.y, halfW, halfH, originX, originY);\n\t\t\t\t\t\tsprite.position.set(projected.x, projected.y);\n\t\t\t\t\t}\n\n\t\t\t\t\tfor (const entity of queries.graphics) {\n\t\t\t\t\t\tconst { graphics, worldTransform } = entity.components;\n\t\t\t\t\t\tconst projected = worldToIsoInto(worldTransform.x, worldTransform.y, halfW, halfH, originX, originY);\n\t\t\t\t\t\tgraphics.position.set(projected.x, projected.y);\n\t\t\t\t\t}\n\n\t\t\t\t\tfor (const entity of queries.containers) {\n\t\t\t\t\t\tconst { container, worldTransform } = entity.components;\n\t\t\t\t\t\tconst projected = worldToIsoInto(worldTransform.x, worldTransform.y, halfW, halfH, originX, originY);\n\t\t\t\t\t\tcontainer.position.set(projected.x, projected.y);\n\t\t\t\t\t}\n\t\t\t\t});\n\n\t\t\t// ==================== Isometric Camera Sync (opt-in) ====================\n\n\t\t\tif (camera) {\n\t\t\t\tworld\n\t\t\t\t\t.addSystem('isometric-camera-sync')\n\t\t\t\t\t.setPriority(900)\n\t\t\t\t\t.inPhase('render')\n\t\t\t\t\t.inGroup(systemGroup)\n\t\t\t\t\t.withResources(['cameraState', 'rootContainer', 'pixiApp'])\n\t\t\t\t\t.setProcess(({ resources: { cameraState: state, rootContainer: root, pixiApp: app } }) => {\n\t\t\t\t\t\tconst centerW = app.screen.width;\n\t\t\t\t\t\tconst centerH = app.screen.height;\n\n\t\t\t\t\t\tconst camIso = worldToIsoInto(\n\t\t\t\t\t\t\tstate.x + state.shakeOffsetX,\n\t\t\t\t\t\t\tstate.y + state.shakeOffsetY,\n\t\t\t\t\t\t\thalfW, halfH, originX, originY,\n\t\t\t\t\t\t);\n\n\t\t\t\t\t\troot.position.set(\n\t\t\t\t\t\t\tcenterW / 2 - camIso.x * state.zoom,\n\t\t\t\t\t\t\tcenterH / 2 - camIso.y * state.zoom,\n\t\t\t\t\t\t);\n\t\t\t\t\t\troot.scale.set(state.zoom);\n\t\t\t\t\t\troot.rotation = -(state.rotation + state.shakeRotation);\n\t\t\t\t\t});\n\t\t\t}\n\t\t});\n}\n"
6
+ ],
7
+ "mappings": "2PAYA,uBAAS,kBAgDT,IAAM,EAAa,CAAE,EAAG,EAAG,EAAG,CAAE,EAEhC,SAAS,CAAc,CACtB,EACA,EACA,EACA,EACA,EACA,EAC2B,CAG3B,OAFA,EAAW,GAAK,EAAS,GAAU,EAAQ,EAC3C,EAAW,GAAK,EAAS,GAAU,EAAQ,EACpC,EAWD,SAAS,CAAU,CACzB,EACA,EACA,EAC2B,CAC3B,MAAO,CACN,GAAI,EAAS,IAAW,EAAM,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,EAAe,EAAe,EAAG,EAAe,EAAG,EAAO,EAAO,EAAS,CAAO,EACnG,EAAS,SAAS,IAAI,EAAU,EAAG,EAAU,CAAC,EAG/C,QAAW,KAAU,EAAQ,WAAY,CACxC,IAAQ,YAAW,kBAAmB,EAAO,WACvC,EAAY,EAAe,EAAe,EAAG,EAAe,EAAG,EAAO,EAAO,EAAS,CAAO,EACnG,EAAU,SAAS,IAAI,EAAU,EAAG,EAAU,CAAC,GAEhD,EAIE,EACH,EACE,UAAU,uBAAuB,EACjC,YAAY,GAAG,EACf,QAAQ,QAAQ,EAChB,QAAQ,CAAW,EACnB,cAAc,CAAC,cAAe,gBAAiB,SAAS,CAAC,EACzD,WAAW,EAAG,WAAa,YAAa,EAAO,cAAe,EAAM,QAAS,MAAY,CACzF,IAAM,EAAU,EAAI,OAAO,MACrB,EAAU,EAAI,OAAO,OAErB,EAAS,EACd,EAAM,EAAI,EAAM,aAChB,EAAM,EAAI,EAAM,aAChB,EAAO,EAAO,EAAS,CACxB,EAEA,EAAK,SAAS,IACb,EAAU,EAAI,EAAO,EAAI,EAAM,KAC/B,EAAU,EAAI,EAAO,EAAI,EAAM,IAChC,EACA,EAAK,MAAM,IAAI,EAAM,IAAI,EACzB,EAAK,SAAW,EAAE,EAAM,SAAW,EAAM,eACzC,EAEH",
8
+ "debugId": "D8E00FC32D5B431064756E2164756E21",
9
+ "names": []
10
+ }
@@ -5,9 +5,8 @@
5
5
  * Uses worldTransform for position (world-space collision).
6
6
  * Supports AABB and circle colliders.
7
7
  */
8
- import { type Plugin, type BasePluginOptions } from 'ecspresso';
9
- import type { WorldConfigFrom } from '../type-utils';
10
- import type { TransformWorldConfig } from './transform';
8
+ import { type BasePluginOptions } from 'ecspresso';
9
+ import type { TransformWorldConfig } from '../spatial/transform';
11
10
  /**
12
11
  * Axis-Aligned Bounding Box collider.
13
12
  */
@@ -60,6 +59,9 @@ export interface CollisionComponentTypes<L extends string = never> {
60
59
  }
61
60
  /**
62
61
  * Event fired when two entities collide.
62
+ *
63
+ * Normal components are flattened (`normalX`/`normalY`) rather than nested
64
+ * in a sub-object to avoid a per-event allocation in the collision hot path.
63
65
  */
64
66
  export interface CollisionEvent<L extends string = never> {
65
67
  /** First entity in the collision */
@@ -70,11 +72,10 @@ export interface CollisionEvent<L extends string = never> {
70
72
  layerA: L;
71
73
  /** Layer of the second entity */
72
74
  layerB: L;
73
- /** Contact normal pointing from entityA toward entityB */
74
- normal: {
75
- x: number;
76
- y: number;
77
- };
75
+ /** Contact normal X, pointing from entityA toward entityB */
76
+ normalX: number;
77
+ /** Contact normal Y, pointing from entityA toward entityB */
78
+ normalY: number;
78
79
  /** Penetration depth (positive = overlapping) */
79
80
  depth: number;
80
81
  }
@@ -281,5 +282,5 @@ export declare function createCollisionPairHandler<W = unknown, L extends string
281
282
  */
282
283
  export declare function createCollisionPlugin<L extends string, G extends string = 'physics'>(options: CollisionPluginOptions<G> & {
283
284
  layers: LayerFactories<Record<L, readonly string[]>>;
284
- }): Plugin<WorldConfigFrom<CollisionComponentTypes<L>, CollisionEventTypes<L>>, TransformWorldConfig, 'collision-detection', G>;
285
+ }): import("ecspresso").Plugin<import("ecspresso").WithEvents<import("ecspresso").WithComponents<import("ecspresso").EmptyConfig, CollisionComponentTypes<L>>, CollisionEventTypes<L>>, TransformWorldConfig, "collision-detection", G, never, never>;
285
286
  export {};
@@ -0,0 +1,4 @@
1
+ var p=((z)=>typeof require<"u"?require:typeof Proxy<"u"?new Proxy(z,{get:(J,Z)=>(typeof require<"u"?require:J)[Z]}):z)(function(z){if(typeof require<"u")return require.apply(this,arguments);throw Error('Dynamic require of "'+z+'" is not supported')});import{definePlugin as v}from"ecspresso";var H={normalX:0,normalY:0,depth:0},S=0;function g(z,J,Z,N,V,O,$,Q){if(z.entityId=J,z.layer=V,z.collidesWith=O,$)return z.x=Z+($.offsetX??0),z.y=N+($.offsetY??0),z.shape=0,z.halfWidth=$.width/2,z.halfHeight=$.height/2,z.radius=0,!0;if(Q)return z.x=Z+(Q.offsetX??0),z.y=N+(Q.offsetY??0),z.shape=1,z.halfWidth=0,z.halfHeight=0,z.radius=Q.radius,!0;return!1}function _(z){return z("spatialIndex")}function m(z,J,Z,N,V,O,$,Q,E){let G=V-z,F=O-J,j=Z+$-Math.abs(G),U=N+Q-Math.abs(F);if(j<=0||U<=0)return!1;if(j<U)return E.normalX=G>=0?1:-1,E.normalY=0,E.depth=j,!0;return E.normalX=0,E.normalY=F>=0?1:-1,E.depth=U,!0}function C(z,J,Z,N,V,O,$){let Q=N-z,E=V-J,G=Q*Q+E*E,F=Z+O;if(G>=F*F)return!1;let j=Math.sqrt(G);if(j===0)return $.normalX=1,$.normalY=0,$.depth=F,!0;return $.normalX=Q/j,$.normalY=E/j,$.depth=F-j,!0}function W(z,J,Z,N,V,O,$,Q){let E=Math.max(z-Z,Math.min(V,z+Z)),G=Math.max(J-N,Math.min(O,J+N)),F=V-E,j=O-G,U=F*F+j*j;if(U>=$*$)return!1;if(U===0){let T=V-(z-Z),q=z+Z-V,K=O-(J-N),L=J+N-O,P=Math.min(T,q,K,L);if(P===q)return Q.normalX=1,Q.normalY=0,Q.depth=q+$,!0;if(P===T)return Q.normalX=-1,Q.normalY=0,Q.depth=T+$,!0;if(P===L)return Q.normalX=0,Q.normalY=1,Q.depth=L+$,!0;return Q.normalX=0,Q.normalY=-1,Q.depth=K+$,!0}let D=Math.sqrt(U);return Q.normalX=F/D,Q.normalY=j/D,Q.depth=$-D,!0}function A(z,J,Z){if(z.shape===0&&J.shape===0)return m(z.x,z.y,z.halfWidth,z.halfHeight,J.x,J.y,J.halfWidth,J.halfHeight,Z);if(z.shape===1&&J.shape===1)return C(z.x,z.y,z.radius,J.x,J.y,J.radius,Z);if(z.shape===0&&J.shape===1)return W(z.x,z.y,z.halfWidth,z.halfHeight,J.x,J.y,J.radius,Z);if(!W(J.x,J.y,J.halfWidth,J.halfHeight,z.x,z.y,z.radius,Z))return!1;return Z.normalX=-Z.normalX,Z.normalY=-Z.normalY,!0}var R=new Set,M=!1,k=50;function B(z,J,Z,N,V,O){if(N)Y(z,J,Z,N,V,O);else X(z,J,V,O)}function X(z,J,Z,N){if(!M&&J>=k)M=!0,console.warn(`[ecspresso] Collision detection is using O(n²) brute force with ${J} colliders. For better performance, install createSpatialIndexPlugin() alongside your collision or physics2D plugin.`);for(let V=0;V<J;V++){let O=z[V];if(!O)continue;for(let $=V+1;$<J;$++){let Q=z[$];if(!Q)continue;if(!O.collidesWith.includes(Q.layer)&&!Q.collidesWith.includes(O.layer))continue;if(!A(O,Q,H))continue;Z(O,Q,H,N)}}}function Y(z,J,Z,N,V,O){Z.clear();for(let $=0;$<J;$++){let Q=z[$];if(!Q)continue;Z.set(Q.entityId,Q)}for(let $=0;$<J;$++){let Q=z[$];if(!Q)continue;let E=Q.shape===0?Q.halfWidth:Q.radius,G=Q.shape===0?Q.halfHeight:Q.radius;R.clear(),N.queryRectInto(Q.x-E,Q.y-G,Q.x+E,Q.y+G,R);for(let F of R){if(F<=Q.entityId)continue;let j=Z.get(F);if(!j)continue;if(!Q.collidesWith.includes(j.layer)&&!j.collidesWith.includes(Q.layer))continue;if(!A(Q,j,H))continue;V(Q,j,H,O)}}}function d(z,J,Z,N){let V={width:z,height:J};if(Z!==void 0)V.offsetX=Z;if(N!==void 0)V.offsetY=N;return{aabbCollider:V}}function u(z,J,Z){let N={radius:z};if(J!==void 0)N.offsetX=J;if(Z!==void 0)N.offsetY=Z;return{circleCollider:N}}function w(z,J){return{collisionLayer:{layer:z,collidesWith:J}}}function n(z){let J={};for(let Z of Object.keys(z)){let N=z[Z];J[Z]=()=>w(Z,N)}return J}function I(z){let J=z.indexOf(":");if(J===-1)throw Error(`Invalid collision pair key "${z}": must contain a colon separator (e.g. "player:enemy")`);let Z=z.slice(0,J),N=z.slice(J+1);if(Z===""||N==="")throw Error(`Invalid collision pair key "${z}": layer names must not be empty`);return[Z,N]}function s(z){let J=new Map,Z=new Set;for(let N of Object.keys(z))I(N),Z.add(N);for(let N of Object.keys(z)){let[V,O]=I(N),$=z[N];if(!$)continue;J.set(N,{callback:$,swapped:!1});let Q=`${O}:${V}`;if(Q!==N&&!Z.has(Q))J.set(Q,{callback:$,swapped:!0})}return function({data:V,ecs:O}){let $=J.get(V.layerA+":"+V.layerB);if(!$)return;if($.swapped)$.callback(V.entityB,V.entityA,O);else $.callback(V.entityA,V.entityB,O)}}function x(z,J,Z,N){N.publish("collision",{entityA:z.entityId,entityB:J.entityId,layerA:z.layer,layerB:J.layer,normalX:Z.normalX,normalY:Z.normalY,depth:Z.depth})}function l(z){let{systemGroup:J="physics",priority:Z=0,phase:N="postUpdate"}=z;return v("collision").withComponentTypes().withEventTypes().withLabels().withGroups().requires().install((V)=>{let O=[],$=new Map;V.addSystem("collision-detection").setPriority(Z).inPhase(N).inGroup(J).addQuery("collidables",{with:["worldTransform","collisionLayer"]}).setProcess(({queries:Q,ecs:E})=>{let G=0;for(let j of Q.collidables){let{worldTransform:U,collisionLayer:D}=j.components,T=E.getComponent(j.id,"aabbCollider"),q=E.getComponent(j.id,"circleCollider");if(!T&&!q)continue;let K=O[G];if(!K)K={entityId:j.id,x:U.x,y:U.y,layer:D.layer,collidesWith:D.collidesWith,shape:S,halfWidth:0,halfHeight:0,radius:0},O[G]=K;if(!g(K,j.id,U.x,U.y,D.layer,D.collidesWith,T,q))continue;G++}let F=_(E.tryGetResource.bind(E));B(O,G,$,F,x,E.eventBus)})})}export{n as defineCollisionLayers,l as createCollisionPlugin,s as createCollisionPairHandler,w as createCollisionLayer,u as createCircleCollider,d as createAABBCollider};
2
+
3
+ //# debugId=F04221DD4AA45FC664756E2164756E21
4
+ //# sourceMappingURL=collision.js.map
@@ -0,0 +1,11 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../src/plugins/physics/collision.ts", "../src/utils/narrowphase.ts"],
4
+ "sourcesContent": [
5
+ "/**\n * Collision Plugin for ECSpresso\n *\n * Provides layer-based collision detection with events.\n * Uses worldTransform for position (world-space collision).\n * Supports AABB and circle colliders.\n */\n\nimport { definePlugin, type BasePluginOptions } from 'ecspresso';\nimport type { TransformWorldConfig } from '../spatial/transform';\nimport { fillBaseColliderInfo, detectCollisions, tryGetSpatialIndex, AABB_SHAPE, type Contact, type BaseColliderInfo } from '../../utils/narrowphase';\n\n// ==================== Component Types ====================\n\n/**\n * Axis-Aligned Bounding Box collider.\n */\nexport interface AABBCollider {\n\t/** Width of the bounding box */\n\twidth: number;\n\t/** Height of the bounding box */\n\theight: number;\n\t/** X offset from entity position (default: 0) */\n\toffsetX?: number;\n\t/** Y offset from entity position (default: 0) */\n\toffsetY?: number;\n}\n\n/**\n * Circle collider.\n */\nexport interface CircleCollider {\n\t/** Radius of the circle */\n\tradius: number;\n\t/** X offset from entity position (default: 0) */\n\toffsetX?: number;\n\t/** Y offset from entity position (default: 0) */\n\toffsetY?: number;\n}\n\n/**\n * Collision layer configuration.\n */\nexport interface CollisionLayer<L extends string = never> {\n\t/** The layer this entity belongs to */\n\tlayer: L;\n\t/** Layers this entity can collide with */\n\tcollidesWith: readonly L[];\n}\n\n/**\n * Component types provided by the collision plugin.\n * Included automatically via `.withPlugin(createCollisionPlugin())`.\n *\n * @example\n * ```typescript\n * const ecs = ECSpresso.create()\n * .withPlugin(createCollisionPlugin())\n * .withComponentTypes<{ sprite: Sprite; enemy: boolean }>()\n * .build();\n * ```\n */\nexport interface CollisionComponentTypes<L extends string = never> {\n\taabbCollider: AABBCollider;\n\tcircleCollider: CircleCollider;\n\tcollisionLayer: CollisionLayer<L>;\n}\n\n// ==================== Event Types ====================\n\n/**\n * Event fired when two entities collide.\n *\n * Normal components are flattened (`normalX`/`normalY`) rather than nested\n * in a sub-object to avoid a per-event allocation in the collision hot path.\n */\nexport interface CollisionEvent<L extends string = never> {\n\t/** First entity in the collision */\n\tentityA: number;\n\t/** Second entity in the collision */\n\tentityB: number;\n\t/** Layer of the first entity */\n\tlayerA: L;\n\t/** Layer of the second entity */\n\tlayerB: L;\n\t/** Contact normal X, pointing from entityA toward entityB */\n\tnormalX: number;\n\t/** Contact normal Y, pointing from entityA toward entityB */\n\tnormalY: number;\n\t/** Penetration depth (positive = overlapping) */\n\tdepth: number;\n}\n\n/**\n * Event types provided by the collision plugin.\n */\nexport interface CollisionEventTypes<L extends string = never> {\n\tcollision: CollisionEvent<L>;\n}\n\n// ==================== Plugin Options ====================\n\n/**\n * Configuration options for the collision plugin.\n */\nexport interface CollisionPluginOptions<G extends string = 'physics'> extends BasePluginOptions<G> {\n\t/** Name of the collision event (default: 'collision') */\n\tcollisionEventName?: string;\n}\n\n// ==================== Helper Functions ====================\n\n/**\n * Create an AABB collider component.\n *\n * @param width Width of the bounding box\n * @param height Height of the bounding box\n * @param offsetX X offset from entity position\n * @param offsetY Y offset from entity position\n * @returns Component object suitable for spreading into spawn()\n *\n * @example\n * ```typescript\n * ecs.spawn({\n * ...createTransform(100, 200),\n * ...createAABBCollider(50, 30),\n * });\n * ```\n */\nexport function createAABBCollider(\n\twidth: number,\n\theight: number,\n\toffsetX?: number,\n\toffsetY?: number\n): { aabbCollider: AABBCollider } {\n\tconst collider: AABBCollider = { width, height };\n\tif (offsetX !== undefined) collider.offsetX = offsetX;\n\tif (offsetY !== undefined) collider.offsetY = offsetY;\n\treturn { aabbCollider: collider };\n}\n\n/**\n * Create a circle collider component.\n *\n * @param radius Radius of the circle\n * @param offsetX X offset from entity position\n * @param offsetY Y offset from entity position\n * @returns Component object suitable for spreading into spawn()\n *\n * @example\n * ```typescript\n * ecs.spawn({\n * ...createTransform(100, 200),\n * ...createCircleCollider(25),\n * });\n * ```\n */\nexport function createCircleCollider(\n\tradius: number,\n\toffsetX?: number,\n\toffsetY?: number\n): { circleCollider: CircleCollider } {\n\tconst collider: CircleCollider = { radius };\n\tif (offsetX !== undefined) collider.offsetX = offsetX;\n\tif (offsetY !== undefined) collider.offsetY = offsetY;\n\treturn { circleCollider: collider };\n}\n\n/**\n * Create a collision layer component.\n *\n * @param layer The layer this entity belongs to\n * @param collidesWith Layers this entity can collide with\n * @returns Component object suitable for spreading into spawn()\n *\n * @example\n * ```typescript\n * ecs.spawn({\n * ...createTransform(100, 200),\n * ...createAABBCollider(50, 30),\n * ...createCollisionLayer('player', ['enemy', 'obstacle']),\n * });\n * ```\n */\nexport function createCollisionLayer<L extends string>(\n\tlayer: L,\n\tcollidesWith: readonly L[]\n): Pick<CollisionComponentTypes<L>, 'collisionLayer'> {\n\treturn {\n\t\tcollisionLayer: { layer, collidesWith },\n\t};\n}\n\n/**\n * Layer factory result from defineCollisionLayers.\n */\nexport type LayerFactories<T extends Record<string, readonly string[]>> = {\n\t[K in keyof T]: () => Pick<CollisionComponentTypes<Extract<keyof T, string>>, 'collisionLayer'>;\n};\n\n/**\n * Extract layer names from a `defineCollisionLayers` result for use with\n * `createCollisionPairHandler`'s `L` type parameter.\n *\n * @example\n * ```typescript\n * const layers = defineCollisionLayers({ player: ['enemy'], enemy: ['player'] });\n * type Layer = LayersOf<typeof layers>;\n * const handler = createCollisionPairHandler<ECS, Layer>({\n * 'player:enemy': (playerId, enemyId, ecs) => { ... },\n * });\n * ```\n */\nexport type LayersOf<T> = Extract<keyof T, string>;\n\n/**\n * Define collision layer relationships and get factory functions.\n *\n * @param rules Object mapping layer names to arrays of layers they collide with\n * @returns Object with factory functions for each layer\n *\n * @example\n * ```typescript\n * const layers = defineCollisionLayers({\n * player: ['enemy', 'enemyProjectile'],\n * playerProjectile: ['enemy'],\n * enemy: ['playerProjectile'],\n * enemyProjectile: ['player'],\n * });\n *\n * // Usage\n * ecs.spawn({\n * ...createTransform(100, 200),\n * ...createAABBCollider(50, 30),\n * ...layers.player(),\n * });\n * ```\n */\n/**\n * Validates that all `collidesWith` values reference actual layer keys.\n * Catches typos at compile time.\n */\ntype ValidateCollidesWith<T> = {\n\t[K in keyof T]: T[K] extends readonly (infer V)[]\n\t\t? [V] extends [Extract<keyof T, string>] ? T[K] : readonly Extract<keyof T, string>[]\n\t\t: never;\n};\n\nexport function defineCollisionLayers<const T extends Record<string, readonly string[]>>(\n\trules: T & ValidateCollidesWith<T>\n): LayerFactories<T> {\n\ttype L = Extract<keyof T, string>;\n\tconst factories = {} as LayerFactories<T>;\n\n\tfor (const layer of Object.keys(rules) as Array<L>) {\n\t\tconst collidesWith = rules[layer] as readonly L[];\n\t\tfactories[layer] = () => createCollisionLayer<L>(layer, collidesWith);\n\t}\n\n\treturn factories;\n}\n\n// ==================== Collision Pair Handler ====================\n\n/**\n * Callback for a collision pair handler.\n *\n * @param firstEntityId Entity belonging to the first layer in the pair key\n * @param secondEntityId Entity belonging to the second layer in the pair key\n * @param ecs The ECS world instance (passed through from the subscriber)\n */\nexport type CollisionPairCallback<W = unknown> = (\n\tfirstEntityId: number,\n\tsecondEntityId: number,\n\tecs: W,\n) => void;\n\ninterface PairEntry<W> {\n\tcallback: CollisionPairCallback<W>;\n\tswapped: boolean;\n}\n\nfunction parsePairKey(key: string): [string, string] {\n\tconst colonIndex = key.indexOf(':');\n\tif (colonIndex === -1) {\n\t\tthrow new Error(`Invalid collision pair key \"${key}\": must contain a colon separator (e.g. \"player:enemy\")`);\n\t}\n\tconst layerA = key.slice(0, colonIndex);\n\tconst layerB = key.slice(colonIndex + 1);\n\tif (layerA === '' || layerB === '') {\n\t\tthrow new Error(`Invalid collision pair key \"${key}\": layer names must not be empty`);\n\t}\n\treturn [layerA, layerB];\n}\n\n/**\n * Create a collision pair handler that routes collision events to\n * layer-pair-specific callbacks.\n *\n * Registering `\"a:b\"` automatically handles both `(layerA=a, layerB=b)` and\n * `(layerA=b, layerB=a)`. Entity arguments are swapped to match the declared\n * key order. If both `\"a:b\"` and `\"b:a\"` are explicitly registered, each gets\n * its own handler with no implicit reverse.\n *\n * @typeParam W - The ECS world type (e.g. `ECSpresso<C, E, R>`). Defaults to `unknown`.\n * @typeParam L - Union of valid layer names. Defaults to `string`.\n * Provide specific layer names for compile-time key validation:\n * `createCollisionPairHandler<ECS, keyof typeof layers>({...})`\n *\n * @param pairs Object mapping `\"layerA:layerB\"` keys to callbacks\n * @returns A dispatch function to call with collision event data and ECS instance\n *\n * @example\n * ```typescript\n * // Basic usage:\n * const handler = createCollisionPairHandler<ECS>({\n * 'playerProjectile:enemy': (projectileId, enemyId, ecs) => {\n * ecs.commands.removeEntity(projectileId);\n * },\n * });\n *\n * // With layer name validation:\n * const layers = defineCollisionLayers({ player: ['enemy'], enemy: ['player'] });\n * type Layer = LayersOf<typeof layers>;\n * const handler = createCollisionPairHandler<ECS, Layer>({\n * 'player:enemy': (playerId, enemyId, ecs) => { ... },\n * });\n *\n * ecs.eventBus.subscribe('collision', (data) => handler({ data, ecs }));\n * ```\n */\nexport function createCollisionPairHandler<W = unknown, L extends string = string>(\n\tpairs: { [K in `${L}:${L}`]?: CollisionPairCallback<W> }\n): (ctx: { data: CollisionEvent<L>; ecs: W }) => void;\nexport function createCollisionPairHandler<W = unknown>(\n\tpairs: Record<string, CollisionPairCallback<W> | undefined>\n): (ctx: { data: CollisionEvent<string>; ecs: W }) => void {\n\tconst lookup = new Map<string, PairEntry<W>>();\n\tconst explicitKeys = new Set<string>();\n\n\t// First pass: collect all explicit keys\n\tfor (const key of Object.keys(pairs)) {\n\t\tparsePairKey(key); // validate\n\t\texplicitKeys.add(key);\n\t}\n\n\t// Second pass: build lookup with forward + conditional reverse entries\n\tfor (const key of Object.keys(pairs)) {\n\t\tconst [layerA, layerB] = parsePairKey(key);\n\t\tconst callback = pairs[key];\n\t\tif (!callback) continue;\n\n\t\t// Forward entry\n\t\tlookup.set(key, { callback, swapped: false });\n\n\t\t// Reverse entry (only if the reverse key wasn't explicitly registered\n\t\t// and it's not a self-collision where forward === reverse)\n\t\tconst reverseKey = `${layerB}:${layerA}`;\n\t\tif (reverseKey !== key && !explicitKeys.has(reverseKey)) {\n\t\t\tlookup.set(reverseKey, { callback, swapped: true });\n\t\t}\n\t}\n\n\treturn function collisionPairDispatch({ data: event, ecs }: { data: CollisionEvent<string>; ecs: W }): void {\n\t\tconst entry = lookup.get(event.layerA + ':' + event.layerB);\n\t\tif (!entry) return;\n\n\t\tif (entry.swapped) {\n\t\t\tentry.callback(event.entityB, event.entityA, ecs);\n\t\t} else {\n\t\t\tentry.callback(event.entityA, event.entityB, ecs);\n\t\t}\n\t};\n}\n\n// ==================== Dependency Types ====================\n\n// ==================== Module-level Collision Callback ====================\n\ninterface CollisionEventBus<L extends string> {\n\tpublish(event: 'collision', data: CollisionEvent<L>): void;\n}\n\nfunction onCollisionDetected<L extends string>(\n\ta: BaseColliderInfo<L>,\n\tb: BaseColliderInfo<L>,\n\tcontact: Contact,\n\teventBus: CollisionEventBus<L>,\n): void {\n\teventBus.publish('collision', {\n\t\tentityA: a.entityId,\n\t\tentityB: b.entityId,\n\t\tlayerA: a.layer,\n\t\tlayerB: b.layer,\n\t\tnormalX: contact.normalX,\n\t\tnormalY: contact.normalY,\n\t\tdepth: contact.depth,\n\t});\n}\n\n// ==================== Plugin Factory ====================\n\n/**\n * Create a collision plugin for ECSpresso.\n *\n * This plugin provides:\n * - Collision detection between entities with colliders\n * - AABB-AABB, circle-circle, and AABB-circle collision\n * - Layer-based filtering for collision pairs\n * - Deduplication of A-B / B-A collisions\n * - Automatic broadphase acceleration when spatialIndex resource is present\n *\n * Uses worldTransform for position (world-space collision detection).\n * The `layers` parameter is required for type inference — at runtime the\n * plugin does not consume it.\n *\n * @example\n * ```typescript\n * const layers = defineCollisionLayers({ player: ['enemy'], enemy: ['player'] });\n * const ecs = ECSpresso\n * .create()\n * .withPlugin(createTransformPlugin())\n * .withPlugin(createCollisionPlugin({ layers }))\n * .build();\n *\n * // Entity with collision\n * ecs.spawn({\n * ...createTransform(100, 200),\n * ...createAABBCollider(50, 30),\n * ...layers.player(),\n * });\n * ```\n */\nexport function createCollisionPlugin<L extends string, G extends string = 'physics'>(\n\toptions: CollisionPluginOptions<G> & { layers: LayerFactories<Record<L, readonly string[]>> }\n) {\n\tconst {\n\t\tsystemGroup = 'physics',\n\t\tpriority = 0,\n\t\tphase = 'postUpdate',\n\t} = options;\n\n\treturn definePlugin('collision')\n\t\t.withComponentTypes<CollisionComponentTypes<L>>()\n\t\t.withEventTypes<CollisionEventTypes<L>>()\n\t\t.withLabels<'collision-detection'>()\n\t\t.withGroups<G>()\n\t\t.requires<TransformWorldConfig>()\n\t\t.install((world) => {\n\t\t\t// Grow-only pool of BaseColliderInfo slots reused across frames.\n\t\t\t// Steady-state: zero allocations per frame once the pool is warm.\n\t\t\tconst colliderPool: BaseColliderInfo<L>[] = [];\n\t\t\t// Reusable entityId → collider lookup for the broadphase path.\n\t\t\tconst broadphaseMap = new Map<number, BaseColliderInfo<L>>();\n\n\t\t\tworld\n\t\t\t\t.addSystem('collision-detection')\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('collidables', {\n\t\t\t\t\twith: ['worldTransform', 'collisionLayer'],\n\t\t\t\t})\n\t\t\t\t.setProcess(({ queries, ecs }) => {\n\t\t\t\t\tlet count = 0;\n\n\t\t\t\t\tfor (const entity of queries.collidables) {\n\t\t\t\t\t\tconst { worldTransform, collisionLayer } = entity.components;\n\t\t\t\t\t\tconst aabb = ecs.getComponent(entity.id, 'aabbCollider');\n\t\t\t\t\t\tconst circle = ecs.getComponent(entity.id, 'circleCollider');\n\t\t\t\t\t\tif (!aabb && !circle) continue;\n\n\t\t\t\t\t\tlet slot = colliderPool[count];\n\t\t\t\t\t\tif (!slot) {\n\t\t\t\t\t\t\tslot = {\n\t\t\t\t\t\t\t\tentityId: entity.id,\n\t\t\t\t\t\t\t\tx: worldTransform.x,\n\t\t\t\t\t\t\t\ty: worldTransform.y,\n\t\t\t\t\t\t\t\tlayer: collisionLayer.layer,\n\t\t\t\t\t\t\t\tcollidesWith: collisionLayer.collidesWith,\n\t\t\t\t\t\t\t\tshape: AABB_SHAPE,\n\t\t\t\t\t\t\t\thalfWidth: 0,\n\t\t\t\t\t\t\t\thalfHeight: 0,\n\t\t\t\t\t\t\t\tradius: 0,\n\t\t\t\t\t\t\t};\n\t\t\t\t\t\t\tcolliderPool[count] = slot;\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tif (!fillBaseColliderInfo(\n\t\t\t\t\t\t\tslot,\n\t\t\t\t\t\t\tentity.id, worldTransform.x, worldTransform.y,\n\t\t\t\t\t\t\tcollisionLayer.layer, collisionLayer.collidesWith,\n\t\t\t\t\t\t\taabb, circle,\n\t\t\t\t\t\t)) continue;\n\n\t\t\t\t\t\tcount++;\n\t\t\t\t\t}\n\n\t\t\t\t\tconst si = tryGetSpatialIndex(ecs.tryGetResource.bind(ecs));\n\t\t\t\t\tdetectCollisions(colliderPool, count, broadphaseMap, si, onCollisionDetected<L>, ecs.eventBus);\n\t\t\t\t});\n\t\t});\n}\n\n",
6
+ "/**\n * Shared Narrowphase Module\n *\n * Provides contact-computing narrowphase tests and a generic collision\n * iteration pipeline used by both the collision plugin (event-only) and\n * the physics2D plugin (impulse response).\n */\n\nimport type { SpatialIndex } from './spatial-hash';\n\n// ==================== Contact ====================\n\n/**\n * Contact result from a narrowphase test. Normal points from A toward B.\n *\n * Narrowphase functions use this as an out-parameter: the caller owns the\n * struct, the function writes fields in place and returns `true` on hit.\n * The `onContact` callback in `detectCollisions` receives a shared module-\n * level instance — **subscribers must consume it synchronously and must not\n * retain the reference across frames**.\n */\nexport interface Contact {\n\tnormalX: number;\n\tnormalY: number;\n\t/** Penetration depth (positive = overlapping) */\n\tdepth: number;\n}\n\n/**\n * Module-level reusable Contact passed down from `detectCollisions` into\n * narrowphase tests and forwarded to the `onContact` callback. Reused across\n * every pair in every frame — zero allocation in the narrowphase hot path.\n */\nconst _sharedContact: Contact = { normalX: 0, normalY: 0, depth: 0 };\n\n// ==================== BaseColliderInfo ====================\n\n/** Collider shape discriminator for the flattened BaseColliderInfo layout. */\nexport const AABB_SHAPE = 0;\nexport const CIRCLE_SHAPE = 1;\nexport type ColliderShape = typeof AABB_SHAPE | typeof CIRCLE_SHAPE;\n\n/**\n * Minimum collider data shared by collision and physics bundles.\n *\n * Flat layout (no nested `aabb` / `circle` sub-objects): the `shape`\n * discriminator tells you whether to read `halfWidth`/`halfHeight`\n * (AABB) or `radius` (Circle). Unused fields are set to 0.\n *\n * This shape is pool-friendly — all fields are assigned in place each\n * frame without allocating nested objects.\n */\nexport interface BaseColliderInfo<L extends string = string> {\n\tentityId: number;\n\tx: number;\n\ty: number;\n\tlayer: L;\n\tcollidesWith: readonly L[];\n\tshape: ColliderShape;\n\thalfWidth: number;\n\thalfHeight: number;\n\tradius: number;\n}\n\n// ==================== Collider Construction ====================\n\n/**\n * Populate a `BaseColliderInfo` slot in place from raw component data.\n * Returns `true` if the slot was filled, `false` if the entity has no\n * collider (caller should skip it).\n *\n * If an entity has both AABB and circle colliders, AABB wins and only\n * the AABB offset is applied. This matches the dispatch precedence in\n * `computeContact`; the previous implementation stacked both offsets,\n * which was a bug.\n */\nexport function fillBaseColliderInfo<L extends string>(\n\tinfo: BaseColliderInfo<L>,\n\tentityId: number,\n\tx: number,\n\ty: number,\n\tlayer: L,\n\tcollidesWith: readonly L[],\n\taabb: { width: number; height: number; offsetX?: number; offsetY?: number } | undefined,\n\tcircle: { radius: number; offsetX?: number; offsetY?: number } | undefined,\n): boolean {\n\tinfo.entityId = entityId;\n\tinfo.layer = layer;\n\tinfo.collidesWith = collidesWith;\n\n\tif (aabb) {\n\t\tinfo.x = x + (aabb.offsetX ?? 0);\n\t\tinfo.y = y + (aabb.offsetY ?? 0);\n\t\tinfo.shape = AABB_SHAPE;\n\t\tinfo.halfWidth = aabb.width / 2;\n\t\tinfo.halfHeight = aabb.height / 2;\n\t\tinfo.radius = 0;\n\t\treturn true;\n\t}\n\n\tif (circle) {\n\t\tinfo.x = x + (circle.offsetX ?? 0);\n\t\tinfo.y = y + (circle.offsetY ?? 0);\n\t\tinfo.shape = CIRCLE_SHAPE;\n\t\tinfo.halfWidth = 0;\n\t\tinfo.halfHeight = 0;\n\t\tinfo.radius = circle.radius;\n\t\treturn true;\n\t}\n\n\treturn false;\n}\n\n// ==================== Spatial Index Lookup ====================\n\n/**\n * Retrieve the optional spatialIndex resource, returning undefined when absent.\n * Centralizes the cross-plugin typed lookup so individual plugins don't each\n * need to import SpatialIndex or repeat the tryGetResource pattern.\n */\nexport function tryGetSpatialIndex(\n\ttryGetResource: <T>(key: string) => T | undefined,\n): SpatialIndex | undefined {\n\treturn tryGetResource<SpatialIndex>('spatialIndex');\n}\n\n// ==================== Narrowphase Tests ====================\n\n/**\n * Write an AABB-AABB contact into `out`. Returns `true` if the shapes\n * overlap (out was filled), `false` otherwise.\n */\nexport function computeAABBvsAABB(\n\tax: number, ay: number, ahw: number, ahh: number,\n\tbx: number, by: number, bhw: number, bhh: number,\n\tout: Contact,\n): boolean {\n\tconst dx = bx - ax;\n\tconst dy = by - ay;\n\tconst overlapX = (ahw + bhw) - Math.abs(dx);\n\tconst overlapY = (ahh + bhh) - Math.abs(dy);\n\n\tif (overlapX <= 0 || overlapY <= 0) return false;\n\n\tif (overlapX < overlapY) {\n\t\tout.normalX = dx >= 0 ? 1 : -1;\n\t\tout.normalY = 0;\n\t\tout.depth = overlapX;\n\t\treturn true;\n\t}\n\tout.normalX = 0;\n\tout.normalY = dy >= 0 ? 1 : -1;\n\tout.depth = overlapY;\n\treturn true;\n}\n\nexport function computeCircleVsCircle(\n\tax: number, ay: number, ar: number,\n\tbx: number, by: number, br: number,\n\tout: Contact,\n): boolean {\n\tconst dx = bx - ax;\n\tconst dy = by - ay;\n\tconst distSq = dx * dx + dy * dy;\n\tconst radiusSum = ar + br;\n\n\tif (distSq >= radiusSum * radiusSum) return false;\n\n\tconst dist = Math.sqrt(distSq);\n\tif (dist === 0) {\n\t\tout.normalX = 1;\n\t\tout.normalY = 0;\n\t\tout.depth = radiusSum;\n\t\treturn true;\n\t}\n\tout.normalX = dx / dist;\n\tout.normalY = dy / dist;\n\tout.depth = radiusSum - dist;\n\treturn true;\n}\n\nexport function computeAABBvsCircle(\n\taabbX: number, aabbY: number, ahw: number, ahh: number,\n\tcircleX: number, circleY: number, radius: number,\n\tout: Contact,\n): boolean {\n\tconst closestX = Math.max(aabbX - ahw, Math.min(circleX, aabbX + ahw));\n\tconst closestY = Math.max(aabbY - ahh, Math.min(circleY, aabbY + ahh));\n\n\tconst dx = circleX - closestX;\n\tconst dy = circleY - closestY;\n\tconst distSq = dx * dx + dy * dy;\n\n\tif (distSq >= radius * radius) return false;\n\n\t// Circle center inside AABB\n\tif (distSq === 0) {\n\t\tconst pushLeft = (circleX - (aabbX - ahw));\n\t\tconst pushRight = ((aabbX + ahw) - circleX);\n\t\tconst pushUp = (circleY - (aabbY - ahh));\n\t\tconst pushDown = ((aabbY + ahh) - circleY);\n\t\tconst minPush = Math.min(pushLeft, pushRight, pushUp, pushDown);\n\n\t\tif (minPush === pushRight) {\n\t\t\tout.normalX = 1; out.normalY = 0; out.depth = pushRight + radius;\n\t\t\treturn true;\n\t\t}\n\t\tif (minPush === pushLeft) {\n\t\t\tout.normalX = -1; out.normalY = 0; out.depth = pushLeft + radius;\n\t\t\treturn true;\n\t\t}\n\t\tif (minPush === pushDown) {\n\t\t\tout.normalX = 0; out.normalY = 1; out.depth = pushDown + radius;\n\t\t\treturn true;\n\t\t}\n\t\tout.normalX = 0; out.normalY = -1; out.depth = pushUp + radius;\n\t\treturn true;\n\t}\n\n\tconst dist = Math.sqrt(distSq);\n\tout.normalX = dx / dist;\n\tout.normalY = dy / dist;\n\tout.depth = radius - dist;\n\treturn true;\n}\n\n// ==================== Contact Dispatcher ====================\n\n/**\n * Dispatch to the correct narrowphase function for the given pair and\n * write the contact into `out`. Returns `true` if the pair overlaps.\n */\nexport function computeContact(a: BaseColliderInfo, b: BaseColliderInfo, out: Contact): boolean {\n\tif (a.shape === AABB_SHAPE && b.shape === AABB_SHAPE) {\n\t\treturn computeAABBvsAABB(\n\t\t\ta.x, a.y, a.halfWidth, a.halfHeight,\n\t\t\tb.x, b.y, b.halfWidth, b.halfHeight,\n\t\t\tout,\n\t\t);\n\t}\n\n\tif (a.shape === CIRCLE_SHAPE && b.shape === CIRCLE_SHAPE) {\n\t\treturn computeCircleVsCircle(\n\t\t\ta.x, a.y, a.radius,\n\t\t\tb.x, b.y, b.radius,\n\t\t\tout,\n\t\t);\n\t}\n\n\tif (a.shape === AABB_SHAPE && b.shape === CIRCLE_SHAPE) {\n\t\treturn computeAABBvsCircle(\n\t\t\ta.x, a.y, a.halfWidth, a.halfHeight,\n\t\t\tb.x, b.y, b.radius,\n\t\t\tout,\n\t\t);\n\t}\n\n\t// a is Circle, b is AABB — compute as AABB-vs-Circle, then flip normal in place\n\tif (!computeAABBvsCircle(\n\t\tb.x, b.y, b.halfWidth, b.halfHeight,\n\t\ta.x, a.y, a.radius,\n\t\tout,\n\t)) return false;\n\tout.normalX = -out.normalX;\n\tout.normalY = -out.normalY;\n\treturn true;\n}\n\n// ==================== Collision Iteration ====================\n\n/** Module-level reusable set for broadphase candidates. */\nconst _broadphaseCandidates = new Set<number>();\n\nlet _bruteForceWarned = false;\nconst BRUTE_FORCE_WARN_THRESHOLD = 50;\n\n/**\n * Generic collision detection pipeline: brute-force or broadphase,\n * with layer filtering and contact computation.\n *\n * `count` is the number of live entries at the front of `colliders`.\n * The array itself may be a grow-only pool — only indices `[0, count)`\n * are iterated, so trailing pool slots are ignored.\n *\n * `workingMap` is a caller-owned `Map<number, I>` used by the broadphase\n * path as an entityId → collider lookup. It is cleared and repopulated on\n * each call; callers should allocate it once and pass the same instance\n * every frame. Unused by the brute-force path but still required so that\n * typed reuse is the default, not an opt-in.\n *\n * Uses a context parameter forwarded to the callback to avoid\n * per-frame closure allocation.\n */\nexport function detectCollisions<I extends BaseColliderInfo, C>(\n\tcolliders: I[],\n\tcount: number,\n\tworkingMap: Map<number, I>,\n\tspatialIndex: SpatialIndex | undefined,\n\tonContact: (a: I, b: I, contact: Contact, context: C) => void,\n\tcontext: C,\n): void {\n\tif (spatialIndex) {\n\t\tbroadphaseDetect(colliders, count, workingMap, spatialIndex, onContact, context);\n\t} else {\n\t\tbruteForceDetect(colliders, count, onContact, context);\n\t}\n}\n\nfunction bruteForceDetect<I extends BaseColliderInfo, C>(\n\tcolliders: I[],\n\tcount: number,\n\tonContact: (a: I, b: I, contact: Contact, context: C) => void,\n\tcontext: C,\n): void {\n\tif (!_bruteForceWarned && count >= BRUTE_FORCE_WARN_THRESHOLD) {\n\t\t_bruteForceWarned = true;\n\t\tconsole.warn(\n\t\t\t`[ecspresso] Collision detection is using O(n²) brute force with ${count} colliders. ` +\n\t\t\t`For better performance, install createSpatialIndexPlugin() alongside your collision or physics2D plugin.`,\n\t\t);\n\t}\n\n\tfor (let i = 0; i < count; i++) {\n\t\tconst a = colliders[i];\n\t\tif (!a) continue;\n\n\t\tfor (let j = i + 1; j < count; j++) {\n\t\t\tconst b = colliders[j];\n\t\t\tif (!b) continue;\n\n\t\t\tif (!a.collidesWith.includes(b.layer) && !b.collidesWith.includes(a.layer)) continue;\n\n\t\t\tif (!computeContact(a, b, _sharedContact)) continue;\n\n\t\t\tonContact(a, b, _sharedContact, context);\n\t\t}\n\t}\n}\n\nfunction broadphaseDetect<I extends BaseColliderInfo, C>(\n\tcolliders: I[],\n\tcount: number,\n\tcolliderMap: Map<number, I>,\n\tspatialIndex: SpatialIndex,\n\tonContact: (a: I, b: I, contact: Contact, context: C) => void,\n\tcontext: C,\n): void {\n\tcolliderMap.clear();\n\tfor (let i = 0; i < count; i++) {\n\t\tconst c = colliders[i];\n\t\tif (!c) continue;\n\t\tcolliderMap.set(c.entityId, c);\n\t}\n\n\tfor (let i = 0; i < count; i++) {\n\t\tconst a = colliders[i];\n\t\tif (!a) continue;\n\n\t\tconst aHalfW = a.shape === AABB_SHAPE ? a.halfWidth : a.radius;\n\t\tconst aHalfH = a.shape === AABB_SHAPE ? a.halfHeight : a.radius;\n\n\t\t_broadphaseCandidates.clear();\n\t\tspatialIndex.queryRectInto(\n\t\t\ta.x - aHalfW, a.y - aHalfH,\n\t\t\ta.x + aHalfW, a.y + aHalfH,\n\t\t\t_broadphaseCandidates,\n\t\t);\n\n\t\tfor (const bId of _broadphaseCandidates) {\n\t\t\tif (bId <= a.entityId) continue;\n\n\t\t\tconst b = colliderMap.get(bId);\n\t\t\tif (!b) continue;\n\n\t\t\tif (!a.collidesWith.includes(b.layer) && !b.collidesWith.includes(a.layer)) continue;\n\n\t\t\tif (!computeContact(a, b, _sharedContact)) continue;\n\n\t\t\tonContact(a, b, _sharedContact, context);\n\t\t}\n\t}\n}\n"
7
+ ],
8
+ "mappings": "2PAQA,uBAAS,kBCyBT,IAAM,EAA0B,CAAE,QAAS,EAAG,QAAS,EAAG,MAAO,CAAE,EAKtD,EAAa,EAsCnB,SAAS,CAAsC,CACrD,EACA,EACA,EACA,EACA,EACA,EACA,EACA,EACU,CAKV,GAJA,EAAK,SAAW,EAChB,EAAK,MAAQ,EACb,EAAK,aAAe,EAEhB,EAOH,OANA,EAAK,EAAI,GAAK,EAAK,SAAW,GAC9B,EAAK,EAAI,GAAK,EAAK,SAAW,GAC9B,EAAK,MAvDmB,EAwDxB,EAAK,UAAY,EAAK,MAAQ,EAC9B,EAAK,WAAa,EAAK,OAAS,EAChC,EAAK,OAAS,EACP,GAGR,GAAI,EAOH,OANA,EAAK,EAAI,GAAK,EAAO,SAAW,GAChC,EAAK,EAAI,GAAK,EAAO,SAAW,GAChC,EAAK,MAhEqB,EAiE1B,EAAK,UAAY,EACjB,EAAK,WAAa,EAClB,EAAK,OAAS,EAAO,OACd,GAGR,MAAO,GAUD,SAAS,CAAkB,CACjC,EAC2B,CAC3B,OAAO,EAA6B,cAAc,EAS5C,SAAS,CAAiB,CAChC,EAAY,EAAY,EAAa,EACrC,EAAY,EAAY,EAAa,EACrC,EACU,CACV,IAAM,EAAK,EAAK,EACV,EAAK,EAAK,EACV,EAAY,EAAM,EAAO,KAAK,IAAI,CAAE,EACpC,EAAY,EAAM,EAAO,KAAK,IAAI,CAAE,EAE1C,GAAI,GAAY,GAAK,GAAY,EAAG,MAAO,GAE3C,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EAAO,WAC5C,EAAO,EAAI,aAAa,EAAO,GAAI,cAAc,EACjD,EAAS,EAAI,aAAa,EAAO,GAAI,gBAAgB,EAC3D,GAAI,CAAC,GAAQ,CAAC,EAAQ,SAEtB,IAAI,EAAO,EAAa,GACxB,GAAI,CAAC,EACJ,EAAO,CACN,SAAU,EAAO,GACjB,EAAG,EAAe,EAClB,EAAG,EAAe,EAClB,MAAO,EAAe,MACtB,aAAc,EAAe,aAC7B,MAAO,EACP,UAAW,EACX,WAAY,EACZ,OAAQ,CACT,EACA,EAAa,GAAS,EAGvB,GAAI,CAAC,EACJ,EACA,EAAO,GAAI,EAAe,EAAG,EAAe,EAC5C,EAAe,MAAO,EAAe,aACrC,EAAM,CACP,EAAG,SAEH,IAGD,IAAM,EAAK,EAAmB,EAAI,eAAe,KAAK,CAAG,CAAC,EAC1D,EAAiB,EAAc,EAAO,EAAe,EAAI,EAAwB,EAAI,QAAQ,EAC7F,EACF",
9
+ "debugId": "F04221DD4AA45FC664756E2164756E21",
10
+ "names": []
11
+ }
@@ -8,10 +8,8 @@
8
8
  * Has its own collision detection in fixedUpdate for physics response;
9
9
  * the existing collision plugin can still run in postUpdate for game logic events.
10
10
  */
11
- import { type Plugin } from 'ecspresso';
12
11
  import type { SystemPhase } from 'ecspresso';
13
- import type { WorldConfigFrom } from '../type-utils';
14
- import type { TransformComponentTypes, TransformWorldConfig } from './transform';
12
+ import type { TransformComponentTypes, TransformWorldConfig } from '../spatial/transform';
15
13
  import type { CollisionComponentTypes, LayerFactories } from './collision';
16
14
  import type { Vector2D } from 'ecspresso';
17
15
  /**
@@ -63,12 +61,17 @@ export interface Physics2DResourceTypes {
63
61
  }
64
62
  /**
65
63
  * Event emitted for each physics collision pair.
64
+ *
65
+ * Normal components are flattened (`normalX`/`normalY`) rather than nested
66
+ * in a `Vector2D` to avoid a per-event allocation in the physics hot path.
66
67
  */
67
68
  export interface Physics2DCollisionEvent {
68
69
  entityA: number;
69
70
  entityB: number;
70
- /** Unit normal pointing from A toward B */
71
- normal: Vector2D;
71
+ /** Unit normal X, pointing from A toward B */
72
+ normalX: number;
73
+ /** Unit normal Y, pointing from A toward B */
74
+ normalY: number;
72
75
  /** Penetration depth (positive) */
73
76
  depth: number;
74
77
  }
@@ -159,5 +162,5 @@ export declare function setVelocity(ecs: {
159
162
  type Physics2DProvides<L extends string = never> = Physics2DOwnComponentTypes & CollisionComponentTypes<L>;
160
163
  export declare function createPhysics2DPlugin<L extends string = never, G extends string = 'physics2D', CG extends string = never>(options?: Physics2DPluginOptions<G, CG> & {
161
164
  layers?: LayerFactories<Record<L, readonly string[]>>;
162
- }): Plugin<WorldConfigFrom<Physics2DProvides<L>, Physics2DEventTypes, Physics2DResourceTypes>, TransformWorldConfig, 'physics2D-integration' | 'physics2D-collision', G | CG>;
165
+ }): import("ecspresso").Plugin<import("ecspresso").WithResources<import("ecspresso").WithEvents<import("ecspresso").WithComponents<import("ecspresso").EmptyConfig, Physics2DProvides<L>>, Physics2DEventTypes>, Physics2DResourceTypes>, TransformWorldConfig, "physics2D-integration" | "physics2D-collision", G | CG, never, never>;
163
166
  export {};
@@ -0,0 +1,4 @@
1
+ var y=((z)=>typeof require<"u"?require:typeof Proxy<"u"?new Proxy(z,{get:(J,Q)=>(typeof require<"u"?require:J)[Q]}):z)(function(z){if(typeof require<"u")return require.apply(this,arguments);throw Error('Dynamic require of "'+z+'" is not supported')});import{definePlugin as f}from"ecspresso";var _={normalX:0,normalY:0,depth:0},Y=0;function A(z,J,Q,j,Z,O,$,K){if(z.entityId=J,z.layer=Z,z.collidesWith=O,$)return z.x=Q+($.offsetX??0),z.y=j+($.offsetY??0),z.shape=0,z.halfWidth=$.width/2,z.halfHeight=$.height/2,z.radius=0,!0;if(K)return z.x=Q+(K.offsetX??0),z.y=j+(K.offsetY??0),z.shape=1,z.halfWidth=0,z.halfHeight=0,z.radius=K.radius,!0;return!1}function M(z){return z("spatialIndex")}function B(z,J,Q,j,Z,O,$,K,U){let D=Z-z,V=O-J,E=Q+$-Math.abs(D),F=j+K-Math.abs(V);if(E<=0||F<=0)return!1;if(E<F)return U.normalX=D>=0?1:-1,U.normalY=0,U.depth=E,!0;return U.normalX=0,U.normalY=V>=0?1:-1,U.depth=F,!0}function x(z,J,Q,j,Z,O,$){let K=j-z,U=Z-J,D=K*K+U*U,V=Q+O;if(D>=V*V)return!1;let E=Math.sqrt(D);if(E===0)return $.normalX=1,$.normalY=0,$.depth=V,!0;return $.normalX=K/E,$.normalY=U/E,$.depth=V-E,!0}function C(z,J,Q,j,Z,O,$,K){let U=Math.max(z-Q,Math.min(Z,z+Q)),D=Math.max(J-j,Math.min(O,J+j)),V=Z-U,E=O-D,F=V*V+E*E;if(F>=$*$)return!1;if(F===0){let H=Z-(z-Q),W=z+Q-Z,N=O-(J-j),T=J+j-O,q=Math.min(H,W,N,T);if(q===W)return K.normalX=1,K.normalY=0,K.depth=W+$,!0;if(q===H)return K.normalX=-1,K.normalY=0,K.depth=H+$,!0;if(q===T)return K.normalX=0,K.normalY=1,K.depth=T+$,!0;return K.normalX=0,K.normalY=-1,K.depth=N+$,!0}let G=Math.sqrt(F);return K.normalX=V/G,K.normalY=E/G,K.depth=$-G,!0}function w(z,J,Q){if(z.shape===0&&J.shape===0)return B(z.x,z.y,z.halfWidth,z.halfHeight,J.x,J.y,J.halfWidth,J.halfHeight,Q);if(z.shape===1&&J.shape===1)return x(z.x,z.y,z.radius,J.x,J.y,J.radius,Q);if(z.shape===0&&J.shape===1)return C(z.x,z.y,z.halfWidth,z.halfHeight,J.x,J.y,J.radius,Q);if(!C(J.x,J.y,J.halfWidth,J.halfHeight,z.x,z.y,z.radius,Q))return!1;return Q.normalX=-Q.normalX,Q.normalY=-Q.normalY,!0}var S=new Set,X=!1,m=50;function I(z,J,Q,j,Z,O){if(j)v(z,J,Q,j,Z,O);else g(z,J,Z,O)}function g(z,J,Q,j){if(!X&&J>=m)X=!0,console.warn(`[ecspresso] Collision detection is using O(n²) brute force with ${J} colliders. For better performance, install createSpatialIndexPlugin() alongside your collision or physics2D plugin.`);for(let Z=0;Z<J;Z++){let O=z[Z];if(!O)continue;for(let $=Z+1;$<J;$++){let K=z[$];if(!K)continue;if(!O.collidesWith.includes(K.layer)&&!K.collidesWith.includes(O.layer))continue;if(!w(O,K,_))continue;Q(O,K,_,j)}}}function v(z,J,Q,j,Z,O){Q.clear();for(let $=0;$<J;$++){let K=z[$];if(!K)continue;Q.set(K.entityId,K)}for(let $=0;$<J;$++){let K=z[$];if(!K)continue;let U=K.shape===0?K.halfWidth:K.radius,D=K.shape===0?K.halfHeight:K.radius;S.clear(),j.queryRectInto(K.x-U,K.y-D,K.x+U,K.y+D,S);for(let V of S){if(V<=K.entityId)continue;let E=Q.get(V);if(!E)continue;if(!K.collidesWith.includes(E.layer)&&!E.collidesWith.includes(K.layer))continue;if(!w(K,E,_))continue;Z(K,E,_,O)}}}function l(z,J){return{rigidBody:{type:z,mass:z==="static"?1/0:J?.mass??1,drag:J?.drag??0,restitution:J?.restitution??0,friction:J?.friction??0,gravityScale:J?.gravityScale??1},force:{x:0,y:0}}}function s(z,J){return{force:{x:z,y:J}}}function n(z,J,Q,j){let Z=z.getComponent(J,"force");if(!Z)return;Z.x+=Q,Z.y+=j}function i(z,J,Q,j){let Z=z.getComponent(J,"velocity"),O=z.getComponent(J,"rigidBody");if(!Z||!O)return;if(O.mass===1/0||O.mass===0)return;Z.x+=Q/O.mass,Z.y+=j/O.mass}function c(z,J,Q,j){let Z=z.getComponent(J,"velocity");if(!Z)return;Z.x=Q,Z.y=j}function h(z,J,Q,j){let Z=z.rigidBody.type==="dynamic"&&z.rigidBody.mass>0&&z.rigidBody.mass!==1/0?1/z.rigidBody.mass:0,O=J.rigidBody.type==="dynamic"&&J.rigidBody.mass>0&&J.rigidBody.mass!==1/0?1/J.rigidBody.mass:0,$=Z+O;if($>0){let K=Q.depth/$;if(Z>0){let E=j.getComponent(z.entityId,"localTransform");if(!E)return;E.x-=K*Z*Q.normalX,E.y-=K*Z*Q.normalY,z.x=E.x,j.markChanged(z.entityId,"localTransform")}if(O>0){let E=j.getComponent(J.entityId,"localTransform");if(!E)return;E.x+=K*O*Q.normalX,E.y+=K*O*Q.normalY,j.markChanged(J.entityId,"localTransform")}let U=J.velocity.x-z.velocity.x,D=J.velocity.y-z.velocity.y,V=U*Q.normalX+D*Q.normalY;if(V<0){let F=-(1+Math.min(z.rigidBody.restitution,J.rigidBody.restitution))*V/$;z.velocity.x-=F*Z*Q.normalX,z.velocity.y-=F*Z*Q.normalY,J.velocity.x+=F*O*Q.normalX,J.velocity.y+=F*O*Q.normalY;let G=U-V*Q.normalX,H=D-V*Q.normalY,W=Math.sqrt(G*G+H*H);if(W>0.000001){let N=G/W,T=H/W,L=Math.sqrt(z.rigidBody.friction*J.rigidBody.friction)*Math.abs(F),k=Math.min(W/$,L);z.velocity.x+=k*Z*N,z.velocity.y+=k*Z*T,J.velocity.x-=k*O*N,J.velocity.y-=k*O*T}}j.markChanged(z.entityId,"velocity"),j.markChanged(J.entityId,"velocity")}j.eventBus.publish("physicsCollision",{entityA:z.entityId,entityB:J.entityId,normalX:Q.normalX,normalY:Q.normalY,depth:Q.depth})}function p(z,J,Q,j){h(z,J,Q,j)}function e(z){let{gravity:J={x:0,y:0},systemGroup:Q="physics2D",collisionSystemGroup:j,integrationPriority:Z=1000,collisionPriority:O=900,phase:$="fixedUpdate"}=z??{};return f("physics2D").withComponentTypes().withEventTypes().withResourceTypes().withLabels().withGroups().requires().install((K)=>{K.registerRequired("rigidBody","velocity",()=>({x:0,y:0})),K.registerRequired("rigidBody","force",()=>({x:0,y:0})),K.addResource("physicsConfig",{gravity:{x:J.x,y:J.y}}),K.addSystem("physics2D-integration").setPriority(Z).inPhase($).inGroup(Q).addQuery("bodies",{with:["localTransform","velocity","rigidBody","force"]}).setProcess(({queries:E,dt:F,ecs:G})=>{let H=G.getResource("physicsConfig"),W=H.gravity.x,N=H.gravity.y;for(let T of E.bodies){let{localTransform:q,velocity:L,rigidBody:k,force:P}=T.components;if(k.type==="static")continue;if(k.type==="dynamic"){if(L.x+=W*k.gravityScale*F,L.y+=N*k.gravityScale*F,k.mass>0&&k.mass!==1/0)L.x+=P.x/k.mass*F,L.y+=P.y/k.mass*F;if(k.drag>0){let R=Math.max(0,1-k.drag*F);L.x*=R,L.y*=R}}q.x+=L.x*F,q.y+=L.y*F,P.x=0,P.y=0,G.markChanged(T.id,"localTransform")}});let U=K.addSystem("physics2D-collision").setPriority(O).inPhase($).inGroup(Q);if(j)U.inGroup(j);let D=[],V=new Map;U.addQuery("collidables",{with:["localTransform","rigidBody","velocity","collisionLayer"]}).setProcess(({queries:E,ecs:F})=>{let G=0;for(let W of E.collidables){let{localTransform:N,rigidBody:T,velocity:q,collisionLayer:L}=W.components,k=F.getComponent(W.id,"aabbCollider"),P=F.getComponent(W.id,"circleCollider");if(!k&&!P)continue;let R=D[G];if(!R)R={entityId:W.id,x:N.x,y:N.y,layer:L.layer,collidesWith:L.collidesWith,shape:Y,halfWidth:0,halfHeight:0,radius:0,rigidBody:T,velocity:q},D[G]=R;else R.rigidBody=T,R.velocity=q;if(!A(R,W.id,N.x,N.y,L.layer,L.collidesWith,k,P))continue;G++}let H=M(F.tryGetResource.bind(F));I(D,G,V,H,p,F)})})}export{c as setVelocity,l as createRigidBody,e as createPhysics2DPlugin,s as createForce,i as applyImpulse,n as applyForce};
2
+
3
+ //# debugId=B8850A0A0D8A257364756E2164756E21
4
+ //# sourceMappingURL=physics2D.js.map
@@ -0,0 +1,11 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../src/plugins/physics/physics2D.ts", "../src/utils/narrowphase.ts"],
4
+ "sourcesContent": [
5
+ "/**\n * Physics 2D Plugin for ECSpresso\n *\n * Provides ECS-native arcade physics: gravity, forces, drag, semi-implicit Euler\n * integration, and impulse-based collision response with friction.\n *\n * Reuses collider types from the collision plugin for shape definitions.\n * Has its own collision detection in fixedUpdate for physics response;\n * the existing collision plugin can still run in postUpdate for game logic events.\n */\n\nimport { definePlugin } from 'ecspresso';\nimport type { SystemPhase } from 'ecspresso';\nimport type { TransformComponentTypes, TransformWorldConfig } from '../spatial/transform';\nimport type { CollisionComponentTypes, LayerFactories } from './collision';\nimport type { Vector2D } from 'ecspresso';\nimport { fillBaseColliderInfo, detectCollisions, tryGetSpatialIndex, AABB_SHAPE, type Contact, type BaseColliderInfo } from '../../utils/narrowphase';\n\n// ==================== Component Types ====================\n\n/**\n * Rigid body types for physics simulation.\n * - 'dynamic': Fully simulated (gravity, forces, collisions)\n * - 'kinematic': Moves via velocity only (ignores gravity/forces, immovable in collisions)\n * - 'static': Immovable (ignores gravity, forces, and velocity)\n */\nexport type BodyType = 'dynamic' | 'kinematic' | 'static';\n\n/**\n * Rigid body component controlling physics behavior.\n */\nexport interface RigidBody {\n\ttype: BodyType;\n\t/** Mass in arbitrary units. Affects force→acceleration. Infinity = immovable. */\n\tmass: number;\n\t/** Linear velocity damping coefficient (units/sec, 0 = none) */\n\tdrag: number;\n\t/** Bounciness 0–1 (0 = no bounce, 1 = perfectly elastic) */\n\trestitution: number;\n\t/** Surface friction coefficient 0–1 */\n\tfriction: number;\n\t/** Per-entity gravity multiplier (0 = no gravity) */\n\tgravityScale: number;\n}\n\n/**\n * Component types directly provided by the physics plugin.\n */\nexport interface Physics2DOwnComponentTypes {\n\trigidBody: RigidBody;\n\tvelocity: Vector2D;\n\tforce: Vector2D;\n}\n\n/**\n * Full component types available when using the physics plugin\n * (own components + transform + collision dependencies).\n * Convenience alias for consumer code.\n */\nexport interface Physics2DComponentTypes<L extends string = never> extends TransformComponentTypes, CollisionComponentTypes<L>, Physics2DOwnComponentTypes {}\n\n// ==================== Resource Types ====================\n\n/**\n * Physics configuration resource.\n */\nexport interface Physics2DConfig {\n\tgravity: Vector2D;\n}\n\nexport interface Physics2DResourceTypes {\n\tphysicsConfig: Physics2DConfig;\n}\n\n// ==================== Event Types ====================\n\n/**\n * Event emitted for each physics collision pair.\n *\n * Normal components are flattened (`normalX`/`normalY`) rather than nested\n * in a `Vector2D` to avoid a per-event allocation in the physics hot path.\n */\nexport interface Physics2DCollisionEvent {\n\tentityA: number;\n\tentityB: number;\n\t/** Unit normal X, pointing from A toward B */\n\tnormalX: number;\n\t/** Unit normal Y, pointing from A toward B */\n\tnormalY: number;\n\t/** Penetration depth (positive) */\n\tdepth: number;\n}\n\nexport interface Physics2DEventTypes {\n\tphysicsCollision: Physics2DCollisionEvent;\n}\n\n// ==================== Plugin Options ====================\n\nexport interface Physics2DPluginOptions<G extends string = 'physics2D', CG extends string = never> {\n\t/** World gravity vector (default: {x: 0, y: 0}) */\n\tgravity?: Vector2D;\n\t/** System group name (default: 'physics2D') */\n\tsystemGroup?: G;\n\t/** Additional group for the collision system only (default: none).\n\t * When set, the collision system belongs to both `systemGroup` and this group,\n\t * allowing independent enable/disable of collision detection. */\n\tcollisionSystemGroup?: CG;\n\t/** Priority for integration system (default: 1000) */\n\tintegrationPriority?: number;\n\t/** Priority for collision system (default: 900) */\n\tcollisionPriority?: number;\n\t/** Execution phase (default: 'fixedUpdate') */\n\tphase?: SystemPhase;\n}\n\n// ==================== Helper Functions ====================\n\nexport interface RigidBodyOptions {\n\tmass?: number;\n\tdrag?: number;\n\trestitution?: number;\n\tfriction?: number;\n\tgravityScale?: number;\n}\n\n/**\n * Create a rigid body + force component pair.\n * Static bodies automatically get mass=Infinity.\n */\nexport function createRigidBody(\n\ttype: BodyType,\n\toptions?: RigidBodyOptions,\n): { rigidBody: RigidBody; force: Vector2D } {\n\treturn {\n\t\trigidBody: {\n\t\t\ttype,\n\t\t\tmass: type === 'static' ? Infinity : (options?.mass ?? 1),\n\t\t\tdrag: options?.drag ?? 0,\n\t\t\trestitution: options?.restitution ?? 0,\n\t\t\tfriction: options?.friction ?? 0,\n\t\t\tgravityScale: options?.gravityScale ?? 1,\n\t\t},\n\t\tforce: { x: 0, y: 0 },\n\t};\n}\n\n/**\n * Create a force component with initial values.\n */\nexport function createForce(x: number, y: number): { force: Vector2D } {\n\treturn { force: { x, y } };\n}\n\n/**\n * Accumulate a force onto an entity's force component.\n */\nexport function applyForce(\n\tecs: { getComponent(id: number, name: 'force'): Vector2D | undefined },\n\tentityId: number,\n\tfx: number,\n\tfy: number,\n): void {\n\tconst force = ecs.getComponent(entityId, 'force');\n\tif (!force) return;\n\tforce.x += fx;\n\tforce.y += fy;\n}\n\n/**\n * Apply an instantaneous impulse: velocity += impulse / mass.\n */\nexport function applyImpulse(\n\tecs: {\n\t\tgetComponent(id: number, name: 'velocity'): Vector2D | undefined;\n\t\tgetComponent(id: number, name: 'rigidBody'): RigidBody | undefined;\n\t},\n\tentityId: number,\n\tix: number,\n\tiy: number,\n): void {\n\tconst velocity = ecs.getComponent(entityId, 'velocity');\n\tconst rigidBody = ecs.getComponent(entityId, 'rigidBody');\n\tif (!velocity || !rigidBody) return;\n\tif (rigidBody.mass === Infinity || rigidBody.mass === 0) return;\n\tvelocity.x += ix / rigidBody.mass;\n\tvelocity.y += iy / rigidBody.mass;\n}\n\n/**\n * Directly set an entity's velocity.\n */\nexport function setVelocity(\n\tecs: { getComponent(id: number, name: 'velocity'): Vector2D | undefined },\n\tentityId: number,\n\tvx: number,\n\tvy: number,\n): void {\n\tconst velocity = ecs.getComponent(entityId, 'velocity');\n\tif (!velocity) return;\n\tvelocity.x = vx;\n\tvelocity.y = vy;\n}\n\n// ==================== Internal: Collider Info ====================\n\ninterface Physics2DColliderInfo<L extends string = string> extends BaseColliderInfo<L> {\n\trigidBody: RigidBody;\n\tvelocity: Vector2D;\n}\n\n// ==================== Collision Response ====================\n\ninterface PhysicsEcsLike {\n\tgetComponent(id: number, name: 'localTransform'): { x: number; y: number } | undefined;\n\teventBus: { publish(event: 'physicsCollision', data: Physics2DCollisionEvent): void };\n\tmarkChanged(entityId: number, componentName: 'localTransform' | 'velocity'): void;\n}\n\n/**\n * Resolve a physics collision pair: position correction, impulse response, event.\n */\nfunction resolvePhysicsContact(\n\ta: Physics2DColliderInfo,\n\tb: Physics2DColliderInfo,\n\tcontact: Contact,\n\tecs: PhysicsEcsLike,\n): void {\n\tconst invMassA = (a.rigidBody.type === 'dynamic' && a.rigidBody.mass > 0 && a.rigidBody.mass !== Infinity)\n\t\t? 1 / a.rigidBody.mass\n\t\t: 0;\n\tconst invMassB = (b.rigidBody.type === 'dynamic' && b.rigidBody.mass > 0 && b.rigidBody.mass !== Infinity)\n\t\t? 1 / b.rigidBody.mass\n\t\t: 0;\n\tconst totalInvMass = invMassA + invMassB;\n\n\t// Position correction\n\tif (totalInvMass > 0) {\n\t\tconst correctionScale = contact.depth / totalInvMass;\n\n\t\tif (invMassA > 0) {\n\t\t\tconst ltA = ecs.getComponent(a.entityId, 'localTransform');\n\t\t\tif (!ltA) return;\n\t\t\tltA.x -= correctionScale * invMassA * contact.normalX;\n\t\t\tltA.y -= correctionScale * invMassA * contact.normalY;\n\t\t\t// Update cached position for subsequent pairs (collider offset already baked in)\n\t\t\ta.x = ltA.x;\n\t\t\tecs.markChanged(a.entityId, 'localTransform');\n\t\t}\n\n\t\tif (invMassB > 0) {\n\t\t\tconst ltB = ecs.getComponent(b.entityId, 'localTransform');\n\t\t\tif (!ltB) return;\n\t\t\tltB.x += correctionScale * invMassB * contact.normalX;\n\t\t\tltB.y += correctionScale * invMassB * contact.normalY;\n\t\t\tecs.markChanged(b.entityId, 'localTransform');\n\t\t}\n\n\t\t// Velocity response (impulse-based)\n\t\tconst relVelX = b.velocity.x - a.velocity.x;\n\t\tconst relVelY = b.velocity.y - a.velocity.y;\n\t\tconst velAlongNormal = relVelX * contact.normalX + relVelY * contact.normalY;\n\n\t\tif (velAlongNormal < 0) {\n\t\t\tconst restitution = Math.min(a.rigidBody.restitution, b.rigidBody.restitution);\n\t\t\tconst normalImpulse = -(1 + restitution) * velAlongNormal / totalInvMass;\n\n\t\t\ta.velocity.x -= normalImpulse * invMassA * contact.normalX;\n\t\t\ta.velocity.y -= normalImpulse * invMassA * contact.normalY;\n\t\t\tb.velocity.x += normalImpulse * invMassB * contact.normalX;\n\t\t\tb.velocity.y += normalImpulse * invMassB * contact.normalY;\n\n\t\t\t// Friction (tangential impulse)\n\t\t\tconst tangentX = relVelX - velAlongNormal * contact.normalX;\n\t\t\tconst tangentY = relVelY - velAlongNormal * contact.normalY;\n\t\t\tconst tangentSpeed = Math.sqrt(tangentX * tangentX + tangentY * tangentY);\n\n\t\t\tif (tangentSpeed > 1e-6) {\n\t\t\t\tconst tangentNX = tangentX / tangentSpeed;\n\t\t\t\tconst tangentNY = tangentY / tangentSpeed;\n\t\t\t\tconst friction = Math.sqrt(a.rigidBody.friction * b.rigidBody.friction);\n\t\t\t\tconst maxFrictionImpulse = friction * Math.abs(normalImpulse);\n\t\t\t\tconst tangentImpulse = Math.min(tangentSpeed / totalInvMass, maxFrictionImpulse);\n\n\t\t\t\ta.velocity.x += tangentImpulse * invMassA * tangentNX;\n\t\t\t\ta.velocity.y += tangentImpulse * invMassA * tangentNY;\n\t\t\t\tb.velocity.x -= tangentImpulse * invMassB * tangentNX;\n\t\t\t\tb.velocity.y -= tangentImpulse * invMassB * tangentNY;\n\t\t\t}\n\t\t}\n\n\t\tecs.markChanged(a.entityId, 'velocity');\n\t\tecs.markChanged(b.entityId, 'velocity');\n\t}\n\n\tecs.eventBus.publish('physicsCollision', {\n\t\tentityA: a.entityId,\n\t\tentityB: b.entityId,\n\t\tnormalX: contact.normalX,\n\t\tnormalY: contact.normalY,\n\t\tdepth: contact.depth,\n\t});\n}\n\n// ==================== Module-level Physics Callback ====================\n\nfunction onPhysicsContact(\n\ta: Physics2DColliderInfo,\n\tb: Physics2DColliderInfo,\n\tcontact: Contact,\n\tecs: PhysicsEcsLike,\n): void {\n\tresolvePhysicsContact(a, b, contact, ecs);\n}\n\n// ==================== Plugin Factory ====================\n\n/**\n * Create a 2D physics plugin for ECSpresso.\n *\n * Provides:\n * - Semi-implicit Euler integration (gravity, forces, drag → velocity → position)\n * - Impulse-based collision response with restitution and friction\n * - physicsCollision events with contact normal and depth\n *\n * @example\n * ```typescript\n * const ecs = ECSpresso.create()\n * .withPlugin(createTransformPlugin())\n * .withPlugin(createPhysics2DPlugin({ gravity: { x: 0, y: 980 } }))\n * .withFixedTimestep(1/60)\n * .build();\n *\n * ecs.spawn({\n * ...createTransform(100, 200),\n * ...createRigidBody('dynamic', { mass: 1, restitution: 0.5 }),\n * velocity: { x: 0, y: 0 },\n * ...createAABBCollider(32, 32),\n * ...createCollisionLayer('player', ['ground']),\n * });\n * ```\n */\n\ntype Physics2DProvides<L extends string = never> = Physics2DOwnComponentTypes & CollisionComponentTypes<L>;\n\nexport function createPhysics2DPlugin<L extends string = never, G extends string = 'physics2D', CG extends string = never>(\n\toptions?: Physics2DPluginOptions<G, CG> & { layers?: LayerFactories<Record<L, readonly string[]>> },\n) {\n\tconst {\n\t\tgravity = { x: 0, y: 0 },\n\t\tsystemGroup = 'physics2D',\n\t\tcollisionSystemGroup,\n\t\tintegrationPriority = 1000,\n\t\tcollisionPriority = 900,\n\t\tphase = 'fixedUpdate',\n\t} = options ?? {};\n\n\treturn definePlugin('physics2D')\n\t\t.withComponentTypes<Physics2DProvides<L>>()\n\t\t.withEventTypes<Physics2DEventTypes>()\n\t\t.withResourceTypes<Physics2DResourceTypes>()\n\t\t.withLabels<'physics2D-integration' | 'physics2D-collision'>()\n\t\t.withGroups<G | CG>()\n\t\t.requires<TransformWorldConfig>()\n\t\t.install((world) => {\n\t\t\t// rigidBody requires velocity and force — auto-add with zero defaults\n\t\t\tworld.registerRequired('rigidBody', 'velocity', () => ({ x: 0, y: 0 }));\n\t\t\tworld.registerRequired('rigidBody', 'force', () => ({ x: 0, y: 0 }));\n\n\t\t\tworld.addResource('physicsConfig', { gravity: { x: gravity.x, y: gravity.y } });\n\n\t\t\t// ==================== Integration System ====================\n\n\t\t\tworld\n\t\t\t\t.addSystem('physics2D-integration')\n\t\t\t\t.setPriority(integrationPriority)\n\t\t\t\t.inPhase(phase)\n\t\t\t\t.inGroup(systemGroup)\n\t\t\t\t.addQuery('bodies', {\n\t\t\t\t\twith: ['localTransform', 'velocity', 'rigidBody', 'force'],\n\t\t\t\t})\n\t\t\t\t.setProcess(({ queries, dt, ecs }) => {\n\t\t\t\t\tconst config = ecs.getResource('physicsConfig');\n\t\t\t\t\tconst gx = config.gravity.x;\n\t\t\t\t\tconst gy = config.gravity.y;\n\n\t\t\t\t\tfor (const entity of queries.bodies) {\n\t\t\t\t\t\tconst { localTransform, velocity, rigidBody, force } = entity.components;\n\n\t\t\t\t\t\t// Static bodies: skip entirely\n\t\t\t\t\t\tif (rigidBody.type === 'static') continue;\n\n\t\t\t\t\t\t// Dynamic bodies: apply gravity, forces, drag\n\t\t\t\t\t\tif (rigidBody.type === 'dynamic') {\n\t\t\t\t\t\t\t// 1. Gravity\n\t\t\t\t\t\t\tvelocity.x += gx * rigidBody.gravityScale * dt;\n\t\t\t\t\t\t\tvelocity.y += gy * rigidBody.gravityScale * dt;\n\n\t\t\t\t\t\t\t// 2. Forces (F = ma → a = F/m)\n\t\t\t\t\t\t\tif (rigidBody.mass > 0 && rigidBody.mass !== Infinity) {\n\t\t\t\t\t\t\t\tvelocity.x += (force.x / rigidBody.mass) * dt;\n\t\t\t\t\t\t\t\tvelocity.y += (force.y / rigidBody.mass) * dt;\n\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\t// 3. Drag\n\t\t\t\t\t\t\tif (rigidBody.drag > 0) {\n\t\t\t\t\t\t\t\tconst damping = Math.max(0, 1 - rigidBody.drag * dt);\n\t\t\t\t\t\t\t\tvelocity.x *= damping;\n\t\t\t\t\t\t\t\tvelocity.y *= damping;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\t// Both dynamic and kinematic: integrate position\n\t\t\t\t\t\tlocalTransform.x += velocity.x * dt;\n\t\t\t\t\t\tlocalTransform.y += velocity.y * dt;\n\n\t\t\t\t\t\t// Clear accumulated forces\n\t\t\t\t\t\tforce.x = 0;\n\t\t\t\t\t\tforce.y = 0;\n\n\t\t\t\t\t\tecs.markChanged(entity.id, 'localTransform');\n\t\t\t\t\t}\n\t\t\t\t});\n\n\t\t\t// ==================== Collision System ====================\n\n\t\t\tconst collisionSystem = world\n\t\t\t\t.addSystem('physics2D-collision')\n\t\t\t\t.setPriority(collisionPriority)\n\t\t\t\t.inPhase(phase)\n\t\t\t\t.inGroup(systemGroup);\n\n\t\t\tif (collisionSystemGroup) {\n\t\t\t\tcollisionSystem.inGroup(collisionSystemGroup);\n\t\t\t}\n\n\t\t\t// Grow-only pool of Physics2DColliderInfo slots reused across frames.\n\t\t\t// Steady-state: zero allocations per frame once the pool is warm.\n\t\t\tconst colliderPool: Physics2DColliderInfo<L>[] = [];\n\t\t\t// Reusable entityId → collider lookup for the broadphase path.\n\t\t\tconst broadphaseMap = new Map<number, Physics2DColliderInfo<L>>();\n\n\t\t\tcollisionSystem\n\t\t\t\t.addQuery('collidables', {\n\t\t\t\t\twith: ['localTransform', 'rigidBody', 'velocity', 'collisionLayer'],\n\t\t\t\t})\n\t\t\t\t.setProcess(({ queries, ecs }) => {\n\t\t\t\t\tlet count = 0;\n\n\t\t\t\t\tfor (const entity of queries.collidables) {\n\t\t\t\t\t\tconst { localTransform, rigidBody, velocity, collisionLayer } = entity.components;\n\t\t\t\t\t\tconst aabb = ecs.getComponent(entity.id, 'aabbCollider');\n\t\t\t\t\t\tconst circle = ecs.getComponent(entity.id, 'circleCollider');\n\t\t\t\t\t\tif (!aabb && !circle) continue;\n\n\t\t\t\t\t\tlet slot = colliderPool[count];\n\t\t\t\t\t\tif (!slot) {\n\t\t\t\t\t\t\tslot = {\n\t\t\t\t\t\t\t\tentityId: entity.id,\n\t\t\t\t\t\t\t\tx: localTransform.x,\n\t\t\t\t\t\t\t\ty: localTransform.y,\n\t\t\t\t\t\t\t\tlayer: collisionLayer.layer,\n\t\t\t\t\t\t\t\tcollidesWith: collisionLayer.collidesWith,\n\t\t\t\t\t\t\t\tshape: AABB_SHAPE,\n\t\t\t\t\t\t\t\thalfWidth: 0,\n\t\t\t\t\t\t\t\thalfHeight: 0,\n\t\t\t\t\t\t\t\tradius: 0,\n\t\t\t\t\t\t\t\trigidBody,\n\t\t\t\t\t\t\t\tvelocity,\n\t\t\t\t\t\t\t};\n\t\t\t\t\t\t\tcolliderPool[count] = slot;\n\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\tslot.rigidBody = rigidBody;\n\t\t\t\t\t\t\tslot.velocity = velocity;\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tif (!fillBaseColliderInfo(\n\t\t\t\t\t\t\tslot,\n\t\t\t\t\t\t\tentity.id, localTransform.x, localTransform.y,\n\t\t\t\t\t\t\tcollisionLayer.layer, collisionLayer.collidesWith,\n\t\t\t\t\t\t\taabb, circle,\n\t\t\t\t\t\t)) continue;\n\n\t\t\t\t\t\tcount++;\n\t\t\t\t\t}\n\n\t\t\t\t\tconst si = tryGetSpatialIndex(ecs.tryGetResource.bind(ecs));\n\t\t\t\t\tdetectCollisions(colliderPool, count, broadphaseMap, si, onPhysicsContact, ecs);\n\t\t\t\t});\n\t\t});\n}\n",
6
+ "/**\n * Shared Narrowphase Module\n *\n * Provides contact-computing narrowphase tests and a generic collision\n * iteration pipeline used by both the collision plugin (event-only) and\n * the physics2D plugin (impulse response).\n */\n\nimport type { SpatialIndex } from './spatial-hash';\n\n// ==================== Contact ====================\n\n/**\n * Contact result from a narrowphase test. Normal points from A toward B.\n *\n * Narrowphase functions use this as an out-parameter: the caller owns the\n * struct, the function writes fields in place and returns `true` on hit.\n * The `onContact` callback in `detectCollisions` receives a shared module-\n * level instance — **subscribers must consume it synchronously and must not\n * retain the reference across frames**.\n */\nexport interface Contact {\n\tnormalX: number;\n\tnormalY: number;\n\t/** Penetration depth (positive = overlapping) */\n\tdepth: number;\n}\n\n/**\n * Module-level reusable Contact passed down from `detectCollisions` into\n * narrowphase tests and forwarded to the `onContact` callback. Reused across\n * every pair in every frame — zero allocation in the narrowphase hot path.\n */\nconst _sharedContact: Contact = { normalX: 0, normalY: 0, depth: 0 };\n\n// ==================== BaseColliderInfo ====================\n\n/** Collider shape discriminator for the flattened BaseColliderInfo layout. */\nexport const AABB_SHAPE = 0;\nexport const CIRCLE_SHAPE = 1;\nexport type ColliderShape = typeof AABB_SHAPE | typeof CIRCLE_SHAPE;\n\n/**\n * Minimum collider data shared by collision and physics bundles.\n *\n * Flat layout (no nested `aabb` / `circle` sub-objects): the `shape`\n * discriminator tells you whether to read `halfWidth`/`halfHeight`\n * (AABB) or `radius` (Circle). Unused fields are set to 0.\n *\n * This shape is pool-friendly — all fields are assigned in place each\n * frame without allocating nested objects.\n */\nexport interface BaseColliderInfo<L extends string = string> {\n\tentityId: number;\n\tx: number;\n\ty: number;\n\tlayer: L;\n\tcollidesWith: readonly L[];\n\tshape: ColliderShape;\n\thalfWidth: number;\n\thalfHeight: number;\n\tradius: number;\n}\n\n// ==================== Collider Construction ====================\n\n/**\n * Populate a `BaseColliderInfo` slot in place from raw component data.\n * Returns `true` if the slot was filled, `false` if the entity has no\n * collider (caller should skip it).\n *\n * If an entity has both AABB and circle colliders, AABB wins and only\n * the AABB offset is applied. This matches the dispatch precedence in\n * `computeContact`; the previous implementation stacked both offsets,\n * which was a bug.\n */\nexport function fillBaseColliderInfo<L extends string>(\n\tinfo: BaseColliderInfo<L>,\n\tentityId: number,\n\tx: number,\n\ty: number,\n\tlayer: L,\n\tcollidesWith: readonly L[],\n\taabb: { width: number; height: number; offsetX?: number; offsetY?: number } | undefined,\n\tcircle: { radius: number; offsetX?: number; offsetY?: number } | undefined,\n): boolean {\n\tinfo.entityId = entityId;\n\tinfo.layer = layer;\n\tinfo.collidesWith = collidesWith;\n\n\tif (aabb) {\n\t\tinfo.x = x + (aabb.offsetX ?? 0);\n\t\tinfo.y = y + (aabb.offsetY ?? 0);\n\t\tinfo.shape = AABB_SHAPE;\n\t\tinfo.halfWidth = aabb.width / 2;\n\t\tinfo.halfHeight = aabb.height / 2;\n\t\tinfo.radius = 0;\n\t\treturn true;\n\t}\n\n\tif (circle) {\n\t\tinfo.x = x + (circle.offsetX ?? 0);\n\t\tinfo.y = y + (circle.offsetY ?? 0);\n\t\tinfo.shape = CIRCLE_SHAPE;\n\t\tinfo.halfWidth = 0;\n\t\tinfo.halfHeight = 0;\n\t\tinfo.radius = circle.radius;\n\t\treturn true;\n\t}\n\n\treturn false;\n}\n\n// ==================== Spatial Index Lookup ====================\n\n/**\n * Retrieve the optional spatialIndex resource, returning undefined when absent.\n * Centralizes the cross-plugin typed lookup so individual plugins don't each\n * need to import SpatialIndex or repeat the tryGetResource pattern.\n */\nexport function tryGetSpatialIndex(\n\ttryGetResource: <T>(key: string) => T | undefined,\n): SpatialIndex | undefined {\n\treturn tryGetResource<SpatialIndex>('spatialIndex');\n}\n\n// ==================== Narrowphase Tests ====================\n\n/**\n * Write an AABB-AABB contact into `out`. Returns `true` if the shapes\n * overlap (out was filled), `false` otherwise.\n */\nexport function computeAABBvsAABB(\n\tax: number, ay: number, ahw: number, ahh: number,\n\tbx: number, by: number, bhw: number, bhh: number,\n\tout: Contact,\n): boolean {\n\tconst dx = bx - ax;\n\tconst dy = by - ay;\n\tconst overlapX = (ahw + bhw) - Math.abs(dx);\n\tconst overlapY = (ahh + bhh) - Math.abs(dy);\n\n\tif (overlapX <= 0 || overlapY <= 0) return false;\n\n\tif (overlapX < overlapY) {\n\t\tout.normalX = dx >= 0 ? 1 : -1;\n\t\tout.normalY = 0;\n\t\tout.depth = overlapX;\n\t\treturn true;\n\t}\n\tout.normalX = 0;\n\tout.normalY = dy >= 0 ? 1 : -1;\n\tout.depth = overlapY;\n\treturn true;\n}\n\nexport function computeCircleVsCircle(\n\tax: number, ay: number, ar: number,\n\tbx: number, by: number, br: number,\n\tout: Contact,\n): boolean {\n\tconst dx = bx - ax;\n\tconst dy = by - ay;\n\tconst distSq = dx * dx + dy * dy;\n\tconst radiusSum = ar + br;\n\n\tif (distSq >= radiusSum * radiusSum) return false;\n\n\tconst dist = Math.sqrt(distSq);\n\tif (dist === 0) {\n\t\tout.normalX = 1;\n\t\tout.normalY = 0;\n\t\tout.depth = radiusSum;\n\t\treturn true;\n\t}\n\tout.normalX = dx / dist;\n\tout.normalY = dy / dist;\n\tout.depth = radiusSum - dist;\n\treturn true;\n}\n\nexport function computeAABBvsCircle(\n\taabbX: number, aabbY: number, ahw: number, ahh: number,\n\tcircleX: number, circleY: number, radius: number,\n\tout: Contact,\n): boolean {\n\tconst closestX = Math.max(aabbX - ahw, Math.min(circleX, aabbX + ahw));\n\tconst closestY = Math.max(aabbY - ahh, Math.min(circleY, aabbY + ahh));\n\n\tconst dx = circleX - closestX;\n\tconst dy = circleY - closestY;\n\tconst distSq = dx * dx + dy * dy;\n\n\tif (distSq >= radius * radius) return false;\n\n\t// Circle center inside AABB\n\tif (distSq === 0) {\n\t\tconst pushLeft = (circleX - (aabbX - ahw));\n\t\tconst pushRight = ((aabbX + ahw) - circleX);\n\t\tconst pushUp = (circleY - (aabbY - ahh));\n\t\tconst pushDown = ((aabbY + ahh) - circleY);\n\t\tconst minPush = Math.min(pushLeft, pushRight, pushUp, pushDown);\n\n\t\tif (minPush === pushRight) {\n\t\t\tout.normalX = 1; out.normalY = 0; out.depth = pushRight + radius;\n\t\t\treturn true;\n\t\t}\n\t\tif (minPush === pushLeft) {\n\t\t\tout.normalX = -1; out.normalY = 0; out.depth = pushLeft + radius;\n\t\t\treturn true;\n\t\t}\n\t\tif (minPush === pushDown) {\n\t\t\tout.normalX = 0; out.normalY = 1; out.depth = pushDown + radius;\n\t\t\treturn true;\n\t\t}\n\t\tout.normalX = 0; out.normalY = -1; out.depth = pushUp + radius;\n\t\treturn true;\n\t}\n\n\tconst dist = Math.sqrt(distSq);\n\tout.normalX = dx / dist;\n\tout.normalY = dy / dist;\n\tout.depth = radius - dist;\n\treturn true;\n}\n\n// ==================== Contact Dispatcher ====================\n\n/**\n * Dispatch to the correct narrowphase function for the given pair and\n * write the contact into `out`. Returns `true` if the pair overlaps.\n */\nexport function computeContact(a: BaseColliderInfo, b: BaseColliderInfo, out: Contact): boolean {\n\tif (a.shape === AABB_SHAPE && b.shape === AABB_SHAPE) {\n\t\treturn computeAABBvsAABB(\n\t\t\ta.x, a.y, a.halfWidth, a.halfHeight,\n\t\t\tb.x, b.y, b.halfWidth, b.halfHeight,\n\t\t\tout,\n\t\t);\n\t}\n\n\tif (a.shape === CIRCLE_SHAPE && b.shape === CIRCLE_SHAPE) {\n\t\treturn computeCircleVsCircle(\n\t\t\ta.x, a.y, a.radius,\n\t\t\tb.x, b.y, b.radius,\n\t\t\tout,\n\t\t);\n\t}\n\n\tif (a.shape === AABB_SHAPE && b.shape === CIRCLE_SHAPE) {\n\t\treturn computeAABBvsCircle(\n\t\t\ta.x, a.y, a.halfWidth, a.halfHeight,\n\t\t\tb.x, b.y, b.radius,\n\t\t\tout,\n\t\t);\n\t}\n\n\t// a is Circle, b is AABB — compute as AABB-vs-Circle, then flip normal in place\n\tif (!computeAABBvsCircle(\n\t\tb.x, b.y, b.halfWidth, b.halfHeight,\n\t\ta.x, a.y, a.radius,\n\t\tout,\n\t)) return false;\n\tout.normalX = -out.normalX;\n\tout.normalY = -out.normalY;\n\treturn true;\n}\n\n// ==================== Collision Iteration ====================\n\n/** Module-level reusable set for broadphase candidates. */\nconst _broadphaseCandidates = new Set<number>();\n\nlet _bruteForceWarned = false;\nconst BRUTE_FORCE_WARN_THRESHOLD = 50;\n\n/**\n * Generic collision detection pipeline: brute-force or broadphase,\n * with layer filtering and contact computation.\n *\n * `count` is the number of live entries at the front of `colliders`.\n * The array itself may be a grow-only pool — only indices `[0, count)`\n * are iterated, so trailing pool slots are ignored.\n *\n * `workingMap` is a caller-owned `Map<number, I>` used by the broadphase\n * path as an entityId → collider lookup. It is cleared and repopulated on\n * each call; callers should allocate it once and pass the same instance\n * every frame. Unused by the brute-force path but still required so that\n * typed reuse is the default, not an opt-in.\n *\n * Uses a context parameter forwarded to the callback to avoid\n * per-frame closure allocation.\n */\nexport function detectCollisions<I extends BaseColliderInfo, C>(\n\tcolliders: I[],\n\tcount: number,\n\tworkingMap: Map<number, I>,\n\tspatialIndex: SpatialIndex | undefined,\n\tonContact: (a: I, b: I, contact: Contact, context: C) => void,\n\tcontext: C,\n): void {\n\tif (spatialIndex) {\n\t\tbroadphaseDetect(colliders, count, workingMap, spatialIndex, onContact, context);\n\t} else {\n\t\tbruteForceDetect(colliders, count, onContact, context);\n\t}\n}\n\nfunction bruteForceDetect<I extends BaseColliderInfo, C>(\n\tcolliders: I[],\n\tcount: number,\n\tonContact: (a: I, b: I, contact: Contact, context: C) => void,\n\tcontext: C,\n): void {\n\tif (!_bruteForceWarned && count >= BRUTE_FORCE_WARN_THRESHOLD) {\n\t\t_bruteForceWarned = true;\n\t\tconsole.warn(\n\t\t\t`[ecspresso] Collision detection is using O(n²) brute force with ${count} colliders. ` +\n\t\t\t`For better performance, install createSpatialIndexPlugin() alongside your collision or physics2D plugin.`,\n\t\t);\n\t}\n\n\tfor (let i = 0; i < count; i++) {\n\t\tconst a = colliders[i];\n\t\tif (!a) continue;\n\n\t\tfor (let j = i + 1; j < count; j++) {\n\t\t\tconst b = colliders[j];\n\t\t\tif (!b) continue;\n\n\t\t\tif (!a.collidesWith.includes(b.layer) && !b.collidesWith.includes(a.layer)) continue;\n\n\t\t\tif (!computeContact(a, b, _sharedContact)) continue;\n\n\t\t\tonContact(a, b, _sharedContact, context);\n\t\t}\n\t}\n}\n\nfunction broadphaseDetect<I extends BaseColliderInfo, C>(\n\tcolliders: I[],\n\tcount: number,\n\tcolliderMap: Map<number, I>,\n\tspatialIndex: SpatialIndex,\n\tonContact: (a: I, b: I, contact: Contact, context: C) => void,\n\tcontext: C,\n): void {\n\tcolliderMap.clear();\n\tfor (let i = 0; i < count; i++) {\n\t\tconst c = colliders[i];\n\t\tif (!c) continue;\n\t\tcolliderMap.set(c.entityId, c);\n\t}\n\n\tfor (let i = 0; i < count; i++) {\n\t\tconst a = colliders[i];\n\t\tif (!a) continue;\n\n\t\tconst aHalfW = a.shape === AABB_SHAPE ? a.halfWidth : a.radius;\n\t\tconst aHalfH = a.shape === AABB_SHAPE ? a.halfHeight : a.radius;\n\n\t\t_broadphaseCandidates.clear();\n\t\tspatialIndex.queryRectInto(\n\t\t\ta.x - aHalfW, a.y - aHalfH,\n\t\t\ta.x + aHalfW, a.y + aHalfH,\n\t\t\t_broadphaseCandidates,\n\t\t);\n\n\t\tfor (const bId of _broadphaseCandidates) {\n\t\t\tif (bId <= a.entityId) continue;\n\n\t\t\tconst b = colliderMap.get(bId);\n\t\t\tif (!b) continue;\n\n\t\t\tif (!a.collidesWith.includes(b.layer) && !b.collidesWith.includes(a.layer)) continue;\n\n\t\t\tif (!computeContact(a, b, _sharedContact)) continue;\n\n\t\t\tonContact(a, b, _sharedContact, context);\n\t\t}\n\t}\n}\n"
7
+ ],
8
+ "mappings": "2PAWA,uBAAS,kBCsBT,IAAM,EAA0B,CAAE,QAAS,EAAG,QAAS,EAAG,MAAO,CAAE,EAKtD,EAAa,EAsCnB,SAAS,CAAsC,CACrD,EACA,EACA,EACA,EACA,EACA,EACA,EACA,EACU,CAKV,GAJA,EAAK,SAAW,EAChB,EAAK,MAAQ,EACb,EAAK,aAAe,EAEhB,EAOH,OANA,EAAK,EAAI,GAAK,EAAK,SAAW,GAC9B,EAAK,EAAI,GAAK,EAAK,SAAW,GAC9B,EAAK,MAvDmB,EAwDxB,EAAK,UAAY,EAAK,MAAQ,EAC9B,EAAK,WAAa,EAAK,OAAS,EAChC,EAAK,OAAS,EACP,GAGR,GAAI,EAOH,OANA,EAAK,EAAI,GAAK,EAAO,SAAW,GAChC,EAAK,EAAI,GAAK,EAAO,SAAW,GAChC,EAAK,MAhEqB,EAiE1B,EAAK,UAAY,EACjB,EAAK,WAAa,EAClB,EAAK,OAAS,EAAO,OACd,GAGR,MAAO,GAUD,SAAS,CAAkB,CACjC,EAC2B,CAC3B,OAAO,EAA6B,cAAc,EAS5C,SAAS,CAAiB,CAChC,EAAY,EAAY,EAAa,EACrC,EAAY,EAAY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