melonjs 14.0.2 → 14.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (214) hide show
  1. package/README.md +2 -0
  2. package/dist/melonjs.mjs/_virtual/_commonjsHelpers.js +10 -0
  3. package/dist/melonjs.mjs/_virtual/arraymultimap.js +10 -0
  4. package/dist/melonjs.mjs/_virtual/earcut.js +10 -0
  5. package/dist/melonjs.mjs/_virtual/howler.js +10 -0
  6. package/dist/melonjs.mjs/_virtual/index.js +10 -0
  7. package/dist/melonjs.mjs/_virtual/index2.js +10 -0
  8. package/dist/melonjs.mjs/_virtual/multimap.js +10 -0
  9. package/dist/melonjs.mjs/_virtual/setmultimap.js +10 -0
  10. package/dist/melonjs.mjs/application/application.js +240 -0
  11. package/dist/melonjs.mjs/audio/audio.js +536 -0
  12. package/dist/melonjs.mjs/camera/camera2d.js +732 -0
  13. package/dist/melonjs.mjs/entity/entity.js +248 -0
  14. package/dist/melonjs.mjs/game.js +29 -0
  15. package/dist/melonjs.mjs/geometries/ellipse.js +275 -0
  16. package/dist/melonjs.mjs/geometries/line.js +116 -0
  17. package/dist/melonjs.mjs/geometries/path2d.js +319 -0
  18. package/dist/melonjs.mjs/geometries/point.js +89 -0
  19. package/dist/melonjs.mjs/geometries/poly.js +500 -0
  20. package/dist/melonjs.mjs/geometries/rectangle.js +375 -0
  21. package/dist/melonjs.mjs/geometries/roundrect.js +168 -0
  22. package/dist/melonjs.mjs/index.js +248 -0
  23. package/dist/melonjs.mjs/input/gamepad.js +501 -0
  24. package/dist/melonjs.mjs/input/input.js +26 -0
  25. package/dist/melonjs.mjs/input/keyboard.js +470 -0
  26. package/dist/melonjs.mjs/input/pointer.js +393 -0
  27. package/dist/melonjs.mjs/input/pointerevent.js +818 -0
  28. package/dist/melonjs.mjs/lang/deprecated.js +157 -0
  29. package/dist/melonjs.mjs/level/level.js +297 -0
  30. package/dist/melonjs.mjs/level/tiled/TMXGroup.js +141 -0
  31. package/dist/melonjs.mjs/level/tiled/TMXLayer.js +448 -0
  32. package/dist/melonjs.mjs/level/tiled/TMXObject.js +355 -0
  33. package/dist/melonjs.mjs/level/tiled/TMXTile.js +194 -0
  34. package/dist/melonjs.mjs/level/tiled/TMXTileMap.js +639 -0
  35. package/dist/melonjs.mjs/level/tiled/TMXTileset.js +311 -0
  36. package/dist/melonjs.mjs/level/tiled/TMXTilesetGroup.js +83 -0
  37. package/dist/melonjs.mjs/level/tiled/TMXUtils.js +367 -0
  38. package/dist/melonjs.mjs/level/tiled/renderer/TMXHexagonalRenderer.js +506 -0
  39. package/dist/melonjs.mjs/level/tiled/renderer/TMXIsometricRenderer.js +220 -0
  40. package/dist/melonjs.mjs/level/tiled/renderer/TMXOrthogonalRenderer.js +157 -0
  41. package/dist/melonjs.mjs/level/tiled/renderer/TMXRenderer.js +125 -0
  42. package/dist/melonjs.mjs/level/tiled/renderer/TMXStaggeredRenderer.js +109 -0
  43. package/dist/melonjs.mjs/loader/loader.js +801 -0
  44. package/dist/melonjs.mjs/loader/loadingscreen.js +120 -0
  45. package/dist/melonjs.mjs/loader/melonjs_logo.png.js +11 -0
  46. package/dist/melonjs.mjs/math/color.js +618 -0
  47. package/dist/melonjs.mjs/math/math.js +218 -0
  48. package/dist/melonjs.mjs/math/matrix2.js +503 -0
  49. package/dist/melonjs.mjs/math/matrix3.js +681 -0
  50. package/dist/melonjs.mjs/math/observable_vector2.js +471 -0
  51. package/dist/melonjs.mjs/math/observable_vector3.js +561 -0
  52. package/dist/melonjs.mjs/math/vector2.js +528 -0
  53. package/dist/melonjs.mjs/math/vector3.js +569 -0
  54. package/dist/melonjs.mjs/node_modules/@teppeis/multimaps/dist/src/arraymultimap.js +73 -0
  55. package/dist/melonjs.mjs/node_modules/@teppeis/multimaps/dist/src/index.js +21 -0
  56. package/dist/melonjs.mjs/node_modules/@teppeis/multimaps/dist/src/multimap.js +324 -0
  57. package/dist/melonjs.mjs/node_modules/@teppeis/multimaps/dist/src/setmultimap.js +69 -0
  58. package/dist/melonjs.mjs/node_modules/earcut/src/earcut.js +691 -0
  59. package/dist/melonjs.mjs/node_modules/eventemitter3/index.js +350 -0
  60. package/dist/melonjs.mjs/node_modules/howler/dist/howler.js +3241 -0
  61. package/dist/melonjs.mjs/particles/emitter.js +267 -0
  62. package/dist/melonjs.mjs/particles/particle.js +188 -0
  63. package/dist/melonjs.mjs/particles/settings.js +319 -0
  64. package/dist/melonjs.mjs/physics/body.js +704 -0
  65. package/dist/melonjs.mjs/physics/bounds.js +460 -0
  66. package/dist/melonjs.mjs/physics/collision.js +132 -0
  67. package/dist/melonjs.mjs/physics/detector.js +194 -0
  68. package/dist/melonjs.mjs/physics/quadtree.js +391 -0
  69. package/dist/melonjs.mjs/physics/response.js +57 -0
  70. package/dist/melonjs.mjs/physics/sat.js +483 -0
  71. package/dist/melonjs.mjs/physics/world.js +221 -0
  72. package/dist/melonjs.mjs/plugin/plugin.js +141 -0
  73. package/dist/melonjs.mjs/renderable/collectable.js +62 -0
  74. package/dist/melonjs.mjs/renderable/colorlayer.js +80 -0
  75. package/dist/melonjs.mjs/renderable/container.js +1018 -0
  76. package/dist/melonjs.mjs/renderable/dragndrop.js +224 -0
  77. package/dist/melonjs.mjs/renderable/imagelayer.js +306 -0
  78. package/dist/melonjs.mjs/renderable/light2d.js +156 -0
  79. package/dist/melonjs.mjs/renderable/nineslicesprite.js +247 -0
  80. package/dist/melonjs.mjs/renderable/renderable.js +783 -0
  81. package/dist/melonjs.mjs/renderable/sprite.js +654 -0
  82. package/dist/melonjs.mjs/renderable/trigger.js +157 -0
  83. package/dist/melonjs.mjs/renderable/ui/uibaseelement.js +213 -0
  84. package/dist/melonjs.mjs/renderable/ui/uispriteelement.js +226 -0
  85. package/dist/melonjs.mjs/renderable/ui/uitextbutton.js +128 -0
  86. package/dist/melonjs.mjs/state/stage.js +237 -0
  87. package/dist/melonjs.mjs/state/state.js +596 -0
  88. package/dist/melonjs.mjs/system/device.js +909 -0
  89. package/dist/melonjs.mjs/system/dom.js +78 -0
  90. package/dist/melonjs.mjs/system/event.js +537 -0
  91. package/dist/melonjs.mjs/system/platform.js +41 -0
  92. package/dist/melonjs.mjs/system/pooling.js +209 -0
  93. package/dist/melonjs.mjs/system/save.js +157 -0
  94. package/dist/melonjs.mjs/system/timer.js +286 -0
  95. package/dist/melonjs.mjs/text/bitmaptext.js +364 -0
  96. package/dist/melonjs.mjs/text/bitmaptextdata.js +199 -0
  97. package/dist/melonjs.mjs/text/glyph.js +66 -0
  98. package/dist/melonjs.mjs/text/text.js +453 -0
  99. package/dist/melonjs.mjs/text/textmetrics.js +176 -0
  100. package/dist/melonjs.mjs/text/textstyle.js +23 -0
  101. package/dist/melonjs.mjs/tweens/easing.js +336 -0
  102. package/dist/melonjs.mjs/tweens/interpolation.js +112 -0
  103. package/dist/melonjs.mjs/tweens/tween.js +480 -0
  104. package/dist/melonjs.mjs/utils/agent.js +76 -0
  105. package/dist/melonjs.mjs/utils/array.js +63 -0
  106. package/dist/melonjs.mjs/utils/file.js +42 -0
  107. package/dist/melonjs.mjs/utils/function.js +70 -0
  108. package/dist/melonjs.mjs/utils/string.js +82 -0
  109. package/dist/melonjs.mjs/utils/utils.js +173 -0
  110. package/dist/melonjs.mjs/video/canvas/canvas_renderer.js +807 -0
  111. package/dist/melonjs.mjs/video/renderer.js +411 -0
  112. package/dist/melonjs.mjs/video/texture/atlas.js +519 -0
  113. package/dist/melonjs.mjs/video/texture/cache.js +143 -0
  114. package/dist/melonjs.mjs/video/texture/canvas_texture.js +144 -0
  115. package/dist/melonjs.mjs/video/video.js +462 -0
  116. package/dist/melonjs.mjs/video/webgl/buffer/vertex.js +143 -0
  117. package/dist/melonjs.mjs/video/webgl/glshader.js +168 -0
  118. package/dist/melonjs.mjs/video/webgl/shaders/primitive.frag.js +10 -0
  119. package/dist/melonjs.mjs/video/webgl/shaders/primitive.vert.js +10 -0
  120. package/dist/melonjs.mjs/video/webgl/shaders/quad.frag.js +10 -0
  121. package/dist/melonjs.mjs/video/webgl/shaders/quad.vert.js +10 -0
  122. package/dist/melonjs.mjs/video/webgl/utils/attributes.js +25 -0
  123. package/dist/melonjs.mjs/video/webgl/utils/precision.js +20 -0
  124. package/dist/melonjs.mjs/video/webgl/utils/program.js +67 -0
  125. package/dist/melonjs.mjs/video/webgl/utils/string.js +25 -0
  126. package/dist/melonjs.mjs/video/webgl/utils/uniforms.js +92 -0
  127. package/dist/melonjs.mjs/video/webgl/webgl_compositor.js +495 -0
  128. package/dist/melonjs.mjs/video/webgl/webgl_renderer.js +1036 -0
  129. package/dist/melonjs.module.d.ts +1163 -1163
  130. package/dist/melonjs.module.js +1903 -3274
  131. package/package.json +22 -17
  132. package/src/application/application.js +3 -3
  133. package/src/audio/audio.js +32 -32
  134. package/src/camera/camera2d.js +31 -31
  135. package/src/entity/entity.js +17 -17
  136. package/src/geometries/ellipse.js +16 -16
  137. package/src/geometries/line.js +5 -5
  138. package/src/geometries/path2d.js +32 -32
  139. package/src/geometries/poly.js +15 -15
  140. package/src/geometries/rectangle.js +18 -18
  141. package/src/geometries/roundrect.js +8 -8
  142. package/src/input/gamepad.js +15 -15
  143. package/src/input/keyboard.js +12 -12
  144. package/src/input/pointer.js +6 -6
  145. package/src/input/pointerevent.js +12 -12
  146. package/src/lang/deprecated.js +12 -12
  147. package/src/level/level.js +25 -25
  148. package/src/level/tiled/TMXLayer.js +22 -22
  149. package/src/level/tiled/TMXTile.js +5 -5
  150. package/src/level/tiled/TMXTileMap.js +6 -6
  151. package/src/level/tiled/TMXTileset.js +2 -2
  152. package/src/level/tiled/TMXUtils.js +5 -5
  153. package/src/level/tiled/renderer/TMXHexagonalRenderer.js +2 -2
  154. package/src/level/tiled/renderer/TMXIsometricRenderer.js +2 -2
  155. package/src/level/tiled/renderer/TMXOrthogonalRenderer.js +1 -1
  156. package/src/level/tiled/renderer/TMXRenderer.js +19 -19
  157. package/src/loader/loader.js +20 -20
  158. package/src/math/color.js +20 -20
  159. package/src/math/math.js +16 -16
  160. package/src/math/matrix2.js +16 -16
  161. package/src/math/matrix3.js +25 -25
  162. package/src/math/observable_vector2.js +14 -14
  163. package/src/math/observable_vector3.js +16 -16
  164. package/src/math/vector2.js +9 -9
  165. package/src/math/vector3.js +10 -10
  166. package/src/particles/emitter.js +6 -6
  167. package/src/particles/particle.js +2 -2
  168. package/src/physics/body.js +28 -28
  169. package/src/physics/bounds.js +8 -8
  170. package/src/physics/collision.js +2 -2
  171. package/src/physics/detector.js +6 -6
  172. package/src/physics/quadtree.js +11 -11
  173. package/src/physics/sat.js +31 -31
  174. package/src/physics/world.js +5 -5
  175. package/src/plugin/plugin.js +5 -5
  176. package/src/renderable/collectable.js +3 -3
  177. package/src/renderable/colorlayer.js +5 -5
  178. package/src/renderable/container.js +21 -21
  179. package/src/renderable/dragndrop.js +14 -14
  180. package/src/renderable/imagelayer.js +13 -13
  181. package/src/renderable/light2d.js +3 -3
  182. package/src/renderable/nineslicesprite.js +16 -16
  183. package/src/renderable/renderable.js +23 -23
  184. package/src/renderable/sprite.js +28 -28
  185. package/src/renderable/trigger.js +15 -15
  186. package/src/renderable/ui/uibaseelement.js +7 -7
  187. package/src/renderable/ui/uispriteelement.js +6 -6
  188. package/src/renderable/ui/uitextbutton.js +13 -13
  189. package/src/state/stage.js +7 -7
  190. package/src/state/state.js +17 -17
  191. package/src/system/device.js +11 -11
  192. package/src/system/event.js +10 -10
  193. package/src/system/pooling.js +9 -9
  194. package/src/system/save.js +2 -2
  195. package/src/system/timer.js +10 -10
  196. package/src/text/bitmaptext.js +18 -18
  197. package/src/text/bitmaptextdata.js +2 -2
  198. package/src/text/text.js +23 -23
  199. package/src/text/textmetrics.js +8 -8
  200. package/src/tweens/tween.js +19 -19
  201. package/src/utils/agent.js +5 -5
  202. package/src/utils/array.js +4 -4
  203. package/src/utils/file.js +2 -2
  204. package/src/utils/function.js +6 -6
  205. package/src/utils/string.js +5 -5
  206. package/src/utils/utils.js +4 -4
  207. package/src/video/canvas/canvas_renderer.js +70 -70
  208. package/src/video/renderer.js +26 -26
  209. package/src/video/texture/atlas.js +22 -22
  210. package/src/video/texture/canvas_texture.js +9 -9
  211. package/src/video/video.js +17 -17
  212. package/src/video/webgl/glshader.js +10 -10
  213. package/src/video/webgl/webgl_compositor.js +41 -41
  214. package/src/video/webgl/webgl_renderer.js +75 -75
