melonjs 14.0.2 → 14.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (214) hide show
  1. package/README.md +2 -0
  2. package/dist/melonjs.mjs/_virtual/_commonjsHelpers.js +10 -0
  3. package/dist/melonjs.mjs/_virtual/arraymultimap.js +10 -0
  4. package/dist/melonjs.mjs/_virtual/earcut.js +10 -0
  5. package/dist/melonjs.mjs/_virtual/howler.js +10 -0
  6. package/dist/melonjs.mjs/_virtual/index.js +10 -0
  7. package/dist/melonjs.mjs/_virtual/index2.js +10 -0
  8. package/dist/melonjs.mjs/_virtual/multimap.js +10 -0
  9. package/dist/melonjs.mjs/_virtual/setmultimap.js +10 -0
  10. package/dist/melonjs.mjs/application/application.js +240 -0
  11. package/dist/melonjs.mjs/audio/audio.js +536 -0
  12. package/dist/melonjs.mjs/camera/camera2d.js +732 -0
  13. package/dist/melonjs.mjs/entity/entity.js +248 -0
  14. package/dist/melonjs.mjs/game.js +29 -0
  15. package/dist/melonjs.mjs/geometries/ellipse.js +275 -0
  16. package/dist/melonjs.mjs/geometries/line.js +116 -0
  17. package/dist/melonjs.mjs/geometries/path2d.js +319 -0
  18. package/dist/melonjs.mjs/geometries/point.js +89 -0
  19. package/dist/melonjs.mjs/geometries/poly.js +500 -0
  20. package/dist/melonjs.mjs/geometries/rectangle.js +375 -0
  21. package/dist/melonjs.mjs/geometries/roundrect.js +168 -0
  22. package/dist/melonjs.mjs/index.js +248 -0
  23. package/dist/melonjs.mjs/input/gamepad.js +501 -0
  24. package/dist/melonjs.mjs/input/input.js +26 -0
  25. package/dist/melonjs.mjs/input/keyboard.js +470 -0
  26. package/dist/melonjs.mjs/input/pointer.js +393 -0
  27. package/dist/melonjs.mjs/input/pointerevent.js +818 -0
  28. package/dist/melonjs.mjs/lang/deprecated.js +157 -0
  29. package/dist/melonjs.mjs/level/level.js +297 -0
  30. package/dist/melonjs.mjs/level/tiled/TMXGroup.js +141 -0
  31. package/dist/melonjs.mjs/level/tiled/TMXLayer.js +448 -0
  32. package/dist/melonjs.mjs/level/tiled/TMXObject.js +355 -0
  33. package/dist/melonjs.mjs/level/tiled/TMXTile.js +194 -0
  34. package/dist/melonjs.mjs/level/tiled/TMXTileMap.js +639 -0
  35. package/dist/melonjs.mjs/level/tiled/TMXTileset.js +311 -0
  36. package/dist/melonjs.mjs/level/tiled/TMXTilesetGroup.js +83 -0
  37. package/dist/melonjs.mjs/level/tiled/TMXUtils.js +367 -0
  38. package/dist/melonjs.mjs/level/tiled/renderer/TMXHexagonalRenderer.js +506 -0
  39. package/dist/melonjs.mjs/level/tiled/renderer/TMXIsometricRenderer.js +220 -0
  40. package/dist/melonjs.mjs/level/tiled/renderer/TMXOrthogonalRenderer.js +157 -0
  41. package/dist/melonjs.mjs/level/tiled/renderer/TMXRenderer.js +125 -0
  42. package/dist/melonjs.mjs/level/tiled/renderer/TMXStaggeredRenderer.js +109 -0
  43. package/dist/melonjs.mjs/loader/loader.js +801 -0
  44. package/dist/melonjs.mjs/loader/loadingscreen.js +120 -0
  45. package/dist/melonjs.mjs/loader/melonjs_logo.png.js +11 -0
  46. package/dist/melonjs.mjs/math/color.js +618 -0
  47. package/dist/melonjs.mjs/math/math.js +218 -0
  48. package/dist/melonjs.mjs/math/matrix2.js +503 -0
  49. package/dist/melonjs.mjs/math/matrix3.js +681 -0
  50. package/dist/melonjs.mjs/math/observable_vector2.js +471 -0
  51. package/dist/melonjs.mjs/math/observable_vector3.js +561 -0
  52. package/dist/melonjs.mjs/math/vector2.js +528 -0
  53. package/dist/melonjs.mjs/math/vector3.js +569 -0
  54. package/dist/melonjs.mjs/node_modules/@teppeis/multimaps/dist/src/arraymultimap.js +73 -0
  55. package/dist/melonjs.mjs/node_modules/@teppeis/multimaps/dist/src/index.js +21 -0
  56. package/dist/melonjs.mjs/node_modules/@teppeis/multimaps/dist/src/multimap.js +324 -0
  57. package/dist/melonjs.mjs/node_modules/@teppeis/multimaps/dist/src/setmultimap.js +69 -0
  58. package/dist/melonjs.mjs/node_modules/earcut/src/earcut.js +691 -0
  59. package/dist/melonjs.mjs/node_modules/eventemitter3/index.js +350 -0
  60. package/dist/melonjs.mjs/node_modules/howler/dist/howler.js +3241 -0
  61. package/dist/melonjs.mjs/particles/emitter.js +267 -0
  62. package/dist/melonjs.mjs/particles/particle.js +188 -0
  63. package/dist/melonjs.mjs/particles/settings.js +319 -0
  64. package/dist/melonjs.mjs/physics/body.js +704 -0
  65. package/dist/melonjs.mjs/physics/bounds.js +460 -0
  66. package/dist/melonjs.mjs/physics/collision.js +132 -0
  67. package/dist/melonjs.mjs/physics/detector.js +194 -0
  68. package/dist/melonjs.mjs/physics/quadtree.js +391 -0
  69. package/dist/melonjs.mjs/physics/response.js +57 -0
  70. package/dist/melonjs.mjs/physics/sat.js +483 -0
  71. package/dist/melonjs.mjs/physics/world.js +221 -0
  72. package/dist/melonjs.mjs/plugin/plugin.js +141 -0
  73. package/dist/melonjs.mjs/renderable/collectable.js +62 -0
  74. package/dist/melonjs.mjs/renderable/colorlayer.js +80 -0
  75. package/dist/melonjs.mjs/renderable/container.js +1018 -0
  76. package/dist/melonjs.mjs/renderable/dragndrop.js +224 -0
  77. package/dist/melonjs.mjs/renderable/imagelayer.js +306 -0
  78. package/dist/melonjs.mjs/renderable/light2d.js +156 -0
  79. package/dist/melonjs.mjs/renderable/nineslicesprite.js +247 -0
  80. package/dist/melonjs.mjs/renderable/renderable.js +783 -0
  81. package/dist/melonjs.mjs/renderable/sprite.js +654 -0
  82. package/dist/melonjs.mjs/renderable/trigger.js +157 -0
  83. package/dist/melonjs.mjs/renderable/ui/uibaseelement.js +213 -0
  84. package/dist/melonjs.mjs/renderable/ui/uispriteelement.js +226 -0
  85. package/dist/melonjs.mjs/renderable/ui/uitextbutton.js +128 -0
  86. package/dist/melonjs.mjs/state/stage.js +237 -0
  87. package/dist/melonjs.mjs/state/state.js +596 -0
  88. package/dist/melonjs.mjs/system/device.js +909 -0
  89. package/dist/melonjs.mjs/system/dom.js +78 -0
  90. package/dist/melonjs.mjs/system/event.js +537 -0
  91. package/dist/melonjs.mjs/system/platform.js +41 -0
  92. package/dist/melonjs.mjs/system/pooling.js +209 -0
  93. package/dist/melonjs.mjs/system/save.js +157 -0
  94. package/dist/melonjs.mjs/system/timer.js +286 -0
  95. package/dist/melonjs.mjs/text/bitmaptext.js +364 -0
  96. package/dist/melonjs.mjs/text/bitmaptextdata.js +199 -0
  97. package/dist/melonjs.mjs/text/glyph.js +66 -0
  98. package/dist/melonjs.mjs/text/text.js +453 -0
  99. package/dist/melonjs.mjs/text/textmetrics.js +176 -0
  100. package/dist/melonjs.mjs/text/textstyle.js +23 -0
  101. package/dist/melonjs.mjs/tweens/easing.js +336 -0
  102. package/dist/melonjs.mjs/tweens/interpolation.js +112 -0
  103. package/dist/melonjs.mjs/tweens/tween.js +480 -0
  104. package/dist/melonjs.mjs/utils/agent.js +76 -0
  105. package/dist/melonjs.mjs/utils/array.js +63 -0
  106. package/dist/melonjs.mjs/utils/file.js +42 -0
  107. package/dist/melonjs.mjs/utils/function.js +70 -0
  108. package/dist/melonjs.mjs/utils/string.js +82 -0
  109. package/dist/melonjs.mjs/utils/utils.js +173 -0
  110. package/dist/melonjs.mjs/video/canvas/canvas_renderer.js +807 -0
  111. package/dist/melonjs.mjs/video/renderer.js +411 -0
  112. package/dist/melonjs.mjs/video/texture/atlas.js +519 -0
  113. package/dist/melonjs.mjs/video/texture/cache.js +143 -0
  114. package/dist/melonjs.mjs/video/texture/canvas_texture.js +144 -0
  115. package/dist/melonjs.mjs/video/video.js +462 -0
  116. package/dist/melonjs.mjs/video/webgl/buffer/vertex.js +143 -0
  117. package/dist/melonjs.mjs/video/webgl/glshader.js +168 -0
  118. package/dist/melonjs.mjs/video/webgl/shaders/primitive.frag.js +10 -0
  119. package/dist/melonjs.mjs/video/webgl/shaders/primitive.vert.js +10 -0
  120. package/dist/melonjs.mjs/video/webgl/shaders/quad.frag.js +10 -0
  121. package/dist/melonjs.mjs/video/webgl/shaders/quad.vert.js +10 -0
  122. package/dist/melonjs.mjs/video/webgl/utils/attributes.js +25 -0
  123. package/dist/melonjs.mjs/video/webgl/utils/precision.js +20 -0
  124. package/dist/melonjs.mjs/video/webgl/utils/program.js +67 -0
  125. package/dist/melonjs.mjs/video/webgl/utils/string.js +25 -0
  126. package/dist/melonjs.mjs/video/webgl/utils/uniforms.js +92 -0
  127. package/dist/melonjs.mjs/video/webgl/webgl_compositor.js +495 -0
  128. package/dist/melonjs.mjs/video/webgl/webgl_renderer.js +1036 -0
  129. package/dist/melonjs.module.d.ts +1163 -1163
  130. package/dist/melonjs.module.js +1903 -3274
  131. package/package.json +22 -17
  132. package/src/application/application.js +3 -3
  133. package/src/audio/audio.js +32 -32
  134. package/src/camera/camera2d.js +31 -31
  135. package/src/entity/entity.js +17 -17
  136. package/src/geometries/ellipse.js +16 -16
  137. package/src/geometries/line.js +5 -5
  138. package/src/geometries/path2d.js +32 -32
  139. package/src/geometries/poly.js +15 -15
  140. package/src/geometries/rectangle.js +18 -18
  141. package/src/geometries/roundrect.js +8 -8
  142. package/src/input/gamepad.js +15 -15
  143. package/src/input/keyboard.js +12 -12
  144. package/src/input/pointer.js +6 -6
  145. package/src/input/pointerevent.js +12 -12
  146. package/src/lang/deprecated.js +12 -12
  147. package/src/level/level.js +25 -25
  148. package/src/level/tiled/TMXLayer.js +22 -22
  149. package/src/level/tiled/TMXTile.js +5 -5
  150. package/src/level/tiled/TMXTileMap.js +6 -6
  151. package/src/level/tiled/TMXTileset.js +2 -2
  152. package/src/level/tiled/TMXUtils.js +5 -5
  153. package/src/level/tiled/renderer/TMXHexagonalRenderer.js +2 -2
  154. package/src/level/tiled/renderer/TMXIsometricRenderer.js +2 -2
  155. package/src/level/tiled/renderer/TMXOrthogonalRenderer.js +1 -1
  156. package/src/level/tiled/renderer/TMXRenderer.js +19 -19
  157. package/src/loader/loader.js +20 -20
  158. package/src/math/color.js +20 -20
  159. package/src/math/math.js +16 -16
  160. package/src/math/matrix2.js +16 -16
  161. package/src/math/matrix3.js +25 -25
  162. package/src/math/observable_vector2.js +14 -14
  163. package/src/math/observable_vector3.js +16 -16
  164. package/src/math/vector2.js +9 -9
  165. package/src/math/vector3.js +10 -10
  166. package/src/particles/emitter.js +6 -6
  167. package/src/particles/particle.js +2 -2
  168. package/src/physics/body.js +28 -28
  169. package/src/physics/bounds.js +8 -8
  170. package/src/physics/collision.js +2 -2
  171. package/src/physics/detector.js +6 -6
  172. package/src/physics/quadtree.js +11 -11
  173. package/src/physics/sat.js +31 -31
  174. package/src/physics/world.js +5 -5
  175. package/src/plugin/plugin.js +5 -5
  176. package/src/renderable/collectable.js +3 -3
  177. package/src/renderable/colorlayer.js +5 -5
  178. package/src/renderable/container.js +21 -21
  179. package/src/renderable/dragndrop.js +14 -14
  180. package/src/renderable/imagelayer.js +13 -13
  181. package/src/renderable/light2d.js +3 -3
  182. package/src/renderable/nineslicesprite.js +16 -16
  183. package/src/renderable/renderable.js +23 -23
  184. package/src/renderable/sprite.js +28 -28
  185. package/src/renderable/trigger.js +15 -15
  186. package/src/renderable/ui/uibaseelement.js +7 -7
  187. package/src/renderable/ui/uispriteelement.js +6 -6
  188. package/src/renderable/ui/uitextbutton.js +13 -13
  189. package/src/state/stage.js +7 -7
  190. package/src/state/state.js +17 -17
  191. package/src/system/device.js +11 -11
  192. package/src/system/event.js +10 -10
  193. package/src/system/pooling.js +9 -9
  194. package/src/system/save.js +2 -2
  195. package/src/system/timer.js +10 -10
  196. package/src/text/bitmaptext.js +18 -18
  197. package/src/text/bitmaptextdata.js +2 -2
  198. package/src/text/text.js +23 -23
  199. package/src/text/textmetrics.js +8 -8
  200. package/src/tweens/tween.js +19 -19
  201. package/src/utils/agent.js +5 -5
  202. package/src/utils/array.js +4 -4
  203. package/src/utils/file.js +2 -2
  204. package/src/utils/function.js +6 -6
  205. package/src/utils/string.js +5 -5
  206. package/src/utils/utils.js +4 -4
  207. package/src/video/canvas/canvas_renderer.js +70 -70
  208. package/src/video/renderer.js +26 -26
  209. package/src/video/texture/atlas.js +22 -22
  210. package/src/video/texture/canvas_texture.js +9 -9
  211. package/src/video/video.js +17 -17
  212. package/src/video/webgl/glshader.js +10 -10
  213. package/src/video/webgl/webgl_compositor.js +41 -41
  214. package/src/video/webgl/webgl_renderer.js +75 -75
