melonjs 14.0.2 → 14.1.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 (219) 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 +238 -0
  11. package/dist/melonjs.mjs/audio/audio.js +536 -0
  12. package/dist/melonjs.mjs/camera/camera2d.js +731 -0
  13. package/dist/melonjs.mjs/entity/entity.js +247 -0
  14. package/dist/melonjs.mjs/game.js +29 -0
  15. package/dist/melonjs.mjs/geometries/ellipse.js +274 -0
  16. package/dist/melonjs.mjs/geometries/line.js +115 -0
  17. package/dist/melonjs.mjs/geometries/path2d.js +318 -0
  18. package/dist/melonjs.mjs/geometries/point.js +88 -0
  19. package/dist/melonjs.mjs/geometries/poly.js +498 -0
  20. package/dist/melonjs.mjs/geometries/rectangle.js +374 -0
  21. package/dist/melonjs.mjs/geometries/roundrect.js +167 -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 +446 -0
  32. package/dist/melonjs.mjs/level/tiled/TMXObject.js +355 -0
  33. package/dist/melonjs.mjs/level/tiled/TMXTile.js +193 -0
  34. package/dist/melonjs.mjs/level/tiled/TMXTileMap.js +636 -0
  35. package/dist/melonjs.mjs/level/tiled/TMXTileset.js +309 -0
  36. package/dist/melonjs.mjs/level/tiled/TMXTilesetGroup.js +81 -0
  37. package/dist/melonjs.mjs/level/tiled/TMXUtils.js +367 -0
  38. package/dist/melonjs.mjs/level/tiled/renderer/TMXHexagonalRenderer.js +504 -0
  39. package/dist/melonjs.mjs/level/tiled/renderer/TMXIsometricRenderer.js +218 -0
  40. package/dist/melonjs.mjs/level/tiled/renderer/TMXOrthogonalRenderer.js +155 -0
  41. package/dist/melonjs.mjs/level/tiled/renderer/TMXRenderer.js +124 -0
  42. package/dist/melonjs.mjs/level/tiled/renderer/TMXStaggeredRenderer.js +107 -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 +616 -0
  47. package/dist/melonjs.mjs/math/math.js +218 -0
  48. package/dist/melonjs.mjs/math/matrix2.js +501 -0
  49. package/dist/melonjs.mjs/math/matrix3.js +679 -0
  50. package/dist/melonjs.mjs/math/observable_vector2.js +469 -0
  51. package/dist/melonjs.mjs/math/observable_vector3.js +559 -0
  52. package/dist/melonjs.mjs/math/vector2.js +526 -0
  53. package/dist/melonjs.mjs/math/vector3.js +567 -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 +265 -0
  62. package/dist/melonjs.mjs/particles/particle.js +186 -0
  63. package/dist/melonjs.mjs/particles/settings.js +319 -0
  64. package/dist/melonjs.mjs/physics/body.js +702 -0
  65. package/dist/melonjs.mjs/physics/bounds.js +459 -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 +394 -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 +219 -0
  72. package/dist/melonjs.mjs/plugin/plugin.js +141 -0
  73. package/dist/melonjs.mjs/renderable/collectable.js +60 -0
  74. package/dist/melonjs.mjs/renderable/colorlayer.js +78 -0
  75. package/dist/melonjs.mjs/renderable/container.js +1016 -0
  76. package/dist/melonjs.mjs/renderable/dragndrop.js +224 -0
  77. package/dist/melonjs.mjs/renderable/imagelayer.js +305 -0
  78. package/dist/melonjs.mjs/renderable/light2d.js +155 -0
  79. package/dist/melonjs.mjs/renderable/nineslicesprite.js +246 -0
  80. package/dist/melonjs.mjs/renderable/renderable.js +781 -0
  81. package/dist/melonjs.mjs/renderable/sprite.js +653 -0
  82. package/dist/melonjs.mjs/renderable/trigger.js +156 -0
  83. package/dist/melonjs.mjs/renderable/ui/uibaseelement.js +212 -0
  84. package/dist/melonjs.mjs/renderable/ui/uispriteelement.js +225 -0
  85. package/dist/melonjs.mjs/renderable/ui/uitextbutton.js +127 -0
  86. package/dist/melonjs.mjs/state/stage.js +236 -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 +363 -0
  96. package/dist/melonjs.mjs/text/bitmaptextdata.js +198 -0
  97. package/dist/melonjs.mjs/text/glyph.js +65 -0
  98. package/dist/melonjs.mjs/text/text.js +452 -0
  99. package/dist/melonjs.mjs/text/textmetrics.js +175 -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 +479 -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 +806 -0
  111. package/dist/melonjs.mjs/video/renderer.js +410 -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 +142 -0
  117. package/dist/melonjs.mjs/video/webgl/glshader.js +167 -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 +494 -0
  128. package/dist/melonjs.mjs/video/webgl/webgl_renderer.js +1035 -0
  129. package/dist/melonjs.module.d.ts +1298 -1359
  130. package/dist/melonjs.module.js +2072 -3520
  131. package/package.json +21 -16
  132. package/src/application/application.js +4 -5
  133. package/src/audio/audio.js +32 -32
  134. package/src/camera/camera2d.js +32 -33
  135. package/src/entity/entity.js +18 -19
  136. package/src/geometries/ellipse.js +17 -18
  137. package/src/geometries/line.js +6 -7
  138. package/src/geometries/path2d.js +33 -34
  139. package/src/geometries/point.js +1 -2
  140. package/src/geometries/poly.js +16 -18
  141. package/src/geometries/rectangle.js +19 -20
  142. package/src/geometries/roundrect.js +9 -10
  143. package/src/input/gamepad.js +15 -15
  144. package/src/input/keyboard.js +12 -12
  145. package/src/input/pointer.js +6 -6
  146. package/src/input/pointerevent.js +12 -12
  147. package/src/lang/deprecated.js +12 -12
  148. package/src/level/level.js +25 -25
  149. package/src/level/tiled/TMXLayer.js +23 -24
  150. package/src/level/tiled/TMXTile.js +6 -7
  151. package/src/level/tiled/TMXTileMap.js +8 -10
  152. package/src/level/tiled/TMXTileset.js +3 -4
  153. package/src/level/tiled/TMXTilesetGroup.js +1 -2
  154. package/src/level/tiled/TMXUtils.js +5 -5
  155. package/src/level/tiled/renderer/TMXHexagonalRenderer.js +3 -4
  156. package/src/level/tiled/renderer/TMXIsometricRenderer.js +3 -4
  157. package/src/level/tiled/renderer/TMXOrthogonalRenderer.js +2 -3
  158. package/src/level/tiled/renderer/TMXRenderer.js +20 -21
  159. package/src/level/tiled/renderer/TMXStaggeredRenderer.js +1 -2
  160. package/src/loader/loader.js +20 -20
  161. package/src/math/color.js +21 -22
  162. package/src/math/math.js +16 -16
  163. package/src/math/matrix2.js +17 -18
  164. package/src/math/matrix3.js +26 -27
  165. package/src/math/observable_vector2.js +15 -16
  166. package/src/math/observable_vector3.js +17 -18
  167. package/src/math/vector2.js +10 -11
  168. package/src/math/vector3.js +11 -12
  169. package/src/particles/emitter.js +7 -8
  170. package/src/particles/particle.js +3 -4
  171. package/src/physics/body.js +29 -30
  172. package/src/physics/bounds.js +10 -10
  173. package/src/physics/collision.js +2 -2
  174. package/src/physics/detector.js +6 -6
  175. package/src/physics/quadtree.js +18 -23
  176. package/src/physics/sat.js +31 -31
  177. package/src/physics/world.js +6 -7
  178. package/src/plugin/plugin.js +5 -5
  179. package/src/renderable/collectable.js +4 -6
  180. package/src/renderable/colorlayer.js +6 -8
  181. package/src/renderable/container.js +25 -27
  182. package/src/renderable/dragndrop.js +14 -14
  183. package/src/renderable/imagelayer.js +14 -15
  184. package/src/renderable/light2d.js +4 -5
  185. package/src/renderable/nineslicesprite.js +17 -18
  186. package/src/renderable/renderable.js +26 -28
  187. package/src/renderable/sprite.js +29 -30
  188. package/src/renderable/trigger.js +16 -17
  189. package/src/renderable/ui/uibaseelement.js +8 -9
  190. package/src/renderable/ui/uispriteelement.js +8 -8
  191. package/src/renderable/ui/uitextbutton.js +15 -15
  192. package/src/state/stage.js +8 -9
  193. package/src/state/state.js +17 -17
  194. package/src/system/device.js +11 -11
  195. package/src/system/event.js +10 -10
  196. package/src/system/pooling.js +9 -9
  197. package/src/system/save.js +2 -2
  198. package/src/system/timer.js +10 -10
  199. package/src/text/bitmaptext.js +19 -20
  200. package/src/text/bitmaptextdata.js +3 -4
  201. package/src/text/glyph.js +1 -2
  202. package/src/text/text.js +24 -25
  203. package/src/text/textmetrics.js +9 -10
  204. package/src/tweens/tween.js +20 -21
  205. package/src/utils/agent.js +5 -5
  206. package/src/utils/array.js +4 -4
  207. package/src/utils/file.js +2 -2
  208. package/src/utils/function.js +6 -6
  209. package/src/utils/string.js +5 -5
  210. package/src/utils/utils.js +4 -4
  211. package/src/video/canvas/canvas_renderer.js +72 -73
  212. package/src/video/renderer.js +27 -28
  213. package/src/video/texture/atlas.js +22 -22
  214. package/src/video/texture/canvas_texture.js +9 -9
  215. package/src/video/video.js +17 -17
  216. package/src/video/webgl/buffer/vertex.js +1 -2
  217. package/src/video/webgl/glshader.js +12 -12
  218. package/src/video/webgl/webgl_compositor.js +42 -43
  219. package/src/video/webgl/webgl_renderer.js +76 -77
