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.
@@ -8,7 +8,15 @@ import { Sprite } from "./Sprite.mjs";
8
8
  const hiddenCanvas = ACL.document.createElement("canvas");
9
9
  const hiddenContext = hiddenCanvas.getContext("2d");
10
10
 
11
+ /**
12
+ * Represents a text sprite.
13
+ * @extends Sprite
14
+ */
11
15
  export class TextSprite extends Sprite {
16
+ /**
17
+ * Creates a new TextSprite.
18
+ * @param {string} [text=""] The text to display.
19
+ */
12
20
  constructor(text = "") {
13
21
  super();
14
22
  this._text = text;
@@ -24,6 +32,10 @@ export class TextSprite extends Sprite {
24
32
  this._updateFont();
25
33
  }
26
34
 
35
+ /**
36
+ * Renders the text sprite.
37
+ * @param {CanvasRenderingContext2D} context The rendering context.
38
+ */
27
39
  render(context) {
28
40
  if (Sprite.prototype.render.call(this, context)) {
29
41
  context.fillStyle = this.fontColor;
@@ -37,7 +49,12 @@ export class TextSprite extends Sprite {
37
49
  context.shadowBlur = this.shadowBlur;
38
50
  }
39
51
 
40
- context.fillText(this._text, this.left + this.scene.x, this.bottom + this.scene.y, this.width);
52
+ context.fillText(
53
+ this._text,
54
+ this.left + this.scene.x,
55
+ this.bottom + this.scene.y,
56
+ this.width
57
+ );
41
58
 
42
59
  if (this.shadowColor) {
43
60
  context.shadowColor = null;
@@ -49,24 +66,44 @@ export class TextSprite extends Sprite {
49
66
  if (this.strokeStyle) {
50
67
  context.lineWidth = this.lineWidth;
51
68
  context.strokeStyle = this.strokeStyle;
52
- context.strokeText(this._text, this.left + this.scene.x, this.bottom + this.scene.y, this.width);
69
+ context.strokeText(
70
+ this._text,
71
+ this.left + this.scene.x,
72
+ this.bottom + this.scene.y,
73
+ this.width
74
+ );
53
75
  context.lineWidth = 0;
54
76
  context.strokeStyle = null;
55
77
  }
56
78
  }
57
79
  }
58
80
 
81
+ /**
82
+ * Sets the font color.
83
+ * @param {string} fontColor The font color.
84
+ * @returns {TextSprite} This text sprite.
85
+ */
59
86
  setFontColor(fontColor) {
60
87
  this.fontColor = fontColor;
61
88
  return this;
62
89
  }
63
90
 
91
+ /**
92
+ * Sets the font family.
93
+ * @param {string} fontFamily The font family.
94
+ * @returns {TextSprite} This text sprite.
95
+ */
64
96
  setFontFamily(fontFamily) {
65
97
  this._fontFamily = fontFamily;
66
98
  this._updateFont();
67
99
  return this;
68
100
  }
69
101
 
102
+ /**
103
+ * Sets the font size.
104
+ * @param {number} fontSize The font size.
105
+ * @returns {TextSprite} This text sprite.
106
+ */
70
107
  setFontSize(fontSize) {
71
108
  this._fontSize = fontSize;
72
109
  this.height = fontSize;
@@ -74,66 +111,129 @@ export class TextSprite extends Sprite {
74
111
  return this;
75
112
  }
76
113
 
114
+ /**
115
+ * Sets the line width.
116
+ * @param {number} lineWidth The line width.
117
+ * @returns {TextSprite} This text sprite.
118
+ */
77
119
  setLineWidth(lineWidth) {
78
120
  this.lineWidth = lineWidth;
79
121
  return this;
80
122
  }
81
123
 
124
+ /**
125
+ * Sets the shadow blur.
126
+ * @param {number} shadowBlur The shadow blur.
127
+ * @returns {TextSprite} This text sprite.
128
+ */
82
129
  setShadowBlur(shadowBlur) {
83
130
  this.shadowBlur = shadowBlur;
84
131
  return this;
85
132
  }
86
133
 
134
+ /**
135
+ * Sets the shadow color.
136
+ * @param {string} shadowColor The shadow color.
137
+ * @returns {TextSprite} This text sprite.
138
+ */
87
139
  setShadowColor(shadowColor) {
88
140
  this.shadowColor = shadowColor;
89
141
  return this;
90
142
  }
91
143
 
144
+ /**
145
+ * Sets the shadow offset x.
146
+ * @param {number} shadowOffsetX The shadow offset x.
147
+ * @returns {TextSprite} This text sprite.
148
+ */
92
149
  setShadowOffsetX(shadowOffsetX) {
93
150
  this.shadowOffsetX = shadowOffsetX;
94
151
  return this;
95
152
  }
96
153
 
154
+ /**
155
+ * Sets the shadow offset y.
156
+ * @param {number} shadowOffsetY The shadow offset y.
157
+ * @returns {TextSprite} This text sprite.
158
+ */
97
159
  setShadowOffsetY(shadowOffsetY) {
98
160
  this.shadowOffsetY = shadowOffsetY;
99
161
  return this;
100
162
  }
101
163
 
164
+ /**
165
+ * Sets the stroke style.
166
+ * @param {string} strokeStyle The stroke style.
167
+ * @returns {TextSprite} This text sprite.
168
+ */
102
169
  setStrokeStyle(strokeStyle) {
103
170
  this.strokeStyle = strokeStyle;
104
171
  return this;
105
172
  }
106
173
 
174
+ /**
175
+ * Sets the text.
176
+ * @param {string} text The text.
177
+ * @returns {TextSprite} This text sprite.
178
+ */
107
179
  setText(text) {
108
180
  this._text = text;
109
181
  this._updateFont();
110
182
  return this;
111
183
  }
112
184
 
185
+ /**
186
+ * The font family.
187
+ * @type {string}
188
+ */
113
189
  get fontFamily() {
114
190
  return this._fontFamily;
115
191
  }
116
192
 
193
+ /**
194
+ * The font size.
195
+ * @type {number}
196
+ */
117
197
  get fontSize() {
118
198
  return this._fontSize;
119
199
  }
120
200
 
201
+ /**
202
+ * The text.
203
+ * @type {string}
204
+ */
121
205
  get text() {
122
206
  return this._text;
123
207
  }
124
208
 
209
+ /**
210
+ * The font family.
211
+ * @type {string}
212
+ */
125
213
  set fontFamily(fontFamily) {
126
214
  this.setFontFamily(fontFamily);
127
215
  }
128
216
 
217
+ /**
218
+ * The font size.
219
+ * @type {number}
220
+ */
129
221
  set fontSize(fontSize) {
130
222
  this.setFontSize(fontSize);
131
223
  }
132
224
 
225
+ /**
226
+ * The text.
227
+ * @type {string}
228
+ */
133
229
  set text(text) {
134
230
  this.setText(text);
135
231
  }
136
232
 
233
+ /**
234
+ * Updates the font.
235
+ * @private
236
+ */
137
237
  _updateFont() {
138
238
  this._font = `${this._fontSize}px ${this._fontFamily}`;
139
239
  hiddenContext.textBaseline = "top";
package/src/Touch.mjs CHANGED
@@ -9,22 +9,34 @@ export class Touch extends Point {
9
9
  this._isDown = true;
10
10
  this.updateCoordinates(event);
11
11
 
12
- addEventListener("touchend", (event) => {
13
- event.preventDefault();
14
- this._isDown = false;
15
- this.updateCoordinates(event);
16
- }, false);
12
+ addEventListener(
13
+ "touchend",
14
+ (event) => {
15
+ event.preventDefault();
16
+ this._isDown = false;
17
+ this.updateCoordinates(event);
18
+ },
19
+ false
20
+ );
17
21
 
18
- addEventListener("touchmove", (event) => {
19
- event.preventDefault();
20
- this.updateCoordinates(event);
21
- }, false);
22
+ addEventListener(
23
+ "touchmove",
24
+ (event) => {
25
+ event.preventDefault();
26
+ this.updateCoordinates(event);
27
+ },
28
+ false
29
+ );
22
30
 
23
- addEventListener("touchstart", () => {
24
- event.preventDefault();
25
- this._isDown = true;
26
- this.updateCoordinates(event);
27
- }, false);
31
+ addEventListener(
32
+ "touchstart",
33
+ () => {
34
+ event.preventDefault();
35
+ this._isDown = true;
36
+ this.updateCoordinates(event);
37
+ },
38
+ false
39
+ );
28
40
  }
29
41
 
30
42
  get command() {
@@ -2,7 +2,7 @@
2
2
 
3
3
  import { Sprite } from "../Sprite.mjs";
4
4
 
5
- export class BaseTile extends Sprite {
5
+ export class BaseTile extends Sprite {
6
6
  constructor(id) {
7
7
  super();
8
8
  this.setImage(id);
@@ -2,7 +2,7 @@
2
2
 
3
3
  import { Sprite } from "../Sprite.mjs";
4
4
 
5
- export class ClickableSprite extends Sprite {
5
+ export class ClickableSprite extends Sprite {
6
6
  constructor() {
7
7
  super();
8
8
  this.addTag("clickable");
@@ -21,4 +21,3 @@ export class ClickableSprite extends Sprite {
21
21
  // no default behavior
22
22
  }
23
23
  }
24
-
@@ -4,7 +4,7 @@ import { Command } from "../Command.mjs";
4
4
  import { Core2D } from "../Core2D.mjs";
5
5
  import { Sprite } from "../Sprite.mjs";
6
6
 
7
- export class ControllableSprite extends Sprite {
7
+ export class ControllableSprite extends Sprite {
8
8
  constructor() {
9
9
  super();
10
10
  this.controller = Core2D.getController();
@@ -21,7 +21,10 @@ export class ControllableSprite extends Sprite {
21
21
  this.x -= this.step;
22
22
  }
23
23
 
24
- if (this.controller.keyDown(Command.RIGHT) && this.right < this.scene.width) {
24
+ if (
25
+ this.controller.keyDown(Command.RIGHT) &&
26
+ this.right < this.scene.width
27
+ ) {
25
28
  this.x += this.step;
26
29
  }
27
30
 
@@ -29,7 +32,10 @@ export class ControllableSprite extends Sprite {
29
32
  this.y -= this.step;
30
33
  }
31
34
 
32
- if (this.controller.keyDown(Command.DOWN) && this.bottom < this.scene.height) {
35
+ if (
36
+ this.controller.keyDown(Command.DOWN) &&
37
+ this.bottom < this.scene.height
38
+ ) {
33
39
  this.y += this.step;
34
40
  }
35
41
 
@@ -4,7 +4,7 @@ import { Core2D } from "../Core2D.mjs";
4
4
  import { Rect } from "../Rect.mjs";
5
5
  import { Sprite } from "../Sprite.mjs";
6
6
 
7
- export class CursorSprite extends Sprite {
7
+ export class CursorSprite extends Sprite {
8
8
  constructor() {
9
9
  super();
10
10
  this.hovering = null;
@@ -4,14 +4,14 @@ import { Sprite } from "../Sprite.mjs";
4
4
 
5
5
  export class Fog extends Sprite {
6
6
  init() {
7
- this.scene.add(new FogLayer()
8
- .setImageId("fogSprite0")
9
- .setSpeedX(-1));
7
+ this.scene.add(new FogLayer().setImageId("fogSprite0").setSpeedX(-1));
10
8
 
11
- this.scene.add(new FogLayer()
12
- .setImageId("fogSprite1")
13
- .setSpeedX(1)
14
- .setRight(this.scene.right));
9
+ this.scene.add(
10
+ new FogLayer()
11
+ .setImageId("fogSprite1")
12
+ .setSpeedX(1)
13
+ .setRight(this.scene.right)
14
+ );
15
15
  }
16
16
  }
17
17
 
@@ -23,7 +23,10 @@ class FogLayer extends Sprite {
23
23
  }
24
24
 
25
25
  update() {
26
- if (this.speedX < 0 && this.right == this.scene.right || this.speedX > 0 && this.left == this.scene.left) {
26
+ if (
27
+ (this.speedX < 0 && this.right == this.scene.right) ||
28
+ (this.speedX > 0 && this.left == this.scene.left)
29
+ ) {
27
30
  const SIGNAL = this.speedX / Math.abs(this.speedX);
28
31
  this.x -= this.scene.width * SIGNAL;
29
32
  }
@@ -52,7 +52,13 @@ export class FontSprite extends Sprite {
52
52
  const IMAGE = Core2D.image(character + this.font + "Font");
53
53
 
54
54
  if (context) {
55
- context.drawImage(IMAGE, this.x + this.scene.x + x, this.y + this.scene.y + y, IMAGE.width, IMAGE.height);
55
+ context.drawImage(
56
+ IMAGE,
57
+ this.x + this.scene.x + x,
58
+ this.y + this.scene.y + y,
59
+ IMAGE.width,
60
+ IMAGE.height
61
+ );
56
62
  }
57
63
 
58
64
  x += IMAGE.width + SPACING;
@@ -71,4 +77,3 @@ export class FontSprite extends Sprite {
71
77
  this.setHeight(y + height);
72
78
  }
73
79
  }
74
-
@@ -140,4 +140,3 @@ export class JumperSprite extends Sprite {
140
140
  return sprite;
141
141
  }
142
142
  }
143
-
@@ -31,4 +31,3 @@ export class RandomRectTransition extends Sprite {
31
31
  return Sprite.prototype.sync.call(this);
32
32
  }
33
33
  }
34
-
@@ -42,14 +42,16 @@ export class Starfield extends Sprite {
42
42
  for (let i = 0; i < STAR_DENSITY; ++i) {
43
43
  const SIZE = 1 + Core2D.random(1);
44
44
 
45
- this.scene.add(new Star()
46
- .setColor(this.colors[Core2D.random(this.colors.length - 1)])
47
- .setX(Core2D.random(this.scene.width))
48
- .setY(Core2D.random(this.scene.height))
49
- .setWidth(SIZE)
50
- .setHeight(SIZE)
51
- .setSpeedX(this.speedX * SIZE * SPEED_SIZE_RATIO)
52
- .setSpeedY(this.speedY * SIZE * SPEED_SIZE_RATIO));
45
+ this.scene.add(
46
+ new Star()
47
+ .setColor(this.colors[Core2D.random(this.colors.length - 1)])
48
+ .setX(Core2D.random(this.scene.width))
49
+ .setY(Core2D.random(this.scene.height))
50
+ .setWidth(SIZE)
51
+ .setHeight(SIZE)
52
+ .setSpeedX(this.speedX * SIZE * SPEED_SIZE_RATIO)
53
+ .setSpeedY(this.speedY * SIZE * SPEED_SIZE_RATIO)
54
+ );
53
55
  }
54
56
  }
55
57