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,209 @@
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
+ /**
9
+ * @classdesc
10
+ * Object pooling - a technique that might speed up your game if used properly.<br>
11
+ * If some of your classes will be instantiated and removed a lot at a time, it is a
12
+ * good idea to add the class to this object pool. A separate pool for that class
13
+ * will be created, which will reuse objects of the class. That way they won't be instantiated
14
+ * each time you need a new one (slowing your game), but stored into that pool and taking one
15
+ * already instantiated when you need it.<br><br>
16
+ * This technique is also used by the engine to instantiate objects defined in the map,
17
+ * which means, that on level loading the engine will try to instantiate every object
18
+ * found in the map, based on the user defined name in each Object Properties<br>
19
+ * <img src="images/object_properties.png"/><br>
20
+ * @see {@link pool} the default global instance of ObjectPool
21
+ */
22
+ class ObjectPool {
23
+
24
+ constructor() {
25
+ this.objectClass = {};
26
+ this.instance_counter = 0;
27
+ }
28
+
29
+ /**
30
+ * register an object to the pool. <br>
31
+ * Pooling must be set to true if more than one such objects will be created. <br>
32
+ * (Note: for an object to be poolable, it must implements a `onResetEvent` method)
33
+ * @param {string} className - as defined in the Name field of the Object Properties (in Tiled)
34
+ * @param {object} classObj - corresponding Class to be instantiated
35
+ * @param {boolean} [recycling=false] - enables object recycling for the specified class
36
+ * @example
37
+ * // implement CherryEntity
38
+ * class CherryEntity extends Spritesheet {
39
+ * onResetEvent() {
40
+ * // reset object mutable properties
41
+ * this.lifeBar = 100;
42
+ * }
43
+ * };
44
+ * // add our users defined entities in the object pool and enable object recycling
45
+ * me.pool.register("cherryentity", CherryEntity, true);
46
+ */
47
+ register(className, classObj, recycling = false) {
48
+ if (typeof (classObj) !== "undefined") {
49
+ this.objectClass[className] = {
50
+ "class" : classObj,
51
+ "pool" : (recycling ? [] : undefined)
52
+ };
53
+ } else {
54
+ throw new Error("Cannot register object '" + className + "', invalid class");
55
+ }
56
+ }
57
+
58
+ /**
59
+ * Pull a new instance of the requested object (if added into the object pool)
60
+ * @param {string} name - as used in {@link pool.register}
61
+ * @param {object} [...arguments] - arguments to be passed when instantiating/reinitializing the object
62
+ * @returns {object} the instance of the requested object
63
+ * @example
64
+ * me.pool.register("bullet", BulletEntity, true);
65
+ * me.pool.register("enemy", EnemyEntity, true);
66
+ * // ...
67
+ * // when we need to manually create a new bullet:
68
+ * var bullet = me.pool.pull("bullet", x, y, direction);
69
+ * // ...
70
+ * // params aren't a fixed number
71
+ * // when we need new enemy we can add more params, that the object construct requires:
72
+ * var enemy = me.pool.pull("enemy", x, y, direction, speed, power, life);
73
+ * // ...
74
+ * // when we want to destroy existing object, the remove
75
+ * // function will ensure the object can then be reallocated later
76
+ * me.game.world.removeChild(enemy);
77
+ * me.game.world.removeChild(bullet);
78
+ */
79
+ pull(name) {
80
+ var args = new Array(arguments.length);
81
+ for (var i = 0; i < arguments.length; i++) {
82
+ args[i] = arguments[i];
83
+ }
84
+ var className = this.objectClass[name];
85
+ if (className) {
86
+ var proto = className["class"],
87
+ poolArray = className.pool,
88
+ obj;
89
+
90
+ if (poolArray && ((obj = poolArray.pop()))) {
91
+ // pull an existing instance from the pool
92
+ args.shift();
93
+ // call the object onResetEvent function if defined
94
+ if (typeof(obj.onResetEvent) === "function") {
95
+ obj.onResetEvent.apply(obj, args);
96
+ }
97
+ this.instance_counter--;
98
+ }
99
+ else {
100
+ // create a new instance
101
+ args[0] = proto;
102
+ obj = new (proto.bind.apply(proto, args))();
103
+ if (poolArray) {
104
+ obj.className = name;
105
+ }
106
+ }
107
+ return obj;
108
+ }
109
+ throw new Error("Cannot instantiate object of type '" + name + "'");
110
+ }
111
+
112
+ /**
113
+ * purge the object pool from any inactive object <br>
114
+ * Object pooling must be enabled for this function to work<br>
115
+ * note: this will trigger the garbage collector
116
+ */
117
+ purge() {
118
+ for (var className in this.objectClass) {
119
+ if (this.objectClass[className]) {
120
+ this.objectClass[className].pool = [];
121
+ }
122
+ }
123
+ this.instance_counter = 0;
124
+ }
125
+
126
+ /**
127
+ * Push back an object instance into the object pool <br>
128
+ * Object pooling for the object class must be enabled,
129
+ * and object must have been instantiated using {@link pool#pull},
130
+ * otherwise this function won't work
131
+ * @throws will throw an error if the object cannot be recycled
132
+ * @param {object} obj - instance to be recycled
133
+ * @param {boolean} [throwOnError=true] - throw an exception if the object cannot be recycled
134
+ * @returns {boolean} true if the object was successfully recycled in the object pool
135
+ */
136
+ push(obj, throwOnError = true) {
137
+ if (!this.poolable(obj)) {
138
+ if (throwOnError === true ) {
139
+ throw new Error("me.pool: object " + obj + " cannot be recycled");
140
+ } else {
141
+ return false;
142
+ }
143
+ }
144
+
145
+ // store back the object instance for later recycling
146
+ this.objectClass[obj.className].pool.push(obj);
147
+ this.instance_counter++;
148
+
149
+ return true;
150
+ }
151
+
152
+ /**
153
+ * Check if an object with the provided name is registered
154
+ * @param {string} name - of the registered object class
155
+ * @returns {boolean} true if the classname is registered
156
+ */
157
+ exists(name) {
158
+ return name in this.objectClass;
159
+ }
160
+
161
+ /**
162
+ * Check if an object is poolable
163
+ * (was properly registered with the recycling feature enable)
164
+ * @see register
165
+ * @param {object} obj - object to be checked
166
+ * @returns {boolean} true if the object is poolable
167
+ * @example
168
+ * if (!me.pool.poolable(myCherryEntity)) {
169
+ * // object was not properly registered
170
+ * }
171
+ */
172
+ poolable(obj) {
173
+ var className = obj.className;
174
+ return (typeof className !== "undefined") &&
175
+ (typeof obj.onResetEvent === "function") &&
176
+ (className in this.objectClass) &&
177
+ (typeof this.objectClass[className].pool !== "undefined");
178
+
179
+ }
180
+
181
+ /**
182
+ * returns the amount of object instance currently in the pool
183
+ * @returns {number} amount of object instance
184
+ */
185
+ getInstanceCount() {
186
+ return this.instance_counter;
187
+ }
188
+ }
189
+
190
+ var pool = new ObjectPool();
191
+
192
+ /**
193
+ * a default global ObjectPool instance
194
+ * @namespace pool
195
+ * @see ObjectPool
196
+ * @example
197
+ * // register our bullet object into the object pool
198
+ * pool.register("bullet", BulletEntity, true);
199
+ * // ...
200
+ * // when we need to manually create a new bullet:
201
+ * var bullet = pool.pull("bullet", x, y, direction, velocity);
202
+ * // ...
203
+ * // when we want to destroy existing object, the remove
204
+ * // function will ensure the object can then be reallocated later
205
+ * game.world.removeChild(bullet);
206
+ */
207
+ var pool$1 = pool;
208
+
209
+ export { pool$1 as default };
@@ -0,0 +1,157 @@
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 { on, BOOT } from './event.js';
9
+
10
+ /**
11
+ * allow to access and manage the device localStorage
12
+ * @example
13
+ * // Initialize "score" and "lives" with default values
14
+ * // This loads the properties from localStorage if they exist, else it sets the given defaults
15
+ * me.save.add({ score : 0, lives : 3 });
16
+ *
17
+ * // Print all
18
+ * // On first load, this prints { score : 0, lives : 3 }
19
+ * // On further reloads, it prints { score : 31337, lives : 3, complexObject : ... }
20
+ * // Because the following changes will be saved to localStorage
21
+ * console.log(JSON.stringify(me.save));
22
+ *
23
+ * // Save score
24
+ * me.save.score = 31337;
25
+ *
26
+ * // Also supports complex objects thanks to the JSON backend
27
+ * me.save.add({ complexObject : {} })
28
+ * me.save.complexObject = { a : "b", c : [ 1, 2, 3, "d" ], e : { f : [{}] } };
29
+ *
30
+ * // WARNING: Do not set any child properties of complex objects directly!
31
+ * // Changes made that way will not save. Always set the entire object value at once.
32
+ * // If you cannot live with this limitation, there's a workaround:
33
+ * me.save.complexObject.c.push("foo"); // Modify a child property
34
+ * me.save.complexObject = me.save.complexObject; // Save the entire object!
35
+ *
36
+ * // Remove "lives" from localStorage
37
+ * me.save.remove("lives");
38
+ * @namespace save
39
+ */
40
+
41
+ // Variable to hold the object data
42
+ var data = {};
43
+
44
+ let hasLocalStorage = false;
45
+
46
+ try {
47
+ // true if localStorage is supported
48
+ hasLocalStorage = typeof globalThis !== "undefined" && typeof globalThis.localStorage !== "undefined";
49
+ } catch (e) {
50
+ // the above generates an exception when cookies are blocked
51
+ hasLocalStorage = false;
52
+ }
53
+
54
+ /**
55
+ * a function to check if the given key is a reserved word
56
+ * @ignore
57
+ */
58
+ function isReserved(key) {
59
+ return (key === "add" || key === "remove");
60
+ }
61
+
62
+
63
+ // Initialize me.save on Boot event
64
+ on(BOOT, () => {
65
+ // Load previous data if local Storage is supported
66
+ if (hasLocalStorage === true) {
67
+ var me_save_content = globalThis.localStorage.getItem("me.save");
68
+
69
+ if (typeof me_save_content === "string" && me_save_content.length > 0) {
70
+ var keys = JSON.parse(me_save_content) || [];
71
+ keys.forEach((key) => {
72
+ data[key] = JSON.parse(globalThis.localStorage.getItem("me.save." + key));
73
+ });
74
+ }
75
+ }
76
+ });
77
+
78
+ var save = {
79
+
80
+ /**
81
+ * Add new keys to localStorage and set them to the given default values if they do not exist
82
+ * @name add
83
+ * @memberof save
84
+ * @param {object} props - key and corresponding values
85
+ * @example
86
+ * // Initialize "score" and "lives" with default values
87
+ * me.save.add({ score : 0, lives : 3 });
88
+ * // get or set the value through me.save
89
+ * me.save.score = 1000;
90
+ */
91
+ add(props) {
92
+ var obj = save;
93
+
94
+ Object.keys(props).forEach((key) => {
95
+ if (isReserved(key)) {
96
+ return;
97
+ }
98
+
99
+ (function (prop) {
100
+ Object.defineProperty(obj, prop, {
101
+ configurable : true,
102
+ enumerable : true,
103
+ /**
104
+ * @ignore
105
+ */
106
+ get () {
107
+ return data[prop];
108
+ },
109
+ /**
110
+ * @ignore
111
+ */
112
+ set (value) {
113
+ data[prop] = value;
114
+ if (hasLocalStorage === true) {
115
+ globalThis.localStorage.setItem("me.save." + prop, JSON.stringify(value));
116
+ }
117
+ }
118
+ });
119
+ })(key);
120
+
121
+ // Set default value for key
122
+ if (!(key in data)) {
123
+ obj[key] = props[key];
124
+ }
125
+ });
126
+
127
+ // Save keys
128
+ if (hasLocalStorage === true) {
129
+ globalThis.localStorage.setItem("me.save", JSON.stringify(Object.keys(data)));
130
+ }
131
+ },
132
+
133
+ /**
134
+ * Remove a key from localStorage
135
+ * @name remove
136
+ * @memberof save
137
+ * @param {string} key - key to be removed
138
+ * @example
139
+ * // Remove the "score" key from localStorage
140
+ * me.save.remove("score");
141
+ */
142
+ remove (key) {
143
+ if (!isReserved(key)) {
144
+ if (typeof data[key] !== "undefined") {
145
+ delete data[key];
146
+ if (hasLocalStorage === true) {
147
+ globalThis.localStorage.removeItem("me.save." + key);
148
+ globalThis.localStorage.setItem("me.save", JSON.stringify(Object.keys(data)));
149
+ }
150
+ }
151
+ }
152
+ }
153
+ };
154
+
155
+ var save$1 = save;
156
+
157
+ export { save$1 as default };
@@ -0,0 +1,286 @@
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 utils from '../utils/utils.js';
9
+ import { on, GAME_BEFORE_UPDATE, BOOT, STATE_RESUME, STATE_RESTART, STATE_CHANGE } from './event.js';
10
+ import state from '../state/state.js';
11
+ import { clamp } from '../math/math.js';
12
+
13
+ /**
14
+ * @classdesc
15
+ * a Timer class to manage timing related function (FPS, Game Tick, Time...)
16
+ * @see {@link timer} the default global timer instance
17
+ */
18
+ class Timer {
19
+
20
+ constructor() {
21
+ /**
22
+ * Last game tick value. <br>
23
+ * Use this value to scale velocities during frame drops due to slow hardware or when setting an FPS limit.
24
+ * This feature is disabled by default (Enable interpolation to use it).
25
+ * @public
26
+ * @see interpolation
27
+ * @See maxfps
28
+ * @type {number}
29
+ * @name tick
30
+ */
31
+ this.tick = 1.0;
32
+
33
+ /**
34
+ * Last measured fps rate.<br>
35
+ * This feature is disabled by default, unless the debugPanel is enabled/visible.
36
+ * @public
37
+ * @type {number}
38
+ * @name fps
39
+ */
40
+ this.fps = 0;
41
+
42
+ /**
43
+ * Set the maximum target display frame per second
44
+ * @public
45
+ * @see tick
46
+ * @type {number}
47
+ * @default 60
48
+ */
49
+ this.maxfps = 60;
50
+
51
+ /**
52
+ * Enable/disable frame interpolation
53
+ * @see tick
54
+ * @type {boolean}
55
+ * @default false
56
+ */
57
+ this.interpolation = false;
58
+
59
+ //hold element to display fps
60
+ this.framecount = 0;
61
+ this.framedelta = 0;
62
+
63
+ /* fps count stuff */
64
+ this.last = 0;
65
+ this.now = 0;
66
+ this.delta = 0;
67
+ // for timeout/interval update
68
+ this.step =0;
69
+ this.minstep = 0;
70
+
71
+ // list of defined timer function
72
+ this.timers = [];
73
+ this.timerId = 0;
74
+
75
+ // Initialize mtimer on Boot event
76
+ on(BOOT, () => {
77
+ // reset variables to initial state
78
+ this.reset();
79
+ this.now = this.last = 0;
80
+ // register to the game before update event
81
+ on(GAME_BEFORE_UPDATE, this.update.bind(this));
82
+ });
83
+
84
+ // reset timer
85
+ on(STATE_RESUME, () => {
86
+ this.reset();
87
+ });
88
+ on(STATE_RESTART, () => {
89
+ this.reset();
90
+ });
91
+ on(STATE_CHANGE, () => {
92
+ this.reset();
93
+ });
94
+ }
95
+
96
+
97
+ /**
98
+ * reset time (e.g. usefull in case of pause)
99
+ * @ignore
100
+ */
101
+ reset() {
102
+ // set to "now"
103
+ this.last = this.now = globalThis.performance.now();
104
+ this.delta = 0;
105
+ // reset delta counting variables
106
+ this.framedelta = 0;
107
+ this.framecount = 0;
108
+ this.step = Math.ceil(1000 / this.maxfps); // ROUND IT ?
109
+ // define some step with some margin
110
+ this.minstep = (1000 / this.maxfps) * 1.25; // IS IT NECESSARY?\
111
+ }
112
+
113
+ /**
114
+ * Calls a function once after a specified delay. See me.timer.setInterval to repeativly call a function.
115
+ * @param {Function} fn - the function you want to execute after delay milliseconds.
116
+ * @param {number} delay - the number of milliseconds (thousandths of a second) that the function call should be delayed by.
117
+ * @param {boolean} [pauseable=true] - respects the pause state of the engine.
118
+ * @param {...*} args - optional parameters which are passed through to the function specified by fn once the timer expires.
119
+ * @returns {number} The numerical ID of the timer, which can be used later with me.timer.clearTimeout().
120
+ * @example
121
+ * // set a timer to call "myFunction" after 1000ms
122
+ * me.timer.setTimeout(myFunction, 1000);
123
+ * // set a timer to call "myFunction" after 1000ms (respecting the pause state) and passing param1 and param2
124
+ * me.timer.setTimeout(myFunction, 1000, true, param1, param2);
125
+ */
126
+ setTimeout(fn, delay, pauseable, ...args) {
127
+ this.timers.push({
128
+ fn : fn,
129
+ delay : delay,
130
+ elapsed : 0,
131
+ repeat : false,
132
+ timerId : ++this.timerId,
133
+ pauseable : pauseable === true || true,
134
+ args : args
135
+ });
136
+ return this.timerId;
137
+ }
138
+
139
+ /**
140
+ * Calls a function continously at the specified interval. See setTimeout to call function a single time.
141
+ * @param {Function} fn - the function to execute
142
+ * @param {number} delay - the number of milliseconds (thousandths of a second) on how often to execute the function
143
+ * @param {boolean} [pauseable=true] - respects the pause state of the engine.
144
+ * @param {...*} args - optional parameters which are passed through to the function specified by fn once the timer expires.
145
+ * @returns {number} The numerical ID of the timer, which can be used later with me.timer.clearInterval().
146
+ * @example
147
+ * // set a timer to call "myFunction" every 1000ms
148
+ * me.timer.setInterval(myFunction, 1000);
149
+ * // set a timer to call "myFunction" every 1000ms (respecting the pause state) and passing param1 and param2
150
+ * me.timer.setInterval(myFunction, 1000, true, param1, param2);
151
+ */
152
+ setInterval(fn, delay, pauseable, ...args) {
153
+ this.timers.push({
154
+ fn : fn,
155
+ delay : delay,
156
+ elapsed : 0,
157
+ repeat : true,
158
+ timerId : ++this.timerId,
159
+ pauseable : pauseable === true || true,
160
+ args : args
161
+ });
162
+ return this.timerId;
163
+ }
164
+
165
+ /**
166
+ * Clears the delay set by me.timer.setTimeout().
167
+ * @param {number} timeoutID - ID of the timeout to be cleared
168
+ */
169
+ clearTimeout(timeoutID) {
170
+ utils.function.defer(this.clearTimer.bind(this), this, timeoutID);
171
+ }
172
+
173
+ /**
174
+ * Clears the Interval set by me.timer.setInterval().
175
+ * @param {number} intervalID - ID of the interval to be cleared
176
+ */
177
+ clearInterval(intervalID) {
178
+ utils.function.defer(this.clearTimer.bind(this), this, intervalID);
179
+ }
180
+
181
+ /**
182
+ * Return the current timestamp in milliseconds <br>
183
+ * since the game has started or since linux epoch (based on browser support for High Resolution Timer)
184
+ * @returns {number}
185
+ */
186
+ getTime() {
187
+ return this.now;
188
+ }
189
+
190
+ /**
191
+ * Return elapsed time in milliseconds since the last update
192
+ * @returns {number}
193
+ */
194
+ getDelta() {
195
+ return this.delta;
196
+ }
197
+
198
+ /**
199
+ * compute the actual frame time and fps rate
200
+ * @ignore
201
+ */
202
+ countFPS() {
203
+ this.framecount++;
204
+ this.framedelta += this.delta;
205
+ if (this.framecount % 10 === 0) {
206
+ this.fps = clamp(Math.round((1000 * this.framecount) / this.framedelta), 0, this.maxfps);
207
+ this.framedelta = 0;
208
+ this.framecount = 0;
209
+ }
210
+ }
211
+
212
+ /**
213
+ * update
214
+ * @ignore
215
+ */
216
+ update(time) {
217
+ this.last = this.now;
218
+ this.now = time;
219
+ this.delta = (this.now - this.last);
220
+
221
+ // fix for negative timestamp returned by wechat or chrome on startup
222
+ if (this.delta < 0) {
223
+ this.delta = 0;
224
+ }
225
+
226
+ // get the game tick
227
+ this.tick = (this.delta > this.minstep && this.interpolation) ? this.delta / this.step : 1;
228
+
229
+ this.updateTimers();
230
+ }
231
+
232
+ /**
233
+ * clear Timers
234
+ * @ignore
235
+ */
236
+ clearTimer(timerId) {
237
+ for (var i = 0, len = this.timers.length; i < len; i++) {
238
+ if (this.timers[i].timerId === timerId) {
239
+ this.timers.splice(i, 1);
240
+ break;
241
+ }
242
+ }
243
+ }
244
+
245
+
246
+ /**
247
+ * update timers
248
+ * @ignore
249
+ */
250
+ updateTimers() {
251
+ for (var i = 0, len = this.timers.length; i < len; i++) {
252
+ var _timer = this.timers[i];
253
+ if (!(_timer.pauseable && state.isPaused())) {
254
+ _timer.elapsed += this.delta;
255
+ }
256
+ if (_timer.elapsed >= _timer.delay) {
257
+ _timer.fn.apply(null, _timer.args);
258
+ if (_timer.repeat === true) {
259
+ _timer.elapsed -= _timer.delay;
260
+ } else {
261
+ this.clearTimeout(_timer.timerId);
262
+ }
263
+ }
264
+ }
265
+ }
266
+ }
267
+
268
+ const timer = new Timer();
269
+
270
+ /**
271
+ * the default global Timer instance
272
+ * @namespace timer
273
+ * @see Timer
274
+ * @example
275
+ * // set a timer to call "myFunction" after 1000ms
276
+ * timer.setTimeout(myFunction, 1000);
277
+ * // set a timer to call "myFunction" after 1000ms (respecting the pause state) and passing param1 and param2
278
+ * timer.setTimeout(myFunction, 1000, true, param1, param2);
279
+ * // set a timer to call "myFunction" every 1000ms
280
+ * timer.setInterval(myFunction, 1000);
281
+ * // set a timer to call "myFunction" every 1000ms (respecting the pause state) and passing param1 and param2
282
+ * timer.setInterval(myFunction, 1000, true, param1, param2);
283
+ */
284
+ var timer$1 = timer;
285
+
286
+ export { timer$1 as default };