@@ -0,0 +1,801 @@
1
+ /*!
2
+ * melonJS Game Engine - v14.1.1
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 { getBasename, getExtension } from '../utils/file.js';
9
+ import { emit, LOADER_PROGRESS, LOADER_COMPLETE } from '../system/event.js';
10
+ import { ua } from '../system/platform.js';
11
+ import { load, unload, unloadAll } from '../audio/audio.js';
12
+ import state from '../state/state.js';
13
+ import level from '../level/level.js';
14
+ import { parse } from '../level/tiled/TMXUtils.js';
15
+ import { isDataUrl } from '../utils/string.js';
16
+
17
+ // contains all the images loaded
18
+ var imgList = {};
19
+ // contains all the TMX loaded
20
+ var tmxList = {};
21
+ // contains all the binary files loaded
22
+ var binList = {};
23
+ // contains all the JSON files
24
+ var jsonList = {};
25
+ // baseURL
26
+ var baseURL = {};
27
+
28
+ // flag to check loading status
29
+ var resourceCount = 0;
30
+ var loadCount = 0;
31
+ var timerId = 0;
32
+
33
+ /**
34
+ * check the loading status
35
+ * @ignore
36
+ */
37
+ function checkLoadStatus(onload) {
38
+ if (loadCount === resourceCount) {
39
+ // wait 1/2s and execute callback (cheap workaround to ensure everything is loaded)
40
+ if (typeof onload === "function" || loader.onload) {
41
+ // make sure we clear the timer
42
+ clearTimeout(timerId);
43
+ // trigger the onload callback
44
+ // we call either the supplied callback (which takes precedence) or the global one
45
+ var callback = onload || loader.onload;
46
+ setTimeout(() => {
47
+ callback();
48
+ emit(LOADER_COMPLETE);
49
+ }, 300);
50
+ }
51
+ else {
52
+ throw new Error("no load callback defined");
53
+ }
54
+ }
55
+ else {
56
+ timerId = setTimeout(() => {
57
+ checkLoadStatus(onload);
58
+ }, 100);
59
+ }
60
+ }
61
+
62
+ /**
63
+ * load Images
64
+ * @example
65
+ * preloadImages([
66
+ * { name : 'image1', src : 'images/image1.png'},
67
+ * { name : 'image2', src : 'images/image2.png'},
68
+ * { name : 'image3', src : 'images/image3.png'},
69
+ * { name : 'image4', src : 'images/image4.png'}
70
+ * ]);
71
+ * @ignore
72
+ */
73
+ function preloadImage(img, onload, onerror) {
74
+ // create new Image object and add to list
75
+ imgList[img.name] = new Image();
76
+ if (typeof onload === "function") {
77
+ imgList[img.name].onload = onload;
78
+ }
79
+ if (typeof onerror === "function") {
80
+ imgList[img.name].onerror = onerror;
81
+ }
82
+ if (typeof (loader.crossOrigin) === "string") {
83
+ imgList[img.name].crossOrigin = loader.crossOrigin;
84
+ }
85
+ imgList[img.name].src = img.src + loader.nocache;
86
+ }
87
+
88
+ /**
89
+ * load a font face
90
+ * @example
91
+ * preloadFontFace(
92
+ * name: "'kenpixel'", type: "fontface", src: "url('data/font/kenvector_future.woff2')"
93
+ * ]);
94
+ * @ignore
95
+ */
96
+ function preloadFontFace(data, onload, onerror) {
97
+
98
+ if (isDataUrl(data.src) === true) {
99
+ // make sure it in the `url(data:[<mediatype>][;base64],<data>)` format as expected by FontFace
100
+ if (!data.src.startsWith("url(")) {
101
+ data.src = "url(" + data.src + ")";
102
+ }
103
+ }
104
+
105
+ var font = new FontFace(data.name, data.src);
106
+
107
+ // loading promise
108
+ font.load().then(() => {
109
+ // apply the font after the font has finished downloading
110
+ document.fonts.add(font);
111
+ document.body.style.fontFamily = data.name;
112
+ if (typeof onload === "function") {
113
+ // onloaded callback
114
+ onload();
115
+ }
116
+ }, () => {
117
+ if (typeof onerror === "function") {
118
+ // rejected
119
+ onerror(data.name);
120
+ }
121
+ });
122
+ }
123
+
124
+ /**
125
+ * preload TMX files
126
+ * @ignore
127
+ */
128
+ function preloadTMX(tmxData, onload, onerror) {
129
+ /**
130
+ * @ignore
131
+ */
132
+ function addToTMXList(data) {
133
+ // set the TMX content
134
+ tmxList[tmxData.name] = data;
135
+
136
+ // add the tmx to the level manager
137
+ if (tmxData.type === "tmx") {
138
+ level.add(tmxData.type, tmxData.name);
139
+ }
140
+ }
141
+
142
+
143
+ //if the data is in the tmxData object, don't get it via a XMLHTTPRequest
144
+ if (tmxData.data) {
145
+ addToTMXList(tmxData.data);
146
+ if (typeof onload === "function") {
147
+ onload();
148
+ }
149
+ return;
150
+ }
151
+
152
+ var xmlhttp = new XMLHttpRequest();
153
+ // check the data format ('tmx', 'json')
154
+ var format = getExtension(tmxData.src);
155
+
156
+ if (xmlhttp.overrideMimeType) {
157
+ if (format === "json") {
158
+ xmlhttp.overrideMimeType("application/json");
159
+ }
160
+ else {
161
+ xmlhttp.overrideMimeType("text/xml");
162
+ }
163
+ }
164
+
165
+ xmlhttp.open("GET", tmxData.src + loader.nocache, true);
166
+ xmlhttp.withCredentials = loader.withCredentials;
167
+ // set the callbacks
168
+ xmlhttp.ontimeout = onerror;
169
+ xmlhttp.onreadystatechange = function () {
170
+ if (xmlhttp.readyState === 4) {
171
+ // status = 0 when file protocol is used, or cross-domain origin,
172
+ // (With Chrome use "--allow-file-access-from-files --disable-web-security")
173
+ if ((xmlhttp.status === 200) || ((xmlhttp.status === 0) && xmlhttp.responseText)) {
174
+ var result = null;
175
+
176
+ // parse response
177
+ switch (format) {
178
+ case "xml":
179
+ case "tmx":
180
+ case "tsx":
181
+ // ie9 does not fully implement the responseXML
182
+ if (ua.match(/msie/i) || !xmlhttp.responseXML) {
183
+ if (globalThis.DOMParser) {
184
+ // manually create the XML DOM
185
+ result = (new DOMParser()).parseFromString(xmlhttp.responseText, "text/xml");
186
+ } else {
187
+ throw new Error("XML file format loading not supported, use the JSON file format instead");
188
+ }
189
+ }
190
+ else {
191
+ result = xmlhttp.responseXML;
192
+ }
193
+ // converts to a JS object
194
+ var data = parse(result);
195
+ switch (format) {
196
+ case "tmx":
197
+ result = data.map;
198
+ break;
199
+
200
+ case "tsx":
201
+ result = data.tilesets[0];
202
+ break;
203
+ }
204
+
205
+ break;
206
+
207
+ case "json":
208
+ result = JSON.parse(xmlhttp.responseText);
209
+ break;
210
+
211
+ default:
212
+ throw new Error("TMX file format " + format + "not supported !");
213
+ }
214
+
215
+ //set the TMX content
216
+ addToTMXList(result);
217
+
218
+ // fire the callback
219
+ if (typeof onload === "function") {
220
+ onload();
221
+ }
222
+ }
223
+ else if (typeof onerror === "function") {
224
+ onerror(tmxData.name);
225
+ }
226
+ }
227
+ };
228
+ // send the request
229
+ xmlhttp.send();
230
+ }
231
+
232
+ /**
233
+ * preload JSON files
234
+ * @ignore
235
+ */
236
+ function preloadJSON(data, onload, onerror) {
237
+ var xmlhttp = new XMLHttpRequest();
238
+
239
+ if (xmlhttp.overrideMimeType) {
240
+ xmlhttp.overrideMimeType("application/json");
241
+ }
242
+
243
+ xmlhttp.open("GET", data.src + loader.nocache, true);
244
+ xmlhttp.withCredentials = loader.withCredentials;
245
+
246
+ // set the callbacks
247
+ xmlhttp.ontimeout = onerror;
248
+ xmlhttp.onreadystatechange = function () {
249
+ if (xmlhttp.readyState === 4) {
250
+ // status = 0 when file protocol is used, or cross-domain origin,
251
+ // (With Chrome use "--allow-file-access-from-files --disable-web-security")
252
+ if ((xmlhttp.status === 200) || ((xmlhttp.status === 0) && xmlhttp.responseText)) {
253
+ // get the Texture Packer Atlas content
254
+ jsonList[data.name] = JSON.parse(xmlhttp.responseText);
255
+ if (typeof onload === "function") {
256
+ // fire the callback
257
+ onload();
258
+ }
259
+ }
260
+ else if (typeof onerror === "function") {
261
+ onerror(data.name);
262
+ }
263
+ }
264
+ };
265
+ // send the request
266
+ xmlhttp.send();
267
+ }
268
+
269
+ /**
270
+ * preload Binary files
271
+ * @ignore
272
+ */
273
+ function preloadBinary(data, onload, onerror) {
274
+ var httpReq = new XMLHttpRequest();
275
+
276
+ // load our file
277
+ httpReq.open("GET", data.src + loader.nocache, true);
278
+ httpReq.withCredentials = loader.withCredentials;
279
+ httpReq.responseType = "arraybuffer";
280
+ httpReq.onerror = onerror;
281
+ httpReq.onload = function () {
282
+ var arrayBuffer = httpReq.response;
283
+ if (arrayBuffer) {
284
+ var byteArray = new Uint8Array(arrayBuffer);
285
+ var buffer = [];
286
+ for (var i = 0; i < byteArray.byteLength; i++) {
287
+ buffer[i] = String.fromCharCode(byteArray[i]);
288
+ }
289
+ binList[data.name] = buffer.join("");
290
+ if (typeof onload === "function") {
291
+ // callback
292
+ onload();
293
+ }
294
+
295
+ }
296
+ };
297
+ httpReq.send();
298
+ }
299
+
300
+ /**
301
+ * preload Binary files
302
+ * @ignore
303
+ */
304
+ function preloadJavascript(data, onload, onerror) {
305
+ var script = document.createElement("script");
306
+
307
+ script.src = data.src;
308
+ script.type = "text/javascript";
309
+ if (typeof (loader.crossOrigin) === "string") {
310
+ script.crossOrigin = loader.crossOrigin;
311
+ }
312
+ script.defer = true;
313
+
314
+ if (typeof onload === "function") {
315
+ script.onload = () => {
316
+ // callback
317
+ onload();
318
+ };
319
+ }
320
+
321
+ if (typeof onerror === "function") {
322
+ script.onerror = () => {
323
+ // callback
324
+ onerror(data.name);
325
+ };
326
+ }
327
+
328
+ document.getElementsByTagName("body")[0].appendChild(script);
329
+ }
330
+
331
+ /**
332
+ * a small class to manage loading of stuff and manage resources
333
+ * @namespace loader
334
+ */
335
+ var loader = {
336
+
337
+ /**
338
+ * to enable/disable caching
339
+ * @ignore
340
+ */
341
+ nocache : "",
342
+
343
+ /*
344
+ * PUBLIC STUFF
345
+ */
346
+
347
+ /**
348
+ * onload callback
349
+ * @public
350
+ * @name onload
351
+ * @memberof loader
352
+ * @example
353
+ * // set a callback when everything is loaded
354
+ * me.loader.onload = this.loaded.bind(this);
355
+ */
356
+ onload : undefined,
357
+
358
+ /**
359
+ * onProgress callback<br>
360
+ * each time a resource is loaded, the loader will fire the specified function,
361
+ * giving the actual progress [0 ... 1], as argument, and an object describing the resource loaded
362
+ * @public
363
+ * @name onProgress
364
+ * @memberof loader
365
+ * @example
366
+ * // set a callback for progress notification
367
+ * me.loader.onProgress = this.updateProgress.bind(this);
368
+ */
369
+ onProgress : undefined,
370
+
371
+ /**
372
+ * crossOrigin attribute to configure the CORS requests for Image data element.
373
+ * By default (that is, when the attribute is not specified), CORS is not used at all.
374
+ * The "anonymous" keyword means that there will be no exchange of user credentials via cookies,
375
+ * client-side SSL certificates or HTTP authentication as described in the Terminology section of the CORS specification.<br>
376
+ * @public
377
+ * @type {string}
378
+ * @name crossOrigin
379
+ * @default undefined
380
+ * @memberof loader
381
+ * @see https://developer.mozilla.org/en-US/docs/Web/HTML/CORS_settings_attributes
382
+ * @example
383
+ * // allow for cross-origin texture loading in WebGL
384
+ * me.loader.crossOrigin = "anonymous";
385
+ *
386
+ * // set all ressources to be loaded
387
+ * me.loader.preload(game.resources, this.loaded.bind(this));
388
+ */
389
+ crossOrigin : undefined,
390
+
391
+ /**
392
+ * indicates whether or not cross-site Access-Control requests should be made using credentials such as cookies,
393
+ * authorization headers or TLS client certificates. Setting withCredentials has no effect on same-site requests.
394
+ * @public
395
+ * @type {boolean}
396
+ * @name withCredentials
397
+ * @default false
398
+ * @memberof loader
399
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/withCredentials
400
+ * @example
401
+ * // enable withCredentials
402
+ * me.loader.withCredentials = true;
403
+ *
404
+ * // set all ressources to be loaded
405
+ * me.loader.preload(game.resources, this.loaded.bind(this));
406
+ */
407
+ withCredentials : false,
408
+
409
+ /**
410
+ * just increment the number of already loaded resources
411
+ * @ignore
412
+ */
413
+ onResourceLoaded(res) {
414
+ // increment the loading counter
415
+ loadCount++;
416
+
417
+ // currrent progress
418
+ var progress = loadCount / resourceCount;
419
+
420
+ // call callback if defined
421
+ if (this.onProgress) {
422
+ // pass the load progress in percent, as parameter
423
+ this.onProgress(progress, res);
424
+ }
425
+ emit(LOADER_PROGRESS, progress, res);
426
+ },
427
+
428
+ /**
429
+ * on error callback for image loading
430
+ * @ignore
431
+ */
432
+ onLoadingError(res) {
433
+ throw new Error("Failed loading resource " + res.src);
434
+ },
435
+
436
+ /**
437
+ * enable the nocache mechanism
438
+ * @ignore
439
+ */
440
+ setNocache(enable) {
441
+ this.nocache = enable ? "?" + ~~(Math.random() * 10000000) : "";
442
+ },
443
+
444
+ /**
445
+ * change the default baseURL for the given asset type.<br>
446
+ * (this will prepend the asset URL and must finish with a '/')
447
+ * @name setBaseURL
448
+ * @memberof loader
449
+ * @public
450
+ * @param {string} type - "*", "audio", binary", "image", "json", "js", "tmx", "tsx"
451
+ * @param {string} [url="./"] - default base URL
452
+ * @example
453
+ * // change the base URL relative address for audio assets
454
+ * me.loader.setBaseURL("audio", "data/audio/");
455
+ * // change the base URL absolute address for all object types
456
+ * me.loader.setBaseURL("*", "http://myurl.com/")
457
+ */
458
+ setBaseURL(type, url) {
459
+ if (type !== "*") {
460
+ baseURL[type] = url;
461
+ } else {
462
+ // "wildcards"
463
+ baseURL["audio"] = url;
464
+ baseURL["binary"] = url;
465
+ baseURL["image"] = url;
466
+ baseURL["json"] = url;
467
+ baseURL["js"] = url;
468
+ baseURL["tmx"] = url;
469
+ baseURL["tsx"] = url;
470
+ // XXX ?
471
+ //baseURL["fontface"] = url;
472
+ }
473
+ },
474
+
475
+ /**
476
+ * set all the specified game resources to be preloaded.
477
+ * @name preload
478
+ * @memberof loader
479
+ * @public
480
+ * @param {object[]} res
481
+ * @param {string} res.name - internal name of the resource
482
+ * @param {string} res.type - "audio", binary", "image", "json","js", "tmx", "tsx", "fontface"
483
+ * @param {string} res.src - path and/or file name of the resource (for audio assets only the path is required)
484
+ * @param {boolean} [res.stream] - Set to true to force HTML5 Audio, which allows not to wait for large file to be downloaded before playing.
485
+ * @param {Function} [onload=loader.onload] - function to be called when all resources are loaded
486
+ * @param {boolean} [switchToLoadState=true] - automatically switch to the loading screen
487
+ * @example
488
+ * game_resources = [
489
+ * // PNG tileset
490
+ * {name: "tileset-platformer", type: "image", src: "data/map/tileset.png"},
491
+ * // PNG packed texture
492
+ * {name: "texture", type:"image", src: "data/gfx/texture.png"}
493
+ * // PNG base64 encoded image
494
+ * {name: "texture", type:"image", src: "data:image/png;base64,iVBORw0KAAAQAAAAEACA..."}
495
+ * // TSX file
496
+ * {name: "meta_tiles", type: "tsx", src: "data/map/meta_tiles.tsx"},
497
+ * // TMX level (XML & JSON)
498
+ * {name: "map1", type: "tmx", src: "data/map/map1.json"},
499
+ * {name: "map2", type: "tmx", src: "data/map/map2.tmx"},
500
+ * {name: "map3", type: "tmx", format: "json", data: {"height":15,"layers":[...],"tilewidth":32,"version":1,"width":20}},
501
+ * {name: "map4", type: "tmx", format: "xml", data: {xml representation of tmx}},
502
+ * // audio resources
503
+ * {name: "bgmusic", type: "audio", src: "data/audio/"},
504
+ * {name: "cling", type: "audio", src: "data/audio/"},
505
+ * // base64 encoded audio resources
506
+ * {name: "band", type: "audio", src: "data:audio/wav;base64,..."},
507
+ * // binary file
508
+ * {name: "ymTrack", type: "binary", src: "data/audio/main.ym"},
509
+ * // JSON file (used for texturePacker)
510
+ * {name: "texture", type: "json", src: "data/gfx/texture.json"},
511
+ * // JavaScript file
512
+ * {name: "plugin", type: "js", src: "data/js/plugin.js"},
513
+ * // Font Face
514
+ * { name: "'kenpixel'", type: "fontface", src: "url('data/font/kenvector_future.woff2')" }
515
+ * ];
516
+ * ...
517
+ * // set all resources to be loaded
518
+ * me.loader.preload(game.resources, this.loaded.bind(this));
519
+ */
520
+ preload(res, onload, switchToLoadState = true) {
521
+ // parse the resources
522
+ for (var i = 0; i < res.length; i++) {
523
+ resourceCount += this.load(
524
+ res[i],
525
+ this.onResourceLoaded.bind(this, res[i]),
526
+ this.onLoadingError.bind(this, res[i])
527
+ );
528
+ }
529
+ // set the onload callback if defined
530
+ if (typeof(onload) !== "undefined") {
531
+ this.onload = onload;
532
+ }
533
+
534
+ if (switchToLoadState === true) {
535
+ // swith to the loading screen
536
+ state.change(state.LOADING);
537
+ }
538
+
539
+ // check load status
540
+ checkLoadStatus(onload);
541
+ },
542
+
543
+ /**
544
+ * Load a single resource (to be used if you need to load additional resource during the game)
545
+ * @name load
546
+ * @memberof loader
547
+ * @public
548
+ * @param {object} res
549
+ * @param {string} res.name - internal name of the resource
550
+ * @param {string} res.type - "audio", binary", "image", "json", "tmx", "tsx"
551
+ * @param {string} res.src - path and/or file name of the resource (for audio assets only the path is required)
552
+ * @param {boolean} [res.stream] - Set to true to force HTML5 Audio, which allows not to wait for large file to be downloaded before playing.
553
+ * @param {Function} [onload] - function to be called when the resource is loaded
554
+ * @param {Function} [onerror] - function to be called in case of error
555
+ * @returns {number} the amount of corresponding resource to be preloaded
556
+ * @example
557
+ * // load an image asset
558
+ * me.loader.load({name: "avatar", type:"image", src: "data/avatar.png"}, this.onload.bind(this), this.onerror.bind(this));
559
+ * // load a base64 image asset
560
+ * me.loader.load({name: "avatar", type:"image", src: "data:image/png;base64,iVBORw0KAAAQAAAAEACA..."};
561
+ * // start loading music
562
+ * me.loader.load({
563
+ * name : "bgmusic",
564
+ * type : "audio",
565
+ * src : "data/audio/"
566
+ * }, function () {
567
+ * me.audio.play("bgmusic");
568
+ * });
569
+ */
570
+ load(res, onload, onerror) {
571
+ // transform the url if necessary
572
+ if (typeof (baseURL[res.type]) !== "undefined") {
573
+ res.src = baseURL[res.type] + res.src;
574
+ }
575
+ // check ressource type
576
+ switch (res.type) {
577
+ case "binary":
578
+ // reuse the preloadImage fn
579
+ preloadBinary.call(this, res, onload, onerror);
580
+ return 1;
581
+
582
+ case "image":
583
+ // reuse the preloadImage fn
584
+ preloadImage.call(this, res, onload, onerror);
585
+ return 1;
586
+
587
+ case "json":
588
+ preloadJSON.call(this, res, onload, onerror);
589
+ return 1;
590
+
591
+ case "js":
592
+ preloadJavascript.call(this, res, onload, onerror);
593
+ return 1;
594
+
595
+ case "tmx":
596
+ case "tsx":
597
+ preloadTMX.call(this, res, onload, onerror);
598
+ return 1;
599
+
600
+ case "audio":
601
+ load(res, !!res.stream, onload, onerror);
602
+ return 1;
603
+
604
+ case "fontface":
605
+ preloadFontFace.call(this, res, onload, onerror);
606
+ return 1;
607
+
608
+ default:
609
+ throw new Error("load : unknown or invalid resource type : " + res.type);
610
+ }
611
+ },
612
+
613
+ /**
614
+ * unload specified resource to free memory
615
+ * @name unload
616
+ * @memberof loader
617
+ * @public
618
+ * @param {object} res
619
+ * @param {string} res.name - internal name of the resource
620
+ * @param {string} res.type - "audio", binary", "image", "json", "tmx", "tsx"
621
+ * @returns {boolean} true if unloaded
622
+ * @example me.loader.unload({name: "avatar", type:"image"});
623
+ */
624
+ unload(res) {
625
+ switch (res.type) {
626
+ case "binary":
627
+ if (!(res.name in binList)) {
628
+ return false;
629
+ }
630
+
631
+ delete binList[res.name];
632
+ return true;
633
+
634
+ case "image":
635
+ if (!(res.name in imgList)) {
636
+ return false;
637
+ }
638
+ delete imgList[res.name];
639
+ return true;
640
+
641
+ case "json":
642
+ if (!(res.name in jsonList)) {
643
+ return false;
644
+ }
645
+
646
+ delete jsonList[res.name];
647
+ return true;
648
+
649
+ case "js":
650
+ // ??
651
+ return true;
652
+
653
+ case "fontface":
654
+ // ??
655
+ return true;
656
+
657
+ case "tmx":
658
+ case "tsx":
659
+ if (!(res.name in tmxList)) {
660
+ return false;
661
+ }
662
+
663
+ delete tmxList[res.name];
664
+ return true;
665
+
666
+ case "audio":
667
+ return unload(res.name);
668
+
669
+ default:
670
+ throw new Error("unload : unknown or invalid resource type : " + res.type);
671
+ }
672
+ },
673
+
674
+ /**
675
+ * unload all resources to free memory
676
+ * @name unloadAll
677
+ * @memberof loader
678
+ * @public
679
+ * @example me.loader.unloadAll();
680
+ */
681
+ unloadAll() {
682
+ var name;
683
+
684
+ // unload all binary resources
685
+ for (name in binList) {
686
+ if (binList.hasOwnProperty(name)) {
687
+ this.unload({
688
+ "name" : name,
689
+ "type" : "binary"
690
+ });
691
+ }
692
+ }
693
+
694
+ // unload all image resources
695
+ for (name in imgList) {
696
+ if (imgList.hasOwnProperty(name)) {
697
+ this.unload({
698
+ "name" : name,
699
+ "type" : "image"
700
+ });
701
+ }
702
+ }
703
+
704
+ // unload all tmx resources
705
+ for (name in tmxList) {
706
+ if (tmxList.hasOwnProperty(name)) {
707
+ this.unload({
708
+ "name" : name,
709
+ "type" : "tmx"
710
+ });
711
+ }
712
+ }
713
+
714
+ // unload all in json resources
715
+ for (name in jsonList) {
716
+ if (jsonList.hasOwnProperty(name)) {
717
+ this.unload({
718
+ "name" : name,
719
+ "type" : "json"
720
+ });
721
+ }
722
+ }
723
+
724
+ // unload all audio resources
725
+ unloadAll();
726
+ },
727
+
728
+ /**
729
+ * return the specified TMX/TSX object
730
+ * @name getTMX
731
+ * @memberof loader
732
+ * @public
733
+ * @param {string} elt - name of the tmx/tsx element ("map1");
734
+ * @returns {object} requested element or null if not found
735
+ */
736
+ getTMX(elt) {
737
+ // force as string
738
+ elt = "" + elt;
739
+ if (elt in tmxList) {
740
+ return tmxList[elt];
741
+ }
742
+ return null;
743
+ },
744
+
745
+ /**
746
+ * return the specified Binary object
747
+ * @name getBinary
748
+ * @memberof loader
749
+ * @public
750
+ * @param {string} elt - name of the binary object ("ymTrack");
751
+ * @returns {object} requested element or null if not found
752
+ */
753
+ getBinary(elt) {
754
+ // force as string
755
+ elt = "" + elt;
756
+ if (elt in binList) {
757
+ return binList[elt];
758
+ }
759
+ return null;
760
+ },
761
+
762
+ /**
763
+ * return the specified Image Object
764
+ * @name getImage
765
+ * @memberof loader
766
+ * @public
767
+ * @param {string} image - name of the Image element ("tileset-platformer");
768
+ * @returns {HTMLImageElement} requested element or null if not found
769
+ */
770
+ getImage(image) {
771
+ // force as string and extract the base name
772
+ image = getBasename("" + image);
773
+ if (image in imgList) {
774
+ // return the corresponding Image object
775
+ return imgList[image];
776
+ }
777
+ return null;
778
+ },
779
+
780
+ /**
781
+ * return the specified JSON Object
782
+ * @name getJSON
783
+ * @memberof loader
784
+ * @public
785
+ * @param {string} elt - name of the json file to load
786
+ * @returns {object}
787
+ */
788
+ getJSON(elt) {
789
+ // force as string
790
+ elt = "" + elt;
791
+ if (elt in jsonList) {
792
+ return jsonList[elt];
793
+ }
794
+ return null;
795
+ }
796
+
797
+ };
798
+
799
+ var loader$1 = loader;
800
+
801
+ export { loader$1 as default };