core2d 2.10.3 → 2.11.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +74 -0
- package/package.json +8 -3
- package/src/ACL.mjs +3 -3
- package/src/Animation.mjs +38 -1
- package/src/Axis.mjs +1 -6
- package/src/ButtonLayout.mjs +1 -1
- package/src/ButtonLayoutMap.mjs +1 -1
- package/src/Controller.mjs +36 -1
- package/src/Core2D.mjs +150 -1
- package/src/Engine.mjs +13 -10
- package/src/Frame.mjs +24 -0
- package/src/GamePad.mjs +8 -4
- package/src/Input.mjs +5 -2
- package/src/Key.mjs +1 -1
- package/src/Keyboard.mjs +3 -3
- package/src/Mouse.mjs +23 -11
- package/src/Point.mjs +36 -0
- package/src/Pointer.mjs +24 -2
- package/src/Rect.mjs +125 -3
- package/src/Scene.mjs +44 -3
- package/src/Sound.mjs +4 -0
- package/src/Sprite.mjs +334 -8
- package/src/Static.mjs +3 -3
- package/src/TextSprite.mjs +102 -2
- package/src/Touch.mjs +26 -14
- package/src/plugin/BaseTile.mjs +1 -1
- package/src/plugin/ClickableSprite.mjs +1 -2
- package/src/plugin/ControllableSprite.mjs +9 -3
- package/src/plugin/CursorSprite.mjs +1 -1
- package/src/plugin/Fog.mjs +11 -8
- package/src/plugin/FontSprite.mjs +7 -2
- package/src/plugin/JumperSprite.mjs +0 -1
- package/src/plugin/RandomRectTransition.mjs +0 -1
- package/src/plugin/Starfield.mjs +10 -8
package/src/Engine.mjs
CHANGED
|
@@ -29,13 +29,11 @@ export const Engine = (() => {
|
|
|
29
29
|
},
|
|
30
30
|
};
|
|
31
31
|
|
|
32
|
-
const RENDER_STRATEGIES = [
|
|
33
|
-
RENDER_STRATEGY.DEFAULT,
|
|
34
|
-
RENDER_STRATEGY.DEGRADED,
|
|
35
|
-
];
|
|
32
|
+
const RENDER_STRATEGIES = [RENDER_STRATEGY.DEFAULT, RENDER_STRATEGY.DEGRADED];
|
|
36
33
|
|
|
37
34
|
let _autoScale = true;
|
|
38
|
-
let _canvas =
|
|
35
|
+
let _canvas =
|
|
36
|
+
Static.getElement("app") || Static.getElements(CANVAS_ELEMENT)[0];
|
|
39
37
|
let _context = _canvas.getContext(CONTEXT);
|
|
40
38
|
let _degraded = 0;
|
|
41
39
|
let _everyOther = true;
|
|
@@ -126,7 +124,11 @@ export const Engine = (() => {
|
|
|
126
124
|
_context.clearRect(0, 0, _width, _height);
|
|
127
125
|
}
|
|
128
126
|
|
|
129
|
-
static colorize(
|
|
127
|
+
static colorize(
|
|
128
|
+
image,
|
|
129
|
+
fillStyle,
|
|
130
|
+
compositeOperation = CompositeOperations.SOURCE_IN
|
|
131
|
+
) {
|
|
130
132
|
const input = Static.getImage(image);
|
|
131
133
|
const output = ACL.document.createElement(CANVAS_ELEMENT);
|
|
132
134
|
output.width = input.width;
|
|
@@ -210,7 +212,7 @@ export const Engine = (() => {
|
|
|
210
212
|
const input = Static.getImage(image);
|
|
211
213
|
degrees = degrees % 360;
|
|
212
214
|
|
|
213
|
-
if (degrees == 0
|
|
215
|
+
if (degrees == 0) {
|
|
214
216
|
return input;
|
|
215
217
|
}
|
|
216
218
|
|
|
@@ -226,7 +228,8 @@ export const Engine = (() => {
|
|
|
226
228
|
|
|
227
229
|
static save(data, namespace) {
|
|
228
230
|
try {
|
|
229
|
-
localStorage[namespace || Engine.getName()] =
|
|
231
|
+
localStorage[namespace || Engine.getName()] =
|
|
232
|
+
data && JSON.stringify(data);
|
|
230
233
|
} catch (error) {
|
|
231
234
|
console.log("Could not save current game: " + error);
|
|
232
235
|
}
|
|
@@ -339,7 +342,7 @@ export const Engine = (() => {
|
|
|
339
342
|
}
|
|
340
343
|
|
|
341
344
|
context.fillStyle = Color.Blue;
|
|
342
|
-
context.fillRect(0, 0, canvas.width * complete / total, canvas.height);
|
|
345
|
+
context.fillRect(0, 0, (canvas.width * complete) / total, canvas.height);
|
|
343
346
|
|
|
344
347
|
if (complete < total) {
|
|
345
348
|
setTimeout(() => {
|
|
@@ -382,7 +385,7 @@ export const Engine = (() => {
|
|
|
382
385
|
output.height = input.height;
|
|
383
386
|
const context = output.getContext(CONTEXT);
|
|
384
387
|
context.translate(isMirror ? output.width : 0, isFlip ? output.height : 0);
|
|
385
|
-
context.scale(isMirror ? -1 : 1, isFlip ? -
|
|
388
|
+
context.scale(isMirror ? -1 : 1, isFlip ? -1 : 1);
|
|
386
389
|
context.drawImage(input, 0, 0);
|
|
387
390
|
return output;
|
|
388
391
|
}
|
package/src/Frame.mjs
CHANGED
|
@@ -2,24 +2,48 @@
|
|
|
2
2
|
|
|
3
3
|
import { Static } from "./Static.mjs";
|
|
4
4
|
|
|
5
|
+
/**
|
|
6
|
+
* Represents a frame in an animation.
|
|
7
|
+
*/
|
|
5
8
|
export class Frame {
|
|
9
|
+
/**
|
|
10
|
+
* Creates a new Frame.
|
|
11
|
+
* @param {HTMLImageElement|HTMLCanvasElement} image The image of the frame.
|
|
12
|
+
* @param {number} [duration=0] The duration of the frame in ticks.
|
|
13
|
+
*/
|
|
6
14
|
constructor(image, duration = 0) {
|
|
7
15
|
this._image = Static.getImage(image);
|
|
8
16
|
this._duration = duration;
|
|
9
17
|
}
|
|
10
18
|
|
|
19
|
+
/**
|
|
20
|
+
* The image of the frame.
|
|
21
|
+
* @type {HTMLImageElement|HTMLCanvasElement}
|
|
22
|
+
*/
|
|
11
23
|
get image() {
|
|
12
24
|
return this._image;
|
|
13
25
|
}
|
|
14
26
|
|
|
27
|
+
/**
|
|
28
|
+
* The duration of the frame in ticks.
|
|
29
|
+
* @type {number}
|
|
30
|
+
*/
|
|
15
31
|
get duration() {
|
|
16
32
|
return this._duration;
|
|
17
33
|
}
|
|
18
34
|
|
|
35
|
+
/**
|
|
36
|
+
* The width of the frame.
|
|
37
|
+
* @type {number}
|
|
38
|
+
*/
|
|
19
39
|
get width() {
|
|
20
40
|
return this._image.width;
|
|
21
41
|
}
|
|
22
42
|
|
|
43
|
+
/**
|
|
44
|
+
* The height of the frame.
|
|
45
|
+
* @type {number}
|
|
46
|
+
*/
|
|
23
47
|
get height() {
|
|
24
48
|
return this._image.height;
|
|
25
49
|
}
|
package/src/GamePad.mjs
CHANGED
|
@@ -25,15 +25,19 @@ export class GamePad {
|
|
|
25
25
|
const BUTTONS = Input.getGamePadButtons(this._id);
|
|
26
26
|
const RESULT = {};
|
|
27
27
|
|
|
28
|
-
if (Input.getGamePadAxes(this._id)[Axis.LEFT_Y] < -
|
|
28
|
+
if (Input.getGamePadAxes(this._id)[Axis.LEFT_Y] < -this.analogThreshold) {
|
|
29
29
|
RESULT[Command.UP] = true;
|
|
30
|
-
} else if (
|
|
30
|
+
} else if (
|
|
31
|
+
Input.getGamePadAxes(this._id)[Axis.LEFT_Y] > this.analogThreshold
|
|
32
|
+
) {
|
|
31
33
|
RESULT[Command.DOWN] = true;
|
|
32
34
|
}
|
|
33
35
|
|
|
34
|
-
if (Input.getGamePadAxes(this._id)[Axis.LEFT_X] < -
|
|
36
|
+
if (Input.getGamePadAxes(this._id)[Axis.LEFT_X] < -this.analogThreshold) {
|
|
35
37
|
RESULT[Command.LEFT] = true;
|
|
36
|
-
} else if (
|
|
38
|
+
} else if (
|
|
39
|
+
Input.getGamePadAxes(this._id)[Axis.LEFT_X] > this.analogThreshold
|
|
40
|
+
) {
|
|
37
41
|
RESULT[Command.RIGHT] = true;
|
|
38
42
|
}
|
|
39
43
|
|
package/src/Input.mjs
CHANGED
|
@@ -51,7 +51,7 @@ export class Input {
|
|
|
51
51
|
|
|
52
52
|
static getGamePadButtons(id) {
|
|
53
53
|
const GAMEPAD = Static.getGamepads()[id];
|
|
54
|
-
return GAMEPAD && GAMEPAD.buttons || [];
|
|
54
|
+
return (GAMEPAD && GAMEPAD.buttons) || [];
|
|
55
55
|
}
|
|
56
56
|
|
|
57
57
|
addController(device) {
|
|
@@ -72,7 +72,10 @@ export class Input {
|
|
|
72
72
|
}
|
|
73
73
|
|
|
74
74
|
checkControllerQueues() {
|
|
75
|
-
if (
|
|
75
|
+
if (
|
|
76
|
+
this._controllerRequestQueue.length > 0 &&
|
|
77
|
+
this._controllerQueue.length > 0
|
|
78
|
+
) {
|
|
76
79
|
const REQUESTER = this._controllerRequestQueue.shift();
|
|
77
80
|
const DEVICE = this._controllerQueue.shift();
|
|
78
81
|
REQUESTER.setDevice(DEVICE);
|
package/src/Key.mjs
CHANGED
package/src/Keyboard.mjs
CHANGED
|
@@ -7,8 +7,8 @@ export class Keyboard {
|
|
|
7
7
|
this._buffer = {};
|
|
8
8
|
|
|
9
9
|
this.onKey(event, true);
|
|
10
|
-
addEventListener("keydown", event => this.onKey(event, true), false);
|
|
11
|
-
addEventListener("keyup", event => this.onKey(event, false), false);
|
|
10
|
+
addEventListener("keydown", (event) => this.onKey(event, true), false);
|
|
11
|
+
addEventListener("keyup", (event) => this.onKey(event, false), false);
|
|
12
12
|
}
|
|
13
13
|
|
|
14
14
|
get commands() {
|
|
@@ -27,7 +27,7 @@ export class Keyboard {
|
|
|
27
27
|
const COMMAND = KeyMap[event.keyCode];
|
|
28
28
|
this._buffer[COMMAND] = isDown;
|
|
29
29
|
|
|
30
|
-
if (isDown && "number" == typeof
|
|
30
|
+
if (isDown && "number" == typeof COMMAND) {
|
|
31
31
|
event.preventDefault();
|
|
32
32
|
}
|
|
33
33
|
}
|
package/src/Mouse.mjs
CHANGED
|
@@ -7,19 +7,31 @@ export class Mouse extends Point {
|
|
|
7
7
|
super();
|
|
8
8
|
this.updateCoordinates(event);
|
|
9
9
|
|
|
10
|
-
addEventListener(
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
10
|
+
addEventListener(
|
|
11
|
+
"mousedown",
|
|
12
|
+
(event) => {
|
|
13
|
+
event.preventDefault();
|
|
14
|
+
this._isDown = true;
|
|
15
|
+
},
|
|
16
|
+
false
|
|
17
|
+
);
|
|
14
18
|
|
|
15
|
-
addEventListener(
|
|
16
|
-
|
|
17
|
-
|
|
19
|
+
addEventListener(
|
|
20
|
+
"mousemove",
|
|
21
|
+
(event) => {
|
|
22
|
+
this.updateCoordinates(event);
|
|
23
|
+
},
|
|
24
|
+
false
|
|
25
|
+
);
|
|
18
26
|
|
|
19
|
-
addEventListener(
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
27
|
+
addEventListener(
|
|
28
|
+
"mouseup",
|
|
29
|
+
(event) => {
|
|
30
|
+
event.preventDefault();
|
|
31
|
+
this._isDown = false;
|
|
32
|
+
},
|
|
33
|
+
false
|
|
34
|
+
);
|
|
23
35
|
}
|
|
24
36
|
|
|
25
37
|
get command() {
|
package/src/Point.mjs
CHANGED
|
@@ -1,35 +1,71 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
|
+
/**
|
|
4
|
+
* Represents a point with x and y coordinates.
|
|
5
|
+
*/
|
|
3
6
|
export class Point {
|
|
7
|
+
/**
|
|
8
|
+
* Creates a new Point.
|
|
9
|
+
* @param {number} [x=0] The x-coordinate.
|
|
10
|
+
* @param {number} [y=0] The y-coordinate.
|
|
11
|
+
*/
|
|
4
12
|
constructor(x = 0, y = 0) {
|
|
5
13
|
this.x = x;
|
|
6
14
|
this.y = y;
|
|
7
15
|
}
|
|
8
16
|
|
|
17
|
+
/**
|
|
18
|
+
* The position of the point.
|
|
19
|
+
* @type {Point}
|
|
20
|
+
*/
|
|
9
21
|
get position() {
|
|
10
22
|
return this;
|
|
11
23
|
}
|
|
12
24
|
|
|
25
|
+
/**
|
|
26
|
+
* Sets the x-coordinate of the point.
|
|
27
|
+
* @param {number} x The x-coordinate.
|
|
28
|
+
* @returns {Point} This point.
|
|
29
|
+
*/
|
|
13
30
|
setX(x) {
|
|
14
31
|
this.x = x;
|
|
15
32
|
return this;
|
|
16
33
|
}
|
|
17
34
|
|
|
35
|
+
/**
|
|
36
|
+
* Sets the y-coordinate of the point.
|
|
37
|
+
* @param {number} y The y-coordinate.
|
|
38
|
+
* @returns {Point} This point.
|
|
39
|
+
*/
|
|
18
40
|
setY(y) {
|
|
19
41
|
this.y = y;
|
|
20
42
|
return this;
|
|
21
43
|
}
|
|
22
44
|
|
|
45
|
+
/**
|
|
46
|
+
* Sets the position of the point.
|
|
47
|
+
* @param {Point} point The new position.
|
|
48
|
+
* @returns {Point} This point.
|
|
49
|
+
*/
|
|
23
50
|
setPosition(point) {
|
|
24
51
|
this.x = point.x;
|
|
25
52
|
this.y = point.y;
|
|
26
53
|
return this;
|
|
27
54
|
}
|
|
28
55
|
|
|
56
|
+
/**
|
|
57
|
+
* The position of the point.
|
|
58
|
+
* @type {Point}
|
|
59
|
+
*/
|
|
29
60
|
set position(point) {
|
|
30
61
|
this.setPosition(point);
|
|
31
62
|
}
|
|
32
63
|
|
|
64
|
+
/**
|
|
65
|
+
* Calculates the distance to another point.
|
|
66
|
+
* @param {Point} point The other point.
|
|
67
|
+
* @returns {number} The distance to the other point.
|
|
68
|
+
*/
|
|
33
69
|
distance(point) {
|
|
34
70
|
const dx = point.x - this.x;
|
|
35
71
|
const dy = point.y - this.y;
|
package/src/Pointer.mjs
CHANGED
|
@@ -3,7 +3,14 @@
|
|
|
3
3
|
import { Engine } from "./Engine.mjs";
|
|
4
4
|
import { Point } from "./Point.mjs";
|
|
5
5
|
|
|
6
|
+
/**
|
|
7
|
+
* Represents a pointer, which can be a mouse or a touch.
|
|
8
|
+
* @extends Point
|
|
9
|
+
*/
|
|
6
10
|
export class Pointer extends Point {
|
|
11
|
+
/**
|
|
12
|
+
* Creates a new Pointer.
|
|
13
|
+
*/
|
|
7
14
|
constructor() {
|
|
8
15
|
super();
|
|
9
16
|
this._active = false;
|
|
@@ -11,18 +18,33 @@ export class Pointer extends Point {
|
|
|
11
18
|
this._hold = false;
|
|
12
19
|
}
|
|
13
20
|
|
|
21
|
+
/**
|
|
22
|
+
* Whether the pointer is down.
|
|
23
|
+
* @type {boolean}
|
|
24
|
+
*/
|
|
14
25
|
get down() {
|
|
15
26
|
return this._active;
|
|
16
27
|
}
|
|
17
28
|
|
|
29
|
+
/**
|
|
30
|
+
* Whether the pointer was just pushed.
|
|
31
|
+
* @type {boolean}
|
|
32
|
+
*/
|
|
18
33
|
get push() {
|
|
19
34
|
return this._active && !this._hold;
|
|
20
35
|
}
|
|
21
36
|
|
|
37
|
+
/**
|
|
38
|
+
* Sets the device of the pointer.
|
|
39
|
+
* @param {object} device The device.
|
|
40
|
+
*/
|
|
22
41
|
setDevice(device) {
|
|
23
42
|
this._device = device;
|
|
24
43
|
}
|
|
25
44
|
|
|
45
|
+
/**
|
|
46
|
+
* Updates the pointer.
|
|
47
|
+
*/
|
|
26
48
|
update() {
|
|
27
49
|
if (!this._device) {
|
|
28
50
|
return;
|
|
@@ -38,7 +60,7 @@ export class Pointer extends Point {
|
|
|
38
60
|
|
|
39
61
|
const REAL_X = this._device.x - Engine.offsetLeft;
|
|
40
62
|
const REAL_Y = this._device.y - Engine.offsetTop;
|
|
41
|
-
this.x = Math.floor(REAL_X * Engine.width / Engine.realWidth);
|
|
42
|
-
this.y = Math.floor(REAL_Y * Engine.height / Engine.realHeight);
|
|
63
|
+
this.x = Math.floor((REAL_X * Engine.width) / Engine.realWidth);
|
|
64
|
+
this.y = Math.floor((REAL_Y * Engine.height) / Engine.realHeight);
|
|
43
65
|
}
|
|
44
66
|
}
|
package/src/Rect.mjs
CHANGED
|
@@ -2,126 +2,248 @@
|
|
|
2
2
|
|
|
3
3
|
import { Point } from "./Point.mjs";
|
|
4
4
|
|
|
5
|
+
/**
|
|
6
|
+
* Represents a rectangle.
|
|
7
|
+
* @extends Point
|
|
8
|
+
*/
|
|
5
9
|
export class Rect extends Point {
|
|
10
|
+
/**
|
|
11
|
+
* Creates a new Rect.
|
|
12
|
+
* @param {number} x The x-coordinate of the rectangle.
|
|
13
|
+
* @param {number} y The y-coordinate of the rectangle.
|
|
14
|
+
* @param {number} [width=0] The width of the rectangle.
|
|
15
|
+
* @param {number} [height=0] The height of the rectangle.
|
|
16
|
+
*/
|
|
6
17
|
constructor(x, y, width = 0, height = 0) {
|
|
7
18
|
super(x, y);
|
|
8
19
|
this.width = width;
|
|
9
20
|
this.height = height;
|
|
10
21
|
}
|
|
11
22
|
|
|
23
|
+
/**
|
|
24
|
+
* The x-coordinate of the left edge of the rectangle.
|
|
25
|
+
* @type {number}
|
|
26
|
+
*/
|
|
12
27
|
get left() {
|
|
13
28
|
return this.x;
|
|
14
29
|
}
|
|
15
30
|
|
|
31
|
+
/**
|
|
32
|
+
* The x-coordinate of the right edge of the rectangle.
|
|
33
|
+
* @type {number}
|
|
34
|
+
*/
|
|
16
35
|
get right() {
|
|
17
36
|
return this.x + this.width - 1;
|
|
18
37
|
}
|
|
19
38
|
|
|
39
|
+
/**
|
|
40
|
+
* The y-coordinate of the top edge of the rectangle.
|
|
41
|
+
* @type {number}
|
|
42
|
+
*/
|
|
20
43
|
get top() {
|
|
21
44
|
return this.y;
|
|
22
45
|
}
|
|
23
46
|
|
|
47
|
+
/**
|
|
48
|
+
* The y-coordinate of the bottom edge of the rectangle.
|
|
49
|
+
* @type {number}
|
|
50
|
+
*/
|
|
24
51
|
get bottom() {
|
|
25
52
|
return this.y + this.height - 1;
|
|
26
53
|
}
|
|
27
54
|
|
|
55
|
+
/**
|
|
56
|
+
* The x-coordinate of the center of the rectangle.
|
|
57
|
+
* @type {number}
|
|
58
|
+
*/
|
|
28
59
|
get centerX() {
|
|
29
60
|
return this.x + Math.floor(this.width / 2);
|
|
30
61
|
}
|
|
31
62
|
|
|
63
|
+
/**
|
|
64
|
+
* The y-coordinate of the center of the rectangle.
|
|
65
|
+
* @type {number}
|
|
66
|
+
*/
|
|
32
67
|
get centerY() {
|
|
33
68
|
return this.y + Math.floor(this.height / 2);
|
|
34
69
|
}
|
|
35
70
|
|
|
71
|
+
/**
|
|
72
|
+
* The center of the rectangle.
|
|
73
|
+
* @type {Point}
|
|
74
|
+
*/
|
|
36
75
|
get center() {
|
|
37
76
|
return new Point(this.centerX, this.centerY);
|
|
38
77
|
}
|
|
39
78
|
|
|
79
|
+
/**
|
|
80
|
+
* Sets the width of the rectangle.
|
|
81
|
+
* @param {number} width The new width.
|
|
82
|
+
* @returns {Rect} This rectangle.
|
|
83
|
+
*/
|
|
40
84
|
setWidth(width) {
|
|
41
85
|
this.width = width;
|
|
42
86
|
return this;
|
|
43
87
|
}
|
|
44
88
|
|
|
89
|
+
/**
|
|
90
|
+
* Sets the height of the rectangle.
|
|
91
|
+
* @param {number} height The new height.
|
|
92
|
+
* @returns {Rect} This rectangle.
|
|
93
|
+
*/
|
|
45
94
|
setHeight(height) {
|
|
46
95
|
this.height = height;
|
|
47
96
|
return this;
|
|
48
97
|
}
|
|
49
98
|
|
|
99
|
+
/**
|
|
100
|
+
* Sets the size of the rectangle.
|
|
101
|
+
* @param {Rect} rect The new size.
|
|
102
|
+
* @returns {Rect} This rectangle.
|
|
103
|
+
*/
|
|
50
104
|
setSize(rect) {
|
|
51
105
|
this.setWidth(rect.width);
|
|
52
106
|
this.setHeight(rect.height);
|
|
53
107
|
return this;
|
|
54
108
|
}
|
|
55
109
|
|
|
110
|
+
/**
|
|
111
|
+
* Sets the x-coordinate of the left edge of the rectangle.
|
|
112
|
+
* @param {number} x The new x-coordinate.
|
|
113
|
+
* @returns {Rect} This rectangle.
|
|
114
|
+
*/
|
|
56
115
|
setLeft(x) {
|
|
57
116
|
this.x = x;
|
|
58
117
|
return this;
|
|
59
118
|
}
|
|
60
119
|
|
|
120
|
+
/**
|
|
121
|
+
* Sets the x-coordinate of the right edge of the rectangle.
|
|
122
|
+
* @param {number} x The new x-coordinate.
|
|
123
|
+
* @returns {Rect} This rectangle.
|
|
124
|
+
*/
|
|
61
125
|
setRight(x) {
|
|
62
126
|
this.x = x - this.width + 1;
|
|
63
127
|
return this;
|
|
64
128
|
}
|
|
65
129
|
|
|
130
|
+
/**
|
|
131
|
+
* Sets the y-coordinate of the top edge of the rectangle.
|
|
132
|
+
* @param {number} y The new y-coordinate.
|
|
133
|
+
* @returns {Rect} This rectangle.
|
|
134
|
+
*/
|
|
66
135
|
setTop(y) {
|
|
67
136
|
this.y = y;
|
|
68
137
|
return this;
|
|
69
138
|
}
|
|
70
139
|
|
|
140
|
+
/**
|
|
141
|
+
* Sets the y-coordinate of the bottom edge of the rectangle.
|
|
142
|
+
* @param {number} y The new y-coordinate.
|
|
143
|
+
* @returns {Rect} This rectangle.
|
|
144
|
+
*/
|
|
71
145
|
setBottom(y) {
|
|
72
146
|
this.y = y - this.height + 1;
|
|
73
147
|
return this;
|
|
74
148
|
}
|
|
75
149
|
|
|
150
|
+
/**
|
|
151
|
+
* Sets the x-coordinate of the center of the rectangle.
|
|
152
|
+
* @param {number} x The new x-coordinate.
|
|
153
|
+
* @returns {Rect} This rectangle.
|
|
154
|
+
*/
|
|
76
155
|
setCenterX(x) {
|
|
77
156
|
this.x = x - Math.floor(this.width / 2);
|
|
78
157
|
return this;
|
|
79
158
|
}
|
|
80
159
|
|
|
160
|
+
/**
|
|
161
|
+
* Sets the y-coordinate of the center of the rectangle.
|
|
162
|
+
* @param {number} y The new y-coordinate.
|
|
163
|
+
* @returns {Rect} This rectangle.
|
|
164
|
+
*/
|
|
81
165
|
setCenterY(y) {
|
|
82
166
|
this.y = y - Math.floor(this.height / 2);
|
|
83
167
|
return this;
|
|
84
168
|
}
|
|
85
169
|
|
|
170
|
+
/**
|
|
171
|
+
* Sets the center of the rectangle.
|
|
172
|
+
* @param {Point} point The new center.
|
|
173
|
+
* @returns {Rect} This rectangle.
|
|
174
|
+
*/
|
|
86
175
|
setCenter(point) {
|
|
87
176
|
this.setCenterX(point.x);
|
|
88
177
|
this.setCenterY(point.y);
|
|
89
178
|
return this;
|
|
90
179
|
}
|
|
91
180
|
|
|
181
|
+
/**
|
|
182
|
+
* The x-coordinate of the left edge of the rectangle.
|
|
183
|
+
* @type {number}
|
|
184
|
+
*/
|
|
92
185
|
set left(x) {
|
|
93
186
|
this.setLeft(x);
|
|
94
187
|
}
|
|
95
188
|
|
|
189
|
+
/**
|
|
190
|
+
* The x-coordinate of the right edge of the rectangle.
|
|
191
|
+
* @type {number}
|
|
192
|
+
*/
|
|
96
193
|
set right(x) {
|
|
97
194
|
this.setRight(x);
|
|
98
195
|
}
|
|
99
196
|
|
|
197
|
+
/**
|
|
198
|
+
* The y-coordinate of the top edge of the rectangle.
|
|
199
|
+
* @type {number}
|
|
200
|
+
*/
|
|
100
201
|
set top(y) {
|
|
101
202
|
this.setTop(y);
|
|
102
203
|
}
|
|
103
204
|
|
|
205
|
+
/**
|
|
206
|
+
* The y-coordinate of the bottom edge of the rectangle.
|
|
207
|
+
* @type {number}
|
|
208
|
+
*/
|
|
104
209
|
set bottom(y) {
|
|
105
210
|
this.setBottom(y);
|
|
106
211
|
}
|
|
107
212
|
|
|
213
|
+
/**
|
|
214
|
+
* The x-coordinate of the center of the rectangle.
|
|
215
|
+
* @type {number}
|
|
216
|
+
*/
|
|
108
217
|
set centerX(x) {
|
|
109
218
|
this.setCenterX(x);
|
|
110
219
|
}
|
|
111
220
|
|
|
221
|
+
/**
|
|
222
|
+
* The y-coordinate of the center of the rectangle.
|
|
223
|
+
* @type {number}
|
|
224
|
+
*/
|
|
112
225
|
set centerY(y) {
|
|
113
226
|
this.setCenterY(y);
|
|
114
227
|
}
|
|
115
228
|
|
|
229
|
+
/**
|
|
230
|
+
* The center of the rectangle.
|
|
231
|
+
* @type {Point}
|
|
232
|
+
*/
|
|
116
233
|
set center(point) {
|
|
117
234
|
this.setCenter(point);
|
|
118
235
|
}
|
|
119
236
|
|
|
237
|
+
/**
|
|
238
|
+
* Creates a new rectangle that is the union of this rectangle and another rectangle.
|
|
239
|
+
* @param {Rect} rect The other rectangle.
|
|
240
|
+
* @returns {Rect} The new rectangle.
|
|
241
|
+
*/
|
|
120
242
|
makeUnion(rect) {
|
|
121
243
|
const RECT = new Rect(Math.min(this.x, rect.x), Math.min(this.y, rect.y));
|
|
122
244
|
|
|
123
|
-
return RECT
|
|
124
|
-
|
|
125
|
-
|
|
245
|
+
return RECT.setWidth(
|
|
246
|
+
Math.max(this.right, rect.right) - RECT.x + 1
|
|
247
|
+
).setHeight(Math.max(this.bottom, rect.bottom) - RECT.y + 1);
|
|
126
248
|
}
|
|
127
249
|
}
|
package/src/Scene.mjs
CHANGED
|
@@ -4,7 +4,14 @@ import { Engine } from "./Engine.mjs";
|
|
|
4
4
|
import { Sprite } from "./Sprite.mjs";
|
|
5
5
|
import { Static } from "./Static.mjs";
|
|
6
6
|
|
|
7
|
+
/**
|
|
8
|
+
* Represents a scene, which is a collection of sprites that are rendered together.
|
|
9
|
+
* @extends Sprite
|
|
10
|
+
*/
|
|
7
11
|
export class Scene extends Sprite {
|
|
12
|
+
/**
|
|
13
|
+
* Creates a new Scene.
|
|
14
|
+
*/
|
|
8
15
|
constructor() {
|
|
9
16
|
super();
|
|
10
17
|
this.height = Engine.height;
|
|
@@ -13,10 +20,18 @@ export class Scene extends Sprite {
|
|
|
13
20
|
this._spritesQueue = [];
|
|
14
21
|
}
|
|
15
22
|
|
|
23
|
+
/**
|
|
24
|
+
* Initializes the scene.
|
|
25
|
+
*/
|
|
16
26
|
init() {
|
|
17
27
|
// no default behavior
|
|
18
28
|
}
|
|
19
29
|
|
|
30
|
+
/**
|
|
31
|
+
* Adds a sprite to the scene.
|
|
32
|
+
* @param {Sprite} sprite The sprite to add.
|
|
33
|
+
* @returns {Scene} This scene.
|
|
34
|
+
*/
|
|
20
35
|
add(sprite) {
|
|
21
36
|
this._spritesQueue.push(sprite);
|
|
22
37
|
sprite.scene = this;
|
|
@@ -26,10 +41,22 @@ export class Scene extends Sprite {
|
|
|
26
41
|
return this;
|
|
27
42
|
}
|
|
28
43
|
|
|
44
|
+
/**
|
|
45
|
+
* Builds a tilemap from a map.
|
|
46
|
+
* @param {string[][]} map The map.
|
|
47
|
+
* @param {function(string): Sprite} [tileFactory=null] A function that creates a tile from an ID.
|
|
48
|
+
* @param {number} [offsetX=0] The horizontal offset between tiles.
|
|
49
|
+
* @param {number} [offsetY=0] The vertical offset between tiles.
|
|
50
|
+
* @param {number} [x=0] The horizontal position of the tilemap.
|
|
51
|
+
* @param {number} [y=0] The vertical position of the tilemap.
|
|
52
|
+
* @returns {Scene} This scene.
|
|
53
|
+
*/
|
|
29
54
|
build(map, tileFactory = null, offsetX = 0, offsetY = 0, x = 0, y = 0) {
|
|
30
|
-
tileFactory =
|
|
31
|
-
|
|
32
|
-
|
|
55
|
+
tileFactory =
|
|
56
|
+
tileFactory ??
|
|
57
|
+
function baseTileFactory(id) {
|
|
58
|
+
return new Sprite().addTag("tile").setImage(id);
|
|
59
|
+
};
|
|
33
60
|
|
|
34
61
|
for (let i = 0; i < map.length; ++i) {
|
|
35
62
|
const LINE = map[i];
|
|
@@ -56,6 +83,10 @@ export class Scene extends Sprite {
|
|
|
56
83
|
return this;
|
|
57
84
|
}
|
|
58
85
|
|
|
86
|
+
/**
|
|
87
|
+
* Synchronizes the scene.
|
|
88
|
+
* @returns {boolean} Whether the scene has expired.
|
|
89
|
+
*/
|
|
59
90
|
sync() {
|
|
60
91
|
Engine.paint(this, this.layerIndex);
|
|
61
92
|
let sprites = [];
|
|
@@ -86,6 +117,11 @@ export class Scene extends Sprite {
|
|
|
86
117
|
return Sprite.prototype.sync.call(this);
|
|
87
118
|
}
|
|
88
119
|
|
|
120
|
+
/**
|
|
121
|
+
* Gets all sprites with a specific tag.
|
|
122
|
+
* @param {string} tag The tag.
|
|
123
|
+
* @returns {Sprite[]} The sprites with the tag.
|
|
124
|
+
*/
|
|
89
125
|
getObjectsWithTag(tag) {
|
|
90
126
|
const RESULT = [];
|
|
91
127
|
const SPRITES = this._sprites.concat(this._spritesQueue);
|
|
@@ -101,6 +137,11 @@ export class Scene extends Sprite {
|
|
|
101
137
|
return RESULT;
|
|
102
138
|
}
|
|
103
139
|
|
|
140
|
+
/**
|
|
141
|
+
* Sets the transition to the next scene.
|
|
142
|
+
* @param {Transition} transition The transition.
|
|
143
|
+
* @returns {Scene} This scene.
|
|
144
|
+
*/
|
|
104
145
|
setTransition(transition) {
|
|
105
146
|
this.transition = transition;
|
|
106
147
|
return this;
|