core2d 2.10.4 → 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/src/Sound.mjs CHANGED
@@ -27,6 +27,10 @@ export class Sound {
27
27
  mute() {
28
28
  this._isMute = !this._isMute;
29
29
 
30
+ if (!this._theme) {
31
+ return;
32
+ }
33
+
30
34
  if (!this._isMute) {
31
35
  this._theme.play();
32
36
  } else {
package/src/Sprite.mjs CHANGED
@@ -6,37 +6,169 @@ import { Frame } from "./Frame.mjs";
6
6
  import { Rect } from "./Rect.mjs";
7
7
  import { Static } from "./Static.mjs";
8
8
 
9
+ /**
10
+ * Represents a sprite, which is a basic game object that can be rendered on the screen.
11
+ * @extends Rect
12
+ */
9
13
  export class Sprite extends Rect {
14
+ /**
15
+ * Creates a new Sprite.
16
+ */
10
17
  constructor() {
11
18
  super();
19
+
20
+ /**
21
+ * The horizontal acceleration of the sprite.
22
+ * @type {number}
23
+ */
12
24
  this.accelerationX = 0;
25
+
26
+ /**
27
+ * The vertical acceleration of the sprite.
28
+ * @type {number}
29
+ */
13
30
  this.accelerationY = 0;
31
+
32
+ /**
33
+ * The alpha transparency of the sprite.
34
+ * @type {number}
35
+ */
14
36
  this.alpha = 1;
37
+
38
+ /**
39
+ * The boundary of the sprite. If the sprite goes outside of this boundary, the `offBoundary` method is called.
40
+ * @type {Rect}
41
+ */
15
42
  this.boundary = null;
43
+
44
+ /**
45
+ * The color of the sprite.
46
+ * @type {string}
47
+ */
16
48
  this.color = null;
49
+
50
+ /**
51
+ * Whether the sprite is essential. If an essential sprite expires, the scene expires as well.
52
+ * @type {boolean}
53
+ */
17
54
  this.essential = false;
55
+
56
+ /**
57
+ * The expiration time of the sprite, in ticks.
58
+ * @type {number}
59
+ */
18
60
  this.expiration = 0;
61
+
62
+ /**
63
+ * Whether the sprite has expired.
64
+ * @type {boolean}
65
+ */
19
66
  this.expired = false;
67
+
68
+ /**
69
+ * The layer index of the sprite. Sprites with a higher layer index are rendered on top of sprites with a lower layer index.
70
+ * @type {number}
71
+ */
20
72
  this.layerIndex = 0;
73
+
74
+ /**
75
+ * The maximum horizontal speed of the sprite.
76
+ * @type {number}
77
+ */
21
78
  this.maxSpeedX = 0;
79
+
80
+ /**
81
+ * The maximum vertical speed of the sprite.
82
+ * @type {number}
83
+ */
22
84
  this.maxSpeedY = 0;
85
+
86
+ /**
87
+ * Whether the sprite is solid. Solid sprites can collide with other solid sprites.
88
+ * @type {boolean}
89
+ */
23
90
  this.solid = false;
91
+
92
+ /**
93
+ * The horizontal speed of the sprite.
94
+ * @type {number}
95
+ */
24
96
  this.speedX = 0;
97
+
98
+ /**
99
+ * The vertical speed of the sprite.
100
+ * @type {number}
101
+ */
25
102
  this.speedY = 0;
103
+
104
+ /**
105
+ * Whether the sprite is visible.
106
+ * @type {boolean}
107
+ */
26
108
  this.visible = true;
109
+
110
+ /**
111
+ * The animation of the sprite.
112
+ * @type {Animation}
113
+ * @private
114
+ */
27
115
  this._animation = null;
116
+
117
+ /**
118
+ * The last horizontal speed of the sprite.
119
+ * @type {number}
120
+ * @private
121
+ */
28
122
  this._lastSpeedX = 0;
123
+
124
+ /**
125
+ * The last vertical speed of the sprite.
126
+ * @type {number}
127
+ * @private
128
+ */
29
129
  this._lastSpeedY = 0;
130
+
131
+ /**
132
+ * The last horizontal position of the sprite.
133
+ * @type {number}
134
+ * @private
135
+ */
30
136
  this._lastX = this.x;
137
+
138
+ /**
139
+ * The last vertical position of the sprite.
140
+ * @type {number}
141
+ * @private
142
+ */
31
143
  this._lastY = this.y;
144
+
145
+ /**
146
+ * The tags of the sprite.
147
+ * @type {Object<string, boolean>}
148
+ * @private
149
+ */
32
150
  this._tags = {};
151
+
152
+ /**
153
+ * The number of ticks that have passed since the sprite was created.
154
+ * @type {number}
155
+ * @private
156
+ */
33
157
  this._tick = 0;
34
158
  }
35
159
 
160
+ /**
161
+ * The angle of the sprite in degrees.
162
+ * @type {number}
163
+ */
36
164
  get angle() {
37
165
  return Static.toDegrees(Math.atan2(this.speedY, this.speedX));
38
166
  }
39
167
 
168
+ /**
169
+ * The direction of the sprite.
170
+ * @type {Direction}
171
+ */
40
172
  get direction() {
41
173
  const DIRECTION = new Direction();
42
174
 
@@ -55,89 +187,177 @@ export class Sprite extends Rect {
55
187
  return DIRECTION;
56
188
  }
57
189
 
190
+ /**
191
+ * The image of the sprite.
192
+ * @type {HTMLImageElement|HTMLCanvasElement}
193
+ */
58
194
  get image() {
59
195
  return this._animation && this._animation.image;
60
196
  }
61
197
 
198
+ /**
199
+ * The number of ticks that have passed since the sprite was created.
200
+ * @type {number}
201
+ */
62
202
  get tick() {
63
203
  return this._tick;
64
204
  }
65
205
 
206
+ /**
207
+ * Sets the horizontal acceleration of the sprite.
208
+ * @param {number} accelerationX The horizontal acceleration.
209
+ * @returns {Sprite} This sprite.
210
+ */
66
211
  setAccelerationX(accelerationX = 0) {
67
212
  this.accelerationX = accelerationX;
68
213
  return this;
69
214
  }
70
215
 
216
+ /**
217
+ * Sets the vertical acceleration of the sprite.
218
+ * @param {number} accelerationY The vertical acceleration.
219
+ * @returns {Sprite} This sprite.
220
+ */
71
221
  setAccelerationY(accelerationY = 0) {
72
222
  this.accelerationY = accelerationY;
73
223
  return this;
74
224
  }
75
225
 
226
+ /**
227
+ * Sets the alpha transparency of the sprite.
228
+ * @param {number} alpha The alpha transparency.
229
+ * @returns {Sprite} This sprite.
230
+ */
76
231
  setAlpha(alpha = 1) {
77
232
  this.alpha = alpha;
78
233
  return this;
79
234
  }
80
235
 
236
+ /**
237
+ * Sets the boundary of the sprite.
238
+ * @param {Rect} rect The boundary.
239
+ * @returns {Sprite} This sprite.
240
+ */
81
241
  setBoundary(rect = null) {
82
242
  this.boundary = rect || this.scene;
83
243
  return this;
84
244
  }
85
245
 
246
+ /**
247
+ * Sets the color of the sprite.
248
+ * @param {string} color The color.
249
+ * @returns {Sprite} This sprite.
250
+ */
86
251
  setColor(color) {
87
252
  this.color = color;
88
253
  return this;
89
254
  }
90
255
 
256
+ /**
257
+ * Sets whether the sprite is essential.
258
+ * @param {boolean} isEssential Whether the sprite is essential.
259
+ * @returns {Sprite} This sprite.
260
+ */
91
261
  setEssential(isEssential = true) {
92
262
  this.essential = isEssential;
93
263
  return this;
94
264
  }
95
265
 
266
+ /**
267
+ * Sets the expiration time of the sprite.
268
+ * @param {number} expiration The expiration time in ticks.
269
+ * @returns {Sprite} This sprite.
270
+ */
96
271
  setExpiration(expiration = 0) {
97
272
  this.expiration = expiration;
98
273
  return this;
99
274
  }
100
275
 
276
+ /**
277
+ * Sets whether the sprite has expired.
278
+ * @param {boolean} isExpired Whether the sprite has expired.
279
+ * @returns {Sprite} This sprite.
280
+ */
101
281
  setExpired(isExpired = true) {
102
282
  this.expired = isExpired;
103
283
  return this;
104
284
  }
105
285
 
286
+ /**
287
+ * Sets the layer index of the sprite.
288
+ * @param {number} layerIndex The layer index.
289
+ * @returns {Sprite} This sprite.
290
+ */
106
291
  setLayerIndex(layerIndex = 0) {
107
292
  this.layerIndex = layerIndex;
108
293
  return this;
109
294
  }
110
295
 
296
+ /**
297
+ * Sets the maximum horizontal speed of the sprite.
298
+ * @param {number} maxSpeedX The maximum horizontal speed.
299
+ * @returns {Sprite} This sprite.
300
+ */
111
301
  setMaxSpeedX(maxSpeedX = 0) {
112
302
  this.maxSpeedX = maxSpeedX;
113
303
  return this;
114
304
  }
115
305
 
306
+ /**
307
+ * Sets the maximum vertical speed of the sprite.
308
+ * @param {number} maxSpeedY The maximum vertical speed.
309
+ * @returns {Sprite} This sprite.
310
+ */
116
311
  setMaxSpeedY(maxSpeedY = 0) {
117
312
  this.maxSpeedY = maxSpeedY;
118
313
  return this;
119
314
  }
120
315
 
316
+ /**
317
+ * Sets whether the sprite is solid.
318
+ * @param {boolean} isSolid Whether the sprite is solid.
319
+ * @returns {Sprite} This sprite.
320
+ */
121
321
  setSolid(isSolid = true) {
122
322
  this.solid = isSolid;
123
323
  return this;
124
324
  }
125
325
 
326
+ /**
327
+ * Sets the horizontal speed of the sprite.
328
+ * @param {number} speedX The horizontal speed.
329
+ * @returns {Sprite} This sprite.
330
+ */
126
331
  setSpeedX(speedX = 0) {
127
332
  this.speedX = speedX;
128
333
  return this;
129
334
  }
130
335
 
336
+ /**
337
+ * Sets the vertical speed of the sprite.
338
+ * @param {number} speedY The vertical speed.
339
+ * @returns {Sprite} This sprite.
340
+ */
131
341
  setSpeedY(speedY = 0) {
132
342
  this.speedY = speedY;
133
343
  return this;
134
344
  }
135
345
 
346
+ /**
347
+ * Sets whether the sprite is visible.
348
+ * @param {boolean} isVisible Whether the sprite is visible.
349
+ * @returns {Sprite} This sprite.
350
+ */
136
351
  setVisible(isVisible = true) {
137
352
  this.visible = isVisible;
138
353
  return this;
139
354
  }
140
355
 
356
+ /**
357
+ * Sets the animation of the sprite.
358
+ * @param {Animation} animation The animation.
359
+ * @returns {Sprite} This sprite.
360
+ */
141
361
  setAnimation(animation) {
142
362
  if (animation == this._animation) {
143
363
  return this;
@@ -150,15 +370,30 @@ export class Sprite extends Rect {
150
370
  return this;
151
371
  }
152
372
 
373
+ /**
374
+ * Sets the image of the sprite.
375
+ * @param {HTMLImageElement|HTMLCanvasElement} image The image.
376
+ * @returns {Sprite} This sprite.
377
+ */
153
378
  setImage(image) {
154
379
  this.setAnimation(new Animation([new Frame(image)]));
155
380
  return this;
156
381
  }
157
382
 
383
+ /**
384
+ * The image of the sprite.
385
+ * @type {HTMLImageElement|HTMLCanvasElement}
386
+ */
158
387
  set image(image) {
159
388
  this.setImage(image);
160
389
  }
161
390
 
391
+ /**
392
+ * Sets the speed of the sprite to an angle.
393
+ * @param {number} speed The speed.
394
+ * @param {number} degrees The angle in degrees.
395
+ * @returns {Sprite} This sprite.
396
+ */
162
397
  setSpeedToAngle(speed, degrees) {
163
398
  const RADIANS = Static.toRadians(degrees);
164
399
  this.setSpeedX(speed * Math.cos(RADIANS));
@@ -166,46 +401,85 @@ export class Sprite extends Rect {
166
401
  return this;
167
402
  }
168
403
 
404
+ /**
405
+ * Sets the speed of the sprite to a point.
406
+ * @param {number} speed The speed.
407
+ * @param {Point} point The point.
408
+ * @returns {Sprite} This sprite.
409
+ */
169
410
  setSpeedToPoint(speed, point) {
170
- const SQUARE_DISTANCE = Math.abs(this.centerX - point.x) + Math.abs(this.centerY - point.y);
171
- this.setSpeedX((point.x - this.centerX) * speed / SQUARE_DISTANCE);
172
- this.setSpeedY((point.y - this.centerY) * speed / SQUARE_DISTANCE);
411
+ const SQUARE_DISTANCE =
412
+ Math.abs(this.centerX - point.x) + Math.abs(this.centerY - point.y);
413
+ this.setSpeedX(((point.x - this.centerX) * speed) / SQUARE_DISTANCE);
414
+ this.setSpeedY(((point.y - this.centerY) * speed) / SQUARE_DISTANCE);
173
415
  return this;
174
416
  }
175
417
 
418
+ /**
419
+ * Adds a tag to the sprite.
420
+ * @param {string} tag The tag.
421
+ * @returns {Sprite} This sprite.
422
+ */
176
423
  addTag(tag) {
177
424
  this._tags[tag] = true;
178
425
  return this;
179
426
  }
180
427
 
428
+ /**
429
+ * Bounces the sprite from a direction.
430
+ * @param {Direction} direction The direction.
431
+ * @returns {Sprite} This sprite.
432
+ */
181
433
  bounceFrom(direction) {
182
- if ((this.speedX < 0 && direction.left) || (this.speedX > 0 && direction.right)) {
434
+ if (
435
+ (this.speedX < 0 && direction.left) ||
436
+ (this.speedX > 0 && direction.right)
437
+ ) {
183
438
  this.bounceX();
184
439
  }
185
440
 
186
- if ((this.speedY < 0 && direction.top) || (this.speedY > 0 && direction.bottom)) {
441
+ if (
442
+ (this.speedY < 0 && direction.top) ||
443
+ (this.speedY > 0 && direction.bottom)
444
+ ) {
187
445
  this.bounceY();
188
446
  }
189
447
 
190
448
  return this;
191
449
  }
192
450
 
451
+ /**
452
+ * Bounces the sprite horizontally.
453
+ * @returns {Sprite} This sprite.
454
+ */
193
455
  bounceX() {
194
456
  this.setSpeedX(this.speedX * -1);
195
457
  this.x += this.speedX;
196
458
  return this;
197
459
  }
198
460
 
461
+ /**
462
+ * Bounces the sprite vertically.
463
+ * @returns {Sprite} This sprite.
464
+ */
199
465
  bounceY() {
200
466
  this.setSpeedY(this.speedY * -1);
201
467
  this.y += this.speedY;
202
468
  return this;
203
469
  }
204
470
 
471
+ /**
472
+ * Expires the sprite.
473
+ */
205
474
  expire() {
206
475
  this.expired = true;
207
476
  }
208
477
 
478
+ /**
479
+ * Gets the collision direction with another sprite.
480
+ * @param {Sprite} sprite The other sprite.
481
+ * @returns {Direction} The collision direction.
482
+ */
209
483
  getCollision(sprite) {
210
484
  const DIRECTION = new Direction();
211
485
  const TA = this.top;
@@ -234,36 +508,64 @@ export class Sprite extends Rect {
234
508
  return DIRECTION;
235
509
  }
236
510
 
511
+ /**
512
+ * Checks if the sprite has a collision with a rectangle.
513
+ * @param {Rect} rect The rectangle.
514
+ * @returns {boolean} Whether the sprite has a collision with the rectangle.
515
+ */
237
516
  hasCollision(rect) {
238
517
  return !(
239
518
  this.left > rect.right ||
240
- this.right < rect.left ||
241
- this.top > rect.bottom ||
242
- this.bottom < rect.top
519
+ this.right < rect.left ||
520
+ this.top > rect.bottom ||
521
+ this.bottom < rect.top
243
522
  );
244
523
  }
245
524
 
525
+ /**
526
+ * Checks if the sprite has a tag.
527
+ * @param {string} tag The tag.
528
+ * @returns {boolean} Whether the sprite has the tag.
529
+ */
246
530
  hasTag(tag) {
247
531
  return this._tags[tag];
248
532
  }
249
533
 
534
+ /**
535
+ * Initializes the sprite.
536
+ */
250
537
  init() {
251
538
  // no default behavior
252
539
  }
253
540
 
541
+ /**
542
+ * Called when the sprite goes off its boundary.
543
+ */
254
544
  offBoundary() {
255
545
  this.setExpired();
256
546
  }
257
547
 
548
+ /**
549
+ * Called when the animation of the sprite loops.
550
+ */
258
551
  onAnimationLoop() {
259
552
  // no default behavior
260
553
  }
261
554
 
555
+ /**
556
+ * Called when the sprite collides with another sprite.
557
+ * @param {Sprite} sprite The other sprite.
558
+ * @returns {Sprite} The other sprite.
559
+ */
262
560
  onCollision(sprite) {
263
561
  // no default behavior
264
562
  return sprite;
265
563
  }
266
564
 
565
+ /**
566
+ * Pauses the sprite.
567
+ * @returns {Sprite} This sprite.
568
+ */
267
569
  pause() {
268
570
  this._lastSpeedX = this.speedX;
269
571
  this._lastSpeedY = this.speedY;
@@ -272,6 +574,11 @@ export class Sprite extends Rect {
272
574
  return this;
273
575
  }
274
576
 
577
+ /**
578
+ * Renders the sprite.
579
+ * @param {CanvasRenderingContext2D} context The rendering context.
580
+ * @returns {boolean} Whether the sprite was rendered.
581
+ */
275
582
  render(context) {
276
583
  if (!this.visible) {
277
584
  return false;
@@ -300,12 +607,19 @@ export class Sprite extends Rect {
300
607
  return true;
301
608
  }
302
609
 
610
+ /**
611
+ * Resumes the sprite.
612
+ * @returns {Sprite} This sprite.
613
+ */
303
614
  resume() {
304
615
  this.speedX = this._lastSpeedX;
305
616
  this.speedY = this._lastSpeedY;
306
617
  return this;
307
618
  }
308
619
 
620
+ /**
621
+ * Stops the sprite.
622
+ */
309
623
  stop() {
310
624
  this._lastSpeedX = 0;
311
625
  this._lastSpeedY = 0;
@@ -313,6 +627,10 @@ export class Sprite extends Rect {
313
627
  this.speedY = 0;
314
628
  }
315
629
 
630
+ /**
631
+ * Synchronizes the sprite.
632
+ * @returns {boolean} Whether the sprite has expired.
633
+ */
316
634
  sync() {
317
635
  this._lastX = this.x;
318
636
  this._lastY = this.y;
@@ -352,10 +670,18 @@ export class Sprite extends Rect {
352
670
  return false;
353
671
  }
354
672
 
673
+ /**
674
+ * Updates the sprite.
675
+ */
355
676
  update() {
356
677
  // no default behavior
357
678
  }
358
679
 
680
+ /**
681
+ * Zooms the sprite.
682
+ * @param {number} ratio The zoom ratio.
683
+ * @returns {Sprite} This sprite.
684
+ */
359
685
  zoom(ratio) {
360
686
  const widthChange = this.width * ratio;
361
687
  this.width += widthChange;
package/src/Static.mjs CHANGED
@@ -45,7 +45,7 @@ export class Static {
45
45
  }
46
46
 
47
47
  static getImage(image) {
48
- if ("string" == typeof(image)) {
48
+ if ("string" == typeof image) {
49
49
  return this.getElement(image);
50
50
  }
51
51
 
@@ -68,10 +68,10 @@ export class Static {
68
68
  }
69
69
 
70
70
  static toDegrees(radians) {
71
- return radians * 180 / Math.PI;
71
+ return (radians * 180) / Math.PI;
72
72
  }
73
73
 
74
74
  static toRadians(degrees) {
75
- return degrees * Math.PI / 180;
75
+ return (degrees * Math.PI) / 180;
76
76
  }
77
77
  }