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.
- package/README.md +2 -0
- package/dist/melonjs.mjs/_virtual/_commonjsHelpers.js +10 -0
- package/dist/melonjs.mjs/_virtual/arraymultimap.js +10 -0
- package/dist/melonjs.mjs/_virtual/earcut.js +10 -0
- package/dist/melonjs.mjs/_virtual/howler.js +10 -0
- package/dist/melonjs.mjs/_virtual/index.js +10 -0
- package/dist/melonjs.mjs/_virtual/index2.js +10 -0
- package/dist/melonjs.mjs/_virtual/multimap.js +10 -0
- package/dist/melonjs.mjs/_virtual/setmultimap.js +10 -0
- package/dist/melonjs.mjs/application/application.js +240 -0
- package/dist/melonjs.mjs/audio/audio.js +536 -0
- package/dist/melonjs.mjs/camera/camera2d.js +732 -0
- package/dist/melonjs.mjs/entity/entity.js +248 -0
- package/dist/melonjs.mjs/game.js +29 -0
- package/dist/melonjs.mjs/geometries/ellipse.js +275 -0
- package/dist/melonjs.mjs/geometries/line.js +116 -0
- package/dist/melonjs.mjs/geometries/path2d.js +319 -0
- package/dist/melonjs.mjs/geometries/point.js +89 -0
- package/dist/melonjs.mjs/geometries/poly.js +500 -0
- package/dist/melonjs.mjs/geometries/rectangle.js +375 -0
- package/dist/melonjs.mjs/geometries/roundrect.js +168 -0
- package/dist/melonjs.mjs/index.js +248 -0
- package/dist/melonjs.mjs/input/gamepad.js +501 -0
- package/dist/melonjs.mjs/input/input.js +26 -0
- package/dist/melonjs.mjs/input/keyboard.js +470 -0
- package/dist/melonjs.mjs/input/pointer.js +393 -0
- package/dist/melonjs.mjs/input/pointerevent.js +818 -0
- package/dist/melonjs.mjs/lang/deprecated.js +157 -0
- package/dist/melonjs.mjs/level/level.js +297 -0
- package/dist/melonjs.mjs/level/tiled/TMXGroup.js +141 -0
- package/dist/melonjs.mjs/level/tiled/TMXLayer.js +448 -0
- package/dist/melonjs.mjs/level/tiled/TMXObject.js +355 -0
- package/dist/melonjs.mjs/level/tiled/TMXTile.js +194 -0
- package/dist/melonjs.mjs/level/tiled/TMXTileMap.js +639 -0
- package/dist/melonjs.mjs/level/tiled/TMXTileset.js +311 -0
- package/dist/melonjs.mjs/level/tiled/TMXTilesetGroup.js +83 -0
- package/dist/melonjs.mjs/level/tiled/TMXUtils.js +367 -0
- package/dist/melonjs.mjs/level/tiled/renderer/TMXHexagonalRenderer.js +506 -0
- package/dist/melonjs.mjs/level/tiled/renderer/TMXIsometricRenderer.js +220 -0
- package/dist/melonjs.mjs/level/tiled/renderer/TMXOrthogonalRenderer.js +157 -0
- package/dist/melonjs.mjs/level/tiled/renderer/TMXRenderer.js +125 -0
- package/dist/melonjs.mjs/level/tiled/renderer/TMXStaggeredRenderer.js +109 -0
- package/dist/melonjs.mjs/loader/loader.js +801 -0
- package/dist/melonjs.mjs/loader/loadingscreen.js +120 -0
- package/dist/melonjs.mjs/loader/melonjs_logo.png.js +11 -0
- package/dist/melonjs.mjs/math/color.js +618 -0
- package/dist/melonjs.mjs/math/math.js +218 -0
- package/dist/melonjs.mjs/math/matrix2.js +503 -0
- package/dist/melonjs.mjs/math/matrix3.js +681 -0
- package/dist/melonjs.mjs/math/observable_vector2.js +471 -0
- package/dist/melonjs.mjs/math/observable_vector3.js +561 -0
- package/dist/melonjs.mjs/math/vector2.js +528 -0
- package/dist/melonjs.mjs/math/vector3.js +569 -0
- package/dist/melonjs.mjs/node_modules/@teppeis/multimaps/dist/src/arraymultimap.js +73 -0
- package/dist/melonjs.mjs/node_modules/@teppeis/multimaps/dist/src/index.js +21 -0
- package/dist/melonjs.mjs/node_modules/@teppeis/multimaps/dist/src/multimap.js +324 -0
- package/dist/melonjs.mjs/node_modules/@teppeis/multimaps/dist/src/setmultimap.js +69 -0
- package/dist/melonjs.mjs/node_modules/earcut/src/earcut.js +691 -0
- package/dist/melonjs.mjs/node_modules/eventemitter3/index.js +350 -0
- package/dist/melonjs.mjs/node_modules/howler/dist/howler.js +3241 -0
- package/dist/melonjs.mjs/particles/emitter.js +267 -0
- package/dist/melonjs.mjs/particles/particle.js +188 -0
- package/dist/melonjs.mjs/particles/settings.js +319 -0
- package/dist/melonjs.mjs/physics/body.js +704 -0
- package/dist/melonjs.mjs/physics/bounds.js +460 -0
- package/dist/melonjs.mjs/physics/collision.js +132 -0
- package/dist/melonjs.mjs/physics/detector.js +194 -0
- package/dist/melonjs.mjs/physics/quadtree.js +391 -0
- package/dist/melonjs.mjs/physics/response.js +57 -0
- package/dist/melonjs.mjs/physics/sat.js +483 -0
- package/dist/melonjs.mjs/physics/world.js +221 -0
- package/dist/melonjs.mjs/plugin/plugin.js +141 -0
- package/dist/melonjs.mjs/renderable/collectable.js +62 -0
- package/dist/melonjs.mjs/renderable/colorlayer.js +80 -0
- package/dist/melonjs.mjs/renderable/container.js +1018 -0
- package/dist/melonjs.mjs/renderable/dragndrop.js +224 -0
- package/dist/melonjs.mjs/renderable/imagelayer.js +306 -0
- package/dist/melonjs.mjs/renderable/light2d.js +156 -0
- package/dist/melonjs.mjs/renderable/nineslicesprite.js +247 -0
- package/dist/melonjs.mjs/renderable/renderable.js +783 -0
- package/dist/melonjs.mjs/renderable/sprite.js +654 -0
- package/dist/melonjs.mjs/renderable/trigger.js +157 -0
- package/dist/melonjs.mjs/renderable/ui/uibaseelement.js +213 -0
- package/dist/melonjs.mjs/renderable/ui/uispriteelement.js +226 -0
- package/dist/melonjs.mjs/renderable/ui/uitextbutton.js +128 -0
- package/dist/melonjs.mjs/state/stage.js +237 -0
- package/dist/melonjs.mjs/state/state.js +596 -0
- package/dist/melonjs.mjs/system/device.js +909 -0
- package/dist/melonjs.mjs/system/dom.js +78 -0
- package/dist/melonjs.mjs/system/event.js +537 -0
- package/dist/melonjs.mjs/system/platform.js +41 -0
- package/dist/melonjs.mjs/system/pooling.js +209 -0
- package/dist/melonjs.mjs/system/save.js +157 -0
- package/dist/melonjs.mjs/system/timer.js +286 -0
- package/dist/melonjs.mjs/text/bitmaptext.js +364 -0
- package/dist/melonjs.mjs/text/bitmaptextdata.js +199 -0
- package/dist/melonjs.mjs/text/glyph.js +66 -0
- package/dist/melonjs.mjs/text/text.js +453 -0
- package/dist/melonjs.mjs/text/textmetrics.js +176 -0
- package/dist/melonjs.mjs/text/textstyle.js +23 -0
- package/dist/melonjs.mjs/tweens/easing.js +336 -0
- package/dist/melonjs.mjs/tweens/interpolation.js +112 -0
- package/dist/melonjs.mjs/tweens/tween.js +480 -0
- package/dist/melonjs.mjs/utils/agent.js +76 -0
- package/dist/melonjs.mjs/utils/array.js +63 -0
- package/dist/melonjs.mjs/utils/file.js +42 -0
- package/dist/melonjs.mjs/utils/function.js +70 -0
- package/dist/melonjs.mjs/utils/string.js +82 -0
- package/dist/melonjs.mjs/utils/utils.js +173 -0
- package/dist/melonjs.mjs/video/canvas/canvas_renderer.js +807 -0
- package/dist/melonjs.mjs/video/renderer.js +411 -0
- package/dist/melonjs.mjs/video/texture/atlas.js +519 -0
- package/dist/melonjs.mjs/video/texture/cache.js +143 -0
- package/dist/melonjs.mjs/video/texture/canvas_texture.js +144 -0
- package/dist/melonjs.mjs/video/video.js +462 -0
- package/dist/melonjs.mjs/video/webgl/buffer/vertex.js +143 -0
- package/dist/melonjs.mjs/video/webgl/glshader.js +168 -0
- package/dist/melonjs.mjs/video/webgl/shaders/primitive.frag.js +10 -0
- package/dist/melonjs.mjs/video/webgl/shaders/primitive.vert.js +10 -0
- package/dist/melonjs.mjs/video/webgl/shaders/quad.frag.js +10 -0
- package/dist/melonjs.mjs/video/webgl/shaders/quad.vert.js +10 -0
- package/dist/melonjs.mjs/video/webgl/utils/attributes.js +25 -0
- package/dist/melonjs.mjs/video/webgl/utils/precision.js +20 -0
- package/dist/melonjs.mjs/video/webgl/utils/program.js +67 -0
- package/dist/melonjs.mjs/video/webgl/utils/string.js +25 -0
- package/dist/melonjs.mjs/video/webgl/utils/uniforms.js +92 -0
- package/dist/melonjs.mjs/video/webgl/webgl_compositor.js +495 -0
- package/dist/melonjs.mjs/video/webgl/webgl_renderer.js +1036 -0
- package/dist/melonjs.module.d.ts +1163 -1163
- package/dist/melonjs.module.js +1903 -3274
- package/package.json +22 -17
- package/src/application/application.js +3 -3
- package/src/audio/audio.js +32 -32
- package/src/camera/camera2d.js +31 -31
- package/src/entity/entity.js +17 -17
- package/src/geometries/ellipse.js +16 -16
- package/src/geometries/line.js +5 -5
- package/src/geometries/path2d.js +32 -32
- package/src/geometries/poly.js +15 -15
- package/src/geometries/rectangle.js +18 -18
- package/src/geometries/roundrect.js +8 -8
- package/src/input/gamepad.js +15 -15
- package/src/input/keyboard.js +12 -12
- package/src/input/pointer.js +6 -6
- package/src/input/pointerevent.js +12 -12
- package/src/lang/deprecated.js +12 -12
- package/src/level/level.js +25 -25
- package/src/level/tiled/TMXLayer.js +22 -22
- package/src/level/tiled/TMXTile.js +5 -5
- package/src/level/tiled/TMXTileMap.js +6 -6
- package/src/level/tiled/TMXTileset.js +2 -2
- package/src/level/tiled/TMXUtils.js +5 -5
- package/src/level/tiled/renderer/TMXHexagonalRenderer.js +2 -2
- package/src/level/tiled/renderer/TMXIsometricRenderer.js +2 -2
- package/src/level/tiled/renderer/TMXOrthogonalRenderer.js +1 -1
- package/src/level/tiled/renderer/TMXRenderer.js +19 -19
- package/src/loader/loader.js +20 -20
- package/src/math/color.js +20 -20
- package/src/math/math.js +16 -16
- package/src/math/matrix2.js +16 -16
- package/src/math/matrix3.js +25 -25
- package/src/math/observable_vector2.js +14 -14
- package/src/math/observable_vector3.js +16 -16
- package/src/math/vector2.js +9 -9
- package/src/math/vector3.js +10 -10
- package/src/particles/emitter.js +6 -6
- package/src/particles/particle.js +2 -2
- package/src/physics/body.js +28 -28
- package/src/physics/bounds.js +8 -8
- package/src/physics/collision.js +2 -2
- package/src/physics/detector.js +6 -6
- package/src/physics/quadtree.js +11 -11
- package/src/physics/sat.js +31 -31
- package/src/physics/world.js +5 -5
- package/src/plugin/plugin.js +5 -5
- package/src/renderable/collectable.js +3 -3
- package/src/renderable/colorlayer.js +5 -5
- package/src/renderable/container.js +21 -21
- package/src/renderable/dragndrop.js +14 -14
- package/src/renderable/imagelayer.js +13 -13
- package/src/renderable/light2d.js +3 -3
- package/src/renderable/nineslicesprite.js +16 -16
- package/src/renderable/renderable.js +23 -23
- package/src/renderable/sprite.js +28 -28
- package/src/renderable/trigger.js +15 -15
- package/src/renderable/ui/uibaseelement.js +7 -7
- package/src/renderable/ui/uispriteelement.js +6 -6
- package/src/renderable/ui/uitextbutton.js +13 -13
- package/src/state/stage.js +7 -7
- package/src/state/state.js +17 -17
- package/src/system/device.js +11 -11
- package/src/system/event.js +10 -10
- package/src/system/pooling.js +9 -9
- package/src/system/save.js +2 -2
- package/src/system/timer.js +10 -10
- package/src/text/bitmaptext.js +18 -18
- package/src/text/bitmaptextdata.js +2 -2
- package/src/text/text.js +23 -23
- package/src/text/textmetrics.js +8 -8
- package/src/tweens/tween.js +19 -19
- package/src/utils/agent.js +5 -5
- package/src/utils/array.js +4 -4
- package/src/utils/file.js +2 -2
- package/src/utils/function.js +6 -6
- package/src/utils/string.js +5 -5
- package/src/utils/utils.js +4 -4
- package/src/video/canvas/canvas_renderer.js +70 -70
- package/src/video/renderer.js +26 -26
- package/src/video/texture/atlas.js +22 -22
- package/src/video/texture/canvas_texture.js +9 -9
- package/src/video/video.js +17 -17
- package/src/video/webgl/glshader.js +10 -10
- package/src/video/webgl/webgl_compositor.js +41 -41
- package/src/video/webgl/webgl_renderer.js +75 -75
|
@@ -0,0 +1,654 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* melonJS Game Engine - v14.1.0
|
|
3
|
+
* http://www.melonjs.org
|
|
4
|
+
* melonjs is licensed under the MIT License.
|
|
5
|
+
* http://www.opensource.org/licenses/mit-license
|
|
6
|
+
* @copyright (C) 2011 - 2022 Olivier Biot (AltByte Pte Ltd)
|
|
7
|
+
*/
|
|
8
|
+
import { renderer } from '../video/video.js';
|
|
9
|
+
import pool from '../system/pooling.js';
|
|
10
|
+
import loader from '../loader/loader.js';
|
|
11
|
+
import { TextureAtlas } from '../video/texture/atlas.js';
|
|
12
|
+
import Renderable from './renderable.js';
|
|
13
|
+
import Color from '../math/color.js';
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* @classdesc
|
|
17
|
+
* An object to display a fixed or animated sprite on screen.
|
|
18
|
+
* @augments Renderable
|
|
19
|
+
*/
|
|
20
|
+
class Sprite extends Renderable {
|
|
21
|
+
/**
|
|
22
|
+
* @param {number} x - the x coordinates of the sprite object
|
|
23
|
+
* @param {number} y - the y coordinates of the sprite object
|
|
24
|
+
* @param {object} settings - Configuration parameters for the Sprite object
|
|
25
|
+
* @param {HTMLImageElement|HTMLCanvasElement|TextureAtlas|string} settings.image - reference to spritesheet image, a texture atlas or to a texture atlas
|
|
26
|
+
* @param {string} [settings.name=""] - name of this object
|
|
27
|
+
* @param {string} [settings.region] - region name of a specific region to use when using a texture atlas, see {@link TextureAtlas}
|
|
28
|
+
* @param {number} [settings.framewidth] - Width of a single frame within the spritesheet
|
|
29
|
+
* @param {number} [settings.frameheight] - Height of a single frame within the spritesheet
|
|
30
|
+
* @param {string|Color} [settings.tint] - a tint to be applied to this sprite
|
|
31
|
+
* @param {number} [settings.flipX] - flip the sprite on the horizontal axis
|
|
32
|
+
* @param {number} [settings.flipY] - flip the sprite on the vertical axis
|
|
33
|
+
* @param {Vector2d} [settings.anchorPoint={x:0.5, y:0.5}] - Anchor point to draw the frame at (defaults to the center of the frame).
|
|
34
|
+
* @example
|
|
35
|
+
* // create a single sprite from a standalone image, with anchor in the center
|
|
36
|
+
* var sprite = new me.Sprite(0, 0, {
|
|
37
|
+
* image : "PlayerTexture",
|
|
38
|
+
* framewidth : 64,
|
|
39
|
+
* frameheight : 64,
|
|
40
|
+
* anchorPoint : new me.Vector2d(0.5, 0.5)
|
|
41
|
+
* });
|
|
42
|
+
*
|
|
43
|
+
* // create a single sprite from a packed texture
|
|
44
|
+
* game.texture = new me.TextureAtlas(
|
|
45
|
+
* me.loader.getJSON("texture"),
|
|
46
|
+
* me.loader.getImage("texture")
|
|
47
|
+
* );
|
|
48
|
+
* var sprite = new me.Sprite(0, 0, {
|
|
49
|
+
* image : game.texture,
|
|
50
|
+
* region : "npc2.png",
|
|
51
|
+
* });
|
|
52
|
+
*/
|
|
53
|
+
constructor(x, y, settings) {
|
|
54
|
+
|
|
55
|
+
// call the super constructor
|
|
56
|
+
super(x, y, 0, 0);
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* pause and resume animation
|
|
60
|
+
* @public
|
|
61
|
+
* @type {boolean}
|
|
62
|
+
* @default false
|
|
63
|
+
* @name Sprite#animationpause
|
|
64
|
+
*/
|
|
65
|
+
this.animationpause = false;
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* animation cycling speed (delay between frame in ms)
|
|
69
|
+
* @public
|
|
70
|
+
* @type {number}
|
|
71
|
+
* @default 100
|
|
72
|
+
* @name Sprite#animationspeed
|
|
73
|
+
*/
|
|
74
|
+
this.animationspeed = 100;
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* global offset for the position to draw from on the source image.
|
|
78
|
+
* @public
|
|
79
|
+
* @type {Vector2d}
|
|
80
|
+
* @default <0.0,0.0>
|
|
81
|
+
* @name offset
|
|
82
|
+
* @memberof Sprite#
|
|
83
|
+
*/
|
|
84
|
+
this.offset = pool.pull("Vector2d", 0, 0);
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* The source texture object this sprite object is using
|
|
88
|
+
* @public
|
|
89
|
+
* @type {TextureAtlas}
|
|
90
|
+
* @name source
|
|
91
|
+
* @memberof Sprite#
|
|
92
|
+
*/
|
|
93
|
+
this.source = null;
|
|
94
|
+
|
|
95
|
+
// hold all defined animation
|
|
96
|
+
this.anim = {};
|
|
97
|
+
|
|
98
|
+
// a flag to reset animation
|
|
99
|
+
this.resetAnim = undefined;
|
|
100
|
+
|
|
101
|
+
// current frame information
|
|
102
|
+
// (reusing current, any better/cleaner place?)
|
|
103
|
+
this.current = {
|
|
104
|
+
// the current animation name
|
|
105
|
+
name : "default",
|
|
106
|
+
// length of the current animation name
|
|
107
|
+
length : 0,
|
|
108
|
+
//current frame texture offset
|
|
109
|
+
offset : pool.pull("Vector2d"),
|
|
110
|
+
// current frame size
|
|
111
|
+
width : 0,
|
|
112
|
+
height : 0,
|
|
113
|
+
// Source rotation angle for pre-rotating the source image
|
|
114
|
+
angle : 0,
|
|
115
|
+
// current frame index
|
|
116
|
+
idx : 0
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
// animation frame delta
|
|
120
|
+
this.dt = 0;
|
|
121
|
+
|
|
122
|
+
// flicker settings
|
|
123
|
+
this._flicker = {
|
|
124
|
+
isFlickering : false,
|
|
125
|
+
duration : 0,
|
|
126
|
+
callback : null,
|
|
127
|
+
state : false
|
|
128
|
+
};
|
|
129
|
+
|
|
130
|
+
// set the proper image/texture to use
|
|
131
|
+
if (settings.image instanceof TextureAtlas) {
|
|
132
|
+
this.source = settings.image;
|
|
133
|
+
this.image = this.source.getTexture();
|
|
134
|
+
this.textureAtlas = settings.image;
|
|
135
|
+
// check for defined region
|
|
136
|
+
if (typeof (settings.region) !== "undefined") {
|
|
137
|
+
// use a texture atlas
|
|
138
|
+
var region = this.source.getRegion(settings.region);
|
|
139
|
+
if (region) {
|
|
140
|
+
// set the sprite region within the texture
|
|
141
|
+
this.setRegion(region);
|
|
142
|
+
// update the default "current" frame size
|
|
143
|
+
this.current.width = settings.framewidth || region.width;
|
|
144
|
+
this.current.height = settings.frameheight || region.height;
|
|
145
|
+
} else {
|
|
146
|
+
// throw an error
|
|
147
|
+
throw new Error("Texture - region for " + settings.region + " not found");
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
} else {
|
|
151
|
+
// HTMLImageElement/Canvas or {string}
|
|
152
|
+
this.image = (typeof settings.image === "object") ? settings.image : loader.getImage(settings.image);
|
|
153
|
+
// throw an error if image ends up being null/undefined
|
|
154
|
+
if (!this.image) {
|
|
155
|
+
throw new Error("me.Sprite: '" + settings.image + "' image/texture not found!");
|
|
156
|
+
}
|
|
157
|
+
// update the default "current" frame size
|
|
158
|
+
this.current.width = settings.framewidth = settings.framewidth || this.image.width;
|
|
159
|
+
this.current.height = settings.frameheight = settings.frameheight || this.image.height;
|
|
160
|
+
this.source = renderer.cache.get(this.image, settings);
|
|
161
|
+
this.textureAtlas = this.source.getAtlas();
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
// store/reset the current atlas information if specified
|
|
165
|
+
if (typeof(settings.atlas) !== "undefined") {
|
|
166
|
+
this.textureAtlas = settings.atlas;
|
|
167
|
+
this.atlasIndices = settings.atlasIndices;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
// resize based on the active frame
|
|
171
|
+
this.width = this.current.width;
|
|
172
|
+
this.height = this.current.height;
|
|
173
|
+
|
|
174
|
+
// apply flip flags if specified
|
|
175
|
+
if (typeof (settings.flipX) !== "undefined") {
|
|
176
|
+
this.flipX(!!settings.flipX);
|
|
177
|
+
}
|
|
178
|
+
if (typeof (settings.flipY) !== "undefined") {
|
|
179
|
+
this.flipY(!!settings.flipY);
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
// set the default rotation angle is defined in the settings
|
|
183
|
+
// * WARNING: rotating sprites decreases performance with Canvas Renderer
|
|
184
|
+
if (typeof (settings.rotation) !== "undefined") {
|
|
185
|
+
this.rotate(settings.rotation);
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
// update anchorPoint
|
|
189
|
+
if (settings.anchorPoint) {
|
|
190
|
+
this.anchorPoint.set(settings.anchorPoint.x, settings.anchorPoint.y);
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
if (typeof (settings.tint) !== "undefined") {
|
|
194
|
+
if (settings.tint instanceof Color) {
|
|
195
|
+
this.tint.copy(settings.tint);
|
|
196
|
+
} else {
|
|
197
|
+
// string (#RGB, #ARGB, #RRGGBB, #AARRGGBB)
|
|
198
|
+
this.tint.parseCSS(settings.tint);
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
// set the sprite name if specified
|
|
203
|
+
if (typeof (settings.name) === "string") {
|
|
204
|
+
this.name = settings.name;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
// displaying order
|
|
208
|
+
if (typeof settings.z !== "undefined") {
|
|
209
|
+
this.pos.z = settings.z;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
// for sprite, addAnimation will return !=0
|
|
213
|
+
if (this.addAnimation("default", null) !== 0) {
|
|
214
|
+
// set as default
|
|
215
|
+
this.setCurrentAnimation("default");
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
// enable currentTransform for me.Sprite based objects
|
|
219
|
+
this.autoTransform = true;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/**
|
|
223
|
+
* return the flickering state of the object
|
|
224
|
+
* @name isFlickering
|
|
225
|
+
* @memberof Sprite
|
|
226
|
+
* @returns {boolean}
|
|
227
|
+
*/
|
|
228
|
+
isFlickering() {
|
|
229
|
+
return this._flicker.isFlickering;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/**
|
|
233
|
+
* make the object flicker
|
|
234
|
+
* @name flicker
|
|
235
|
+
* @memberof Sprite
|
|
236
|
+
* @param {number} duration - expressed in milliseconds
|
|
237
|
+
* @param {Function} callback - Function to call when flickering ends
|
|
238
|
+
* @returns {Sprite} Reference to this object for method chaining
|
|
239
|
+
* @example
|
|
240
|
+
* // make the object flicker for 1 second
|
|
241
|
+
* // and then remove it
|
|
242
|
+
* this.flicker(1000, function () {
|
|
243
|
+
* me.game.world.removeChild(this);
|
|
244
|
+
* });
|
|
245
|
+
*/
|
|
246
|
+
flicker(duration, callback) {
|
|
247
|
+
this._flicker.duration = duration;
|
|
248
|
+
if (this._flicker.duration <= 0) {
|
|
249
|
+
this._flicker.isFlickering = false;
|
|
250
|
+
this._flicker.callback = null;
|
|
251
|
+
}
|
|
252
|
+
else if (!this._flicker.isFlickering) {
|
|
253
|
+
this._flicker.callback = callback;
|
|
254
|
+
this._flicker.isFlickering = true;
|
|
255
|
+
}
|
|
256
|
+
return this;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
/**
|
|
260
|
+
* add an animation <br>
|
|
261
|
+
* For fixed-sized cell sprite sheet, the index list must follow the
|
|
262
|
+
* logic as per the following example :<br>
|
|
263
|
+
* <img src="images/spritesheet_grid.png"/>
|
|
264
|
+
* @name addAnimation
|
|
265
|
+
* @memberof Sprite
|
|
266
|
+
* @param {string} name - animation id
|
|
267
|
+
* @param {number[]|string[]|object[]} index - list of sprite index or name
|
|
268
|
+
* defining the animation. Can also use objects to specify delay for each frame, see below
|
|
269
|
+
* @param {number} [animationspeed] - cycling speed for animation in ms
|
|
270
|
+
* @returns {number} frame amount of frame added to the animation (delay between each frame).
|
|
271
|
+
* @see Sprite#animationspeed
|
|
272
|
+
* @example
|
|
273
|
+
* // walking animation
|
|
274
|
+
* this.addAnimation("walk", [ 0, 1, 2, 3, 4, 5 ]);
|
|
275
|
+
* // standing animation
|
|
276
|
+
* this.addAnimation("stand", [ 11, 12 ]);
|
|
277
|
+
* // eating animation
|
|
278
|
+
* this.addAnimation("eat", [ 6, 6 ]);
|
|
279
|
+
* // rolling animation
|
|
280
|
+
* this.addAnimation("roll", [ 7, 8, 9, 10 ]);
|
|
281
|
+
* // slower animation
|
|
282
|
+
* this.addAnimation("roll", [ 7, 8, 9, 10 ], 200);
|
|
283
|
+
* // or get more specific with delay for each frame. Good solution instead of repeating:
|
|
284
|
+
* this.addAnimation("turn", [{ name: 0, delay: 200 }, { name: 1, delay: 100 }])
|
|
285
|
+
* // can do this with atlas values as well:
|
|
286
|
+
* this.addAnimation("turn", [{ name: "turnone", delay: 200 }, { name: "turntwo", delay: 100 }])
|
|
287
|
+
* // define an dying animation that stop on the last frame
|
|
288
|
+
* this.addAnimation("die", [{ name: 3, delay: 200 }, { name: 4, delay: 100 }, { name: 5, delay: Infinity }])
|
|
289
|
+
* // set the standing animation as default
|
|
290
|
+
* this.setCurrentAnimation("stand");
|
|
291
|
+
*/
|
|
292
|
+
addAnimation(name, index, animationspeed) {
|
|
293
|
+
this.anim[name] = {
|
|
294
|
+
name : name,
|
|
295
|
+
frames : [],
|
|
296
|
+
idx : 0,
|
|
297
|
+
length : 0
|
|
298
|
+
};
|
|
299
|
+
|
|
300
|
+
// # of frames
|
|
301
|
+
var counter = 0;
|
|
302
|
+
|
|
303
|
+
if (typeof (this.textureAtlas) !== "object") {
|
|
304
|
+
return 0;
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
|
|
308
|
+
if (index == null) {
|
|
309
|
+
index = [];
|
|
310
|
+
// create a default animation with all frame
|
|
311
|
+
Object.keys(this.textureAtlas).forEach((v, i) => {
|
|
312
|
+
index[i] = i;
|
|
313
|
+
});
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
// set each frame configuration (offset, size, etc..)
|
|
317
|
+
for (var i = 0, len = index.length; i < len; i++) {
|
|
318
|
+
var frame = index[i];
|
|
319
|
+
var frameObject;
|
|
320
|
+
if (typeof(frame) === "number" || typeof(frame) === "string") {
|
|
321
|
+
frameObject = {
|
|
322
|
+
name: frame,
|
|
323
|
+
delay: animationspeed || this.animationspeed
|
|
324
|
+
};
|
|
325
|
+
}
|
|
326
|
+
else {
|
|
327
|
+
frameObject = frame;
|
|
328
|
+
}
|
|
329
|
+
var frameObjectName = frameObject.name;
|
|
330
|
+
if (typeof(frameObjectName) === "number") {
|
|
331
|
+
if (typeof (this.textureAtlas[frameObjectName]) !== "undefined") {
|
|
332
|
+
// TODO: adding the cache source coordinates add undefined entries in webGL mode
|
|
333
|
+
this.anim[name].frames[i] = Object.assign(
|
|
334
|
+
{},
|
|
335
|
+
this.textureAtlas[frameObjectName],
|
|
336
|
+
frameObject
|
|
337
|
+
);
|
|
338
|
+
counter++;
|
|
339
|
+
}
|
|
340
|
+
} else { // string
|
|
341
|
+
if (this.source.getFormat().includes("Spritesheet")) {
|
|
342
|
+
throw new Error(
|
|
343
|
+
"string parameters for addAnimation are not allowed for standard spritesheet based Texture"
|
|
344
|
+
);
|
|
345
|
+
} else {
|
|
346
|
+
this.anim[name].frames[i] = Object.assign(
|
|
347
|
+
{},
|
|
348
|
+
this.textureAtlas[this.atlasIndices[frameObjectName]],
|
|
349
|
+
frameObject
|
|
350
|
+
);
|
|
351
|
+
counter++;
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
this.anim[name].length = counter;
|
|
356
|
+
|
|
357
|
+
return counter;
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
/**
|
|
361
|
+
* set the current animation
|
|
362
|
+
* this will always change the animation & set the frame to zero
|
|
363
|
+
* @name setCurrentAnimation
|
|
364
|
+
* @memberof Sprite
|
|
365
|
+
* @param {string} name - animation id
|
|
366
|
+
* @param {string|Function} [resetAnim] - animation id to switch to when complete, or callback
|
|
367
|
+
* @param {boolean} [preserve_dt=false] - if false will reset the elapsed time counter since last frame
|
|
368
|
+
* @returns {Sprite} Reference to this object for method chaining
|
|
369
|
+
* @example
|
|
370
|
+
* // set "walk" animation
|
|
371
|
+
* this.setCurrentAnimation("walk");
|
|
372
|
+
*
|
|
373
|
+
* // set "walk" animation if it is not the current animation
|
|
374
|
+
* if (this.isCurrentAnimation("walk")) {
|
|
375
|
+
* this.setCurrentAnimation("walk");
|
|
376
|
+
* }
|
|
377
|
+
*
|
|
378
|
+
* // set "eat" animation, and switch to "walk" when complete
|
|
379
|
+
* this.setCurrentAnimation("eat", "walk");
|
|
380
|
+
*
|
|
381
|
+
* // set "die" animation, and remove the object when finished
|
|
382
|
+
* this.setCurrentAnimation("die", (function () {
|
|
383
|
+
* me.game.world.removeChild(this);
|
|
384
|
+
* return false; // do not reset to first frame
|
|
385
|
+
* }).bind(this));
|
|
386
|
+
*
|
|
387
|
+
* // set "attack" animation, and pause for a short duration
|
|
388
|
+
* this.setCurrentAnimation("die", (function () {
|
|
389
|
+
* this.animationpause = true;
|
|
390
|
+
*
|
|
391
|
+
* // back to "standing" animation after 1 second
|
|
392
|
+
* setTimeout(function () {
|
|
393
|
+
* this.setCurrentAnimation("standing");
|
|
394
|
+
* }, 1000);
|
|
395
|
+
*
|
|
396
|
+
* return false; // do not reset to first frame
|
|
397
|
+
* }).bind(this));
|
|
398
|
+
*/
|
|
399
|
+
setCurrentAnimation(name, resetAnim, preserve_dt) {
|
|
400
|
+
if (this.anim[name]) {
|
|
401
|
+
this.current.name = name;
|
|
402
|
+
this.current.length = this.anim[this.current.name].length;
|
|
403
|
+
if (typeof resetAnim === "string") {
|
|
404
|
+
this.resetAnim = this.setCurrentAnimation.bind(this, resetAnim, null, true);
|
|
405
|
+
} else if (typeof resetAnim === "function") {
|
|
406
|
+
this.resetAnim = resetAnim;
|
|
407
|
+
} else {
|
|
408
|
+
this.resetAnim = undefined;
|
|
409
|
+
}
|
|
410
|
+
this.setAnimationFrame(this.current.idx);
|
|
411
|
+
if (!preserve_dt) {
|
|
412
|
+
this.dt = 0;
|
|
413
|
+
}
|
|
414
|
+
this.isDirty = true;
|
|
415
|
+
} else {
|
|
416
|
+
throw new Error("animation id '" + name + "' not defined");
|
|
417
|
+
}
|
|
418
|
+
return this;
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
/**
|
|
422
|
+
* reverse the given or current animation if none is specified
|
|
423
|
+
* @name reverseAnimation
|
|
424
|
+
* @memberof Sprite
|
|
425
|
+
* @param {string} [name] - animation id
|
|
426
|
+
* @returns {Sprite} Reference to this object for method chaining
|
|
427
|
+
* @see Sprite#animationspeed
|
|
428
|
+
*/
|
|
429
|
+
reverseAnimation(name) {
|
|
430
|
+
if (typeof name !== "undefined" && typeof this.anim[name] !== "undefined") {
|
|
431
|
+
this.anim[name].frames.reverse();
|
|
432
|
+
} else {
|
|
433
|
+
this.anim[this.current.name].frames.reverse();
|
|
434
|
+
}
|
|
435
|
+
this.isDirty = true;
|
|
436
|
+
return this;
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
/**
|
|
440
|
+
* return true if the specified animation is the current one.
|
|
441
|
+
* @name isCurrentAnimation
|
|
442
|
+
* @memberof Sprite
|
|
443
|
+
* @param {string} name - animation id
|
|
444
|
+
* @returns {boolean}
|
|
445
|
+
* @example
|
|
446
|
+
* if (!this.isCurrentAnimation("walk")) {
|
|
447
|
+
* // do something funny...
|
|
448
|
+
* }
|
|
449
|
+
*/
|
|
450
|
+
isCurrentAnimation(name) {
|
|
451
|
+
return this.current.name === name;
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
/**
|
|
455
|
+
* change the current texture atlas region for this sprite
|
|
456
|
+
* @see Texture.getRegion
|
|
457
|
+
* @name setRegion
|
|
458
|
+
* @memberof Sprite
|
|
459
|
+
* @param {object} region - typically returned through me.Texture.getRegion()
|
|
460
|
+
* @returns {Sprite} Reference to this object for method chaining
|
|
461
|
+
* @example
|
|
462
|
+
* // change the sprite to "shadedDark13.png";
|
|
463
|
+
* mySprite.setRegion(game.texture.getRegion("shadedDark13.png"));
|
|
464
|
+
*/
|
|
465
|
+
setRegion(region) {
|
|
466
|
+
// set the source texture for the given region
|
|
467
|
+
this.image = this.source.getTexture(region);
|
|
468
|
+
// set the sprite offset within the texture
|
|
469
|
+
this.current.offset.setV(region.offset);
|
|
470
|
+
// set angle if defined
|
|
471
|
+
this.current.angle = region.angle;
|
|
472
|
+
// update the default "current" size
|
|
473
|
+
this.width = this.current.width = region.width;
|
|
474
|
+
this.height = this.current.height = region.height;
|
|
475
|
+
// set global anchortPoint if defined
|
|
476
|
+
if (region.anchorPoint) {
|
|
477
|
+
this.anchorPoint.set(
|
|
478
|
+
this._flip.x && region.trimmed === true ? 1 - region.anchorPoint.x : region.anchorPoint.x,
|
|
479
|
+
this._flip.y && region.trimmed === true ? 1 - region.anchorPoint.y : region.anchorPoint.y
|
|
480
|
+
);
|
|
481
|
+
}
|
|
482
|
+
this.isDirty = true;
|
|
483
|
+
return this;
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
/**
|
|
487
|
+
* force the current animation frame index.
|
|
488
|
+
* @name setAnimationFrame
|
|
489
|
+
* @memberof Sprite
|
|
490
|
+
* @param {number} [idx=0] - animation frame index
|
|
491
|
+
* @returns {Sprite} Reference to this object for method chaining
|
|
492
|
+
* @example
|
|
493
|
+
* // reset the current animation to the first frame
|
|
494
|
+
* this.setAnimationFrame();
|
|
495
|
+
*/
|
|
496
|
+
setAnimationFrame(idx) {
|
|
497
|
+
this.current.idx = (idx || 0) % this.current.length;
|
|
498
|
+
return this.setRegion(this.getAnimationFrameObjectByIndex(this.current.idx));
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
/**
|
|
502
|
+
* return the current animation frame index.
|
|
503
|
+
* @name getCurrentAnimationFrame
|
|
504
|
+
* @memberof Sprite
|
|
505
|
+
* @returns {number} current animation frame index
|
|
506
|
+
*/
|
|
507
|
+
getCurrentAnimationFrame() {
|
|
508
|
+
return this.current.idx;
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
/**
|
|
512
|
+
* Returns the frame object by the index.
|
|
513
|
+
* @name getAnimationFrameObjectByIndex
|
|
514
|
+
* @memberof Sprite
|
|
515
|
+
* @ignore
|
|
516
|
+
* @param {number} id - the frame id
|
|
517
|
+
* @returns {number} if using number indices. Returns {object} containing frame data if using texture atlas
|
|
518
|
+
*/
|
|
519
|
+
getAnimationFrameObjectByIndex(id) {
|
|
520
|
+
return this.anim[this.current.name].frames[id];
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
/**
|
|
524
|
+
* update function. <br>
|
|
525
|
+
* automatically called by the game manager {@link game}
|
|
526
|
+
* @name update
|
|
527
|
+
* @memberof Sprite
|
|
528
|
+
* @protected
|
|
529
|
+
* @param {number} dt - time since the last update in milliseconds.
|
|
530
|
+
* @returns {boolean} true if the Sprite is dirty
|
|
531
|
+
*/
|
|
532
|
+
update(dt) {
|
|
533
|
+
// Update animation if necessary
|
|
534
|
+
if (!this.animationpause && this.current && this.current.length > 0) {
|
|
535
|
+
var duration = this.getAnimationFrameObjectByIndex(this.current.idx).delay;
|
|
536
|
+
this.dt += dt;
|
|
537
|
+
while (this.dt >= duration) {
|
|
538
|
+
this.isDirty = true;
|
|
539
|
+
this.dt -= duration;
|
|
540
|
+
|
|
541
|
+
var nextFrame = (this.current.length > 1? this.current.idx+1: this.current.idx);
|
|
542
|
+
this.setAnimationFrame(nextFrame);
|
|
543
|
+
|
|
544
|
+
// Switch animation if we reach the end of the strip and a callback is defined
|
|
545
|
+
if (this.current.idx === 0 && typeof this.resetAnim === "function") {
|
|
546
|
+
// Otherwise is must be callable
|
|
547
|
+
if (this.resetAnim() === false) {
|
|
548
|
+
// Reset to last frame
|
|
549
|
+
this.setAnimationFrame(this.current.length - 1);
|
|
550
|
+
|
|
551
|
+
// Bail early without skipping any more frames.
|
|
552
|
+
this.dt %= duration;
|
|
553
|
+
break;
|
|
554
|
+
}
|
|
555
|
+
}
|
|
556
|
+
// Get next frame duration
|
|
557
|
+
duration = this.getAnimationFrameObjectByIndex(this.current.idx).delay;
|
|
558
|
+
}
|
|
559
|
+
}
|
|
560
|
+
|
|
561
|
+
// update the sprite bounding box
|
|
562
|
+
/*
|
|
563
|
+
if (this.isDirty === true && !this.currentTransform.isIdentity()) {
|
|
564
|
+
this.getBounds().clear();
|
|
565
|
+
this.getBounds().addFrame(
|
|
566
|
+
0,
|
|
567
|
+
0,
|
|
568
|
+
this.current.width,
|
|
569
|
+
this.current.height,
|
|
570
|
+
this.currentTransform
|
|
571
|
+
);
|
|
572
|
+
this.updateBoundsPos(this.pos.x, this.pos.y);
|
|
573
|
+
}
|
|
574
|
+
*/
|
|
575
|
+
|
|
576
|
+
//update the "flickering" state if necessary
|
|
577
|
+
if (this._flicker.isFlickering) {
|
|
578
|
+
this._flicker.duration -= dt;
|
|
579
|
+
if (this._flicker.duration < 0) {
|
|
580
|
+
if (typeof (this._flicker.callback) === "function") {
|
|
581
|
+
this._flicker.callback();
|
|
582
|
+
}
|
|
583
|
+
this.flicker(-1);
|
|
584
|
+
}
|
|
585
|
+
this.isDirty = true;
|
|
586
|
+
}
|
|
587
|
+
|
|
588
|
+
return super.update(dt);
|
|
589
|
+
}
|
|
590
|
+
|
|
591
|
+
/**
|
|
592
|
+
* Destroy function<br>
|
|
593
|
+
* @ignore
|
|
594
|
+
*/
|
|
595
|
+
destroy() {
|
|
596
|
+
pool.push(this.offset);
|
|
597
|
+
this.offset = undefined;
|
|
598
|
+
super.destroy();
|
|
599
|
+
}
|
|
600
|
+
|
|
601
|
+
/**
|
|
602
|
+
* draw this srite (automatically called by melonJS)
|
|
603
|
+
* @name draw
|
|
604
|
+
* @memberof Sprite
|
|
605
|
+
* @protected
|
|
606
|
+
* @param {CanvasRenderer|WebGLRenderer} renderer - a renderer instance
|
|
607
|
+
* @param {Camera2d} [viewport] - the viewport to (re)draw
|
|
608
|
+
*/
|
|
609
|
+
draw(renderer, viewport) { // eslint-disable-line no-unused-vars
|
|
610
|
+
// do nothing if we are flickering
|
|
611
|
+
if (this._flicker.isFlickering) {
|
|
612
|
+
this._flicker.state = !this._flicker.state;
|
|
613
|
+
if (!this._flicker.state) {
|
|
614
|
+
return;
|
|
615
|
+
}
|
|
616
|
+
}
|
|
617
|
+
|
|
618
|
+
// the frame to draw
|
|
619
|
+
var frame = this.current;
|
|
620
|
+
|
|
621
|
+
// cache the current position and size
|
|
622
|
+
var xpos = this.pos.x,
|
|
623
|
+
ypos = this.pos.y;
|
|
624
|
+
|
|
625
|
+
var w = frame.width,
|
|
626
|
+
h = frame.height;
|
|
627
|
+
|
|
628
|
+
// frame offset in the texture/atlas
|
|
629
|
+
var frame_offset = frame.offset;
|
|
630
|
+
var g_offset = this.offset;
|
|
631
|
+
|
|
632
|
+
|
|
633
|
+
// remove image's TexturePacker/ShoeBox rotation
|
|
634
|
+
if (frame.angle !== 0) {
|
|
635
|
+
renderer.translate(-xpos, -ypos);
|
|
636
|
+
renderer.rotate(frame.angle);
|
|
637
|
+
xpos -= h;
|
|
638
|
+
w = frame.height;
|
|
639
|
+
h = frame.width;
|
|
640
|
+
}
|
|
641
|
+
|
|
642
|
+
renderer.drawImage(
|
|
643
|
+
this.image,
|
|
644
|
+
g_offset.x + frame_offset.x, // sx
|
|
645
|
+
g_offset.y + frame_offset.y, // sy
|
|
646
|
+
w, h, // sw,sh
|
|
647
|
+
xpos, ypos, // dx,dy
|
|
648
|
+
w, h // dw,dh
|
|
649
|
+
);
|
|
650
|
+
}
|
|
651
|
+
}
|
|
652
|
+
var Sprite$1 = Sprite;
|
|
653
|
+
|
|
654
|
+
export { Sprite$1 as default };
|