@@ -0,0 +1,519 @@
1
+ /*!
2
+ * melonJS Game Engine - v14.1.0
3
+ * http://www.melonjs.org
4
+ * melonjs is licensed under the MIT License.
5
+ * http://www.opensource.org/licenses/mit-license
6
+ * @copyright (C) 2011 - 2022 Olivier Biot (AltByte Pte Ltd)
7
+ */
8
+ import Vector2d from '../../math/vector2.js';
9
+ import WebGLRenderer from '../webgl/webgl_renderer.js';
10
+ import TextureCache from './cache.js';
11
+ import Sprite from '../../renderable/sprite.js';
12
+ import { renderer } from '../video.js';
13
+ import pool from '../../system/pooling.js';
14
+ import loader from '../../loader/loader.js';
15
+ import { ETA } from '../../math/math.js';
16
+
17
+ /**
18
+ * create a simple 1 frame texture atlas based on the given parameters
19
+ * @ignore
20
+ */
21
+ function createAtlas(width, height, name = "default", repeat = "no-repeat") {
22
+ return {
23
+ "meta" : {
24
+ "app" : "melonJS",
25
+ "size" : { "w" : width, "h" : height },
26
+ "repeat" : repeat,
27
+ "image" : "default"
28
+ },
29
+ "frames" : [{
30
+ "filename" : name,
31
+ "frame" : { "x" : 0, "y" : 0, "w" : width, "h" : height }
32
+ }]
33
+ };
34
+ }
35
+
36
+ /**
37
+ * @classdesc
38
+ * A Texture atlas class, currently supports : <br>
39
+ * - [TexturePacker]{@link http://www.codeandweb.com/texturepacker/} : through JSON export (standard and multipack texture atlas) <br>
40
+ * - [ShoeBox]{@link http://renderhjs.net/shoebox/} : through JSON export using the
41
+ * melonJS setting [file]{@link https://github.com/melonjs/melonJS/raw/master/media/shoebox_JSON_export.sbx} <br>
42
+ * - [Free Texture Packer]{@link http://free-tex-packer.com/app/} : through JSON export (standard and multipack texture atlas) <br>
43
+ * - Standard (fixed cell size) spritesheet : through a {framewidth:xx, frameheight:xx, anchorPoint:me.Vector2d} object
44
+ * );
45
+ */
46
+ class TextureAtlas {
47
+ /**
48
+ * @param {object|object[]} atlases - atlas information. See {@link loader.getJSON}
49
+ * @param {HTMLImageElement|HTMLCanvasElement|string|HTMLImageElement[]|HTMLCanvasElement[]|string[]} [src=atlas.meta.image] - Image source
50
+ * @param {boolean} [cache=false] - Use true to skip caching this Texture
51
+ * @example
52
+ * // create a texture atlas from a JSON Object
53
+ * game.texture = new me.TextureAtlas(
54
+ * me.loader.getJSON("texture")
55
+ * );
56
+ *
57
+ * // create a texture atlas from a multipack JSON Object
58
+ * game.texture = new me.TextureAtlas([
59
+ * me.loader.getJSON("texture-0"),
60
+ * me.loader.getJSON("texture-1"),
61
+ * me.loader.getJSON("texture-2")
62
+ * ]);
63
+ *
64
+ * // create a texture atlas for a spritesheet with an anchorPoint in the center of each frame
65
+ * game.texture = new me.TextureAtlas(
66
+ * {
67
+ * framewidth : 32,
68
+ * frameheight : 32,
69
+ * anchorPoint : new me.Vector2d(0.5, 0.5)
70
+ * },
71
+ * me.loader.getImage("spritesheet")
72
+ */
73
+ constructor (atlases, src, cache) {
74
+ /**
75
+ * to identify the atlas format (e.g. texture packer)
76
+ * @ignore
77
+ */
78
+ this.format = null;
79
+
80
+ /**
81
+ * the texture source(s) itself
82
+ * @type {Map}
83
+ * @ignore
84
+ */
85
+ this.sources = new Map();
86
+
87
+ /**
88
+ * the atlas dictionnaries
89
+ * @type {Map}
90
+ * @ignore
91
+ */
92
+ this.atlases = new Map();
93
+
94
+ // parse given atlas(es) paremeters
95
+ if (typeof (atlases) !== "undefined") {
96
+ // normalize to array to keep the following code generic
97
+ atlases = Array.isArray(atlases) ? atlases : [atlases];
98
+ for (var i in atlases) {
99
+ var atlas = atlases[i];
100
+
101
+ if (typeof(atlas.meta) !== "undefined") {
102
+ // Texture Packer or Free Texture Packer
103
+ if (atlas.meta.app.includes("texturepacker") || atlas.meta.app.includes("free-tex-packer")) {
104
+ this.format = "texturepacker";
105
+ // set the texture
106
+ if (typeof(src) === "undefined") {
107
+ // get the texture name from the atlas meta data
108
+ var image = loader.getImage(atlas.meta.image);
109
+ if (!image) {
110
+ throw new Error(
111
+ "Atlas texture '" + image + "' not found"
112
+ );
113
+ }
114
+ this.sources.set(atlas.meta.image, image);
115
+ } else {
116
+ this.sources.set(atlas.meta.image || "default", typeof src === "string" ? loader.getImage(src) : src);
117
+ }
118
+ this.repeat = "no-repeat";
119
+ }
120
+ // ShoeBox
121
+ else if (atlas.meta.app.includes("ShoeBox")) {
122
+ if (!atlas.meta.exporter || !atlas.meta.exporter.includes("melonJS")) {
123
+ throw new Error(
124
+ "ShoeBox requires the JSON exporter : " +
125
+ "https://github.com/melonjs/melonJS/tree/master/media/shoebox_JSON_export.sbx"
126
+ );
127
+ }
128
+ this.format = "ShoeBox";
129
+ this.repeat = "no-repeat";
130
+ this.sources.set("default", typeof src === "string" ? loader.getImage(src) : src);
131
+ }
132
+ // Internal texture atlas
133
+ else if (atlas.meta.app.includes("melonJS")) {
134
+ this.format = "melonJS";
135
+ this.repeat = atlas.meta.repeat || "no-repeat";
136
+ this.sources.set("default", typeof src === "string" ? loader.getImage(src) : src);
137
+ }
138
+ // initialize the atlas
139
+ this.atlases.set(atlas.meta.image || "default", this.parse(atlas));
140
+
141
+ } else {
142
+ // a regular spritesheet
143
+ if (typeof(atlas.framewidth) !== "undefined" &&
144
+ typeof(atlas.frameheight) !== "undefined") {
145
+ this.format = "Spritesheet (fixed cell size)";
146
+ this.repeat = "no-repeat";
147
+
148
+ if (typeof(src) !== "undefined") {
149
+ // overwrite if specified
150
+ atlas.image = typeof src === "string" ? loader.getImage(src) : src;
151
+ }
152
+ // initialize the atlas
153
+ this.atlases.set("default", this.parseFromSpriteSheet(atlas));
154
+ this.sources.set("default", atlas.image);
155
+
156
+ }
157
+ }
158
+ } // end forEach
159
+ }
160
+
161
+ // if format not recognized
162
+ if (this.atlases.length === 0) {
163
+ throw new Error("texture atlas format not supported");
164
+ }
165
+
166
+ // Add self to TextureCache if cache !== false
167
+ if (cache !== false) {
168
+ this.sources.forEach((source) => {
169
+ if (cache instanceof TextureCache) {
170
+ cache.set(source, this);
171
+ } else {
172
+ renderer.cache.set(source, this);
173
+ }
174
+ });
175
+ }
176
+ }
177
+
178
+ /**
179
+ * build an atlas from the given data
180
+ * @ignore
181
+ */
182
+ parse(data) {
183
+ var atlas = {};
184
+
185
+ data.frames.forEach((frame) => {
186
+ // fix wrongly formatted JSON (e.g. last dummy object in ShoeBox)
187
+ if (frame.hasOwnProperty("filename")) {
188
+ // Source coordinates
189
+ var s = frame.frame;
190
+
191
+ var originX, originY;
192
+ // Pixel-based offset origin from the top-left of the source frame
193
+ var hasTextureAnchorPoint = (frame.spriteSourceSize && frame.sourceSize && frame.pivot);
194
+ if (hasTextureAnchorPoint) {
195
+ originX = (frame.sourceSize.w * frame.pivot.x) - ((frame.trimmed) ? frame.spriteSourceSize.x : 0);
196
+ originY = (frame.sourceSize.h * frame.pivot.y) - ((frame.trimmed) ? frame.spriteSourceSize.y : 0);
197
+ }
198
+
199
+ atlas[frame.filename] = {
200
+ name : frame.filename, // frame name
201
+ texture : data.meta.image || "default", // the source texture
202
+ offset : new Vector2d(s.x, s.y),
203
+ anchorPoint : (hasTextureAnchorPoint) ? new Vector2d(originX / s.w, originY / s.h) : null,
204
+ trimmed : !!frame.trimmed,
205
+ width : s.w,
206
+ height : s.h,
207
+ angle : (frame.rotated === true) ? -ETA : 0
208
+ };
209
+ this.addUVs(atlas, frame.filename, data.meta.size.w, data.meta.size.h);
210
+ }
211
+ });
212
+ return atlas;
213
+ }
214
+
215
+ /**
216
+ * build an atlas from the given spritesheet
217
+ * @ignore
218
+ */
219
+ parseFromSpriteSheet(data) {
220
+ var atlas = {};
221
+ var image = data.image;
222
+ var spacing = data.spacing || 0;
223
+ var margin = data.margin || 0;
224
+
225
+ var width = image.width;
226
+ var height = image.height;
227
+
228
+ // calculate the sprite count (line, col)
229
+ var spritecount = pool.pull("Vector2d",
230
+ ~~((width - margin + spacing) / (data.framewidth + spacing)),
231
+ ~~((height - margin + spacing) / (data.frameheight + spacing))
232
+ );
233
+
234
+ // verifying the texture size
235
+ if ((width % (data.framewidth + spacing)) !== 0 ||
236
+ (height % (data.frameheight + spacing)) !== 0) {
237
+ var computed_width = spritecount.x * (data.framewidth + spacing);
238
+ var computed_height = spritecount.y * (data.frameheight + spacing);
239
+ if (computed_width - width !== spacing && computed_height - height !== spacing) {
240
+ // "truncate size" if delta is different from the spacing size
241
+ width = computed_width;
242
+ height = computed_height;
243
+ // warning message
244
+ console.warn(
245
+ "Spritesheet Texture for image: " + image.src +
246
+ " is not divisible by " + (data.framewidth + spacing) +
247
+ "x" + (data.frameheight + spacing) +
248
+ ", truncating effective size to " + width + "x" + height
249
+ );
250
+ }
251
+ }
252
+
253
+ // build the local atlas
254
+ for (var frame = 0, count = spritecount.x * spritecount.y; frame < count; frame++) {
255
+ var name = "" + frame;
256
+ atlas[name] = {
257
+ name : name,
258
+ texture : "default", // the source texture
259
+ offset : new Vector2d(
260
+ margin + (spacing + data.framewidth) * (frame % spritecount.x),
261
+ margin + (spacing + data.frameheight) * ~~(frame / spritecount.x)
262
+ ),
263
+ anchorPoint : (data.anchorPoint || null),
264
+ trimmed : false,
265
+ width : data.framewidth,
266
+ height : data.frameheight,
267
+ angle : 0
268
+ };
269
+ this.addUVs(atlas, name, width, height);
270
+ }
271
+
272
+ pool.push(spritecount);
273
+
274
+ return atlas;
275
+ }
276
+
277
+ /**
278
+ * return the default or specified atlas dictionnary
279
+ * @param {string} [name] - atlas name in case of multipack textures
280
+ * @returns {object}
281
+ */
282
+ getAtlas(name) {
283
+ if (typeof name === "string") {
284
+ return this.atlases.get(name);
285
+ } else {
286
+ return this.atlases.values().next().value;
287
+ }
288
+ }
289
+
290
+ /**
291
+ * return the format of the atlas dictionnary
292
+ * @returns {string} will return "texturepacker", or "ShoeBox", or "melonJS", or "Spritesheet (fixed cell size)"
293
+ */
294
+ getFormat() {
295
+ return this.format;
296
+ }
297
+
298
+ /**
299
+ * return the source texture for the given region (or default one if none specified)
300
+ * @param {object} [region] - region name in case of multipack textures
301
+ * @returns {HTMLImageElement|HTMLCanvasElement}
302
+ */
303
+ getTexture(region) {
304
+ if ((typeof region === "object") && (typeof region.texture === "string")) {
305
+ return this.sources.get(region.texture);
306
+ } else {
307
+ return this.sources.values().next().value;
308
+ }
309
+ }
310
+
311
+ /**
312
+ * add a region to the atlas
313
+ * @param {string} name - region mame
314
+ * @param {number} x - x origin of the region
315
+ * @param {number} y - y origin of the region
316
+ * @param {number} w - width of the region
317
+ * @param {number} h - height of the region
318
+ * @returns {object} the created region
319
+ */
320
+ addRegion(name, x, y, w, h) {
321
+ // TODO: Require proper atlas regions instead of caching arbitrary region keys
322
+ if (renderer.settings.verbose === true) {
323
+ console.warn("Adding texture region", name, "for texture", this);
324
+ }
325
+
326
+ var source = this.getTexture();
327
+ var atlas = this.getAtlas();
328
+ var dw = source.width;
329
+ var dh = source.height;
330
+
331
+ atlas[name] = {
332
+ name : name,
333
+ offset : new Vector2d(x, y),
334
+ width : w,
335
+ height : h,
336
+ angle : 0
337
+ };
338
+
339
+ this.addUVs(atlas, name, dw, dh);
340
+
341
+ return atlas[name];
342
+ }
343
+
344
+ /**
345
+ * return a normalized region (or frame) information for the specified sprite name
346
+ * @param {string} name - name of the sprite
347
+ * @param {string} [atlas] - name of a specific atlas where to search for the region
348
+ * @returns {object}
349
+ */
350
+ getRegion(name, atlas) {
351
+ var region;
352
+ if (typeof atlas === "string") {
353
+ region = this.getAtlas(atlas)[name];
354
+ } else {
355
+ // look for the given region in each existing atlas
356
+ this.atlases.forEach((atlas) => {
357
+ if (typeof atlas[name] !== "undefined") {
358
+ // there should be only one
359
+ region = atlas[name];
360
+ }
361
+ });
362
+ }
363
+ return region;
364
+ }
365
+
366
+ /**
367
+ * return the uvs mapping for the given region
368
+ * @param {object} name - region (or frame) name
369
+ * @returns {Float32Array} region Uvs
370
+ */
371
+ getUVs(name) {
372
+ // Get the source texture region
373
+ var region = this.getRegion(name);
374
+
375
+ if (typeof(region) === "undefined") {
376
+ // TODO: Require proper atlas regions instead of caching arbitrary region keys
377
+ var keys = name.split(","),
378
+ sx = +keys[0],
379
+ sy = +keys[1],
380
+ sw = +keys[2],
381
+ sh = +keys[3];
382
+ region = this.addRegion(name, sx, sy, sw, sh);
383
+ }
384
+ return region.uvs;
385
+ }
386
+
387
+ /**
388
+ * add uvs mapping for the given region
389
+ * @param {object} atlas - the atlas dictionnary where the region is define
390
+ * @param {object} name - region (or frame) name
391
+ * @param {number} w - the width of the region
392
+ * @param {number} h - the height of the region
393
+ * @returns {Float32Array} the created region UVs
394
+ */
395
+ addUVs(atlas, name, w, h) {
396
+ // ignore if using the Canvas Renderer
397
+ if (renderer instanceof WebGLRenderer) {
398
+ // Source coordinates
399
+ var s = atlas[name].offset;
400
+ var sw = atlas[name].width;
401
+ var sh = atlas[name].height;
402
+
403
+ atlas[name].uvs = new Float32Array([
404
+ s.x / w, // u0 (left)
405
+ s.y / h, // v0 (top)
406
+ (s.x + sw) / w, // u1 (right)
407
+ (s.y + sh) / h // v1 (bottom)
408
+ ]);
409
+ // Cache source coordinates
410
+ // TODO: Remove this when the Batcher only accepts a region name
411
+ var key = s.x + "," + s.y + "," + w + "," + h;
412
+ atlas[key] = atlas[name];
413
+ }
414
+ return atlas[name].uvs;
415
+ }
416
+
417
+ /**
418
+ * Create a sprite object using the first region found using the specified name
419
+ * @param {string} name - name of the sprite
420
+ * @param {object} [settings] - Additional settings passed to the {@link Sprite} contructor
421
+ * @param {boolean} [nineSlice=false] - if true returns a 9-slice sprite
422
+ * @returns {Sprite|NineSliceSprite}
423
+ * @example
424
+ * // create a new texture object under the `game` namespace
425
+ * game.texture = new me.TextureAtlas(
426
+ * me.loader.getJSON("texture"),
427
+ * me.loader.getImage("texture")
428
+ * );
429
+ * ...
430
+ * ...
431
+ * // create a new "coin" sprite
432
+ * var sprite = game.texture.createSpriteFromName("coin.png");
433
+ * // set the renderable position to bottom center
434
+ * sprite.anchorPoint.set(0.5, 1.0);
435
+ * ...
436
+ * ...
437
+ * // create a 9-slice sprite
438
+ * var dialogPanel = game.texture.createSpriteFromName(
439
+ * "rpg_dialo.png",
440
+ * // width & height are mandatory for 9-slice sprites
441
+ * { width: this.width, height: this.height },
442
+ * true
443
+ * );
444
+ */
445
+ createSpriteFromName(name, settings, nineSlice = false) {
446
+ // instantiate a new sprite object
447
+ return pool.pull(
448
+ nineSlice === true ? "me.NineSliceSprite" : "me.Sprite",
449
+ 0, 0,
450
+ Object.assign({
451
+ image: this,
452
+ region : name
453
+ }, settings || {})
454
+ );
455
+ }
456
+
457
+ /**
458
+ * Create an animation object using the first region found using all specified names
459
+ * @param {string[]|number[]} names - list of names for each sprite
460
+ * (when manually creating a Texture out of a spritesheet, only numeric values are authorized)
461
+ * @param {object} [settings] - Additional settings passed to the {@link Sprite} contructor
462
+ * @returns {Sprite}
463
+ * @example
464
+ * // create a new texture object under the `game` namespace
465
+ * game.texture = new me.TextureAtlas(
466
+ * me.loader.getJSON("texture"),
467
+ * me.loader.getImage("texture")
468
+ * );
469
+ *
470
+ * // create a new Animated Sprite
471
+ * var sprite = game.texture.createAnimationFromName([
472
+ * "walk0001.png", "walk0002.png", "walk0003.png",
473
+ * "walk0004.png", "walk0005.png", "walk0006.png",
474
+ * "walk0007.png", "walk0008.png", "walk0009.png",
475
+ * "walk0010.png", "walk0011.png"
476
+ * ]);
477
+ *
478
+ * // define an additional basic walking animation
479
+ * sprite.addAnimation ("simple_walk", [0,2,1]);
480
+ * // you can also use frame name to define your animation
481
+ * sprite.addAnimation ("speed_walk", ["walk0007.png", "walk0008.png", "walk0009.png", "walk0010.png"]);
482
+ * // set the default animation
483
+ * sprite.setCurrentAnimation("simple_walk");
484
+ * // set the renderable position to bottom center
485
+ * sprite.anchorPoint.set(0.5, 1.0);
486
+ */
487
+ createAnimationFromName(names, settings) {
488
+ var tpAtlas = [], indices = {};
489
+ var width = 0, height = 0;
490
+ var region;
491
+ // iterate through the given names
492
+ // and create a "normalized" atlas
493
+ for (var i = 0; i < names.length; ++i) {
494
+ region = this.getRegion(names[i]);
495
+ if (region == null) {
496
+ // throw an error
497
+ throw new Error("Texture - region for " + names[i] + " not found");
498
+ }
499
+ tpAtlas[i] = region;
500
+ // save the corresponding index
501
+ indices[names[i]] = i;
502
+ // calculate the max size of a frame
503
+ width = Math.max(region.width, width);
504
+ height = Math.max(region.height, height);
505
+ }
506
+ // instantiate a new animation sheet object
507
+ return new Sprite(0, 0, Object.assign({
508
+ image: this,
509
+ framewidth: width,
510
+ frameheight: height,
511
+ margin: 0,
512
+ spacing: 0,
513
+ atlas: tpAtlas,
514
+ atlasIndices: indices
515
+ }, settings || {}));
516
+ }
517
+ }
518
+
519
+ export { TextureAtlas, createAtlas };
@@ -0,0 +1,143 @@
1
+ /*!
2
+ * melonJS Game Engine - v14.1.0
3
+ * http://www.melonjs.org
4
+ * melonjs is licensed under the MIT License.
5
+ * http://www.opensource.org/licenses/mit-license
6
+ * @copyright (C) 2011 - 2022 Olivier Biot (AltByte Pte Ltd)
7
+ */
8
+ import { renderer } from '../video.js';
9
+ import { getBasename } from '../../utils/file.js';
10
+ import { createAtlas, TextureAtlas } from './atlas.js';
11
+ import { isPowerOfTwo } from '../../math/math.js';
12
+ import '../../node_modules/@teppeis/multimaps/dist/src/index.js';
13
+ import { __exports as src } from '../../_virtual/index.js';
14
+
15
+ /**
16
+ * a basic texture cache object
17
+ * @ignore
18
+ */
19
+ class TextureCache {
20
+
21
+ /**
22
+ * @ignore
23
+ */
24
+ constructor(max_size) {
25
+ // cache uses an array to allow for duplicated key
26
+ this.cache = new src.ArrayMultimap();
27
+ this.tinted = new Map();
28
+ this.units = new Map();
29
+ this.max_size = max_size || Infinity;
30
+ this.clear();
31
+ }
32
+
33
+ /**
34
+ * @ignore
35
+ */
36
+ clear() {
37
+ this.cache.clear();
38
+ this.tinted.clear();
39
+ this.units.clear();
40
+ this.length = 0;
41
+ }
42
+
43
+ /**
44
+ * @ignore
45
+ */
46
+ validate() {
47
+ if (this.length >= this.max_size) {
48
+ // TODO: Merge textures instead of throwing an exception
49
+ throw new Error(
50
+ "Texture cache overflow: " + this.max_size +
51
+ " texture units available for this GPU."
52
+ );
53
+ }
54
+ }
55
+
56
+ /**
57
+ * @ignore
58
+ */
59
+ get(image, atlas) {
60
+ var entry;
61
+
62
+ if (typeof atlas === "undefined") {
63
+ entry = this.cache.get(image)[0];
64
+ } else {
65
+ // manage cases where a specific atlas is specified
66
+ this.cache.forEach((value, key) => {
67
+ var _atlas = value.getAtlas();
68
+ if (key === image && _atlas[0].width === atlas.framewidth && _atlas[0].height === atlas.frameheight) {
69
+ entry = value;
70
+ }
71
+ });
72
+ }
73
+
74
+ if (typeof entry === "undefined") {
75
+ if (!atlas) {
76
+ atlas = createAtlas(image.width, image.height, image.src ? getBasename(image.src) : undefined);
77
+ }
78
+ entry = new TextureAtlas(atlas, image, false);
79
+ this.set(image, entry);
80
+ }
81
+
82
+ return entry;
83
+ }
84
+
85
+ /**
86
+ * @ignore
87
+ */
88
+ delete(image) {
89
+ if (!this.cache.has(image)) {
90
+ this.cache.delete(image);
91
+ }
92
+ }
93
+
94
+ /**
95
+ * @ignore
96
+ */
97
+ tint(src, color) {
98
+ // make sure the src is in the cache
99
+ var image_cache = this.tinted.get(src);
100
+
101
+ if (image_cache === undefined) {
102
+ image_cache = this.tinted.set(src, new Map());
103
+ }
104
+
105
+ if (!image_cache.has(color)) {
106
+ image_cache.set(color, renderer.tint(src, color, "multiply"));
107
+ }
108
+
109
+ return image_cache.get(color);
110
+ }
111
+
112
+ /**
113
+ * @ignore
114
+ */
115
+ set(image, texture) {
116
+ var width = image.width;
117
+ var height = image.height;
118
+
119
+ // warn if a non POT texture is added to the cache when using WebGL1
120
+ if (renderer.WebGLVersion === 1 && (!isPowerOfTwo(width) || !isPowerOfTwo(height))) {
121
+ var src = typeof image.src !== "undefined" ? image.src : image;
122
+ console.warn(
123
+ "[Texture] " + src + " is not a POT texture " +
124
+ "(" + width + "x" + height + ")"
125
+ );
126
+ }
127
+ return this.cache.put(image, texture);
128
+ }
129
+
130
+ /**
131
+ * @ignore
132
+ */
133
+ getUnit(texture) {
134
+ if (!this.units.has(texture)) {
135
+ this.validate();
136
+ this.units.set(texture, this.length++);
137
+ }
138
+ return this.units.get(texture);
139
+ }
140
+ }
141
+ var TextureCache$1 = TextureCache;
142
+
143
+ export { TextureCache$1 as default };