bard-legends-framework 0.9.5 → 0.9.7

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 (239) hide show
  1. package/dist/_interfaces/index.d.ts +2 -2
  2. package/dist/_interfaces/index.js +2 -3
  3. package/dist/game-entities/attachable/attachable.d.ts +6 -6
  4. package/dist/game-entities/attachable/attachable.js +28 -30
  5. package/dist/game-entities/attachable/attachable.spec.js +129 -128
  6. package/dist/game-entities/attachable/attachable.store.d.ts +5 -5
  7. package/dist/game-entities/attachable/attachable.store.js +34 -32
  8. package/dist/game-entities/attachable/attachable.test.js +132 -131
  9. package/dist/game-entities/attachable/destroyable.d.ts +13 -13
  10. package/dist/game-entities/attachable/destroyable.js +64 -69
  11. package/dist/game-entities/attachable/helpers/decorate-actions-lib.d.ts +15 -15
  12. package/dist/game-entities/attachable/helpers/decorate-actions-lib.js +125 -116
  13. package/dist/game-entities/attachable/helpers/referance-variable.d.ts +15 -15
  14. package/dist/game-entities/attachable/helpers/referance-variable.js +47 -48
  15. package/dist/game-entities/attachable/interfaces.d.ts +6 -6
  16. package/dist/game-entities/attachable/interfaces.js +2 -3
  17. package/dist/game-entities/base/helpers/decorate-actions-lib.js +0 -1
  18. package/dist/game-entities/controller/controller.js +0 -1
  19. package/dist/game-entities/controller/controller.spec.js +128 -125
  20. package/dist/game-entities/entity/entity.spec.js +263 -293
  21. package/dist/game-entities/entity/helpers/view-creation.helper.d.ts +7 -7
  22. package/dist/game-entities/entity/helpers/view-creation.helper.js +48 -48
  23. package/dist/game-entities/scene/scene.spec.js +219 -223
  24. package/dist/game-entities/service/service.js +0 -4
  25. package/dist/game-entities/service/service.spec.js +265 -237
  26. package/dist/game-entities/service/service.test.js +4 -2
  27. package/dist/game-entities/update-loop.d.ts +18 -18
  28. package/dist/game-entities/update-loop.js +46 -28
  29. package/dist/game-entities/view/view.js +1 -1
  30. package/dist/game-entities/view/view.spec.js +490 -506
  31. package/dist/game-entities/view/view.test.js +1 -1
  32. package/dist/lib/animator/animations.d.ts +13 -13
  33. package/dist/lib/animator/animations.js +43 -40
  34. package/dist/lib/animator/animator.d.ts +41 -41
  35. package/dist/lib/animator/animator.js +194 -197
  36. package/dist/lib/animator/animator.spec.js +699 -612
  37. package/dist/lib/animator/index.js +24 -19
  38. package/dist/lib/libraries/animator/animations.d.ts +13 -13
  39. package/dist/lib/libraries/animator/animations.js +43 -40
  40. package/dist/lib/libraries/animator/animator.d.ts +41 -41
  41. package/dist/lib/libraries/animator/animator.js +194 -197
  42. package/dist/lib/libraries/animator/animator.spec.js +699 -612
  43. package/dist/lib/libraries/animator/index.js +24 -19
  44. package/dist/lib/libraries/path-finder/path-finder.d.ts +2 -2
  45. package/dist/lib/libraries/path-finder/path-finder.js +19 -20
  46. package/dist/lib/path-finder/path-finder.d.ts +2 -2
  47. package/dist/lib/path-finder/path-finder.js +19 -20
  48. package/dist/lib/services/keyboard/keyboard.d.ts +4 -4
  49. package/dist/lib/services/keyboard/keyboard.js +39 -31
  50. package/dist/lib/services/mouse/mouse.service.d.ts +8 -8
  51. package/dist/lib/services/mouse/mouse.service.js +65 -53
  52. package/dist/lib/services/mouse/mouser-target-focus.service.d.ts +11 -11
  53. package/dist/lib/services/mouse/mouser-target-focus.service.js +83 -81
  54. package/dist/lib/update-loop.d.ts +10 -10
  55. package/dist/lib/update-loop.js +35 -27
  56. package/dist/physics/entitity-types/immovable-physics-entity.d.ts +6 -6
  57. package/dist/physics/entitity-types/immovable-physics-entity.js +23 -24
  58. package/dist/physics/entitity-types/movable-entity.d.ts +6 -6
  59. package/dist/physics/entitity-types/movable-entity.js +21 -22
  60. package/dist/physics/entitity-types/movable-physics-entity.d.ts +14 -14
  61. package/dist/physics/entitity-types/movable-physics-entity.js +83 -88
  62. package/dist/physics/entitity-types/physics-entity.d.ts +23 -23
  63. package/dist/physics/entitity-types/physics-entity.js +97 -88
  64. package/dist/physics/helpers/closest-available-space.helper.d.ts +16 -16
  65. package/dist/physics/helpers/closest-available-space.helper.js +98 -96
  66. package/dist/physics/helpers/closest-available-space.helper.spec.js +115 -68
  67. package/dist/physics/helpers/p2js.helper.d.ts +3 -3
  68. package/dist/physics/helpers/p2js.helper.js +21 -22
  69. package/dist/physics/helpers/shape-factory.d.ts +9 -4
  70. package/dist/physics/helpers/shape-factory.js +99 -75
  71. package/dist/physics/libs/p2js.helper.js +1 -1
  72. package/dist/physics/libs/position-to-grid-position-converter.d.ts +3 -3
  73. package/dist/physics/libs/position-to-grid-position-converter.js +15 -13
  74. package/dist/physics/module//360/237/223/220services/availability-grid.service.d.ts +10 -5
  75. package/dist/physics/module//360/237/223/220services/availability-grid.service.js +77 -57
  76. package/dist/physics/module//360/237/223/220services/border.service.d.ts +2 -2
  77. package/dist/physics/module//360/237/223/220services/border.service.js +57 -37
  78. package/dist/physics/module//360/237/223/220services/collision/physics-body-group.service.js +1 -1
  79. package/dist/physics/module//360/237/223/220services/collisions.service.d.ts +7 -7
  80. package/dist/physics/module//360/237/223/220services/collisions.service.js +110 -98
  81. package/dist/physics/module//360/237/223/220services/debug-visuals.service.d.ts +28 -6
  82. package/dist/physics/module//360/237/223/220services/debug-visuals.service.js +121 -89
  83. package/dist/physics/module//360/237/223/220services/eliptic-explosion.service.d.ts +15 -7
  84. package/dist/physics/module//360/237/223/220services/eliptic-explosion.service.js +117 -93
  85. package/dist/physics/module//360/237/223/220services/explosion.service.d.ts +14 -7
  86. package/dist/physics/module//360/237/223/220services/explosion.service.js +102 -77
  87. package/dist/physics/module//360/237/223/220services/explosions/eliptic-explosion.service.d.ts +15 -7
  88. package/dist/physics/module//360/237/223/220services/explosions/eliptic-explosion.service.js +117 -93
  89. package/dist/physics/module//360/237/223/220services/explosions/explosion.service.d.ts +14 -7
  90. package/dist/physics/module//360/237/223/220services/explosions/explosion.service.js +102 -77
  91. package/dist/physics/module//360/237/223/220services/explosions/helpers/ray-cast-hit-converter.d.ts +3 -3
  92. package/dist/physics/module//360/237/223/220services/explosions/helpers/ray-cast-hit-converter.js +71 -67
  93. package/dist/physics/module//360/237/223/220services/helpers/ray-cast-hit-converter.d.ts +3 -3
  94. package/dist/physics/module//360/237/223/220services/helpers/ray-cast-hit-converter.js +71 -67
  95. package/dist/physics/module//360/237/223/220services/impact/eliptic-explosion.service.js +1 -1
  96. package/dist/physics/module//360/237/223/220services/materials.service.d.ts +16 -11
  97. package/dist/physics/module//360/237/223/220services/materials.service.js +118 -108
  98. package/dist/physics/module//360/237/223/220services/path-finder.service.d.ts +19 -8
  99. package/dist/physics/module//360/237/223/220services/path-finder.service.js +121 -82
  100. package/dist/physics/module//360/237/223/220services/physics-world.service.d.ts +30 -15
  101. package/dist/physics/module//360/237/223/220services/physics-world.service.js +122 -100
  102. package/dist/physics/module//360/237/223/220services/ray-casting.service.d.ts +3 -3
  103. package/dist/physics/module//360/237/223/220services/ray-casting.service.js +87 -86
  104. package/dist/physics/module//360/237/223/220services/shape-creation.service.d.ts +9 -4
  105. package/dist/physics/module//360/237/223/220services/shape-creation.service.js +113 -84
  106. package/dist/physics/module//360/237/223/220services/test-visuals/test-visuals.service.js +2 -1
  107. package/dist/physics/module//360/237/247/212entities/data-structures/availability-grid-cache.d.ts +8 -8
  108. package/dist/physics/module//360/237/247/212entities/data-structures/availability-grid-cache.js +32 -33
  109. package/dist/physics/physics-world.d.ts +50 -34
  110. package/dist/physics/physics-world.js +142 -133
  111. package/dist/physics/sub-elements/available-spaces/helpers/availability-grid.helper.d.ts +3 -3
  112. package/dist/physics/sub-elements/available-spaces/helpers/availability-grid.helper.js +15 -13
  113. package/dist/physics/sub-elements/available-spaces/physics-availability-grid.d.ts +9 -9
  114. package/dist/physics/sub-elements/available-spaces/physics-availability-grid.js +82 -77
  115. package/dist/physics/sub-elements/available-spaces/physics-available-spaces.d.ts +18 -14
  116. package/dist/physics/sub-elements/available-spaces/physics-available-spaces.js +91 -79
  117. package/dist/physics/sub-elements/collisions/contact-equation-orginiser.d.ts +11 -11
  118. package/dist/physics/sub-elements/collisions/contact-equation-orginiser.js +84 -76
  119. package/dist/physics/sub-elements/collisions/physics-collision.d.ts +7 -7
  120. package/dist/physics/sub-elements/collisions/physics-collision.js +43 -44
  121. package/dist/physics/sub-elements/elements/physics-body-groups.d.ts +11 -11
  122. package/dist/physics/sub-elements/elements/physics-body-groups.js +54 -54
  123. package/dist/physics/sub-elements/elements/physics-materials.d.ts +15 -10
  124. package/dist/physics/sub-elements/elements/physics-materials.js +101 -100
  125. package/dist/physics/sub-elements/elements/physics-world-borders.d.ts +3 -3
  126. package/dist/physics/sub-elements/elements/physics-world-borders.js +69 -45
  127. package/dist/physics/sub-elements/physics-body-groups.d.ts +8 -8
  128. package/dist/physics/sub-elements/physics-body-groups.js +41 -42
  129. package/dist/physics/sub-elements/physics-collision.d.ts +16 -16
  130. package/dist/physics/sub-elements/physics-collision.js +141 -121
  131. package/dist/physics/sub-elements/physics-materials.d.ts +15 -10
  132. package/dist/physics/sub-elements/physics-materials.js +101 -100
  133. package/dist/physics/sub-elements/physics-world-borders.d.ts +2 -2
  134. package/dist/physics/sub-elements/physics-world-borders.js +25 -26
  135. package/dist/physics/sub-elements/raycasting/physics-body-explosion-hit.d.ts +15 -11
  136. package/dist/physics/sub-elements/raycasting/physics-body-explosion-hit.js +59 -59
  137. package/dist/physics/sub-elements/raycasting/physics-explosion.d.ts +13 -6
  138. package/dist/physics/sub-elements/raycasting/physics-explosion.js +108 -73
  139. package/dist/physics/sub-elements/raycasting/ray-cast.d.ts +19 -19
  140. package/dist/physics/sub-elements/raycasting/ray-cast.js +93 -98
  141. package/dist/pixi/display-object/components/filters.d.ts +18 -18
  142. package/dist/pixi/display-object/components/filters.js +106 -81
  143. package/dist/pixi/display-object/components/glow-effect.d.ts +8 -8
  144. package/dist/pixi/display-object/components/glow-effect.js +33 -36
  145. package/dist/pixi/display-object/components/glow-filter.d.ts +8 -8
  146. package/dist/pixi/display-object/components/glow-filter.js +33 -36
  147. package/dist/pixi/display-object/container-attributes.js +1 -1
  148. package/dist/pixi/display-object/display-object-attributes.d.ts +41 -41
  149. package/dist/pixi/display-object/display-object-attributes.js +148 -150
  150. package/dist/pixi/display-object/display-object.d.ts +17 -17
  151. package/dist/pixi/display-object/display-object.js +79 -75
  152. package/dist/pixi/display-object/filters/base-filters/alpha-primer-color.filter.d.ts +9 -4
  153. package/dist/pixi/display-object/filters/base-filters/alpha-primer-color.filter.js +89 -77
  154. package/dist/pixi/display-object/filters/base-filters/blur.filter.js +30 -30
  155. package/dist/pixi/display-object/filters/base-filters/contrast-filter.d.ts +9 -4
  156. package/dist/pixi/display-object/filters/base-filters/contrast-filter.js +62 -53
  157. package/dist/pixi/display-object/filters/base-filters/expand-with-alpha-color.d.ts +10 -5
  158. package/dist/pixi/display-object/filters/base-filters/expand-with-alpha-color.js +93 -81
  159. package/dist/pixi/display-object/filters/base-filters/expand-with-color-gradient.d.ts +12 -7
  160. package/dist/pixi/display-object/filters/base-filters/expand-with-color-gradient.js +120 -109
  161. package/dist/pixi/display-object/filters/base-filters/expand-with-color.d.ts +10 -5
  162. package/dist/pixi/display-object/filters/base-filters/expand-with-color.filter.js +32 -32
  163. package/dist/pixi/display-object/filters/base-filters/expand-with-color.js +92 -85
  164. package/dist/pixi/display-object/filters/templates/checking-neighbors.template.js +28 -28
  165. package/dist/pixi/display-object/filters/templates/plane.template.js +20 -20
  166. package/dist/pixi/display-object/filters.js +20 -4
  167. package/dist/pixi/display-object/objects/components/filters.d.ts +18 -18
  168. package/dist/pixi/display-object/objects/components/filters.js +106 -81
  169. package/dist/pixi/display-object/objects/container.d.ts +2 -2
  170. package/dist/pixi/display-object/objects/container.js +39 -30
  171. package/dist/pixi/display-object/objects/graphics.d.ts +28 -28
  172. package/dist/pixi/display-object/objects/graphics.js +218 -202
  173. package/dist/pixi/display-object/objects/helpers/filters.d.ts +25 -25
  174. package/dist/pixi/display-object/objects/helpers/filters.js +150 -124
  175. package/dist/pixi/display-object/objects/helpers/glow-sprite-generator.d.ts +7 -7
  176. package/dist/pixi/display-object/objects/helpers/glow-sprite-generator.js +114 -95
  177. package/dist/pixi/display-object/objects/premade-objects/sprite-with-glowing-shapes.d.ts +5 -5
  178. package/dist/pixi/display-object/objects/premade-objects/sprite-with-glowing-shapes.js +81 -57
  179. package/dist/pixi/display-object/objects/sprite.d.ts +43 -43
  180. package/dist/pixi/display-object/objects/sprite.js +248 -233
  181. package/dist/pixi/display-object/objects/text/helpers/process-steps/1-data-in-converter/rich-text-data-in-converter.js +1 -1
  182. package/dist/pixi/display-object/objects/text/helpers/process-steps/1-data-in-converter/rich-text-data-in-converter.test.js +9 -9
  183. package/dist/pixi/display-object/objects/text/helpers/rich-text-to-plane-text.d.ts +1 -1
  184. package/dist/pixi/display-object/objects/text.d.ts +25 -25
  185. package/dist/pixi/display-object/objects/text.js +111 -102
  186. package/dist/pixi/display-object/premade-objects/sprite-with-glowing-shapes.d.ts +13 -9
  187. package/dist/pixi/display-object/premade-objects/sprite-with-glowing-shapes.js +96 -67
  188. package/dist/pixi/game.d.ts +1 -1
  189. package/dist/pixi/game.js +2 -2
  190. package/dist/pixi/helpers/glow-sprite-generator.d.ts +6 -6
  191. package/dist/pixi/helpers/glow-sprite-generator.js +99 -84
  192. package/dist/pixi/helpers/pixi-container.helper.d.ts +1 -1
  193. package/dist/pixi/helpers/pixi-container.helper.js +10 -8
  194. package/dist/pixi/helpers/screen-position-to-stage.helper.d.ts +1 -1
  195. package/dist/pixi/helpers/screen-position-to-stage.helper.js +12 -12
  196. package/dist/pixi/helpers/screen-position-to-stage.helper.test.js +174 -79
  197. package/dist/pixi/modules/CAMERA/index.d.ts +1 -1
  198. package/dist/pixi/modules/CAMERA/index.js +3 -3
  199. package/dist/services/keyboard/keyboard.d.ts +4 -4
  200. package/dist/services/keyboard/keyboard.js +39 -31
  201. package/dist/services/mouse/mouse.service.d.ts +8 -8
  202. package/dist/services/mouse/mouse.service.js +65 -53
  203. package/dist/services/mouse/mouser-target-focus.service.d.ts +11 -11
  204. package/dist/services/mouse/mouser-target-focus.service.js +83 -81
  205. package/dist/utilities/libraries/animator/animating-content/state-animation.d.ts +29 -26
  206. package/dist/utilities/libraries/animator/animating-content/state-animation.js +159 -165
  207. package/dist/utilities/libraries/animator/animating-content/state-animation.test.js +386 -375
  208. package/dist/utilities/libraries/animator/animator.spec.js +765 -656
  209. package/dist/utilities/libraries/binary-heap/binary-heap.d.ts +11 -11
  210. package/dist/utilities/libraries/binary-heap/binary-heap.js +79 -81
  211. package/dist/utilities/libraries/data-structures/binary-heap/binary-heap.d.ts +11 -11
  212. package/dist/utilities/libraries/data-structures/binary-heap/binary-heap.js +79 -81
  213. package/dist/utilities/libraries/data-structures/grid/grid.d.ts +35 -31
  214. package/dist/utilities/libraries/data-structures/grid/grid.js +128 -117
  215. package/dist/utilities/libraries/data-structures/grid/grid.spec.js +176 -138
  216. package/dist/utilities/libraries/data-structures/grid/grid.test.js +218 -141
  217. package/dist/utilities/libraries/data-structures/grid/position-to-grid-position-converter.d.ts +3 -3
  218. package/dist/utilities/libraries/data-structures/grid/position-to-grid-position-converter.js +15 -13
  219. package/dist/utilities/libraries/data-structures/queue/queue.d.ts +21 -21
  220. package/dist/utilities/libraries/data-structures/queue/queue.js +63 -66
  221. package/dist/utilities/libraries/data-structures/queue/queue.test.js +54 -55
  222. package/dist/utilities/libraries/grid-algorithms/closest-available-space/closest-available-space.helper.spec.js +122 -68
  223. package/dist/utilities/libraries/grid-algorithms/path-finder/path-finder.spec.js +140 -135
  224. package/dist/utilities/libraries/grid-algorithms/vector-field-path-finder/vector-field-path-finder.spec.js +293 -229
  225. package/dist/utilities/libraries/path-finder/path-finder.d.ts +7 -7
  226. package/dist/utilities/libraries/path-finder/path-finder.js +113 -109
  227. package/dist/utilities/libraries/path-finder/path-finder.spec.js +131 -106
  228. package/dist/utilities/services/keyboard/keyboard.d.ts +4 -4
  229. package/dist/utilities/services/keyboard/keyboard.js +42 -34
  230. package/dist/utilities/services/mouse/helpers/mouse-position.helper.d.ts +1 -1
  231. package/dist/utilities/services/mouse/helpers/mouse-position.helper.js +12 -12
  232. package/dist/utilities/services/mouse/helpers/mouse-position.helper.test.js +168 -73
  233. package/dist/utilities/services/mouse/mouse-target-focus.service.d.ts +15 -15
  234. package/dist/utilities/services/mouse/mouse-target-focus.service.js +79 -76
  235. package/dist/utilities/services/mouse/mouse.service.d.ts +9 -9
  236. package/dist/utilities/services/mouse/mouse.service.js +86 -72
  237. package/dist/utilities/services/mouse/mouser-target-focus.service.d.ts +11 -11
  238. package/dist/utilities/services/mouse/mouser-target-focus.service.js +83 -81
  239. package/package.json +6 -12
