melonjs 10.0.2 → 10.2.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "melonjs",
3
- "version": "10.0.2",
3
+ "version": "10.2.1",
4
4
  "description": "melonJS Game Engine",
5
5
  "homepage": "http://www.melonjs.org/",
6
6
  "keywords": [
@@ -64,22 +64,22 @@
64
64
  "@rollup/plugin-replace": "^3.0.0",
65
65
  "cheerio": "^1.0.0-rc.10",
66
66
  "del-cli": "^4.0.1",
67
- "eslint": "^8.1.0",
67
+ "eslint": "^8.3.0",
68
68
  "ghpages": "0.0.10",
69
69
  "jasmine-core": "^3.10.1",
70
70
  "jsdoc": "^3.6.7",
71
- "karma": "^6.3.6",
71
+ "karma": "^6.3.9",
72
72
  "karma-chrome-launcher": "^3.1.0",
73
73
  "karma-coverage": "^2.0.3",
74
74
  "karma-html-detailed-reporter": "^2.1.0",
75
75
  "karma-jasmine": "^4.0.1",
76
76
  "karma-nyan-reporter": "0.2.5",
77
77
  "qs": "^6.10.1",
78
- "rollup": "^2.58.3",
78
+ "rollup": "^2.60.1",
79
79
  "rollup-plugin-bundle-size": "^1.0.3",
80
80
  "rollup-plugin-string": "^3.0.0",
81
- "terser": "^5.8.0",
82
- "typescript": "^4.4.4"
81
+ "terser": "^5.10.0",
82
+ "typescript": "^4.5.2"
83
83
  },