@@ -0,0 +1,783 @@
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 ObservableVector2d from '../math/observable_vector2.js';
9
+ import ObservableVector3d from '../math/observable_vector3.js';
10
+ import Rect from '../geometries/rectangle.js';
11
+ import Container from './container.js';
12
+ import pool from '../system/pooling.js';
13
+ import { clamp } from '../math/math.js';
14
+ import Color from '../math/color.js';
15
+ import { releaseAllPointerEvents } from '../input/pointerevent.js';
16
+
17
+ /**
18
+ * @classdesc
19
+ * A base class for renderable objects.
20
+ * @augments Rect
21
+ */
22
+ class Renderable extends Rect {
23
+ /**
24
+ * @param {number} x - position of the renderable object (accessible through inherited pos.x property)
25
+ * @param {number} y - position of the renderable object (accessible through inherited pos.y property)
26
+ * @param {number} width - object width
27
+ * @param {number} height - object height
28
+ */
29
+ constructor(x, y, width, height) {
30
+
31
+ // parent constructor
32
+ super(x, y, width, height);
33
+
34
+ /**
35
+ * to identify the object as a renderable object
36
+ * @ignore
37
+ */
38
+ this.isRenderable = true;
39
+
40
+ /**
41
+ * If true then physic collision and input events will not impact this renderable
42
+ * @type {boolean}
43
+ * @default true
44
+ */
45
+ this.isKinematic = true;
46
+
47
+ /**
48
+ * the renderable physic body
49
+ * @type {Body}
50
+ * @example
51
+ * // define a new Player Class
52
+ * class PlayerEntity extends me.Sprite {
53
+ * // constructor
54
+ * constructor(x, y, settings) {
55
+ * // call the parent constructor
56
+ * super(x, y , settings);
57
+ *
58
+ * // define a basic walking animation
59
+ * this.addAnimation("walk", [...]);
60
+ * // define a standing animation (using the first frame)
61
+ * this.addAnimation("stand", [...]);
62
+ * // set the standing animation as default
63
+ * this.setCurrentAnimation("stand");
64
+ *
65
+ * // add a physic body
66
+ * this.body = new me.Body(this);
67
+ * // add a default collision shape
68
+ * this.body.addShape(new me.Rect(0, 0, this.width, this.height));
69
+ * // configure max speed, friction, and initial force to be applied
70
+ * this.body.setMaxVelocity(3, 15);
71
+ * this.body.setFriction(0.4, 0);
72
+ * this.body.force.set(3, 0);
73
+ *
74
+ * // set the display to follow our position on both axis
75
+ * me.game.viewport.follow(this.pos, me.game.viewport.AXIS.BOTH);
76
+ * }
77
+ *
78
+ * ...
79
+ *
80
+ * }
81
+ */
82
+ this.body = undefined;
83
+
84
+ if (typeof this.currentTransform === "undefined") {
85
+ /**
86
+ * the renderable default transformation matrix
87
+ * @type {Matrix2d}
88
+ */
89
+ this.currentTransform = pool.pull("Matrix2d");
90
+ }
91
+ this.currentTransform.identity();
92
+
93
+ /**
94
+ * (G)ame (U)nique (Id)entifier" <br>
95
+ * a GUID will be allocated for any renderable object added <br>
96
+ * to an object container (including the `me.game.world` container)
97
+ * @type {string}
98
+ */
99
+ this.GUID = undefined;
100
+
101
+ /**
102
+ * an event handler that is called when the renderable leave or enter a camera viewport
103
+ * @type {Function}
104
+ * @default undefined
105
+ * @example
106
+ * this.onVisibilityChange = function(inViewport) {
107
+ * if (inViewport === true) {
108
+ * console.log("object has entered the in a camera viewport!");
109
+ * }
110
+ * };
111
+ */
112
+ this.onVisibilityChange = undefined;
113
+
114
+ /**
115
+ * Whether the renderable object will always update, even when outside of the viewport<br>
116
+ * @type {boolean}
117
+ * @default false
118
+ */
119
+ this.alwaysUpdate = false;
120
+
121
+ /**
122
+ * Whether to update this object when the game is paused.
123
+ * @type {boolean}
124
+ * @default false
125
+ */
126
+ this.updateWhenPaused = false;
127
+
128
+ /**
129
+ * make the renderable object persistent over level changes<br>
130
+ * @type {boolean}
131
+ * @default false
132
+ */
133
+ this.isPersistent = false;
134
+
135
+ /**
136
+ * If true, this renderable will be rendered using screen coordinates,
137
+ * as opposed to world coordinates. Use this, for example, to define UI elements.
138
+ * @type {boolean}
139
+ * @default false
140
+ */
141
+ this.floating = false;
142
+
143
+ if (this.anchorPoint instanceof ObservableVector2d) {
144
+ this.anchorPoint.setMuted(0.5, 0.5).setCallback(this.onAnchorUpdate, this);
145
+ } else {
146
+ /**
147
+ * The anchor point is used for attachment behavior, and/or when applying transformations.<br>
148
+ * The coordinate system places the origin at the top left corner of the frame (0, 0) and (1, 1) means the bottom-right corner<br>
149
+ * <img src="images/anchor_point.png"/><br>
150
+ * a Renderable's anchor point defaults to (0.5,0.5), which corresponds to the center position.<br>
151
+ * <br>
152
+ * <i><b>Note:</b> Object created through Tiled will have their anchorPoint set to (0, 0) to match Tiled Level editor implementation.
153
+ * To specify a value through Tiled, use a json expression like `json:{"x":0.5,"y":0.5}`. </i>
154
+ * @type {ObservableVector2d}
155
+ * @default <0.5,0.5>
156
+ */
157
+ this.anchorPoint = pool.pull("ObservableVector2d", 0.5, 0.5, { onUpdate: this.onAnchorUpdate, scope: this });
158
+ }
159
+
160
+ /**
161
+ * When enabled, an object container will automatically apply
162
+ * any defined transformation before calling the child draw method.
163
+ * @type {boolean}
164
+ * @default true
165
+ * @example
166
+ * // enable "automatic" transformation when the object is activated
167
+ * onActivateEvent: function () {
168
+ * // reset the transformation matrix
169
+ * this.currentTransform.identity();
170
+ * // ensure the anchor point is the renderable center
171
+ * this.anchorPoint.set(0.5, 0.5);
172
+ * // enable auto transform
173
+ * this.autoTransform = true;
174
+ * ....
175
+ * }
176
+ */
177
+ this.autoTransform = true;
178
+
179
+ /**
180
+ * Define the renderable opacity<br>
181
+ * Set to zero if you do not wish an object to be drawn
182
+ * @see Renderable#setOpacity
183
+ * @see Renderable#getOpacity
184
+ * @type {number}
185
+ * @default 1.0
186
+ */
187
+ this.alpha = 1.0;
188
+
189
+ /**
190
+ * a reference to the parent object that contains this renderable
191
+ * @type {Container|Entity}
192
+ * @default undefined
193
+ */
194
+ this.ancestor = undefined;
195
+
196
+ /**
197
+ * A mask limits rendering elements to the shape and position of the given mask object.
198
+ * So, if the renderable is larger than the mask, only the intersecting part of the renderable will be visible.
199
+ * @type {Rect|RoundRect|Polygon|Line|Ellipse}
200
+ * @default undefined
201
+ * @example
202
+ * // apply a mask in the shape of a Star
203
+ * myNPCSprite.mask = new me.Polygon(myNPCSprite.width / 2, 0, [
204
+ * // draw a star
205
+ * {x: 0, y: 0},
206
+ * {x: 14, y: 30},
207
+ * {x: 47, y: 35},
208
+ * {x: 23, y: 57},
209
+ * {x: 44, y: 90},
210
+ * {x: 0, y: 62},
211
+ * {x: -44, y: 90},
212
+ * {x: -23, y: 57},
213
+ * {x: -47, y: 35},
214
+ * {x: -14, y: 30}
215
+ * ]);
216
+ */
217
+ this.mask = undefined;
218
+
219
+ /**
220
+ * the blend mode to be applied to this renderable (see renderer setBlendMode for available blend mode)
221
+ * @type {string}
222
+ * @default "normal"
223
+ * @see CanvasRenderer#setBlendMode
224
+ * @see WebGLRenderer#setBlendMode
225
+ */
226
+ this.blendMode = "normal";
227
+
228
+ /**
229
+ * The name of the renderable
230
+ * @type {string}
231
+ * @default ""
232
+ */
233
+ this.name = "";
234
+
235
+ if (this.pos instanceof ObservableVector3d) {
236
+ this.pos.setMuted(x, y, 0).setCallback(this.updateBoundsPos, this);
237
+ } else {
238
+ /**
239
+ * Position of the Renderable relative to its parent container
240
+ * @public
241
+ * @type {ObservableVector3d}
242
+ */
243
+ this.pos = pool.pull("ObservableVector3d", x, y, 0, { onUpdate: this.updateBoundsPos, scope: this});
244
+ }
245
+
246
+ /**
247
+ * when true the renderable will be redrawn during the next update cycle
248
+ * @type {boolean}
249
+ * @default false
250
+ */
251
+ this.isDirty = false;
252
+
253
+ // keep track of when we flip
254
+ this._flip = {
255
+ x : false,
256
+ y : false
257
+ };
258
+
259
+ // viewport flag
260
+ this._inViewport = false;
261
+
262
+ // ensure it's fully opaque by default
263
+ this.setOpacity(1.0);
264
+ }
265
+
266
+ /**
267
+ * Whether the renderable object is floating, or contained in a floating container
268
+ * @see Renderable#floating
269
+ * @type {boolean}
270
+ */
271
+ get isFloating() {
272
+ return this.floating === true || (typeof this.ancestor !== "undefined" && this.ancestor.floating === true);
273
+ }
274
+
275
+ /**
276
+ * define a tint for this renderable. a (255, 255, 255) r, g, b value will remove the tint effect.
277
+ * @type {Color}
278
+ * @default (255, 255, 255)
279
+ * @example
280
+ * // add a red tint to this renderable
281
+ * this.tint.setColor(255, 128, 128);
282
+ * // remove the tint
283
+ * this.tint.setColor(255, 255, 255);
284
+ */
285
+ get tint() {
286
+ if (typeof this._tint === "undefined") {
287
+ this._tint = pool.pull("Color", 255, 255, 255, 1.0);
288
+ }
289
+ return this._tint;
290
+ }
291
+ set tint(value) {
292
+ if (typeof this._tint === "undefined") {
293
+ this._tint = pool.pull("Color", 255, 255, 255, 1.0);
294
+ }
295
+ if (value instanceof Color) {
296
+ this._tint.copy(value);
297
+ } else {
298
+ // string (#RGB, #ARGB, #RRGGBB, #AARRGGBB)
299
+ this._tint.parseCSS(value);
300
+ }
301
+ }
302
+
303
+ /**
304
+ * Whether the renderable object is visible and within the viewport
305
+ * @type {boolean}
306
+ * @default false
307
+ */
308
+ get inViewport() {
309
+ return this._inViewport;
310
+ }
311
+ set inViewport(value) {
312
+ if (this._inViewport !== value) {
313
+ this._inViewport = value;
314
+ if (typeof this.onVisibilityChange === "function") {
315
+ this.onVisibilityChange.call(this, value);
316
+ }
317
+ }
318
+ }
319
+
320
+ /**
321
+ * returns true if this renderable is flipped on the horizontal axis
322
+ * @public
323
+ * @see Renderable#flipX
324
+ * @type {boolean}
325
+ */
326
+ get isFlippedX() {
327
+ return this._flip.x === true;
328
+ }
329
+
330
+ /**
331
+ * returns true if this renderable is flipped on the vertical axis
332
+ * @public
333
+ * @see Renderable#flipY
334
+ * @type {boolean}
335
+ */
336
+ get isFlippedY() {
337
+ return this._flip.y === true;
338
+ }
339
+
340
+ /**
341
+ * returns the bounding box for this renderable
342
+ * @returns {Bounds} bounding box Rectangle object
343
+ */
344
+ getBounds() {
345
+ if (typeof this._bounds === "undefined") {
346
+ super.getBounds();
347
+ if (this.isFinite()) {
348
+ this._bounds.setMinMax(this.pos.x, this.pos.y, this.pos.x + this.width, this.pos.y + this.height);
349
+ } else {
350
+ // e.g. containers or game world can have infinite size
351
+ this._bounds.setMinMax(this.pos.x, this.pos.y, this.width, this.height);
352
+ }
353
+
354
+ }
355
+ return this._bounds;
356
+ }
357
+
358
+ /**
359
+ * get the renderable alpha channel value<br>
360
+ * @returns {number} current opacity value between 0 and 1
361
+ */
362
+ getOpacity() {
363
+ return this.alpha;
364
+ }
365
+
366
+ /**
367
+ * set the renderable alpha channel value<br>
368
+ * @param {number} alpha - opacity value between 0.0 and 1.0
369
+ */
370
+ setOpacity(alpha) {
371
+ if (typeof (alpha) === "number") {
372
+ this.alpha = clamp(alpha, 0.0, 1.0);
373
+ // Set to 1 if alpha is NaN
374
+ if (isNaN(this.alpha)) {
375
+ this.alpha = 1.0;
376
+ }
377
+ this.isDirty = true;
378
+ }
379
+ }
380
+
381
+ /**
382
+ * flip the renderable on the horizontal axis (around the center of the renderable)
383
+ * @see Matrix2d#scaleX
384
+ * @param {boolean} [flip=true] - `true` to flip this renderable.
385
+ * @returns {Renderable} Reference to this object for method chaining
386
+ */
387
+ flipX(flip = true) {
388
+ this._flip.x = !!flip;
389
+ this.isDirty = true;
390
+ return this;
391
+ }
392
+
393
+ /**
394
+ * flip the renderable on the vertical axis (around the center of the renderable)
395
+ * @see Matrix2d#scaleY
396
+ * @param {boolean} [flip=true] - `true` to flip this renderable.
397
+ * @returns {Renderable} Reference to this object for method chaining
398
+ */
399
+ flipY(flip = true) {
400
+ this._flip.y = !!flip;
401
+ this.isDirty = true;
402
+ return this;
403
+ }
404
+
405
+ /**
406
+ * multiply the renderable currentTransform with the given matrix
407
+ * @see Renderable#currentTransform
408
+ * @param {Matrix2d} m - the transformation matrix
409
+ * @returns {Renderable} Reference to this object for method chaining
410
+ */
411
+ transform(m) {
412
+ this.currentTransform.multiply(m);
413
+ //super.transform(m);
414
+ this.updateBoundsPos(this.pos.x, this.pos.y);
415
+ this.isDirty = true;
416
+ return this;
417
+ }
418
+
419
+ /**
420
+ * return the angle to the specified target
421
+ * @param {Renderable|Vector2d|Vector3d} target
422
+ * @returns {number} angle in radians
423
+ */
424
+ angleTo(target) {
425
+ var a = this.getBounds();
426
+ var ax, ay;
427
+
428
+ if (target instanceof Renderable) {
429
+ var b = target.getBounds();
430
+ ax = b.centerX - a.centerX;
431
+ ay = b.centerY - a.centerY;
432
+ } else { // vector object
433
+ ax = target.x - a.centerX;
434
+ ay = target.y - a.centerY;
435
+ }
436
+
437
+ return Math.atan2(ay, ax);
438
+ }
439
+
440
+ /**
441
+ * return the distance to the specified target
442
+ * @param {Renderable|Vector2d|Vector3d} target
443
+ * @returns {number} distance
444
+ */
445
+ distanceTo(target) {
446
+ var a = this.getBounds();
447
+ var dx, dy;
448
+
449
+ if (target instanceof Renderable) {
450
+ var b = target.getBounds();
451
+ dx = a.centerX - b.centerX;
452
+ dy = a.centerY - b.centerY;
453
+ } else { // vector object
454
+ dx = a.centerX - target.x;
455
+ dy = a.centerY - target.y;
456
+ }
457
+
458
+ return Math.sqrt(dx * dx + dy * dy);
459
+ }
460
+
461
+ /**
462
+ * Rotate this renderable towards the given target.
463
+ * @param {Renderable|Vector2d|Vector3d} target - the renderable or position to look at
464
+ * @returns {Renderable} Reference to this object for method chaining
465
+ */
466
+ lookAt(target) {
467
+ var position;
468
+
469
+ if (target instanceof Renderable) {
470
+ position = target.pos;
471
+ } else {
472
+ position = target;
473
+ }
474
+
475
+ var angle = this.angleTo(position);
476
+
477
+ this.rotate(angle);
478
+
479
+ return this;
480
+ }
481
+
482
+ /**
483
+ * Rotate this renderable by the specified angle (in radians).
484
+ * @param {number} angle - The angle to rotate (in radians)
485
+ * @param {Vector2d|ObservableVector2d} [v] - an optional point to rotate around
486
+ * @returns {Renderable} Reference to this object for method chaining
487
+ */
488
+ rotate(angle, v) {
489
+ if (!isNaN(angle)) {
490
+ this.currentTransform.rotate(angle, v);
491
+ //this.updateBoundsPos(this.pos.x, this.pos.y);
492
+ this.isDirty = true;
493
+ }
494
+ return this;
495
+ }
496
+
497
+ /**
498
+ * scale the renderable around his anchor point. Scaling actually applies changes
499
+ * to the currentTransform member wich is used by the renderer to scale the object
500
+ * when rendering. It does not scale the object itself. For example if the renderable
501
+ * is an image, the image.width and image.height properties are unaltered but the currentTransform
502
+ * member will be changed.
503
+ * @param {number} x - a number representing the abscissa of the scaling vector.
504
+ * @param {number} [y=x] - a number representing the ordinate of the scaling vector.
505
+ * @returns {Renderable} Reference to this object for method chaining
506
+ */
507
+ scale(x, y) {
508
+ this.currentTransform.scale(x, y);
509
+ super.scale(x, y);
510
+ this.isDirty = true;
511
+ return this;
512
+ }
513
+
514
+ /**
515
+ * scale the renderable around his anchor point
516
+ * @param {Vector2d} v - scaling vector
517
+ * @returns {Renderable} Reference to this object for method chaining
518
+ */
519
+ scaleV(v) {
520
+ this.scale(v.x, v.y);
521
+ return this;
522
+ }
523
+
524
+ /**
525
+ * update function (automatically called by melonJS).
526
+ * @param {number} dt - time since the last update in milliseconds.
527
+ * @returns {boolean} true if the renderable is dirty
528
+ */
529
+ update(dt) { // eslint-disable-line no-unused-vars
530
+ return this.isDirty;
531
+ }
532
+
533
+ /**
534
+ * update the bounding box for this shape.
535
+ * @ignore
536
+ * @returns {Bounds} this shape bounding box Rectangle object
537
+ */
538
+ updateBounds() {
539
+ super.updateBounds();
540
+ this.updateBoundsPos(this.pos.x, this.pos.y);
541
+ return this.getBounds();
542
+ }
543
+
544
+ /**
545
+ * update the renderable's bounding rect (private)
546
+ * @ignore
547
+ */
548
+ updateBoundsPos(newX, newY) {
549
+ var bounds = this.getBounds();
550
+
551
+ bounds.shift(newX, newY);
552
+
553
+ if (typeof this.anchorPoint !== "undefined" && bounds.isFinite()) {
554
+ bounds.translate(
555
+ -(this.anchorPoint.x * bounds.width),
556
+ -(this.anchorPoint.y * bounds.height)
557
+ );
558
+ }
559
+
560
+ /*
561
+ if (typeof this.body !== "undefined") {
562
+ var bodyBounds = this.body.getBounds();
563
+ bounds.translate(bodyBounds.x, bodyBounds.y);
564
+ }
565
+ */
566
+
567
+ // XXX: This is called from the constructor, before it gets an ancestor
568
+ if (this.ancestor instanceof Container && this.floating !== true) {
569
+ bounds.translate(this.ancestor.getAbsolutePosition());
570
+ }
571
+
572
+ this.isDirty = true;
573
+ }
574
+
575
+ /**
576
+ * return the renderable absolute position in the game world
577
+ * @returns {Vector2d}
578
+ */
579
+ getAbsolutePosition() {
580
+ if (typeof this._absPos === "undefined") {
581
+ this._absPos = pool.pull("Vector2d");
582
+ }
583
+ // XXX Cache me or something
584
+ this._absPos.set(this.pos.x, this.pos.y);
585
+ if (this.ancestor instanceof Container && this.floating !== true) {
586
+ this._absPos.add(this.ancestor.getAbsolutePosition());
587
+ }
588
+ return this._absPos;
589
+ }
590
+
591
+ /**
592
+ * called when the anchor point value is changed
593
+ * @private
594
+ * @param {number} x - the new X value to be set for the anchor
595
+ * @param {number} y - the new Y value to be set for the anchor
596
+ */
597
+ onAnchorUpdate(x, y) {
598
+ // since the callback is called before setting the new value
599
+ // manually update the anchor point (required for updateBoundsPos)
600
+ this.anchorPoint.setMuted(x, y);
601
+ // then call updateBounds
602
+ this.updateBoundsPos(this.pos.x, this.pos.y);
603
+ }
604
+
605
+ /**
606
+ * Prepare the rendering context before drawing (automatically called by melonJS).
607
+ * This will apply any defined transforms, anchor point, tint or blend mode and translate the context accordingly to this renderable position.
608
+ * @see Renderable#draw
609
+ * @see Renderable#postDraw
610
+ * @param {CanvasRenderer|WebGLRenderer} renderer - a renderer object
611
+ */
612
+ preDraw(renderer) {
613
+ var bounds = this.getBounds();
614
+ var ax = bounds.width * this.anchorPoint.x,
615
+ ay = bounds.height * this.anchorPoint.y;
616
+
617
+ // save renderer context
618
+ renderer.save();
619
+
620
+ // apply the defined alpha value
621
+ renderer.setGlobalAlpha(renderer.globalAlpha() * this.getOpacity());
622
+
623
+ // apply flip
624
+ if (this._flip.x || this._flip.y) {
625
+ var dx = this._flip.x ? this.centerX - ax : 0,
626
+ dy = this._flip.y ? this.centerY - ay : 0;
627
+
628
+ renderer.translate(dx, dy);
629
+ renderer.scale(this._flip.x ? -1 : 1, this._flip.y ? -1 : 1);
630
+ renderer.translate(-dx, -dy);
631
+ }
632
+
633
+ // apply stencil mask if defined
634
+ if (typeof this.mask !== "undefined") {
635
+ renderer.translate(this.pos.x, this.pos.y);
636
+ renderer.setMask(this.mask);
637
+ renderer.translate(-this.pos.x, -this.pos.y);
638
+ }
639
+
640
+ if ((this.autoTransform === true) && (!this.currentTransform.isIdentity())) {
641
+ // apply the renderable transformation matrix
642
+ renderer.translate(this.pos.x, this.pos.y);
643
+ renderer.transform(this.currentTransform);
644
+ renderer.translate(-this.pos.x, -this.pos.y);
645
+ }
646
+
647
+ // offset by the anchor point
648
+ renderer.translate(-ax, -ay);
649
+
650
+ // apply the current tint and opacity
651
+ renderer.setTint(this.tint, this.getOpacity());
652
+
653
+ // apply blending if different from "normal"
654
+ if (this.blendMode !== renderer.getBlendMode()) {
655
+ renderer.setBlendMode(this.blendMode);
656
+ }
657
+ }
658
+
659
+ /**
660
+ * Draw this renderable (automatically called by melonJS).
661
+ * All draw operations for renderable are made respectively
662
+ * to the position or transforms set or applied by the preDraw method.
663
+ * The main draw loop will first call preDraw() to prepare the context for drawing the renderable,
664
+ * then draw() to draw the renderable, and finally postDraw() to clear the context.
665
+ * If you override this method, be mindful about the drawing logic; for example if you draw a shape
666
+ * from the draw method, you should make sure that your draw it at the 0, 0 coordinates.
667
+ * @see Renderable#preDraw
668
+ * @see Renderable#postDraw
669
+ * @param {CanvasRenderer|WebGLRenderer} renderer - a renderer instance
670
+ * @param {Camera2d} [viewport] - the viewport to (re)draw
671
+ */
672
+ draw(renderer, viewport) { // eslint-disable-line no-unused-vars
673
+ // empty one !
674
+ }
675
+
676
+ /**
677
+ * restore the rendering context after drawing (automatically called by melonJS).
678
+ * @see Renderable#preDraw
679
+ * @see Renderable#draw
680
+ * @param {CanvasRenderer|WebGLRenderer} renderer - a renderer object
681
+ */
682
+ postDraw(renderer) {
683
+ // remove the previously applied tint
684
+ renderer.clearTint();
685
+
686
+ // clear the mask if set
687
+ if (typeof this.mask !== "undefined") {
688
+ renderer.clearMask();
689
+ }
690
+
691
+ // restore the context
692
+ renderer.restore();
693
+
694
+ // reset the dirty flag
695
+ this.isDirty = false;
696
+ }
697
+
698
+ /**
699
+ * onCollision callback, triggered in case of collision,
700
+ * when this renderable body is colliding with another one
701
+ * @param {ResponseObject} response - the collision response object
702
+ * @param {Renderable} other - the other renderable touching this one (a reference to response.a or response.b)
703
+ * @returns {boolean} true if the object should respond to the collision (its position and velocity will be corrected)
704
+ * @example
705
+ * // colision handler
706
+ * onCollision(response) {
707
+ * if (response.b.body.collisionType === me.collision.types.ENEMY_OBJECT) {
708
+ * // makes the other object solid, by substracting the overlap vector to the current position
709
+ * this.pos.sub(response.overlapV);
710
+ * this.hurt();
711
+ * // not solid
712
+ * return false;
713
+ * }
714
+ * // Make the object solid
715
+ * return true;
716
+ * },
717
+ */
718
+ onCollision(response, other) { // eslint-disable-line no-unused-vars
719
+ return false;
720
+ }
721
+
722
+ /**
723
+ * Destroy function<br>
724
+ * @ignore
725
+ */
726
+ destroy() {
727
+ // allow recycling object properties
728
+ pool.push(this.currentTransform);
729
+ this.currentTransform = undefined;
730
+
731
+ pool.push(this.anchorPoint);
732
+ this.anchorPoint = undefined;
733
+
734
+ pool.push(this.pos);
735
+ this.pos = undefined;
736
+
737
+ if (typeof this._absPos !== "undefined") {
738
+ pool.push(this._absPos);
739
+ this._absPos = undefined;
740
+ }
741
+
742
+ pool.push(this._bounds);
743
+ this._bounds = undefined;
744
+
745
+ this.onVisibilityChange = undefined;
746
+
747
+ if (typeof this.mask !== "undefined") {
748
+ pool.push(this.mask);
749
+ this.mask = undefined;
750
+ }
751
+
752
+ if (typeof this._tint !== "undefined") {
753
+ pool.push(this._tint);
754
+ this._tint = undefined;
755
+ }
756
+
757
+ this.ancestor = undefined;
758
+
759
+ // destroy the physic body if defined
760
+ if (typeof this.body !== "undefined") {
761
+ this.body.destroy.apply(this.body, arguments);
762
+ this.body = undefined;
763
+ }
764
+
765
+ // release all registered events
766
+ releaseAllPointerEvents(this);
767
+
768
+ // call the user defined destroy method
769
+ this.onDestroyEvent.apply(this, arguments);
770
+ }
771
+
772
+ /**
773
+ * OnDestroy Notification function<br>
774
+ * Called by engine before deleting the object
775
+ */
776
+ onDestroyEvent() {
777
+ // to be extended !
778
+ }
779
+
780
+ }
781
+ var Renderable$1 = Renderable;
782
+
783
+ export { Renderable$1 as default };