@@ -1,97 +1,106 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
1
+ Object.defineProperty(exports, '__esModule', { value: true });
3
2
  exports.PhysicsEntity = void 0;
4
- const actions_lib_1 = require("actions-lib");
5
- const helpers_lib_1 = require("helpers-lib");
6
- const physics_world_1 = require("../physics-world");
7
- const p2js_helper_1 = require("../helpers/p2js.helper");
8
- const shape_factory_1 = require("../helpers/shape-factory");
9
- const game_entities_1 = require("../../game-entities");
10
- const update_cycle_1 = require("../../game-entities/update-cycle");
3
+ const actions_lib_1 = require('actions-lib');
4
+ const helpers_lib_1 = require('helpers-lib');
5
+ const physics_world_1 = require('../physics-world');
6
+ const p2js_helper_1 = require('../helpers/p2js.helper');
7
+ const shape_factory_1 = require('../helpers/shape-factory');
8
+ const game_entities_1 = require('../../game-entities');
9
+ const update_cycle_1 = require('../../game-entities/update-cycle');
11
10
  class PhysicsEntity extends game_entities_1.Entity {
12
- static { this.p2BodyIdToPhysicsBody = new Map(); }
13
- static { this.physicsEntities = new Map(); }
14
- static getFromP2BodyId(p2BodyId) {
15
- return PhysicsEntity.p2BodyIdToPhysicsBody.get(p2BodyId);
11
+ static {
12
+ PhysicsEntity.p2BodyIdToPhysicsBody = new Map();
13
+ }
14
+ static {
15
+ PhysicsEntity.physicsEntities = new Map();
16
+ }
17
+ static getFromP2BodyId(p2BodyId) {
18
+ return PhysicsEntity.p2BodyIdToPhysicsBody.get(p2BodyId);
19
+ }
20
+ static getFromP2BodyIdOrFail(p2BodyId) {
21
+ const physicsBody = PhysicsEntity.p2BodyIdToPhysicsBody.get(p2BodyId);
22
+ if (!physicsBody) {
23
+ throw new Error(`PhysicsEntity with p2BodyId ${p2BodyId} not found`);
16
24
  }
17
- static getFromP2BodyIdOrFail(p2BodyId) {
18
- let physicsBody = PhysicsEntity.p2BodyIdToPhysicsBody.get(p2BodyId);
19
- if (!physicsBody) {
20
- throw new Error(`PhysicsEntity with p2BodyId ${p2BodyId} not found`);
21
- }
22
- return physicsBody;
25
+ return physicsBody;
26
+ }
27
+ static getPhysicsInstanceOrFail(id) {
28
+ const physicsEntity = PhysicsEntity.physicsEntities.get(id);
29
+ if (!physicsEntity) {
30
+ throw new Error(`PhysicsEntity with id ${id} not found`);
23
31
  }
24
- static getPhysicsInstanceOrFail(id) {
25
- let physicsEntity = PhysicsEntity.physicsEntities.get(id);
26
- if (!physicsEntity) {
27
- throw new Error(`PhysicsEntity with id ${id} not found`);
28
- }
29
- return physicsEntity;
32
+ return physicsEntity;
33
+ }
34
+ static getPhysicsEntities() {
35
+ return Array.from(PhysicsEntity.physicsEntities.values());
36
+ }
37
+ get area() {
38
+ return this._area;
39
+ }
40
+ get mass() {
41
+ return this._mass;
42
+ }
43
+ get collisions() {
44
+ return this._collisions;
45
+ }
46
+ constructor(physicsWorld, physicsEntityDefinition, type) {
47
+ super();
48
+ this.onCollision = new actions_lib_1.Action();
49
+ this.type = 'dynamic';
50
+ this.physicsDestroy = () => {
51
+ this.physicsWorld.removeBody(this.p2Body);
52
+ PhysicsEntity.p2BodyIdToPhysicsBody.delete(this.p2Body.id);
53
+ PhysicsEntity.physicsEntities.delete(this.id);
54
+ };
55
+ this._area = 0;
56
+ this._mass = 0;
57
+ this._collisions = [];
58
+ if (helpers_lib_1.Comparator.isString(physicsWorld)) {
59
+ this.physicsWorld = physics_world_1.PhysicsWorld.getInstanceByIdOrFail(physicsWorld);
60
+ } else {
61
+ this.physicsWorld = physicsWorld;
30
62
  }
31
- static getPhysicsEntities() {
32
- return Array.from(PhysicsEntity.physicsEntities.values());
63
+ const material = this.physicsWorld.physicsMaterials.getMaterial(physicsEntityDefinition.shapeDefinition.materialName);
64
+ const materialDefinition = this.physicsWorld.physicsMaterials.getMaterialDefinition(
65
+ physicsEntityDefinition.shapeDefinition.materialName
66
+ );
67
+ this.type = type;
68
+ this.physicsBodyGroup = physicsEntityDefinition.shapeDefinition.group;
69
+ this.includeInPathfinding = physicsEntityDefinition.includeOnPathfinding;
70
+ this.p2Body = shape_factory_1.ShapeFactory.createBody(physicsEntityDefinition, material, materialDefinition, this.type);
71
+ this._area = this.p2Body.getArea();
72
+ this._mass = this.p2Body.mass;
73
+ this.p2Body.position = p2js_helper_1.P2JSHelper.vectorToArray(physicsEntityDefinition.position);
74
+ this.p2Body.angle = physicsEntityDefinition.rotation;
75
+ const addedToTheWorld = this.physicsWorld.addBody(
76
+ this.p2Body,
77
+ physicsEntityDefinition.addInEmptySpace,
78
+ physicsEntityDefinition.shapeDefinition.group,
79
+ physicsEntityDefinition.includeOnPathfinding
80
+ );
81
+ if (!addedToTheWorld) {
82
+ throw new Error('PhysicsEntity was not able to be added to the world!');
33
83
  }
34
- get area() {
35
- return this._area;
36
- }
37
- get mass() {
38
- return this._mass;
39
- }
40
- get collisions() {
41
- return this._collisions;
42
- }
43
- constructor(physicsWorld, physicsEntityDefinition, type) {
44
- super();
45
- this.onCollision = new actions_lib_1.Action();
46
- this.type = 'dynamic';
47
- this.physicsDestroy = () => {
48
- this.physicsWorld.removeBody(this.p2Body);
49
- PhysicsEntity.p2BodyIdToPhysicsBody.delete(this.p2Body.id);
50
- PhysicsEntity.physicsEntities.delete(this.id);
51
- };
52
- this._area = 0;
53
- this._mass = 0;
54
- this._collisions = [];
55
- if (helpers_lib_1.Comparator.isString(physicsWorld)) {
56
- this.physicsWorld = physics_world_1.PhysicsWorld.getInstanceByIdOrFail(physicsWorld);
57
- }
58
- else {
59
- this.physicsWorld = physicsWorld;
60
- }
61
- let material = this.physicsWorld.physicsMaterials.getMaterial(physicsEntityDefinition.shapeDefinition.materialName);
62
- let materialDefinition = this.physicsWorld.physicsMaterials.getMaterialDefinition(physicsEntityDefinition.shapeDefinition.materialName);
63
- this.type = type;
64
- this.physicsBodyGroup = physicsEntityDefinition.shapeDefinition.group;
65
- this.includeInPathfinding = physicsEntityDefinition.includeOnPathfinding;
66
- this.p2Body = shape_factory_1.ShapeFactory.createBody(physicsEntityDefinition, material, materialDefinition, this.type);
67
- this._area = this.p2Body.getArea();
68
- this._mass = this.p2Body.mass;
69
- this.p2Body.position = p2js_helper_1.P2JSHelper.vectorToArray(physicsEntityDefinition.position);
70
- this.p2Body.angle = physicsEntityDefinition.rotation;
71
- let addedToTheWorld = this.physicsWorld.addBody(this.p2Body, physicsEntityDefinition.addInEmptySpace, physicsEntityDefinition.shapeDefinition.group, physicsEntityDefinition.includeOnPathfinding);
72
- if (!addedToTheWorld) {
73
- throw new Error('PhysicsEntity was not able to be added to the world!');
74
- }
75
- PhysicsEntity.p2BodyIdToPhysicsBody.set(this.p2Body.id, this);
76
- PhysicsEntity.physicsEntities.set(this.id, this);
77
- this.destroy = function () {
78
- update_cycle_1.UpdateCycle.waitUntilDestroyPhase(() => {
79
- this.destroyed = true;
80
- this.attachableDestroy();
81
- this.entityDestroy();
82
- this.physicsDestroy();
83
- this.childDestroy();
84
- });
85
- };
86
- }
87
- destroy() { }
88
- // Managed by PhysicsCollision
89
- setCollisionReports(collisionReports) {
90
- this._collisions = collisionReports;
91
- if (collisionReports.length > 0) {
92
- this.onCollision.trigger(collisionReports);
93
- }
84
+ PhysicsEntity.p2BodyIdToPhysicsBody.set(this.p2Body.id, this);
85
+ PhysicsEntity.physicsEntities.set(this.id, this);
86
+ this.destroy = function () {
87
+ update_cycle_1.UpdateCycle.waitUntilDestroyPhase(() => {
88
+ this.destroyed = true;
89
+ this.attachableDestroy();
90
+ this.entityDestroy();
91
+ this.physicsDestroy();
92
+ this.childDestroy();
93
+ });
94
+ };
95
+ }
96
+ destroy() {}
97
+ // Managed by PhysicsCollision
98
+ setCollisionReports(collisionReports) {
99
+ this._collisions = collisionReports;
100
+ if (collisionReports.length > 0) {
101
+ this.onCollision.trigger(collisionReports);
94
102
  }
103
+ }
95
104
  }
96
105
  exports.PhysicsEntity = PhysicsEntity;
97
- //# sourceMappingURL=physics-entity.js.map
106
+ //# sourceMappingURL=physics-entity.js.map
@@ -1,19 +1,19 @@
1
1
  import { Rectangle, Vec2 } from 'helpers-lib';
2
2
  export declare class ClosestAvailableSpaceHelper {
3
- private grid;
4
- private worldWidth;
5
- private worldHeight;
6
- private cellSize;
7
- constructor(worldWidth: number, worldHeight: number, cellSize: number);
8
- markObjectOnGrid(object: Rectangle): void;
9
- /**
10
- * @param newObject The object to find a closest available position for
11
- * @returns the needed position change vector to the closest available position,
12
- * zero vector if the location is available by itself,
13
- * or undefined if no available space found
14
- */
15
- findClosestAvailableSpace(newObject: Rectangle): Vec2 | undefined;
16
- private canPlaceObject;
17
- private isWithinBounds;
18
- private fitInsideWorld;
3
+ private grid;
4
+ private worldWidth;
5
+ private worldHeight;
6
+ private cellSize;
7
+ constructor(worldWidth: number, worldHeight: number, cellSize: number);
8
+ markObjectOnGrid(object: Rectangle): void;
9
+ /**
10
+ * @param newObject The object to find a closest available position for
11
+ * @returns the needed position change vector to the closest available position,
12
+ * zero vector if the location is available by itself,
13
+ * or undefined if no available space found
14
+ */
15
+ findClosestAvailableSpace(newObject: Rectangle): Vec2 | undefined;
16
+ private canPlaceObject;
17
+ private isWithinBounds;
18
+ private fitInsideWorld;
19
19
  }
@@ -1,107 +1,109 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
1
+ Object.defineProperty(exports, '__esModule', { value: true });
3
2
  exports.ClosestAvailableSpaceHelper = void 0;
4
- const p2js_helper_1 = require("./p2js.helper");
3
+ const p2js_helper_1 = require('./p2js.helper');
5
4
  class ClosestAvailableSpaceHelper {
6
- constructor(worldWidth, worldHeight, cellSize) {
7
- this.worldWidth = worldWidth;
8
- this.worldHeight = worldHeight;
9
- this.cellSize = cellSize;
10
- this.grid = Array(Math.floor(worldHeight / cellSize))
11
- .fill(undefined)
12
- .map(() => Array(Math.floor(worldWidth / cellSize)).fill(false));
5
+ constructor(worldWidth, worldHeight, cellSize) {
6
+ this.worldWidth = worldWidth;
7
+ this.worldHeight = worldHeight;
8
+ this.cellSize = cellSize;
9
+ this.grid = Array(Math.floor(worldHeight / cellSize))
10
+ .fill(undefined)
11
+ .map(() => Array(Math.floor(worldWidth / cellSize)).fill(false));
12
+ }
13
+ markObjectOnGrid(object) {
14
+ object = this.fitInsideWorld(object);
15
+ const startX = Math.floor(object.topLeft.x / this.cellSize);
16
+ const startY = Math.floor(object.topLeft.y / this.cellSize);
17
+ const endX = Math.ceil(object.bottomRight.x / this.cellSize);
18
+ const endY = Math.ceil(object.bottomRight.y / this.cellSize);
19
+ for (let y = startY; y < endY && y < this.grid.length; y++) {
20
+ for (let x = startX; x < endX && x < this.grid[y].length; x++) {
21
+ this.grid[y][x] = true;
22
+ }
13
23
  }
14
- markObjectOnGrid(object) {
15
- object = this.fitInsideWorld(object);
16
- let startX = Math.floor(object.topLeft.x / this.cellSize);
17
- let startY = Math.floor(object.topLeft.y / this.cellSize);
18
- let endX = Math.ceil(object.bottomRight.x / this.cellSize);
19
- let endY = Math.ceil(object.bottomRight.y / this.cellSize);
20
- for (let y = startY; y < endY && y < this.grid.length; y++) {
21
- for (let x = startX; x < endX && x < this.grid[y].length; x++) {
22
- this.grid[y][x] = true;
23
- }
24
- }
24
+ }
25
+ /**
26
+ * @param newObject The object to find a closest available position for
27
+ * @returns the needed position change vector to the closest available position,
28
+ * zero vector if the location is available by itself,
29
+ * or undefined if no available space found
30
+ */
31
+ findClosestAvailableSpace(newObject) {
32
+ const newObjectWidth = newObject.bottomRight.x - newObject.topLeft.x;
33
+ const newObjectHeight = newObject.bottomRight.y - newObject.topLeft.y;
34
+ const visited = new Set();
35
+ const targetWidthCells = Math.ceil(newObjectWidth / this.cellSize);
36
+ const targetHeightCells = Math.ceil(newObjectHeight / this.cellSize);
37
+ const newObjectInsideWorld = this.fitInsideWorld(newObject);
38
+ const firstQueueItem = {
39
+ x: Math.floor(newObjectInsideWorld.topLeft.x / this.cellSize),
40
+ y: Math.floor(newObjectInsideWorld.topLeft.y / this.cellSize)
41
+ };
42
+ const queue = [firstQueueItem];
43
+ if (p2js_helper_1.P2JSHelper.isEqualBoundingBox(newObject, newObjectInsideWorld)) {
44
+ const startingColumn = Math.floor(newObjectInsideWorld.topLeft.x / this.cellSize);
45
+ const endingColumn = Math.floor(newObjectInsideWorld.bottomRight.x / this.cellSize);
46
+ const startingRow = Math.floor(newObjectInsideWorld.topLeft.y / this.cellSize);
47
+ const endingRow = Math.floor(newObjectInsideWorld.bottomRight.y / this.cellSize);
48
+ if (this.canPlaceObject(firstQueueItem, endingColumn - startingColumn + 1, endingRow - startingRow + 1)) {
49
+ return { x: 0, y: 0 };
50
+ }
25
51
  }
26
- /**
27
- * @param newObject The object to find a closest available position for
28
- * @returns the needed position change vector to the closest available position,
29
- * zero vector if the location is available by itself,
30
- * or undefined if no available space found
31
- */
32
- findClosestAvailableSpace(newObject) {
33
- let newObjectWidth = newObject.bottomRight.x - newObject.topLeft.x;
34
- let newObjectHeight = newObject.bottomRight.y - newObject.topLeft.y;
35
- let visited = new Set();
36
- let targetWidthCells = Math.ceil(newObjectWidth / this.cellSize);
37
- let targetHeightCells = Math.ceil(newObjectHeight / this.cellSize);
38
- let newObjectInsideWorld = this.fitInsideWorld(newObject);
39
- let firstQueueItem = {
40
- x: Math.floor(newObjectInsideWorld.topLeft.x / this.cellSize),
41
- y: Math.floor(newObjectInsideWorld.topLeft.y / this.cellSize)
42
- };
43
- let queue = [firstQueueItem];
44
- if (p2js_helper_1.P2JSHelper.isEqualBoundingBox(newObject, newObjectInsideWorld)) {
45
- let startingColumn = Math.floor(newObjectInsideWorld.topLeft.x / this.cellSize);
46
- let endingColumn = Math.floor(newObjectInsideWorld.bottomRight.x / this.cellSize);
47
- let startingRow = Math.floor(newObjectInsideWorld.topLeft.y / this.cellSize);
48
- let endingRow = Math.floor(newObjectInsideWorld.bottomRight.y / this.cellSize);
49
- if (this.canPlaceObject(firstQueueItem, endingColumn - startingColumn + 1, endingRow - startingRow + 1)) {
50
- return { x: 0, y: 0 };
51
- }
52
+ while (queue.length > 0) {
53
+ const shiftedPoint = queue.shift();
54
+ if (shiftedPoint) {
55
+ const currentPoint = shiftedPoint;
56
+ const key = `${currentPoint.x},${currentPoint.y}`;
57
+ if (visited.has(key)) {
58
+ continue;
52
59
  }
53
- while (queue.length > 0) {
54
- let shiftedPoint = queue.shift();
55
- if (shiftedPoint) {
56
- let currentPoint = shiftedPoint;
57
- let key = `${currentPoint.x},${currentPoint.y}`;
58
- if (visited.has(key)) {
59
- continue;
60
- }
61
- visited.add(key);
62
- if (this.canPlaceObject(currentPoint, targetWidthCells, targetHeightCells)) {
63
- return {
64
- x: currentPoint.x * this.cellSize + (targetWidthCells * this.cellSize - newObjectWidth) / 2 - newObject.topLeft.x,
65
- y: currentPoint.y * this.cellSize + (targetHeightCells * this.cellSize - newObjectHeight) / 2 - newObject.topLeft.y
66
- };
67
- }
68
- // Add neighboring cells to the queue
69
- [
70
- [1, 0],
71
- [0, 1],
72
- [-1, 0],
73
- [0, -1]
74
- ].forEach(([dx, dy]) => {
75
- let nextPoint = { x: currentPoint.x + dx, y: currentPoint.y + dy };
76
- if (this.isWithinBounds(nextPoint) && !visited.has(`${nextPoint.x},${nextPoint.y}`)) {
77
- queue.push(nextPoint);
78
- }
79
- });
80
- }
60
+ visited.add(key);
61
+ if (this.canPlaceObject(currentPoint, targetWidthCells, targetHeightCells)) {
62
+ return {
63
+ x: currentPoint.x * this.cellSize + (targetWidthCells * this.cellSize - newObjectWidth) / 2 - newObject.topLeft.x,
64
+ y: currentPoint.y * this.cellSize + (targetHeightCells * this.cellSize - newObjectHeight) / 2 - newObject.topLeft.y
65
+ };
81
66
  }
82
- return undefined; // No available space found
67
+ // Add neighboring cells to the queue
68
+ [
69
+ [1, 0],
70
+ [0, 1],
71
+ [-1, 0],
72
+ [0, -1]
73
+ ].forEach(([dx, dy]) => {
74
+ const nextPoint = { x: currentPoint.x + dx, y: currentPoint.y + dy };
75
+ if (this.isWithinBounds(nextPoint) && !visited.has(`${nextPoint.x},${nextPoint.y}`)) {
76
+ queue.push(nextPoint);
77
+ }
78
+ });
79
+ }
83
80
  }
84
- canPlaceObject(point, widthCells, heightCells) {
85
- for (let y = point.y; y < point.y + heightCells; y++) {
86
- for (let x = point.x; x < point.x + widthCells; x++) {
87
- if (!this.isWithinBounds({ x, y }) || this.grid[y][x]) {
88
- return false;
89
- }
90
- }
81
+ return undefined; // No available space found
82
+ }
83
+ canPlaceObject(point, widthCells, heightCells) {
84
+ for (let y = point.y; y < point.y + heightCells; y++) {
85
+ for (let x = point.x; x < point.x + widthCells; x++) {
86
+ if (!this.isWithinBounds({ x, y }) || this.grid[y][x]) {
87
+ return false;
91
88
  }
92
- return true;
93
- }
94
- isWithinBounds(point) {
95
- return point.x >= 0 && point.y >= 0 && point.x < this.grid[0]?.length && point.y < this.grid.length;
96
- }
97
- fitInsideWorld(object) {
98
- let topLeft = { x: Math.max(0, object.topLeft.x), y: Math.max(0, object.topLeft.y) };
99
- let bottomRight = {
100
- x: Math.min(this.worldWidth, object.bottomRight.x),
101
- y: Math.min(this.worldHeight, object.bottomRight.y)
102
- };
103
- return { topLeft, bottomRight };
89
+ }
104
90
  }
91
+ return true;
92
+ }
93
+ isWithinBounds(point) {
94
+ return point.x >= 0 && point.y >= 0 && point.x < this.grid[0]?.length && point.y < this.grid.length;
95
+ }
96
+ fitInsideWorld(object) {
97
+ const topLeft = {
98
+ x: Math.max(0, object.topLeft.x),
99
+ y: Math.max(0, object.topLeft.y)
100
+ };
101
+ const bottomRight = {
102
+ x: Math.min(this.worldWidth, object.bottomRight.x),
103
+ y: Math.min(this.worldHeight, object.bottomRight.y)
104
+ };
105
+ return { topLeft, bottomRight };
106
+ }
105
107
  }
106
108
  exports.ClosestAvailableSpaceHelper = ClosestAvailableSpaceHelper;
107
- //# sourceMappingURL=closest-available-space.helper.js.map
109
+ //# sourceMappingURL=closest-available-space.helper.js.map
@@ -1,71 +1,118 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const closest_available_space_helper_1 = require("./closest-available-space.helper");
1
+ Object.defineProperty(exports, '__esModule', { value: true });
2
+ const closest_available_space_helper_1 = require('./closest-available-space.helper');
4
3
  describe('findClosestAvailableSpace', () => {
5
- let helper;
6
- let newObject;
7
- beforeEach(() => {
8
- helper = new closest_available_space_helper_1.ClosestAvailableSpaceHelper(100, 100, 10);
9
- });
10
- it('should return no change vector if the location is available', () => {
11
- newObject = { topLeft: { x: 50, y: 50 }, bottomRight: { x: 60, y: 60 } };
12
- expect(helper.findClosestAvailableSpace(newObject)).toEqual({ x: 0, y: 0 });
13
- });
14
- it('should support object positions outside of the world size', () => {
15
- newObject = { topLeft: { x: -5, y: -5 }, bottomRight: { x: 5, y: 5 } };
16
- expect(helper.findClosestAvailableSpace(newObject)).toEqual({ x: 5, y: 5 });
17
- newObject = { topLeft: { x: 95, y: 95 }, bottomRight: { x: 105, y: 105 } };
18
- expect(helper.findClosestAvailableSpace(newObject)).toEqual({ x: -5, y: -5 });
19
- });
20
- it('should find the no change vector space next to an existing object', () => {
21
- helper.markObjectOnGrid({ topLeft: { x: 0, y: 0 }, bottomRight: { x: 10, y: 10 } });
22
- newObject = { topLeft: { x: 10, y: 10 }, bottomRight: { x: 20, y: 20 } };
23
- expect(helper.findClosestAvailableSpace(newObject)).toEqual({ x: 0, y: 0 });
24
- });
25
- it('should find the closest available space if trying to spawn in occupied space', () => {
26
- helper.markObjectOnGrid({ topLeft: { x: 0, y: 0 }, bottomRight: { x: 20, y: 20 } });
27
- newObject = { topLeft: { x: 5, y: 5 }, bottomRight: { x: 15, y: 15 } };
28
- expect(helper.findClosestAvailableSpace(newObject)).toEqual({ x: 15, y: -5 });
29
- });
30
- it('should find the closest available space if trying to spawn in crowded space', () => {
31
- // Crowded space
32
- // 0 1 2 3 4 5 6 7 8 9
33
- // 0 □ 1 1 1 2 2 □ □ □ □
34
- // 1 □ 1 1 1 2 2 □ □ □ □
35
- // 2 □ 1 1 1 2 2 □ □ □ □
36
- // 3 3 3 3 ■ ■ ■ □ □ □ □
37
- // 4 □ □ □ ■ ■ ■ □ □ □ □
38
- // 5 □ □ 4 ■ ■ ■ 6 6 6 □
39
- // 6 □ □ 4 5 5 5 □ □ □ □
40
- helper.markObjectOnGrid({ topLeft: { x: 12, y: 5 }, bottomRight: { x: 35, y: 25 } });
41
- helper.markObjectOnGrid({ topLeft: { x: 45, y: 5 }, bottomRight: { x: 52, y: 25 } });
42
- helper.markObjectOnGrid({ topLeft: { x: -2, y: 32 }, bottomRight: { x: 12, y: 35 } });
43
- helper.markObjectOnGrid({ topLeft: { x: 22, y: 55 }, bottomRight: { x: 28, y: 62 } });
44
- helper.markObjectOnGrid({ topLeft: { x: 35, y: 65 }, bottomRight: { x: 52, y: 75 } });
45
- helper.markObjectOnGrid({ topLeft: { x: 74, y: 42 }, bottomRight: { x: 95, y: 48 } });
46
- newObject = { topLeft: { x: 10, y: 20 }, bottomRight: { x: 40, y: 50 } };
47
- expect(helper.findClosestAvailableSpace(newObject)).toEqual({ x: 20, y: 10 });
48
- });
49
- it('should not fooled by initial position occupying more space in grid and find narrower gap', () => {
50
- helper.markObjectOnGrid({ topLeft: { x: 10, y: 0 }, bottomRight: { x: 40, y: 30 } });
51
- newObject = { topLeft: { x: 4, y: 2 }, bottomRight: { x: 12, y: 20 } };
52
- expect(helper.findClosestAvailableSpace(newObject)).toEqual({ x: -3, y: -1 });
53
- });
54
- it('should not fooled by initial position occupying more space in grid and find narrower gap negative starting point', () => {
55
- helper.markObjectOnGrid({ topLeft: { x: 10, y: 0 }, bottomRight: { x: 40, y: 30 } });
56
- newObject = { topLeft: { x: -6, y: -8 }, bottomRight: { x: 2, y: 10 } };
57
- expect(helper.findClosestAvailableSpace(newObject)).toEqual({ x: 7, y: 9 });
58
- });
59
- it('should not suggest a location at the edge of the map', () => {
60
- helper = new closest_available_space_helper_1.ClosestAvailableSpaceHelper(10, 10, 9);
61
- helper.markObjectOnGrid({ topLeft: { x: 0, y: 0 }, bottomRight: { x: 9, y: 9 } });
62
- newObject = { topLeft: { x: 0, y: 0 }, bottomRight: { x: 1, y: 1 } };
63
- expect(helper.findClosestAvailableSpace(newObject)).toBeUndefined();
64
- });
65
- it('should return undefined if there is no available space', () => {
66
- helper.markObjectOnGrid({ topLeft: { x: 0, y: 0 }, bottomRight: { x: 100, y: 100 } });
67
- newObject = { topLeft: { x: 0, y: 0 }, bottomRight: { x: 10, y: 10 } };
68
- expect(helper.findClosestAvailableSpace(newObject)).toBeUndefined();
4
+ let helper;
5
+ let newObject;
6
+ beforeEach(() => {
7
+ helper = new closest_available_space_helper_1.ClosestAvailableSpaceHelper(100, 100, 10);
8
+ });
9
+ it('should return no change vector if the location is available', () => {
10
+ newObject = { topLeft: { x: 50, y: 50 }, bottomRight: { x: 60, y: 60 } };
11
+ expect(helper.findClosestAvailableSpace(newObject)).toEqual({ x: 0, y: 0 });
12
+ });
13
+ it('should support object positions outside of the world size', () => {
14
+ newObject = { topLeft: { x: -5, y: -5 }, bottomRight: { x: 5, y: 5 } };
15
+ expect(helper.findClosestAvailableSpace(newObject)).toEqual({ x: 5, y: 5 });
16
+ newObject = { topLeft: { x: 95, y: 95 }, bottomRight: { x: 105, y: 105 } };
17
+ expect(helper.findClosestAvailableSpace(newObject)).toEqual({
18
+ x: -5,
19
+ y: -5
69
20
  });
21
+ });
22
+ it('should find the no change vector space next to an existing object', () => {
23
+ helper.markObjectOnGrid({
24
+ topLeft: { x: 0, y: 0 },
25
+ bottomRight: { x: 10, y: 10 }
26
+ });
27
+ newObject = { topLeft: { x: 10, y: 10 }, bottomRight: { x: 20, y: 20 } };
28
+ expect(helper.findClosestAvailableSpace(newObject)).toEqual({ x: 0, y: 0 });
29
+ });
30
+ it('should find the closest available space if trying to spawn in occupied space', () => {
31
+ helper.markObjectOnGrid({
32
+ topLeft: { x: 0, y: 0 },
33
+ bottomRight: { x: 20, y: 20 }
34
+ });
35
+ newObject = { topLeft: { x: 5, y: 5 }, bottomRight: { x: 15, y: 15 } };
36
+ expect(helper.findClosestAvailableSpace(newObject)).toEqual({
37
+ x: 15,
38
+ y: -5
39
+ });
40
+ });
41
+ it('should find the closest available space if trying to spawn in crowded space', () => {
42
+ // Crowded space
43
+ // 0 1 2 3 4 5 6 7 8 9
44
+ // 0 □ 1 1 1 2 2 □ □ □ □
45
+ // 1 □ 1 1 1 2 2 □ □ □ □
46
+ // 2 □ 1 1 1 2 2 □ □ □ □
47
+ // 3 3 3 3 ■ ■ ■ □ □ □ □
48
+ // 4 □ □ □ ■ ■ ■ □ □ □ □
49
+ // 5 □ □ 4 ■ ■ ■ 6 6 6 □
50
+ // 6 □ □ 4 5 5 5 □ □ □ □
51
+ helper.markObjectOnGrid({
52
+ topLeft: { x: 12, y: 5 },
53
+ bottomRight: { x: 35, y: 25 }
54
+ });
55
+ helper.markObjectOnGrid({
56
+ topLeft: { x: 45, y: 5 },
57
+ bottomRight: { x: 52, y: 25 }
58
+ });
59
+ helper.markObjectOnGrid({
60
+ topLeft: { x: -2, y: 32 },
61
+ bottomRight: { x: 12, y: 35 }
62
+ });
63
+ helper.markObjectOnGrid({
64
+ topLeft: { x: 22, y: 55 },
65
+ bottomRight: { x: 28, y: 62 }
66
+ });
67
+ helper.markObjectOnGrid({
68
+ topLeft: { x: 35, y: 65 },
69
+ bottomRight: { x: 52, y: 75 }
70
+ });
71
+ helper.markObjectOnGrid({
72
+ topLeft: { x: 74, y: 42 },
73
+ bottomRight: { x: 95, y: 48 }
74
+ });
75
+ newObject = { topLeft: { x: 10, y: 20 }, bottomRight: { x: 40, y: 50 } };
76
+ expect(helper.findClosestAvailableSpace(newObject)).toEqual({
77
+ x: 20,
78
+ y: 10
79
+ });
80
+ });
81
+ it('should not fooled by initial position occupying more space in grid and find narrower gap', () => {
82
+ helper.markObjectOnGrid({
83
+ topLeft: { x: 10, y: 0 },
84
+ bottomRight: { x: 40, y: 30 }
85
+ });
86
+ newObject = { topLeft: { x: 4, y: 2 }, bottomRight: { x: 12, y: 20 } };
87
+ expect(helper.findClosestAvailableSpace(newObject)).toEqual({
88
+ x: -3,
89
+ y: -1
90
+ });
91
+ });
92
+ it('should not fooled by initial position occupying more space in grid and find narrower gap negative starting point', () => {
93
+ helper.markObjectOnGrid({
94
+ topLeft: { x: 10, y: 0 },
95
+ bottomRight: { x: 40, y: 30 }
96
+ });
97
+ newObject = { topLeft: { x: -6, y: -8 }, bottomRight: { x: 2, y: 10 } };
98
+ expect(helper.findClosestAvailableSpace(newObject)).toEqual({ x: 7, y: 9 });
99
+ });
100
+ it('should not suggest a location at the edge of the map', () => {
101
+ helper = new closest_available_space_helper_1.ClosestAvailableSpaceHelper(10, 10, 9);
102
+ helper.markObjectOnGrid({
103
+ topLeft: { x: 0, y: 0 },
104
+ bottomRight: { x: 9, y: 9 }
105
+ });
106
+ newObject = { topLeft: { x: 0, y: 0 }, bottomRight: { x: 1, y: 1 } };
107
+ expect(helper.findClosestAvailableSpace(newObject)).toBeUndefined();
108
+ });
109
+ it('should return undefined if there is no available space', () => {
110
+ helper.markObjectOnGrid({
111
+ topLeft: { x: 0, y: 0 },
112
+ bottomRight: { x: 100, y: 100 }
113
+ });
114
+ newObject = { topLeft: { x: 0, y: 0 }, bottomRight: { x: 10, y: 10 } };
115
+ expect(helper.findClosestAvailableSpace(newObject)).toBeUndefined();
116
+ });
70
117
  });
71
- //# sourceMappingURL=closest-available-space.helper.spec.js.map
118
+ //# sourceMappingURL=closest-available-space.helper.spec.js.map
@@ -1,7 +1,7 @@
1
1
  /// <reference types="p2" />
2
2
  import { Rectangle, Vector } from 'helpers-lib';
3
3
  export declare class P2JSHelper {
4
- static aabbToBoundingBox(aabb: p2.AABB): Rectangle;
5
- static arrayToVector(array: [number, number]): Vector;
6
- static vectorToArray(vector: Vector): [number, number];
4
+ static aabbToBoundingBox(aabb: p2.AABB): Rectangle;
5
+ static arrayToVector(array: [number, number]): Vector;
6
+ static vectorToArray(vector: Vector): [number, number];
7
7
  }