84
84
  "scripts": {
85
85
  "build": "npm run lint && rollup -c --silent",
@@ -221,10 +221,10 @@ class Camera2d extends Renderable {
221
221
  * @param {Number} [x=0]
222
222
  * @param {Number} [y=0]
223
223
  */
224
- reset(x, y) {
224
+ reset(x = 0, y = 0) {
225
225
  // reset the initial camera position to 0,0
226
- this.pos.x = x || 0;
227
- this.pos.y = y || 0;
226
+ this.pos.x = x;
227
+ this.pos.y = y;
228
228
 
229
229
  // reset the target
230
230
  this.unfollow();
package/src/game.js CHANGED
@@ -130,7 +130,7 @@ export function reset () {
130
130
  * Update the renderer framerate using the system config variables.
131
131
  * @function me.game.updateFrameRate
132
132
  * @see me.timer.maxfps
133
- * @see me.game.world.fps
133
+ * @see me.World.fps
134
134
  */
135
135
  export function updateFrameRate() {
136
136
  // reset the frame counter
package/src/index.js CHANGED
@@ -48,6 +48,7 @@ import BitmapTextData from "./text/bitmaptextdata.js";
48
48
  import ColorLayer from "./renderable/colorlayer.js";
49
49
  import ImageLayer from "./renderable/imagelayer.js";
50
50
  import Sprite from "./renderable/sprite.js";
51
+ import NineSliceSprite from "./renderable/nineslicesprite.js";
51
52
  import GUI_Object from "./renderable/GUI.js";
52
53
  import Collectable from "./renderable/collectable.js";
53
54
  import Trigger from "./renderable/trigger.js";
@@ -145,6 +146,7 @@ export {
145
146
  ColorLayer,
146
147
  ImageLayer,
147
148
  Sprite,
149
+ NineSliceSprite,
148
150
  GUI_Object,
149
151
  Collectable,
150
152
  Trigger,
@@ -220,6 +222,7 @@ export function boot() {
220
222
  pool.register("me.Color", Color, true);
221
223
  pool.register("me.Particle", Particle, true);
222
224
  pool.register("me.Sprite", Sprite);
225
+ pool.register("me.NineSliceSprite", NineSliceSprite);
223
226
  pool.register("me.Renderable", Renderable);
224
227
  pool.register("me.Text", Text, true);
225
228
  pool.register("me.BitmapText", BitmapText);
@@ -246,6 +249,7 @@ export function boot() {
246
249
  pool.register("Color", Color, true);
247
250
  pool.register("Particle", Particle, true);
248
251
  pool.register("Sprite", Sprite);
252
+ pool.register("NineSliceSprite", NineSliceSprite);
249
253
  pool.register("Renderable", Renderable);
250
254
  pool.register("Text", Text, true);
251
255
  pool.register("BitmapText", BitmapText);
@@ -1,25 +1,25 @@
1
1
  import Vector2d from "./../math/vector2.js";
2
2
  import device from "./../system/device.js";
3
- import Rect from "./../shapes/rectangle.js";
3
+ import Bounds from "./../physics/bounds.js";
4
4
  import { viewport } from "./../game.js";
5
5
  import { globalToLocal } from "./input.js";
6
6
 
7
7
 
8
8
  /**
9
- * cache value for the offset of the canvas position within the page
9
+ * a temporary vector object
10
10
  * @ignore
11
11
  */
12
- var viewportOffset = new Vector2d();
12
+ var tmpVec = new Vector2d();
13
13
 
14
14
  /**
15
15
  * @classdesc
16
16
  * a pointer object, representing a single finger on a touch enabled device.
17
17
  * @class
18
- * @extends me.Rect
18
+ * @extends me.Bounds
19
19
  * @memberOf me
20
20
  * @constructor
21
21
  */
22
- class Pointer extends Rect {
22
+ class Pointer extends Bounds {
23
23
 
24
24
  /**
25
25
  * @ignore
@@ -27,7 +27,10 @@ class Pointer extends Rect {
27
27
  constructor(x = 0, y = 0, w = 1, h = 1) {
28
28
 
29
29
  // parent constructor
30
- super(x, y, w, h);
30
+ super();
31
+
32
+ // initial coordinates/size
33
+ this.setMinMax(x, y, x + w, y + h);
31
34
 
32
35
  /**
33
36
  * constant for left button
@@ -280,9 +283,6 @@ class Pointer extends Rect {
280
283
  * @param {Number} [pointedId=1] the Pointer, Touch or Mouse event Id (1)
281
284
  */
282
285
  setEvent(event, pageX = 0, pageY = 0, clientX = 0, clientY = 0, pointerId = 1) {
283
- var width = 1;
284
- var height = 1;
285
-
286
286
  // the original event object
287
287
  this.event = event;
288
288
 
@@ -292,7 +292,9 @@ class Pointer extends Rect {
292
292
  this.clientY = clientY;
293
293
 
294
294
  // translate to local coordinates
295
- globalToLocal(this.pageX, this.pageY, this.pos);
295
+ globalToLocal(this.pageX, this.pageY, tmpVec);
296
+ this.gameScreenX = this.x = tmpVec.x;
297
+ this.gameScreenY = this.y = tmpVec.y;
296
298
 
297
299
  // true if not originally a pointer event
298
300
  this.isNormalized = !device.PointerEvent || (device.PointerEvent && !(event instanceof window.PointerEvent));
@@ -318,30 +320,29 @@ class Pointer extends Rect {
318
320
 
319
321
  this.type = event.type;
320
322
 
321
- this.gameScreenX = this.pos.x;
322
- this.gameScreenY = this.pos.y;
323
+
323
324
 
324
325
  // get the current screen to game world offset
325
326
  if (typeof viewport !== "undefined") {
326
- viewport.localToWorld(this.gameScreenX, this.gameScreenY, viewportOffset);
327
+ viewport.localToWorld(this.gameScreenX, this.gameScreenY, tmpVec);
327
328
  }
328
329
 
329
330
  /* Initialize the two coordinate space properties. */
330
- this.gameWorldX = viewportOffset.x;
331
- this.gameWorldY = viewportOffset.y;
331
+ this.gameWorldX = tmpVec.x;
332
+ this.gameWorldY = tmpVec.y;
332
333
 
333
334
  // get the pointer size
334
335
  if (this.isNormalized === false) {
335
336
  // native PointerEvent
336
- width = event.width || 1;
337
- height = event.height || 1;
337
+ this.width = event.width || 1;
338
+ this.height = event.height || 1;
338
339
  } else if (typeof(event.radiusX) === "number") {
339
340
  // TouchEvent
340
- width = (event.radiusX * 2) || 1;
341
- height = (event.radiusY * 2) || 1;
341
+ this.width = (event.radiusX * 2) || 1;
342
+ this.height = (event.radiusY * 2) || 1;
343
+ } else {
344
+ this.width = this.height = 1;
342
345
  }
343
- // resize the pointer object accordingly
344
- this.resize(width, height);
345
346
  }
346
347
  };
347
348
 
@@ -121,8 +121,6 @@ function enablePointerEvent() {
121
121
  // the current pointer area
122
122
  currentPointer = new Rect(0, 0, 1, 1);
123
123
 
124
- pointer = new Pointer(0, 0, 1, 1);
125
-
126
124
  // instantiate a pool of pointer catched
127
125
  for (var v = 0; v < device.maxTouchPoints; v++) {
128
126
  T_POINTERS.push(new Pointer());
@@ -412,15 +410,15 @@ function dispatchEvent(normalizedEvents) {
412
410
  * @ignore
413
411
  */
414
412
  function normalizeEvent(originalEvent) {
415
- var pointer;
413
+ var _pointer;
416
414
 
417
415
  // PointerEvent or standard Mouse event
418
416
  if (device.TouchEvent && originalEvent.changedTouches) {
419
417
  // iOS/Android Touch event
420
418
  for (var i = 0, l = originalEvent.changedTouches.length; i < l; i++) {
421
419
  var touchEvent = originalEvent.changedTouches[i];
422
- pointer = T_POINTERS.pop();
423
- pointer.setEvent(
420
+ _pointer = T_POINTERS.pop();
421
+ _pointer.setEvent(
424
422
  originalEvent,
425
423
  touchEvent.pageX,
426
424
  touchEvent.pageY,
@@ -428,12 +426,12 @@ function normalizeEvent(originalEvent) {
428
426
  touchEvent.clientY,
429
427
  touchEvent.identifier
430
428
  );
431
- normalizedEvents.push(pointer);
429
+ normalizedEvents.push(_pointer);
432
430
  }
433
431
  } else {
434
432
  // Mouse or PointerEvent
435
- pointer = T_POINTERS.pop();
436
- pointer.setEvent(
433
+ _pointer = T_POINTERS.pop();
434
+ _pointer.setEvent(
437
435
  originalEvent,
438
436
  originalEvent.pageX,
439
437
  originalEvent.pageY,
@@ -441,7 +439,7 @@ function normalizeEvent(originalEvent) {
441
439
  originalEvent.clientY,
442
440
  originalEvent.pointerId
443
441
  );
444
- normalizedEvents.push(pointer);
442
+ normalizedEvents.push(_pointer);
445
443
  }
446
444
 
447
445
  // if event.isPrimary is defined and false, return
@@ -513,7 +511,7 @@ function onPointerEvent(e) {
513
511
  * @name pointer
514
512
  * @memberOf me.input
515
513
  */
516
- export var pointer;
514
+ export var pointer = new Pointer(0, 0, 1, 1);
517
515
 
518
516
  /**
519
517
  * time interval for event throttling in milliseconds<br>
@@ -535,8 +533,8 @@ export var throttlingInterval;
535
533
  * @function
536
534
  * @param {Number} x the global x coordinate to be translated.
537
535
  * @param {Number} y the global y coordinate to be translated.
538
- * @param {Number} [v] an optional vector object where to set the
539
- * @return {me.Vector2d} A vector object with the corresponding translated coordinates.
536
+ * @param {me.Vector2d} [v] an optional vector object where to set the translated coordinates
537
+ * @return {me.Vector2d} A vector object with the corresponding translated coordinates
540
538
  * @example
541
539
  * onMouseEvent : function (pointer) {
542
540
  * // convert the given into local (viewport) relative coordinates
@@ -297,7 +297,7 @@ class TMXLayer extends Renderable {
297
297
  * // get the TMX Map Layer called "Front layer"
298
298
  * var layer = me.game.world.getChildByName("Front Layer")[0];
299
299
  * // get the tile object corresponding to the latest pointer position
300
- * var tile = layer.getTile(me.input.pointer.pos.x, me.input.pointer.pos.y);
300
+ * var tile = layer.getTile(me.input.pointer.x, me.input.pointer.y);
301
301
  */
302
302
  getTile(x, y) {
303
303
  var tile = null;
@@ -478,7 +478,7 @@ var loader = {
478
478
  * // set all resources to be loaded
479
479
  * me.loader.preload(game.resources, this.loaded.bind(this));
480
480
  */
481
- preload(res, onload, switchToLoadState) {
481
+ preload(res, onload, switchToLoadState = true) {
482
482
  // parse the resources
483
483
  for (var i = 0; i < res.length; i++) {
484
484
  resourceCount += this.load(
@@ -492,7 +492,7 @@ var loader = {
492
492
  this.onload = onload;
493
493
  }
494
494
 
495
- if (switchToLoadState !== false) {
495
+ if (switchToLoadState === true) {
496
496
  // swith to the loading screen
497
497
  state.change(state.LOADING);
498
498
  }
@@ -114,70 +114,6 @@ class IconLogo extends Renderable {
114
114
  }
115
115
  };
116
116
 
117
- // the melonJS Text Logo
118
- class TextLogo extends Renderable {
119
- /**
120
- * @ignore
121
- */
122
- constructor(w, h) {
123
- super(0, 0, w, h);
124
-
125
- this.textWidth = 0;
126
-
127
- // offscreen cache canvas
128
- this.fontCanvas = createCanvas(256, 64, true);
129
- this.drawFont(renderer.getContext2d(this.fontCanvas));
130
-
131
- this.anchorPoint.set(0, 0.5);
132
- }
133
-
134
- drawFont(context) {
135
- var logo1 = pool.pull("Text", 0, 0, {
136
- font: "century gothic",
137
- size: 32,
138
- fillStyle: "white",
139
- textAlign: "middle",
140
- textBaseline : "top",
141
- text: "melon"
142
- });
143
- var logo2 = pool.pull("Text", 0, 0, {
144
- font: "century gothic",
145
- size: 32,
146
- fillStyle: "#55aa00",
147
- textAlign: "middle",
148
- textBaseline : "top",
149
- bold: true,
150
- text: "JS"
151
- });
152
-
153
-
154
- // compute both logo respective size
155
- var logo1_width = logo1.measureText(context).width;
156
- var logo2_width = logo2.measureText(context).width;
157
-
158
- this.textWidth = logo1_width + logo2_width;
159
-
160
- // calculate the final rendering position
161
- this.pos.x = Math.round(this.width - this.textWidth / 2);
162
- this.pos.y = Math.round(this.height + 16);
163
-
164
- // use the private _drawFont method to directly draw on the canvas context
165
- logo1._drawFont(context, ["melon"], 0, 0);
166
- logo2._drawFont(context, ["JS"], logo1_width, 0);
167
-
168
- // put them back into the object pool
169
- pool.push(logo1);
170
- pool.push(logo2);
171
- }
172
-
173
- /**
174
- * @ignore
175
- */
176
- draw(renderer) {
177
- renderer.drawImage(this.fontCanvas, Math.round((renderer.getWidth() - this.textWidth) / 2), this.pos.y);
178
- }
179
-
180
- };
181
117
 
182
118
  /**
183
119
  * a default loading screen
@@ -185,12 +121,12 @@ class TextLogo extends Renderable {
185
121
  * @ignore
186
122
  * @constructor
187
123
  */
188
- var defaultLoadingScreen = new Stage({
124
+ class DefaultLoadingScreen extends Stage {
189
125
  /**
190
126
  * call when the loader is resetted
191
127
  * @ignore
192
128
  */
193
- onResetEvent : function () {
129
+ onResetEvent() {
194
130
  var barHeight = 8;
195
131
 
196
132
  // clear the background
@@ -211,12 +147,44 @@ var defaultLoadingScreen = new Stage({
211
147
 
212
148
  ), 2);
213
149
 
150
+ var logo1 = pool.pull("Text",
151
+ renderer.getWidth() / 2,
152
+ (renderer.getHeight() / 2) + 16, {
153
+ font: "century gothic",
154
+ size: 32,
155
+ fillStyle: "white",
156
+ textAlign: "left",
157
+ textBaseline : "top",
158
+ text: "melon",
159
+ offScreenCanvas: true
160
+ }
161
+ );
162
+ logo1.anchorPoint.set(0, 0);
163
+
164
+ var logo2 = pool.pull("Text",
165
+ renderer.getWidth() / 2,
166
+ (renderer.getHeight() / 2) + 16, {
167
+ font: "century gothic",
168
+ size: 32,
169
+ fillStyle: "#55aa00",
170
+ textAlign: "left",
171
+ textBaseline : "top",
172
+ bold: true,
173
+ text: "JS",
174
+ offScreenCanvas: true
175
+ }
176
+ );
177
+ logo2.anchorPoint.set(0, 0);
178
+
179
+ // adjust position of both text
180
+ var text_width = logo1.getBounds().width + logo2.getBounds().width;
181
+ logo1.pos.x = renderer.getWidth() / 2 - text_width / 2;
182
+ logo2.pos.x = logo1.pos.x + logo1.getBounds().width;
183
+
214
184
  // melonJS text
215
- world.addChild(new TextLogo(
216
- renderer.getWidth(),
217
- renderer.getHeight()
218
- ), 2);
185
+ world.addChild(logo1, 2);
186
+ world.addChild(logo2, 2);
219
187
  }
220
- });
188
+ };
221
189
 
222
- export default defaultLoadingScreen;
190
+ export default DefaultLoadingScreen;
package/src/math/color.js CHANGED
@@ -202,7 +202,7 @@ class Color {
202
202
  }
203
203
 
204
204
  /**
205
- * Color Red Component
205
+ * Color Red Component [0 .. 255]
206
206
  * @type Number
207
207
  * @name r
208
208
  * @readonly
@@ -224,7 +224,7 @@ class Color {
224
224
 
225
225
 
226
226
  /**
227
- * Color Green Component
227
+ * Color Green Component [0 .. 255]
228
228
  * @type Number
229
229
  * @name g
230
230
  * @readonly
@@ -246,7 +246,7 @@ class Color {
246
246
 
247
247
 
248
248
  /**
249
- * Color Blue Component
249
+ * Color Blue Component [0 .. 255]
250
250
  * @type Number
251
251
  * @name b
252
252
  * @readonly
@@ -266,7 +266,7 @@ class Color {
266
266
  }
267
267
 
268
268
  /**
269
- * Color Alpha Component
269
+ * Color Alpha Component [0.0 .. 1.0]
270
270
  * @type Number
271
271
  * @name alpha
272
272
  * @readonly
@@ -550,6 +550,23 @@ class Color {
550
550
  );
551
551
  }
552
552
 
553
+ /**
554
+ * Pack this color into a Uint32 ARGB representation
555
+ * @name toUint32
556
+ * @memberOf me.Color
557
+ * @function
558
+ * @param {Number} [alpha=1.0] alpha value [0.0 .. 1.0]
559
+ * @return {Uint32}
560
+ */
561
+ toUint32(alpha = this.alpha) {
562
+ var ur = this.r & 0xff;
563
+ var ug = this.g & 0xff;
564
+ var ub = this.b & 0xff;
565
+ var ua = (alpha * 255) & 0xff;
566
+
567
+ return (ua << 24) + (ur << 16) + (ug << 8) + ub;
568
+ }
569
+
553
570
  /**
554
571
  * return an array representation of this object
555
572
  * @name toArray
@@ -561,6 +578,7 @@ class Color {
561
578
  return this.glArray;
562
579
  }
563
580
 
581
+
564
582
  /**
565
583
  * Get the color in "#RRGGBB" format
566
584
  * @name toHex
@@ -11,13 +11,7 @@ import { world } from "./../game.js";
11
11
 
12
12
 
13
13
  /**
14
- * a Generic Body Object with some physic properties and behavior functionality<br>
15
- The body object is attached as a member of a Renderable. The Body object can handle movements of the parent with
16
- the body.update call. It is important to know that when body.update is called there are several things that happen related to
17
- the movement and positioning of the parent renderable object. 1) The force/gravity/friction parameters are used
18
- to calculate a new velocity and 2) the parent position is updated by adding this to the parent.pos (position me.Vector2d)
19
- value. Thus Affecting the movement of the parent. Look at the source code for /src/physics/body.js:update (me.Body.update) for
20
- a better understanding.
14
+ * a Generic Physic Body Object with some physic properties and behavior functionality, to as a member of a Renderable.
21
15
  * @class Body
22
16
  * @memberOf me
23
17
  * @constructor
@@ -347,8 +347,8 @@ class Bounds {
347
347
  * @return {boolean} True if the bounds overlap, otherwise false
348
348
  */
349
349
  overlaps(bounds) {
350
- return (this.left <= bounds.right && this.right >= bounds.left
351
- && this.bottom >= bounds.top && this.top <= bounds.bottom);
350
+ return !(this.right < bounds.left || this.left > bounds.right ||
351
+ this.bottom < bounds.top || this.top > bounds.bottom);
352
352
  }
353
353
 
354
354
  /**
@@ -48,7 +48,6 @@ function shouldCollide(a, b) {
48
48
  * @name ResponseObject
49
49
  * @memberOf me.collision
50
50
  * @public
51
- * @see me.collision.check
52
51
  */
53
52
  class ResponseObject {
54
53
  constructor() {
@@ -7,8 +7,9 @@ import { collisionCheck } from "./detector.js";
7
7
  import state from "./../state/state.js";
8
8
 
9
9
  /**
10
+ * @classdesc
10
11
  * an object representing the physic world, and responsible for managing and updating all childs and physics
11
- * @class
12
+ * @class World
12
13
  * @extends me.Container
13
14
  * @memberOf me
14
15
  * @constructor
@@ -17,6 +17,7 @@ var deferredRemove = function (child, keepalive) {
17
17
  var globalFloatingCounter = 0;
18
18
 
19
19
  /**
20
+ * @classdesc
20
21
  * me.Container represents a collection of child objects
21
22
  * @class Container
22
23
  * @extends me.Renderable
@@ -239,6 +239,8 @@ class ImageLayer extends Sprite {
239
239
  else {
240
240
  this.pos.y = y;
241
241
  }
242
+
243
+ this.isDirty = true;
242
244
  }
243
245
 
244
246
  /*