melonjs 14.5.0 → 15.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.
- package/LICENSE.md +1 -1
- package/README.md +6 -6
- package/dist/melonjs.mjs/_virtual/_commonjsHelpers.js +1 -1
- package/dist/melonjs.mjs/_virtual/arraymultimap.js +1 -1
- package/dist/melonjs.mjs/_virtual/earcut.js +1 -1
- package/dist/melonjs.mjs/_virtual/howler.js +1 -1
- package/dist/melonjs.mjs/_virtual/index.js +7 -3
- package/dist/melonjs.mjs/_virtual/index2.js +1 -1
- package/dist/melonjs.mjs/_virtual/multimap.js +1 -1
- package/dist/melonjs.mjs/_virtual/setmultimap.js +1 -1
- package/dist/melonjs.mjs/application/application.js +27 -17
- package/dist/melonjs.mjs/application/header.js +6 -5
- package/dist/melonjs.mjs/application/resize.js +20 -20
- package/dist/melonjs.mjs/application/settings.js +1 -1
- package/dist/melonjs.mjs/audio/audio.js +18 -20
- package/dist/melonjs.mjs/camera/camera2d.js +10 -10
- package/dist/melonjs.mjs/const.js +1 -1
- package/dist/melonjs.mjs/entity/entity.js +4 -4
- package/dist/melonjs.mjs/geometries/ellipse.js +6 -6
- package/dist/melonjs.mjs/geometries/line.js +9 -9
- package/dist/melonjs.mjs/geometries/path2d.js +56 -56
- package/dist/melonjs.mjs/geometries/point.js +2 -2
- package/dist/melonjs.mjs/geometries/poly.js +29 -29
- package/dist/melonjs.mjs/geometries/rectangle.js +8 -8
- package/dist/melonjs.mjs/geometries/roundrect.js +6 -6
- package/dist/melonjs.mjs/index.js +8 -6
- package/dist/melonjs.mjs/input/gamepad.js +30 -30
- package/dist/melonjs.mjs/input/input.js +2 -2
- package/dist/melonjs.mjs/input/keyboard.js +15 -15
- package/dist/melonjs.mjs/input/pointer.js +2 -2
- package/dist/melonjs.mjs/input/pointerevent.js +59 -59
- package/dist/melonjs.mjs/lang/console.js +3 -3
- package/dist/melonjs.mjs/lang/deprecated.js +2 -2
- package/dist/melonjs.mjs/level/level.js +9 -9
- package/dist/melonjs.mjs/level/tiled/TMXGroup.js +3 -3
- package/dist/melonjs.mjs/level/tiled/TMXLayer.js +31 -31
- package/dist/melonjs.mjs/level/tiled/TMXObject.js +8 -8
- package/dist/melonjs.mjs/level/tiled/TMXTile.js +8 -8
- package/dist/melonjs.mjs/level/tiled/TMXTileMap.js +19 -19
- package/dist/melonjs.mjs/level/tiled/TMXTileset.js +15 -15
- package/dist/melonjs.mjs/level/tiled/TMXTilesetGroup.js +3 -3
- package/dist/melonjs.mjs/level/tiled/TMXUtils.js +46 -41
- package/dist/melonjs.mjs/level/tiled/constants.js +1 -1
- package/dist/melonjs.mjs/level/tiled/renderer/TMXHexagonalRenderer.js +30 -30
- package/dist/melonjs.mjs/level/tiled/renderer/TMXIsometricRenderer.js +21 -21
- package/dist/melonjs.mjs/level/tiled/renderer/TMXOrthogonalRenderer.js +10 -10
- package/dist/melonjs.mjs/level/tiled/renderer/TMXRenderer.js +2 -2
- package/dist/melonjs.mjs/level/tiled/renderer/TMXStaggeredRenderer.js +6 -6
- package/dist/melonjs.mjs/level/tiled/renderer/autodetect.js +1 -1
- package/dist/melonjs.mjs/loader/cache.js +1 -1
- package/dist/melonjs.mjs/loader/loader.js +7 -7
- package/dist/melonjs.mjs/loader/loadingscreen.js +2 -2
- package/dist/melonjs.mjs/loader/melonjs_logo.png.js +1 -1
- package/dist/melonjs.mjs/loader/parser.js +13 -13
- package/dist/melonjs.mjs/loader/settings.js +3 -3
- package/dist/melonjs.mjs/math/color.js +25 -24
- package/dist/melonjs.mjs/math/math.js +2 -2
- package/dist/melonjs.mjs/math/matrix2.js +22 -22
- package/dist/melonjs.mjs/math/matrix3.js +52 -52
- package/dist/melonjs.mjs/math/observable_vector2.js +12 -12
- package/dist/melonjs.mjs/math/observable_vector3.js +22 -22
- package/dist/melonjs.mjs/math/vector2.js +11 -11
- package/dist/melonjs.mjs/math/vector3.js +21 -21
- package/dist/melonjs.mjs/node_modules/@teppeis/multimaps/dist/esm/arraymultimap.js +45 -0
- package/dist/melonjs.mjs/node_modules/@teppeis/multimaps/dist/esm/multimap.js +130 -0
- package/dist/melonjs.mjs/node_modules/@teppeis/multimaps/dist/src/arraymultimap.js +1 -1
- package/dist/melonjs.mjs/node_modules/@teppeis/multimaps/dist/src/index.js +1 -1
- package/dist/melonjs.mjs/node_modules/@teppeis/multimaps/dist/src/multimap.js +1 -1
- package/dist/melonjs.mjs/node_modules/@teppeis/multimaps/dist/src/setmultimap.js +1 -1
- package/dist/melonjs.mjs/node_modules/earcut/src/earcut.js +1 -1
- package/dist/melonjs.mjs/node_modules/eventemitter3/index.js +2 -2
- package/dist/melonjs.mjs/node_modules/eventemitter3/index2.js +1 -1
- package/dist/melonjs.mjs/node_modules/howler/dist/howler.js +1 -1
- package/dist/melonjs.mjs/particles/emitter.js +5 -5
- package/dist/melonjs.mjs/particles/particle.js +8 -8
- package/dist/melonjs.mjs/particles/settings.js +3 -3
- package/dist/melonjs.mjs/physics/body.js +17 -17
- package/dist/melonjs.mjs/physics/bounds.js +12 -12
- package/dist/melonjs.mjs/physics/collision.js +3 -3
- package/dist/melonjs.mjs/physics/detector.js +14 -14
- package/dist/melonjs.mjs/physics/quadtree.js +19 -19
- package/dist/melonjs.mjs/physics/response.js +1 -1
- package/dist/melonjs.mjs/physics/sat.js +60 -60
- package/dist/melonjs.mjs/physics/world.js +4 -4
- package/dist/melonjs.mjs/plugin/plugin.js +11 -11
- package/dist/melonjs.mjs/renderable/collectable.js +2 -2
- package/dist/melonjs.mjs/renderable/colorlayer.js +1 -1
- package/dist/melonjs.mjs/renderable/container.js +77 -59
- package/dist/melonjs.mjs/renderable/dragndrop.js +5 -5
- package/dist/melonjs.mjs/renderable/imagelayer.js +7 -6
- package/dist/melonjs.mjs/renderable/light2d.js +6 -6
- package/dist/melonjs.mjs/renderable/nineslicesprite.js +12 -12
- package/dist/melonjs.mjs/renderable/renderable.js +35 -13
- package/dist/melonjs.mjs/renderable/sprite.js +25 -25
- package/dist/melonjs.mjs/renderable/trigger.js +11 -9
- package/dist/melonjs.mjs/renderable/ui/uibaseelement.js +97 -27
- package/dist/melonjs.mjs/renderable/ui/uispriteelement.js +25 -25
- package/dist/melonjs.mjs/renderable/ui/uitextbutton.js +83 -65
- package/dist/melonjs.mjs/state/stage.js +7 -7
- package/dist/melonjs.mjs/state/state.js +15 -15
- package/dist/melonjs.mjs/system/device.js +3 -3
- package/dist/melonjs.mjs/system/dom.js +1 -1
- package/dist/melonjs.mjs/system/event.js +2 -2
- package/dist/melonjs.mjs/system/platform.js +1 -1
- package/dist/melonjs.mjs/system/pooling.js +9 -9
- package/dist/melonjs.mjs/system/save.js +6 -6
- package/dist/melonjs.mjs/system/timer.js +16 -12
- package/dist/melonjs.mjs/text/bitmaptext.js +20 -20
- package/dist/melonjs.mjs/text/bitmaptextdata.js +22 -22
- package/dist/melonjs.mjs/text/glyph.js +3 -3
- package/dist/melonjs.mjs/text/text.js +63 -92
- package/dist/melonjs.mjs/text/textmetrics.js +15 -15
- package/dist/melonjs.mjs/text/textstyle.js +4 -6
- package/dist/melonjs.mjs/tweens/easing.js +4 -4
- package/dist/melonjs.mjs/tweens/interpolation.js +8 -8
- package/dist/melonjs.mjs/tweens/tween.js +11 -11
- package/dist/melonjs.mjs/utils/agent.js +6 -6
- package/dist/melonjs.mjs/utils/array.js +4 -4
- package/dist/melonjs.mjs/utils/file.js +1 -1
- package/dist/melonjs.mjs/utils/function.js +5 -5
- package/dist/melonjs.mjs/utils/string.js +3 -3
- package/dist/melonjs.mjs/utils/utils.js +16 -16
- package/dist/melonjs.mjs/video/canvas/canvas_renderer.js +41 -119
- package/dist/melonjs.mjs/video/renderer.js +70 -14
- package/dist/melonjs.mjs/video/texture/atlas.js +39 -45
- package/dist/melonjs.mjs/video/texture/cache.js +9 -10
- package/dist/melonjs.mjs/video/texture/canvas_texture.js +63 -3
- package/dist/melonjs.mjs/video/utils/autodetect.js +1 -1
- package/dist/melonjs.mjs/video/video.js +2 -2
- package/dist/melonjs.mjs/video/webgl/buffer/vertex.js +23 -19
- package/dist/melonjs.mjs/video/webgl/compositors/compositor.js +80 -80
- package/dist/melonjs.mjs/video/webgl/compositors/primitive_compositor.js +77 -0
- package/dist/melonjs.mjs/video/webgl/compositors/quad_compositor.js +252 -0
- package/dist/melonjs.mjs/video/webgl/compositors/webgl_compositor.js +1 -1
- package/dist/melonjs.mjs/video/webgl/glshader.js +7 -7
- package/dist/melonjs.mjs/video/webgl/shaders/primitive.frag.js +1 -1
- package/dist/melonjs.mjs/video/webgl/shaders/primitive.vert.js +2 -2
- package/dist/melonjs.mjs/video/webgl/shaders/quad.frag.js +1 -1
- package/dist/melonjs.mjs/video/webgl/shaders/quad.vert.js +2 -2
- package/dist/melonjs.mjs/video/webgl/utils/attributes.js +2 -2
- package/dist/melonjs.mjs/video/webgl/utils/precision.js +1 -1
- package/dist/melonjs.mjs/video/webgl/utils/program.js +7 -7
- package/dist/melonjs.mjs/video/webgl/utils/string.js +1 -1
- package/dist/melonjs.mjs/video/webgl/utils/uniforms.js +4 -4
- package/dist/melonjs.mjs/video/webgl/webgl_renderer.js +188 -181
- package/dist/melonjs.module.js +25040 -25193
- package/dist/types/application/application.d.ts +11 -8
- package/dist/types/audio/audio.d.ts +3 -3
- package/dist/types/entity/entity.d.ts +1 -1
- package/dist/types/index.d.ts +7 -4
- package/dist/types/input/input.d.ts +1 -1
- package/dist/types/input/keyboard.d.ts +1 -1
- package/dist/types/input/pointerevent.d.ts +6 -6
- package/dist/types/level/level.d.ts +2 -2
- package/dist/types/level/tiled/TMXLayer.d.ts +4 -4
- package/dist/types/level/tiled/TMXTileMap.d.ts +2 -2
- package/dist/types/level/tiled/TMXUtils.d.ts +5 -4
- package/dist/types/loader/loader.d.ts +2 -2
- package/dist/types/loader/settings.d.ts +2 -2
- package/dist/types/particles/emitter.d.ts +1 -1
- package/dist/types/physics/collision.d.ts +1 -1
- package/dist/types/physics/detector.d.ts +1 -1
- package/dist/types/plugin/plugin.d.ts +2 -2
- package/dist/types/renderable/container.d.ts +21 -17
- package/dist/types/renderable/renderable.d.ts +6 -0
- package/dist/types/renderable/sprite.d.ts +11 -11
- package/dist/types/renderable/trigger.d.ts +1 -1
- package/dist/types/renderable/ui/uibaseelement.d.ts +28 -6
- package/dist/types/renderable/ui/uispriteelement.d.ts +12 -14
- package/dist/types/renderable/ui/uitextbutton.d.ts +52 -12
- package/dist/types/state/stage.d.ts +1 -1
- package/dist/types/system/device.d.ts +1 -1
- package/dist/types/system/pooling.d.ts +3 -3
- package/dist/types/system/timer.d.ts +5 -5
- package/dist/types/text/bitmaptext.d.ts +1 -1
- package/dist/types/text/text.d.ts +7 -21
- package/dist/types/text/textstyle.d.ts +1 -1
- package/dist/types/utils/array.d.ts +2 -2
- package/dist/types/video/canvas/canvas_renderer.d.ts +8 -81
- package/dist/types/video/renderer.d.ts +48 -4
- package/dist/types/video/texture/atlas.d.ts +3 -3
- package/dist/types/video/texture/canvas_texture.d.ts +40 -0
- package/dist/types/video/webgl/buffer/vertex.d.ts +3 -3
- package/dist/types/video/webgl/compositors/compositor.d.ts +38 -32
- package/dist/types/video/webgl/compositors/primitive_compositor.d.ts +22 -0
- package/dist/types/video/webgl/compositors/quad_compositor.d.ts +72 -0
- package/dist/types/video/webgl/glshader.d.ts +1 -1
- package/dist/types/video/webgl/webgl_renderer.d.ts +47 -37
- package/package.json +16 -17
- package/src/application/application.js +26 -16
- package/src/application/header.js +5 -4
- package/src/application/resize.js +19 -19
- package/src/audio/audio.js +17 -19
- package/src/camera/camera2d.js +9 -9
- package/src/entity/entity.js +3 -3
- package/src/geometries/ellipse.js +5 -5
- package/src/geometries/line.js +8 -8
- package/src/geometries/path2d.js +55 -55
- package/src/geometries/point.js +1 -1
- package/src/geometries/poly.js +28 -28
- package/src/geometries/rectangle.js +7 -7
- package/src/geometries/roundrect.js +5 -5
- package/src/index.js +9 -4
- package/src/input/gamepad.js +29 -29
- package/src/input/input.js +1 -1
- package/src/input/keyboard.js +14 -14
- package/src/input/pointer.js +1 -1
- package/src/input/pointerevent.js +52 -52
- package/src/lang/console.js +2 -2
- package/src/lang/deprecated.js +1 -1
- package/src/level/level.js +8 -8
- package/src/level/tiled/TMXGroup.js +2 -2
- package/src/level/tiled/TMXLayer.js +30 -30
- package/src/level/tiled/TMXObject.js +7 -7
- package/src/level/tiled/TMXTile.js +7 -7
- package/src/level/tiled/TMXTileMap.js +18 -18
- package/src/level/tiled/TMXTileset.js +14 -14
- package/src/level/tiled/TMXTilesetGroup.js +2 -2
- package/src/level/tiled/TMXUtils.js +45 -40
- package/src/level/tiled/renderer/TMXHexagonalRenderer.js +29 -29
- package/src/level/tiled/renderer/TMXIsometricRenderer.js +20 -20
- package/src/level/tiled/renderer/TMXOrthogonalRenderer.js +9 -9
- package/src/level/tiled/renderer/TMXRenderer.js +1 -1
- package/src/level/tiled/renderer/TMXStaggeredRenderer.js +5 -5
- package/src/loader/loader.js +6 -6
- package/src/loader/loadingscreen.js +1 -1
- package/src/loader/parser.js +12 -12
- package/src/loader/settings.js +2 -2
- package/src/math/color.js +24 -23
- package/src/math/math.js +1 -1
- package/src/math/matrix2.js +21 -21
- package/src/math/matrix3.js +51 -51
- package/src/math/observable_vector2.js +11 -11
- package/src/math/observable_vector3.js +21 -21
- package/src/math/vector2.js +10 -10
- package/src/math/vector3.js +20 -20
- package/src/particles/emitter.js +4 -4
- package/src/particles/particle.js +7 -7
- package/src/particles/settings.js +2 -2
- package/src/physics/body.js +16 -16
- package/src/physics/bounds.js +11 -11
- package/src/physics/collision.js +2 -2
- package/src/physics/detector.js +13 -13
- package/src/physics/quadtree.js +18 -18
- package/src/physics/sat.js +59 -59
- package/src/physics/world.js +3 -3
- package/src/plugin/plugin.js +8 -8
- package/src/polyfill/index.js +0 -2
- package/src/renderable/collectable.js +1 -1
- package/src/renderable/container.js +76 -58
- package/src/renderable/dragndrop.js +4 -4
- package/src/renderable/imagelayer.js +6 -5
- package/src/renderable/light2d.js +5 -5
- package/src/renderable/nineslicesprite.js +11 -11
- package/src/renderable/renderable.js +34 -12
- package/src/renderable/sprite.js +24 -24
- package/src/renderable/trigger.js +10 -8
- package/src/renderable/ui/uibaseelement.js +96 -26
- package/src/renderable/ui/uispriteelement.js +24 -24
- package/src/renderable/ui/uitextbutton.js +85 -67
- package/src/state/stage.js +6 -6
- package/src/state/state.js +14 -14
- package/src/system/device.js +2 -2
- package/src/system/event.js +1 -1
- package/src/system/pooling.js +8 -8
- package/src/system/save.js +5 -5
- package/src/system/timer.js +15 -11
- package/src/text/bitmaptext.js +19 -19
- package/src/text/bitmaptextdata.js +21 -21
- package/src/text/glyph.js +2 -2
- package/src/text/text.js +62 -91
- package/src/text/textmetrics.js +14 -14
- package/src/text/textstyle.js +3 -5
- package/src/tweens/easing.js +3 -3
- package/src/tweens/interpolation.js +7 -7
- package/src/tweens/tween.js +10 -10
- package/src/utils/agent.js +5 -5
- package/src/utils/array.js +3 -3
- package/src/utils/function.js +4 -4
- package/src/utils/string.js +2 -2
- package/src/utils/utils.js +15 -15
- package/src/video/canvas/canvas_renderer.js +39 -117
- package/src/video/renderer.js +68 -12
- package/src/video/texture/atlas.js +38 -44
- package/src/video/texture/cache.js +6 -6
- package/src/video/texture/canvas_texture.js +62 -2
- package/src/video/video.js +1 -1
- package/src/video/webgl/buffer/vertex.js +22 -18
- package/src/video/webgl/compositors/compositor.js +79 -80
- package/src/video/webgl/compositors/primitive_compositor.js +68 -0
- package/src/video/webgl/compositors/{webgl_compositor.js → quad_compositor.js} +52 -109
- package/src/video/webgl/glshader.js +6 -6
- package/src/video/webgl/shaders/primitive.vert +2 -5
- package/src/video/webgl/shaders/quad.vert +3 -1
- package/src/video/webgl/utils/attributes.js +1 -1
- package/src/video/webgl/utils/program.js +6 -6
- package/src/video/webgl/utils/uniforms.js +3 -3
- package/src/video/webgl/webgl_renderer.js +186 -179
- package/dist/melonjs.mjs/_virtual/make-built-in.js +0 -10
- package/dist/melonjs.mjs/_virtual/object-define-property.js +0 -10
- package/dist/melonjs.mjs/_virtual/object-get-own-property-descriptor.js +0 -10
- package/dist/melonjs.mjs/_virtual/object-get-own-property-names.js +0 -10
- package/dist/melonjs.mjs/_virtual/object-get-own-property-symbols.js +0 -10
- package/dist/melonjs.mjs/_virtual/object-property-is-enumerable.js +0 -10
- package/dist/melonjs.mjs/_virtual/shared.js +0 -10
- package/dist/melonjs.mjs/game.js +0 -29
- package/dist/melonjs.mjs/polyfill/console.js +0 -18
- package/dist/melonjs.mjs/polyfill/performance.js +0 -27
- package/dist/melonjs.mjs/polyfill/requestAnimationFrame.js +0 -46
- package/dist/melonjs.mjs/polyfill/roundrect.js +0 -242
- package/dist/melonjs.mjs/renderable/re_container.js +0 -1016
- package/dist/melonjs.mjs/video/utils/resize.js +0 -116
- package/dist/melonjs.mjs/video/webgl/webgl_compositor.js +0 -494
- package/src/polyfill/performance.js +0 -20
- package/src/polyfill/requestAnimationFrame.js +0 -39
|
@@ -1,1016 +0,0 @@
|
|
|
1
|
-
/*!
|
|
2
|
-
* melonJS Game Engine - v14.1.3
|
|
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 Renderable from './renderable.js';
|
|
9
|
-
import utils from '../utils/utils.js';
|
|
10
|
-
import { game } from '../index.js';
|
|
11
|
-
import { on, CANVAS_ONRESIZE } from '../system/event.js';
|
|
12
|
-
import pool from '../system/pooling.js';
|
|
13
|
-
import state from '../state/state.js';
|
|
14
|
-
import Body from '../physics/body.js';
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* Private function to re-use for object removal in a defer
|
|
18
|
-
* @ignore
|
|
19
|
-
*/
|
|
20
|
-
function deferredRemove(child, keepalive) {
|
|
21
|
-
this.removeChildNow(child, keepalive);
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
let globalFloatingCounter = 0;
|
|
25
|
-
|
|
26
|
-
/**
|
|
27
|
-
* @classdesc
|
|
28
|
-
* Container represents a collection of child objects
|
|
29
|
-
* @augments Renderable
|
|
30
|
-
*/
|
|
31
|
-
class Container extends Renderable {
|
|
32
|
-
/**
|
|
33
|
-
* @param {number} [x=0] - position of the container (accessible via the inherited pos.x property)
|
|
34
|
-
* @param {number} [y=0] - position of the container (accessible via the inherited pos.y property)
|
|
35
|
-
* @param {number} [width=game.viewport.width] - width of the container
|
|
36
|
-
* @param {number} [height=game.viewport.height] - height of the container
|
|
37
|
-
*/
|
|
38
|
-
constructor(x = 0, y = 0, width, height, root = false) {
|
|
39
|
-
|
|
40
|
-
// call the super constructor
|
|
41
|
-
super(
|
|
42
|
-
x, y,
|
|
43
|
-
typeof width === "undefined" ? (typeof game.viewport !== "undefined" ? game.viewport.width : Infinity) : width,
|
|
44
|
-
typeof height === "undefined" ? (typeof game.viewport !== "undefined" ? game.viewport.height : Infinity) : height
|
|
45
|
-
);
|
|
46
|
-
|
|
47
|
-
/**
|
|
48
|
-
* keep track of pending sort
|
|
49
|
-
* @ignore
|
|
50
|
-
*/
|
|
51
|
-
this.pendingSort = null;
|
|
52
|
-
|
|
53
|
-
/**
|
|
54
|
-
* whether the container is the root of the scene
|
|
55
|
-
* @public
|
|
56
|
-
* @type {boolean}
|
|
57
|
-
* @default false
|
|
58
|
-
* @name root
|
|
59
|
-
* @memberof Container
|
|
60
|
-
*/
|
|
61
|
-
this.root = root;
|
|
62
|
-
|
|
63
|
-
/**
|
|
64
|
-
* The array of children of this container.
|
|
65
|
-
* @ignore
|
|
66
|
-
*/
|
|
67
|
-
this.children = undefined;
|
|
68
|
-
|
|
69
|
-
/**
|
|
70
|
-
* The property of the child object that should be used to sort on <br>
|
|
71
|
-
* value : "x", "y", "z"
|
|
72
|
-
* @public
|
|
73
|
-
* @type {string}
|
|
74
|
-
* @default me.game.sortOn
|
|
75
|
-
* @name sortOn
|
|
76
|
-
* @memberof Container
|
|
77
|
-
*/
|
|
78
|
-
this.sortOn = game.sortOn;
|
|
79
|
-
|
|
80
|
-
/**
|
|
81
|
-
* Specify if the children list should be automatically sorted when adding a new child
|
|
82
|
-
* @public
|
|
83
|
-
* @type {boolean}
|
|
84
|
-
* @default true
|
|
85
|
-
* @name autoSort
|
|
86
|
-
* @memberof Container
|
|
87
|
-
*/
|
|
88
|
-
this.autoSort = true;
|
|
89
|
-
|
|
90
|
-
/**
|
|
91
|
-
* Specify if the children z index should automatically be managed by the parent container
|
|
92
|
-
* @public
|
|
93
|
-
* @type {boolean}
|
|
94
|
-
* @default true
|
|
95
|
-
* @name autoDepth
|
|
96
|
-
* @memberof Container
|
|
97
|
-
*/
|
|
98
|
-
this.autoDepth = true;
|
|
99
|
-
|
|
100
|
-
/**
|
|
101
|
-
* Specify if the container draw operation should clip his children to its own bounds
|
|
102
|
-
* @public
|
|
103
|
-
* @type {boolean}
|
|
104
|
-
* @default false
|
|
105
|
-
* @name clipping
|
|
106
|
-
* @memberof Container
|
|
107
|
-
*/
|
|
108
|
-
this.clipping = false;
|
|
109
|
-
|
|
110
|
-
/**
|
|
111
|
-
* a callback to be extended, triggered after a child has been added or removed
|
|
112
|
-
* @name onChildChange
|
|
113
|
-
* @memberof Container#
|
|
114
|
-
* @param {number} index - added or removed child index
|
|
115
|
-
*/
|
|
116
|
-
this.onChildChange = function (index) { // eslint-disable-line no-unused-vars
|
|
117
|
-
// to be extended
|
|
118
|
-
};
|
|
119
|
-
|
|
120
|
-
/**
|
|
121
|
-
* Specify if the container bounds should automatically take in account
|
|
122
|
-
* all child bounds when updated (this is expensive and disabled by default,
|
|
123
|
-
* only enable if necessary)
|
|
124
|
-
* @public
|
|
125
|
-
* @type {boolean}
|
|
126
|
-
* @default false
|
|
127
|
-
* @name enableChildBoundsUpdate
|
|
128
|
-
* @memberof Container
|
|
129
|
-
*/
|
|
130
|
-
this.enableChildBoundsUpdate = false;
|
|
131
|
-
|
|
132
|
-
/**
|
|
133
|
-
* define a background color for this container
|
|
134
|
-
* @public
|
|
135
|
-
* @type {Color}
|
|
136
|
-
* @name backgroundColor
|
|
137
|
-
* @default (0, 0, 0, 0.0)
|
|
138
|
-
* @memberof Container
|
|
139
|
-
* @example
|
|
140
|
-
* // add a red background color to this container
|
|
141
|
-
* this.backgroundColor.setColor(255, 0, 0);
|
|
142
|
-
*/
|
|
143
|
-
this.backgroundColor = pool.pull("Color", 0, 0, 0, 0.0);
|
|
144
|
-
|
|
145
|
-
/**
|
|
146
|
-
* Used by the debug panel plugin
|
|
147
|
-
* @ignore
|
|
148
|
-
*/
|
|
149
|
-
this.drawCount = 0;
|
|
150
|
-
|
|
151
|
-
// container self apply any defined transformation
|
|
152
|
-
this.autoTransform = true;
|
|
153
|
-
|
|
154
|
-
// enable collision and event detection
|
|
155
|
-
this.isKinematic = false;
|
|
156
|
-
|
|
157
|
-
this.anchorPoint.set(0, 0);
|
|
158
|
-
|
|
159
|
-
// subscribe on the canvas resize event
|
|
160
|
-
if (this.root === true) {
|
|
161
|
-
// Workaround for not updating container child-bounds automatically (it's expensive!)
|
|
162
|
-
on(CANVAS_ONRESIZE, this.updateBounds.bind(this, true));
|
|
163
|
-
}
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
/**
|
|
167
|
-
* reset the container, removing all childrens, and reseting transforms.
|
|
168
|
-
* @name reset
|
|
169
|
-
* @memberof Container
|
|
170
|
-
*/
|
|
171
|
-
reset() {
|
|
172
|
-
// cancel any sort operation
|
|
173
|
-
if (this.pendingSort) {
|
|
174
|
-
clearTimeout(this.pendingSort);
|
|
175
|
-
this.pendingSort = null;
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
// delete all children
|
|
179
|
-
var children = this.getChildren();
|
|
180
|
-
for (var i = children.length, child; i >= 0; (child = children[--i])) {
|
|
181
|
-
// don't remove it if a persistent object
|
|
182
|
-
if (child && child.isPersistent !== true) {
|
|
183
|
-
this.removeChildNow(child);
|
|
184
|
-
}
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
if (typeof this.currentTransform !== "undefined") {
|
|
188
|
-
// just reset some variables
|
|
189
|
-
this.currentTransform.identity();
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
this.backgroundColor.setColor(0, 0, 0, 0.0);
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
/**
|
|
196
|
-
* Add a child to the container <br>
|
|
197
|
-
* if auto-sort is disable, the object will be appended at the bottom of the list.
|
|
198
|
-
* Adding a child to the container will automatically remove it from its other container.
|
|
199
|
-
* Meaning a child can only have one parent. This is important if you add a renderable
|
|
200
|
-
* to a container then add it to the me.game.world container it will move it out of the
|
|
201
|
-
* orginal container. Then when the me.game.world.reset() is called the renderable
|
|
202
|
-
* will not be in any container. <br>
|
|
203
|
-
* if the given child implements a onActivateEvent method, that method will be called
|
|
204
|
-
* once the child is added to this container.
|
|
205
|
-
* @name addChild
|
|
206
|
-
* @memberof Container
|
|
207
|
-
* @param {Renderable} child
|
|
208
|
-
* @param {number} [z] - forces the z index of the child to the specified value
|
|
209
|
-
* @returns {Renderable} the added child
|
|
210
|
-
*/
|
|
211
|
-
addChild(child, z) {
|
|
212
|
-
if (child.ancestor instanceof Container) {
|
|
213
|
-
child.ancestor.removeChildNow(child);
|
|
214
|
-
}
|
|
215
|
-
else {
|
|
216
|
-
// only allocate a GUID if the object has no previous ancestor
|
|
217
|
-
// (e.g. move one child from one container to another)
|
|
218
|
-
if (child.isRenderable) {
|
|
219
|
-
// allocated a GUID value (use child.id as based index if defined)
|
|
220
|
-
child.GUID = utils.createGUID(child.id);
|
|
221
|
-
}
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
child.ancestor = this;
|
|
225
|
-
this.getChildren().push(child);
|
|
226
|
-
|
|
227
|
-
// set the child z value if required
|
|
228
|
-
if (typeof(child.pos) !== "undefined") {
|
|
229
|
-
if (typeof(z) === "number") {
|
|
230
|
-
child.pos.z = z;
|
|
231
|
-
} else if (this.autoDepth === true) {
|
|
232
|
-
child.pos.z = this.getChildren().length;
|
|
233
|
-
}
|
|
234
|
-
}
|
|
235
|
-
|
|
236
|
-
if (this.autoSort === true) {
|
|
237
|
-
this.sort();
|
|
238
|
-
}
|
|
239
|
-
|
|
240
|
-
if (typeof child.onActivateEvent === "function" && this.isAttachedToRoot()) {
|
|
241
|
-
child.onActivateEvent();
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
// force repaint in case this is a static non-animated object
|
|
245
|
-
if (this.isAttachedToRoot() === true) {
|
|
246
|
-
game.repaint();
|
|
247
|
-
}
|
|
248
|
-
|
|
249
|
-
// force bounds update if required
|
|
250
|
-
if (this.enableChildBoundsUpdate) {
|
|
251
|
-
this.updateBounds(true);
|
|
252
|
-
}
|
|
253
|
-
|
|
254
|
-
// if a physic body is defined, add it to the game world
|
|
255
|
-
if (child.body instanceof Body) {
|
|
256
|
-
game.world.addBody(child.body);
|
|
257
|
-
}
|
|
258
|
-
|
|
259
|
-
// triggered callback if defined
|
|
260
|
-
this.onChildChange.call(this, this.getChildren().length - 1);
|
|
261
|
-
|
|
262
|
-
return child;
|
|
263
|
-
}
|
|
264
|
-
|
|
265
|
-
/**
|
|
266
|
-
* Add a child to the container at the specified index<br>
|
|
267
|
-
* (the list won't be sorted after insertion)
|
|
268
|
-
* @name addChildAt
|
|
269
|
-
* @memberof Container
|
|
270
|
-
* @param {Renderable} child
|
|
271
|
-
* @param {number} index
|
|
272
|
-
* @returns {Renderable} the added child
|
|
273
|
-
*/
|
|
274
|
-
addChildAt(child, index) {
|
|
275
|
-
if (index >= 0 && index < this.getChildren().length) {
|
|
276
|
-
if (child.ancestor instanceof Container) {
|
|
277
|
-
child.ancestor.removeChildNow(child);
|
|
278
|
-
}
|
|
279
|
-
else {
|
|
280
|
-
// only allocate a GUID if the object has no previous ancestor
|
|
281
|
-
// (e.g. move one child from one container to another)
|
|
282
|
-
if (child.isRenderable) {
|
|
283
|
-
// allocated a GUID value
|
|
284
|
-
child.GUID = utils.createGUID();
|
|
285
|
-
}
|
|
286
|
-
}
|
|
287
|
-
child.ancestor = this;
|
|
288
|
-
|
|
289
|
-
this.getChildren().splice(index, 0, child);
|
|
290
|
-
|
|
291
|
-
if (typeof child.onActivateEvent === "function" && this.isAttachedToRoot()) {
|
|
292
|
-
child.onActivateEvent();
|
|
293
|
-
}
|
|
294
|
-
|
|
295
|
-
// force repaint in case this is a static non-animated object
|
|
296
|
-
if (this.isAttachedToRoot() === true) {
|
|
297
|
-
game.repaint();
|
|
298
|
-
}
|
|
299
|
-
|
|
300
|
-
// force bounds update if required
|
|
301
|
-
if (this.enableChildBoundsUpdate) {
|
|
302
|
-
this.updateBounds(true);
|
|
303
|
-
}
|
|
304
|
-
|
|
305
|
-
// if a physic body is defined, add it to the game world
|
|
306
|
-
if (child.body instanceof Body) {
|
|
307
|
-
game.world.addBody(child.body);
|
|
308
|
-
}
|
|
309
|
-
|
|
310
|
-
// triggered callback if defined
|
|
311
|
-
this.onChildChange.call(this, index);
|
|
312
|
-
|
|
313
|
-
return child;
|
|
314
|
-
}
|
|
315
|
-
else {
|
|
316
|
-
throw new Error("Index (" + index + ") Out Of Bounds for addChildAt()");
|
|
317
|
-
}
|
|
318
|
-
}
|
|
319
|
-
|
|
320
|
-
/**
|
|
321
|
-
* The forEach() method executes a provided function once per child element. <br>
|
|
322
|
-
* the callback function is invoked with three arguments: <br>
|
|
323
|
-
* - The current element being processed in the array <br>
|
|
324
|
-
* - The index of element in the array. <br>
|
|
325
|
-
* - The array forEach() was called upon. <br>
|
|
326
|
-
* @name forEach
|
|
327
|
-
* @memberof Container
|
|
328
|
-
* @param {Function} callback - fnction to execute on each element
|
|
329
|
-
* @param {object} [thisArg] - value to use as this(i.e reference Object) when executing callback.
|
|
330
|
-
* @example
|
|
331
|
-
* // iterate through all children of the root container
|
|
332
|
-
* me.game.world.forEach((child) => {
|
|
333
|
-
* // do something with the child
|
|
334
|
-
* child.doSomething();
|
|
335
|
-
* });
|
|
336
|
-
* me.game.world.forEach((child, index) => { ... });
|
|
337
|
-
* me.game.world.forEach((child, index, array) => { ... });
|
|
338
|
-
* me.game.world.forEach((child, index, array) => { ... }, thisArg);
|
|
339
|
-
*/
|
|
340
|
-
forEach(callback, thisArg) {
|
|
341
|
-
var context = this, i = 0;
|
|
342
|
-
var children = this.getChildren();
|
|
343
|
-
|
|
344
|
-
var len = children.length;
|
|
345
|
-
|
|
346
|
-
if (typeof callback !== "function") {
|
|
347
|
-
throw new Error(callback + " is not a function");
|
|
348
|
-
}
|
|
349
|
-
|
|
350
|
-
if (arguments.length > 1) {
|
|
351
|
-
context = thisArg;
|
|
352
|
-
}
|
|
353
|
-
|
|
354
|
-
while (i < len) {
|
|
355
|
-
callback.call(context, children[i], i, children);
|
|
356
|
-
i++;
|
|
357
|
-
}
|
|
358
|
-
}
|
|
359
|
-
|
|
360
|
-
/**
|
|
361
|
-
* Swaps the position (z-index) of 2 children
|
|
362
|
-
* @name swapChildren
|
|
363
|
-
* @memberof Container
|
|
364
|
-
* @param {Renderable} child
|
|
365
|
-
* @param {Renderable} child2
|
|
366
|
-
*/
|
|
367
|
-
swapChildren(child, child2) {
|
|
368
|
-
var index = this.getChildIndex(child);
|
|
369
|
-
var index2 = this.getChildIndex(child2);
|
|
370
|
-
|
|
371
|
-
if ((index !== -1) && (index2 !== -1)) {
|
|
372
|
-
// swap z index
|
|
373
|
-
var _z = child.pos.z;
|
|
374
|
-
child.pos.z = child2.pos.z;
|
|
375
|
-
child2.pos.z = _z;
|
|
376
|
-
// swap the positions..
|
|
377
|
-
this.getChildren()[index] = child2;
|
|
378
|
-
this.getChildren()[index2] = child;
|
|
379
|
-
// mark the container as dirty
|
|
380
|
-
this.isDirty = true;
|
|
381
|
-
}
|
|
382
|
-
else {
|
|
383
|
-
throw new Error(child + " Both the supplied childs must be a child of the caller " + this);
|
|
384
|
-
}
|
|
385
|
-
}
|
|
386
|
-
|
|
387
|
-
/**
|
|
388
|
-
* Returns the Child at the specified index
|
|
389
|
-
* @name getChildAt
|
|
390
|
-
* @memberof Container
|
|
391
|
-
* @param {number} index
|
|
392
|
-
* @returns {Renderable} the child at the specified index
|
|
393
|
-
*/
|
|
394
|
-
getChildAt(index) {
|
|
395
|
-
if (index >= 0 && index < this.getChildren().length) {
|
|
396
|
-
return this.getChildren()[index];
|
|
397
|
-
}
|
|
398
|
-
else {
|
|
399
|
-
throw new Error("Index (" + index + ") Out Of Bounds for getChildAt()");
|
|
400
|
-
}
|
|
401
|
-
}
|
|
402
|
-
|
|
403
|
-
/**
|
|
404
|
-
* Returns the index of the given Child
|
|
405
|
-
* @name getChildIndex
|
|
406
|
-
* @memberof Container
|
|
407
|
-
* @param {Renderable} child
|
|
408
|
-
* @returns {number} index
|
|
409
|
-
*/
|
|
410
|
-
getChildIndex(child) {
|
|
411
|
-
return this.getChildren().indexOf(child);
|
|
412
|
-
}
|
|
413
|
-
|
|
414
|
-
/**
|
|
415
|
-
* Returns the next child within the container or undefined if none
|
|
416
|
-
* @name getNextChild
|
|
417
|
-
* @memberof Container
|
|
418
|
-
* @param {Renderable} child
|
|
419
|
-
* @returns {Renderable} child
|
|
420
|
-
*/
|
|
421
|
-
getNextChild(child) {
|
|
422
|
-
var index = this.getChildren().indexOf(child) - 1;
|
|
423
|
-
if (index >= 0 && index < this.getChildren().length) {
|
|
424
|
-
return this.getChildAt(index);
|
|
425
|
-
}
|
|
426
|
-
return undefined;
|
|
427
|
-
}
|
|
428
|
-
|
|
429
|
-
/**
|
|
430
|
-
* Returns true if contains the specified Child
|
|
431
|
-
* @name hasChild
|
|
432
|
-
* @memberof Container
|
|
433
|
-
* @param {Renderable} child
|
|
434
|
-
* @returns {boolean}
|
|
435
|
-
*/
|
|
436
|
-
hasChild(child) {
|
|
437
|
-
return this === child.ancestor;
|
|
438
|
-
}
|
|
439
|
-
|
|
440
|
-
/**
|
|
441
|
-
* return the child corresponding to the given property and value.<br>
|
|
442
|
-
* note : avoid calling this function every frame since
|
|
443
|
-
* it parses the whole object tree each time
|
|
444
|
-
* @name getChildByProp
|
|
445
|
-
* @memberof Container
|
|
446
|
-
* @public
|
|
447
|
-
* @param {string} prop - Property name
|
|
448
|
-
* @param {string|RegExp|number|boolean} value - Value of the property
|
|
449
|
-
* @returns {Renderable[]} Array of childs
|
|
450
|
-
* @example
|
|
451
|
-
* // get the first child object called "mainPlayer" in a specific container :
|
|
452
|
-
* var ent = myContainer.getChildByProp("name", "mainPlayer");
|
|
453
|
-
*
|
|
454
|
-
* // or query the whole world :
|
|
455
|
-
* var ent = me.game.world.getChildByProp("name", "mainPlayer");
|
|
456
|
-
*
|
|
457
|
-
* // partial property matches are also allowed by using a RegExp.
|
|
458
|
-
* // the following matches "redCOIN", "bluecoin", "bagOfCoins", etc :
|
|
459
|
-
* var allCoins = me.game.world.getChildByProp("name", /coin/i);
|
|
460
|
-
*
|
|
461
|
-
* // searching for numbers or other data types :
|
|
462
|
-
* var zIndex10 = me.game.world.getChildByProp("z", 10);
|
|
463
|
-
* var inViewport = me.game.world.getChildByProp("inViewport", true);
|
|
464
|
-
*/
|
|
465
|
-
getChildByProp(prop, value) {
|
|
466
|
-
var objList = [];
|
|
467
|
-
|
|
468
|
-
/**
|
|
469
|
-
* @ignore
|
|
470
|
-
*/
|
|
471
|
-
function compare(obj, prop) {
|
|
472
|
-
var v = obj[prop];
|
|
473
|
-
if (value instanceof RegExp && typeof(v) === "string") {
|
|
474
|
-
if (value.test(v)) {
|
|
475
|
-
objList.push(obj);
|
|
476
|
-
}
|
|
477
|
-
}
|
|
478
|
-
else if (v === value) {
|
|
479
|
-
objList.push(obj);
|
|
480
|
-
}
|
|
481
|
-
}
|
|
482
|
-
|
|
483
|
-
this.forEach((child) => {
|
|
484
|
-
compare(child, prop);
|
|
485
|
-
if (child instanceof Container) {
|
|
486
|
-
objList = objList.concat(child.getChildByProp(prop, value));
|
|
487
|
-
}
|
|
488
|
-
});
|
|
489
|
-
|
|
490
|
-
return objList;
|
|
491
|
-
}
|
|
492
|
-
|
|
493
|
-
/**
|
|
494
|
-
* returns the list of childs with the specified class type
|
|
495
|
-
* @name getChildByType
|
|
496
|
-
* @memberof Container
|
|
497
|
-
* @public
|
|
498
|
-
* @param {object} classType
|
|
499
|
-
* @returns {Renderable[]} Array of children
|
|
500
|
-
*/
|
|
501
|
-
getChildByType(classType) {
|
|
502
|
-
var objList = [];
|
|
503
|
-
|
|
504
|
-
this.forEach((child) => {
|
|
505
|
-
if (child instanceof classType) {
|
|
506
|
-
objList.push(child);
|
|
507
|
-
}
|
|
508
|
-
if (child instanceof Container) {
|
|
509
|
-
objList = objList.concat(child.getChildByType(classType));
|
|
510
|
-
}
|
|
511
|
-
});
|
|
512
|
-
|
|
513
|
-
return objList;
|
|
514
|
-
}
|
|
515
|
-
|
|
516
|
-
/**
|
|
517
|
-
* returns the list of childs with the specified name<br>
|
|
518
|
-
* as defined in Tiled (Name field of the Object Properties)<br>
|
|
519
|
-
* note : avoid calling this function every frame since
|
|
520
|
-
* it parses the whole object list each time
|
|
521
|
-
* @name getChildByName
|
|
522
|
-
* @memberof Container
|
|
523
|
-
* @public
|
|
524
|
-
* @param {string|RegExp|number|boolean} name - child name
|
|
525
|
-
* @returns {Renderable[]} Array of children
|
|
526
|
-
*/
|
|
527
|
-
getChildByName(name) {
|
|
528
|
-
return this.getChildByProp("name", name);
|
|
529
|
-
}
|
|
530
|
-
|
|
531
|
-
/**
|
|
532
|
-
* return the child corresponding to the specified GUID<br>
|
|
533
|
-
* note : avoid calling this function every frame since
|
|
534
|
-
* it parses the whole object list each time
|
|
535
|
-
* @name getChildByGUID
|
|
536
|
-
* @memberof Container
|
|
537
|
-
* @public
|
|
538
|
-
* @param {string|RegExp|number|boolean} guid - child GUID
|
|
539
|
-
* @returns {Renderable} corresponding child or null
|
|
540
|
-
*/
|
|
541
|
-
getChildByGUID(guid) {
|
|
542
|
-
var obj = this.getChildByProp("GUID", guid);
|
|
543
|
-
return (obj.length > 0) ? obj[0] : null;
|
|
544
|
-
}
|
|
545
|
-
|
|
546
|
-
/**
|
|
547
|
-
* return all child in this container
|
|
548
|
-
* @name getChildren
|
|
549
|
-
* @memberof Container
|
|
550
|
-
* @public
|
|
551
|
-
* @returns {Renderable[]} an array of renderable object
|
|
552
|
-
*/
|
|
553
|
-
getChildren() {
|
|
554
|
-
if (typeof this.children === "undefined") {
|
|
555
|
-
this.children = [];
|
|
556
|
-
}
|
|
557
|
-
return this.children;
|
|
558
|
-
}
|
|
559
|
-
|
|
560
|
-
/**
|
|
561
|
-
* update the bounding box for this shape.
|
|
562
|
-
* @ignore
|
|
563
|
-
* @name updateBounds
|
|
564
|
-
* @memberof Renderable
|
|
565
|
-
* @returns {Bounds} this shape bounding box Rectangle object
|
|
566
|
-
*/
|
|
567
|
-
updateBounds(forceUpdateChildBounds = false) {
|
|
568
|
-
|
|
569
|
-
// call parent method
|
|
570
|
-
super.updateBounds();
|
|
571
|
-
|
|
572
|
-
var bounds = this.getBounds();
|
|
573
|
-
|
|
574
|
-
if (forceUpdateChildBounds === true || this.enableChildBoundsUpdate === true) {
|
|
575
|
-
this.forEach((child) => {
|
|
576
|
-
if (child.isRenderable) {
|
|
577
|
-
var childBounds = child.getBounds();
|
|
578
|
-
if (childBounds.isFinite()) {
|
|
579
|
-
bounds.addBounds(child.getBounds());
|
|
580
|
-
}
|
|
581
|
-
}
|
|
582
|
-
});
|
|
583
|
-
}
|
|
584
|
-
|
|
585
|
-
return bounds;
|
|
586
|
-
}
|
|
587
|
-
|
|
588
|
-
/**
|
|
589
|
-
* Checks if this container is root or if it's attached to the root container.
|
|
590
|
-
* @private
|
|
591
|
-
* @name isAttachedToRoot
|
|
592
|
-
* @memberof Container
|
|
593
|
-
* @returns {boolean}
|
|
594
|
-
*/
|
|
595
|
-
isAttachedToRoot() {
|
|
596
|
-
if (this.root === true) {
|
|
597
|
-
return true;
|
|
598
|
-
} else {
|
|
599
|
-
var ancestor = this.ancestor;
|
|
600
|
-
while (ancestor) {
|
|
601
|
-
if (ancestor.root === true) {
|
|
602
|
-
return true;
|
|
603
|
-
}
|
|
604
|
-
ancestor = ancestor.ancestor;
|
|
605
|
-
}
|
|
606
|
-
return false;
|
|
607
|
-
}
|
|
608
|
-
}
|
|
609
|
-
|
|
610
|
-
/**
|
|
611
|
-
* update the cointainer's bounding rect (private)
|
|
612
|
-
* @ignore
|
|
613
|
-
* @name updateBoundsPos
|
|
614
|
-
* @memberof Container
|
|
615
|
-
*/
|
|
616
|
-
updateBoundsPos(newX, newY) {
|
|
617
|
-
// call the parent method
|
|
618
|
-
super.updateBoundsPos(newX, newY);
|
|
619
|
-
|
|
620
|
-
// Notify children that the parent's position has changed
|
|
621
|
-
this.forEach((child) => {
|
|
622
|
-
if (child.isRenderable) {
|
|
623
|
-
child.updateBoundsPos(
|
|
624
|
-
// workaround on this.pos being updated after
|
|
625
|
-
// the callback being triggered
|
|
626
|
-
child.pos.x + newX - this.pos.x,
|
|
627
|
-
child.pos.y + newY - this.pos.y
|
|
628
|
-
);
|
|
629
|
-
}
|
|
630
|
-
});
|
|
631
|
-
return this.getBounds();
|
|
632
|
-
}
|
|
633
|
-
|
|
634
|
-
/**
|
|
635
|
-
* @ignore
|
|
636
|
-
*/
|
|
637
|
-
onActivateEvent() {
|
|
638
|
-
this.forEach((child) => {
|
|
639
|
-
if (typeof child.onActivateEvent === "function") {
|
|
640
|
-
child.onActivateEvent();
|
|
641
|
-
}
|
|
642
|
-
});
|
|
643
|
-
}
|
|
644
|
-
|
|
645
|
-
/**
|
|
646
|
-
* Invokes the removeChildNow in a defer, to ensure the child is removed safely after the update & draw stack has completed. <br>
|
|
647
|
-
* if the given child implements a onDeactivateEvent() method, that method will be called once the child is removed from this container.
|
|
648
|
-
* @name removeChild
|
|
649
|
-
* @memberof Container
|
|
650
|
-
* @public
|
|
651
|
-
* @param {Renderable} child
|
|
652
|
-
* @param {boolean} [keepalive=false] - true to prevent calling child.destroy()
|
|
653
|
-
*/
|
|
654
|
-
removeChild(child, keepalive) {
|
|
655
|
-
if (this.hasChild(child)) {
|
|
656
|
-
utils.function.defer(deferredRemove, this, child, keepalive);
|
|
657
|
-
}
|
|
658
|
-
else {
|
|
659
|
-
throw new Error("Child is not mine.");
|
|
660
|
-
}
|
|
661
|
-
}
|
|
662
|
-
|
|
663
|
-
/**
|
|
664
|
-
* Removes (and optionally destroys) a child from the container.<br>
|
|
665
|
-
* (removal is immediate and unconditional)<br>
|
|
666
|
-
* Never use keepalive=true with objects from {@link pool}. Doing so will create a memory leak.
|
|
667
|
-
* @name removeChildNow
|
|
668
|
-
* @memberof Container
|
|
669
|
-
* @param {Renderable} child
|
|
670
|
-
* @param {boolean} [keepalive=False] - True to prevent calling child.destroy()
|
|
671
|
-
*/
|
|
672
|
-
removeChildNow(child, keepalive) {
|
|
673
|
-
if (this.hasChild(child) && (this.getChildIndex(child) >= 0)) {
|
|
674
|
-
if (typeof child.onDeactivateEvent === "function") {
|
|
675
|
-
child.onDeactivateEvent();
|
|
676
|
-
}
|
|
677
|
-
|
|
678
|
-
// remove the body first to avoid a condition where a body can be detached
|
|
679
|
-
// from its parent, before the body is removed from the game world
|
|
680
|
-
if (child.body instanceof Body) {
|
|
681
|
-
game.world.removeBody(child.body);
|
|
682
|
-
}
|
|
683
|
-
|
|
684
|
-
if (!keepalive) {
|
|
685
|
-
// attempt at recycling the object
|
|
686
|
-
if (pool.push(child, false) === false ) {
|
|
687
|
-
// else just destroy it
|
|
688
|
-
if (typeof child.destroy === "function") {
|
|
689
|
-
child.destroy();
|
|
690
|
-
}
|
|
691
|
-
}
|
|
692
|
-
}
|
|
693
|
-
|
|
694
|
-
// Don't cache the child index; another element might have been removed
|
|
695
|
-
// by the child's `onDeactivateEvent` or `destroy` methods
|
|
696
|
-
var childIndex = this.getChildIndex(child);
|
|
697
|
-
if (childIndex >= 0) {
|
|
698
|
-
this.getChildren().splice(childIndex, 1);
|
|
699
|
-
child.ancestor = undefined;
|
|
700
|
-
}
|
|
701
|
-
|
|
702
|
-
// force repaint in case this is a static non-animated object
|
|
703
|
-
if (this.isAttachedToRoot() === true) {
|
|
704
|
-
game.repaint();
|
|
705
|
-
}
|
|
706
|
-
|
|
707
|
-
// force bounds update if required
|
|
708
|
-
if (this.enableChildBoundsUpdate) {
|
|
709
|
-
this.updateBounds(true);
|
|
710
|
-
}
|
|
711
|
-
|
|
712
|
-
// triggered callback if defined
|
|
713
|
-
this.onChildChange.call(this, childIndex);
|
|
714
|
-
}
|
|
715
|
-
}
|
|
716
|
-
|
|
717
|
-
/**
|
|
718
|
-
* Automatically set the specified property of all childs to the given value
|
|
719
|
-
* @name setChildsProperty
|
|
720
|
-
* @memberof Container
|
|
721
|
-
* @param {string} prop - property name
|
|
722
|
-
* @param {object} value - property value
|
|
723
|
-
* @param {boolean} [recursive=false] - recursively apply the value to child containers if true
|
|
724
|
-
*/
|
|
725
|
-
setChildsProperty(prop, value, recursive) {
|
|
726
|
-
this.forEach((child) => {
|
|
727
|
-
if ((recursive === true) && (child instanceof Container)) {
|
|
728
|
-
child.setChildsProperty(prop, value, recursive);
|
|
729
|
-
}
|
|
730
|
-
child[prop] = value;
|
|
731
|
-
});
|
|
732
|
-
}
|
|
733
|
-
|
|
734
|
-
/**
|
|
735
|
-
* Move the child in the group one step forward (z depth).
|
|
736
|
-
* @name moveUp
|
|
737
|
-
* @memberof Container
|
|
738
|
-
* @param {Renderable} child
|
|
739
|
-
*/
|
|
740
|
-
moveUp(child) {
|
|
741
|
-
var childIndex = this.getChildIndex(child);
|
|
742
|
-
if (childIndex - 1 >= 0) {
|
|
743
|
-
// note : we use an inverted loop
|
|
744
|
-
this.swapChildren(child, this.getChildAt(childIndex - 1));
|
|
745
|
-
// mark the container as dirty
|
|
746
|
-
this.isDirty = true;
|
|
747
|
-
}
|
|
748
|
-
}
|
|
749
|
-
|
|
750
|
-
/**
|
|
751
|
-
* Move the child in the group one step backward (z depth).
|
|
752
|
-
* @name moveDown
|
|
753
|
-
* @memberof Container
|
|
754
|
-
* @param {Renderable} child
|
|
755
|
-
*/
|
|
756
|
-
moveDown(child) {
|
|
757
|
-
var childIndex = this.getChildIndex(child);
|
|
758
|
-
if (childIndex >= 0 && (childIndex + 1) < this.getChildren().length) {
|
|
759
|
-
// note : we use an inverted loop
|
|
760
|
-
this.swapChildren(child, this.getChildAt(childIndex + 1));
|
|
761
|
-
// mark the container as dirty
|
|
762
|
-
this.isDirty = true;
|
|
763
|
-
}
|
|
764
|
-
}
|
|
765
|
-
|
|
766
|
-
/**
|
|
767
|
-
* Move the specified child to the top(z depth).
|
|
768
|
-
* @name moveToTop
|
|
769
|
-
* @memberof Container
|
|
770
|
-
* @param {Renderable} child
|
|
771
|
-
*/
|
|
772
|
-
moveToTop(child) {
|
|
773
|
-
var childIndex = this.getChildIndex(child);
|
|
774
|
-
if (childIndex > 0) {
|
|
775
|
-
var children = this.getChildren();
|
|
776
|
-
// note : we use an inverted loop
|
|
777
|
-
children.splice(0, 0, children.splice(childIndex, 1)[0]);
|
|
778
|
-
// increment our child z value based on the previous child depth
|
|
779
|
-
child.pos.z = children[1].pos.z + 1;
|
|
780
|
-
// mark the container as dirty
|
|
781
|
-
this.isDirty = true;
|
|
782
|
-
}
|
|
783
|
-
}
|
|
784
|
-
|
|
785
|
-
/**
|
|
786
|
-
* Move the specified child the bottom (z depth).
|
|
787
|
-
* @name moveToBottom
|
|
788
|
-
* @memberof Container
|
|
789
|
-
* @param {Renderable} child
|
|
790
|
-
*/
|
|
791
|
-
moveToBottom(child) {
|
|
792
|
-
var childIndex = this.getChildIndex(child);
|
|
793
|
-
var children = this.getChildren();
|
|
794
|
-
if (childIndex >= 0 && childIndex < (children.length - 1)) {
|
|
795
|
-
// note : we use an inverted loop
|
|
796
|
-
children.splice((children.length - 1), 0, children.splice(childIndex, 1)[0]);
|
|
797
|
-
// increment our child z value based on the next child depth
|
|
798
|
-
child.pos.z = children[(children.length - 2)].pos.z - 1;
|
|
799
|
-
// mark the container as dirty
|
|
800
|
-
this.isDirty = true;
|
|
801
|
-
}
|
|
802
|
-
}
|
|
803
|
-
|
|
804
|
-
/**
|
|
805
|
-
* Manually trigger the sort of all the childs in the container</p>
|
|
806
|
-
* @name sort
|
|
807
|
-
* @memberof Container
|
|
808
|
-
* @public
|
|
809
|
-
* @param {boolean} [recursive=false] - recursively sort all containers if true
|
|
810
|
-
*/
|
|
811
|
-
sort(recursive) {
|
|
812
|
-
// do nothing if there is already a pending sort
|
|
813
|
-
if (!this.pendingSort) {
|
|
814
|
-
if (recursive === true) {
|
|
815
|
-
this.forEach((child) => {
|
|
816
|
-
if (child instanceof Container) {
|
|
817
|
-
// note : this will generate one defered sorting function
|
|
818
|
-
// for each existing containe
|
|
819
|
-
child.sort(recursive);
|
|
820
|
-
}
|
|
821
|
-
});
|
|
822
|
-
}
|
|
823
|
-
/** @ignore */
|
|
824
|
-
this.pendingSort = utils.function.defer(function () {
|
|
825
|
-
// sort everything in this container
|
|
826
|
-
this.getChildren().sort(this["_sort" + this.sortOn.toUpperCase()]);
|
|
827
|
-
// clear the defer id
|
|
828
|
-
this.pendingSort = null;
|
|
829
|
-
// make sure we redraw everything
|
|
830
|
-
game.repaint();
|
|
831
|
-
}, this);
|
|
832
|
-
}
|
|
833
|
-
}
|
|
834
|
-
|
|
835
|
-
/**
|
|
836
|
-
* @ignore
|
|
837
|
-
*/
|
|
838
|
-
onDeactivateEvent() {
|
|
839
|
-
this.forEach((child) => {
|
|
840
|
-
if (typeof child.onDeactivateEvent === "function") {
|
|
841
|
-
child.onDeactivateEvent();
|
|
842
|
-
}
|
|
843
|
-
});
|
|
844
|
-
}
|
|
845
|
-
|
|
846
|
-
/**
|
|
847
|
-
* Z Sorting function
|
|
848
|
-
* @ignore
|
|
849
|
-
*/
|
|
850
|
-
_sortZ(a, b) {
|
|
851
|
-
return (b.pos && a.pos) ? (b.pos.z - a.pos.z) : (a.pos ? -Infinity : Infinity);
|
|
852
|
-
}
|
|
853
|
-
|
|
854
|
-
/**
|
|
855
|
-
* Reverse Z Sorting function
|
|
856
|
-
* @ignore
|
|
857
|
-
*/
|
|
858
|
-
_sortReverseZ(a, b) {
|
|
859
|
-
return (a.pos && b.pos) ? (a.pos.z - b.pos.z) : (a.pos ? Infinity : -Infinity);
|
|
860
|
-
}
|
|
861
|
-
|
|
862
|
-
/**
|
|
863
|
-
* X Sorting function
|
|
864
|
-
* @ignore
|
|
865
|
-
*/
|
|
866
|
-
_sortX(a, b) {
|
|
867
|
-
if (!b.pos || !a.pos) {
|
|
868
|
-
return (a.pos ? -Infinity : Infinity);
|
|
869
|
-
}
|
|
870
|
-
var result = b.pos.z - a.pos.z;
|
|
871
|
-
return (result ? result : (b.pos.x - a.pos.x));
|
|
872
|
-
}
|
|
873
|
-
|
|
874
|
-
/**
|
|
875
|
-
* Y Sorting function
|
|
876
|
-
* @ignore
|
|
877
|
-
*/
|
|
878
|
-
_sortY(a, b) {
|
|
879
|
-
if (!b.pos || !a.pos) {
|
|
880
|
-
return (a.pos ? -Infinity : Infinity);
|
|
881
|
-
}
|
|
882
|
-
var result = b.pos.z - a.pos.z;
|
|
883
|
-
return (result ? result : (b.pos.y - a.pos.y));
|
|
884
|
-
}
|
|
885
|
-
|
|
886
|
-
/**
|
|
887
|
-
* Destroy function<br>
|
|
888
|
-
* @ignore
|
|
889
|
-
*/
|
|
890
|
-
destroy() {
|
|
891
|
-
// empty the container
|
|
892
|
-
this.reset();
|
|
893
|
-
// call the parent destroy method
|
|
894
|
-
super.destroy(arguments);
|
|
895
|
-
}
|
|
896
|
-
|
|
897
|
-
/**
|
|
898
|
-
* container update function. <br>
|
|
899
|
-
* automatically called by the game manager {@link game}
|
|
900
|
-
* @name update
|
|
901
|
-
* @memberof Container
|
|
902
|
-
* @protected
|
|
903
|
-
* @param {number} dt - time since the last update in milliseconds.
|
|
904
|
-
* @returns {boolean} true if the Container is dirty
|
|
905
|
-
*/
|
|
906
|
-
update(dt) {
|
|
907
|
-
var isFloating = false;
|
|
908
|
-
var isPaused = state.isPaused();
|
|
909
|
-
var children = this.getChildren();
|
|
910
|
-
|
|
911
|
-
for (var i = children.length, obj; i--, (obj = children[i]);) {
|
|
912
|
-
if (isPaused && (!obj.updateWhenPaused)) {
|
|
913
|
-
// skip this object
|
|
914
|
-
continue;
|
|
915
|
-
}
|
|
916
|
-
|
|
917
|
-
if (obj.isRenderable) {
|
|
918
|
-
isFloating = (globalFloatingCounter > 0 || obj.floating);
|
|
919
|
-
if (isFloating) {
|
|
920
|
-
globalFloatingCounter++;
|
|
921
|
-
}
|
|
922
|
-
|
|
923
|
-
// check if object is in any active cameras
|
|
924
|
-
obj.inViewport = false;
|
|
925
|
-
// iterate through all cameras
|
|
926
|
-
state.current().cameras.forEach((camera) => {
|
|
927
|
-
if (camera.isVisible(obj, isFloating)) {
|
|
928
|
-
obj.inViewport = true;
|
|
929
|
-
}
|
|
930
|
-
});
|
|
931
|
-
|
|
932
|
-
// update our object
|
|
933
|
-
this.isDirty |= ((obj.inViewport || obj.alwaysUpdate) && obj.update(dt));
|
|
934
|
-
|
|
935
|
-
if (globalFloatingCounter > 0) {
|
|
936
|
-
globalFloatingCounter--;
|
|
937
|
-
}
|
|
938
|
-
}
|
|
939
|
-
else {
|
|
940
|
-
// just directly call update() for non renderable object
|
|
941
|
-
this.isDirty |= obj.update(dt);
|
|
942
|
-
}
|
|
943
|
-
}
|
|
944
|
-
|
|
945
|
-
// call the parent method
|
|
946
|
-
return super.update(dt);
|
|
947
|
-
}
|
|
948
|
-
|
|
949
|
-
/**
|
|
950
|
-
* draw this renderable (automatically called by melonJS)
|
|
951
|
-
* @name draw
|
|
952
|
-
* @memberof Container
|
|
953
|
-
* @protected
|
|
954
|
-
* @param {CanvasRenderer|WebGLRenderer} renderer - a renderer instance
|
|
955
|
-
* @param {Camera2d} [viewport] - the viewport to (re)draw
|
|
956
|
-
*/
|
|
957
|
-
draw(renderer, viewport) {
|
|
958
|
-
var isFloating = false;
|
|
959
|
-
var bounds = this.getBounds();
|
|
960
|
-
|
|
961
|
-
this.drawCount = 0;
|
|
962
|
-
|
|
963
|
-
// clip the containter children to the container bounds
|
|
964
|
-
if (this.root === false && this.clipping === true && bounds.isFinite() === true) {
|
|
965
|
-
renderer.clipRect(
|
|
966
|
-
bounds.left,
|
|
967
|
-
bounds.top,
|
|
968
|
-
bounds.width,
|
|
969
|
-
bounds.height
|
|
970
|
-
);
|
|
971
|
-
}
|
|
972
|
-
|
|
973
|
-
// adjust position if required (e.g. canvas/window centering)
|
|
974
|
-
renderer.translate(this.pos.x, this.pos.y);
|
|
975
|
-
|
|
976
|
-
// color background if defined
|
|
977
|
-
if (this.backgroundColor.alpha > 1 / 255) {
|
|
978
|
-
renderer.clearColor(this.backgroundColor);
|
|
979
|
-
}
|
|
980
|
-
|
|
981
|
-
var children = this.getChildren();
|
|
982
|
-
for (var i = children.length, obj; i--, (obj = children[i]);) {
|
|
983
|
-
if (obj.isRenderable) {
|
|
984
|
-
|
|
985
|
-
isFloating = obj.floating === true;
|
|
986
|
-
|
|
987
|
-
if ((obj.inViewport || isFloating)) {
|
|
988
|
-
|
|
989
|
-
if (isFloating) {
|
|
990
|
-
// translate to screen coordinates
|
|
991
|
-
renderer.save();
|
|
992
|
-
renderer.resetTransform();
|
|
993
|
-
}
|
|
994
|
-
|
|
995
|
-
// predraw (apply transforms)
|
|
996
|
-
obj.preDraw(renderer);
|
|
997
|
-
|
|
998
|
-
// draw the object
|
|
999
|
-
obj.draw(renderer, viewport);
|
|
1000
|
-
|
|
1001
|
-
// postdraw (clean-up);
|
|
1002
|
-
obj.postDraw(renderer);
|
|
1003
|
-
|
|
1004
|
-
// restore the previous "state"
|
|
1005
|
-
if (isFloating) {
|
|
1006
|
-
renderer.restore();
|
|
1007
|
-
}
|
|
1008
|
-
|
|
1009
|
-
this.drawCount++;
|
|
1010
|
-
}
|
|
1011
|
-
}
|
|
1012
|
-
}
|
|
1013
|
-
}
|
|
1014
|
-
}
|
|
1015
|
-
|
|
1016
|
-
export { Container as default };
|