core2d 2.11.1 → 2.11.2

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/Static.mjs CHANGED
@@ -3,75 +3,75 @@
3
3
  import { ACL } from "./ACL.mjs";
4
4
 
5
5
  export class Static {
6
- static checkCollisions(sprites) {
7
- const LENGTH = sprites.length;
6
+ static checkCollisions(sprites) {
7
+ const LENGTH = sprites.length;
8
8
 
9
- for (let i = 0; i < LENGTH - 1; ++i) {
10
- const LEFT_SPRITE = sprites[i];
9
+ for (let i = 0; i < LENGTH - 1; ++i) {
10
+ const LEFT_SPRITE = sprites[i];
11
11
 
12
- for (let j = i + 1; j < LENGTH; ++j) {
13
- const RIGHT_SPRITE = sprites[j];
12
+ for (let j = i + 1; j < LENGTH; ++j) {
13
+ const RIGHT_SPRITE = sprites[j];
14
14
 
15
- if (LEFT_SPRITE.hasCollision(RIGHT_SPRITE)) {
16
- LEFT_SPRITE.collided = true;
17
- LEFT_SPRITE.onCollision(RIGHT_SPRITE);
18
- RIGHT_SPRITE.collided = true;
19
- RIGHT_SPRITE.onCollision(LEFT_SPRITE);
20
- }
21
- }
22
- }
23
- }
15
+ if (LEFT_SPRITE.hasCollision(RIGHT_SPRITE)) {
16
+ LEFT_SPRITE.collided = true;
17
+ LEFT_SPRITE.onCollision(RIGHT_SPRITE);
18
+ RIGHT_SPRITE.collided = true;
19
+ RIGHT_SPRITE.onCollision(LEFT_SPRITE);
20
+ }
21
+ }
22
+ }
23
+ }
24
24
 
25
- static getElement(id) {
26
- const element = ACL.document.getElementById(id);
25
+ static getElement(id) {
26
+ const element = ACL.document.getElementById(id);
27
27
 
28
- if (!element) {
29
- console.warn(`Could not find element with id: ${id}`);
30
- }
28
+ if (!element) {
29
+ console.warn(`Could not find element with id: ${id}`);
30
+ }
31
31
 
32
- return element;
33
- }
32
+ return element;
33
+ }
34
34
 
35
- static getElements(name) {
36
- return Array.from(ACL.document.getElementsByTagName(name));
37
- }
35
+ static getElements(name) {
36
+ return Array.from(ACL.document.getElementsByTagName(name));
37
+ }
38
38
 
39
- static getGamepads() {
40
- if (!navigator.getGamepads) {
41
- return [];
42
- }
39
+ static getGamepads() {
40
+ if (!navigator.getGamepads) {
41
+ return [];
42
+ }
43
43
 
44
- return navigator.getGamepads();
45
- }
44
+ return navigator.getGamepads();
45
+ }
46
46
 
47
- static getImage(image) {
48
- if ("string" == typeof image) {
49
- return this.getElement(image);
50
- }
47
+ static getImage(image) {
48
+ if ("string" == typeof image) {
49
+ return this.getElement(image);
50
+ }
51
51
 
52
- return image;
53
- }
52
+ return image;
53
+ }
54
54
 
55
- static makeEnum(array) {
56
- return this.makeHash(array, true);
57
- }
55
+ static makeEnum(array) {
56
+ return this.makeHash(array, true);
57
+ }
58
58
 
59
- static makeHash(array, indexed) {
60
- const RESULT = {};
59
+ static makeHash(array, indexed) {
60
+ const RESULT = {};
61
61
 
62
- for (let i = 0; i < array.length; ++i) {
63
- const VALUE = array[i];
64
- RESULT[VALUE] = indexed ? i : VALUE;
65
- }
62
+ for (let i = 0; i < array.length; ++i) {
63
+ const VALUE = array[i];
64
+ RESULT[VALUE] = indexed ? i : VALUE;
65
+ }
66
66
 
67
- return RESULT;
68
- }
67
+ return RESULT;
68
+ }
69
69
 
70
- static toDegrees(radians) {
71
- return (radians * 180) / Math.PI;
72
- }
70
+ static toDegrees(radians) {
71
+ return (radians * 180) / Math.PI;
72
+ }
73
73
 
74
- static toRadians(degrees) {
75
- return (degrees * Math.PI) / 180;
76
- }
74
+ static toRadians(degrees) {
75
+ return (degrees * Math.PI) / 180;
76
+ }
77
77
  }
@@ -13,232 +13,232 @@ const hiddenContext = hiddenCanvas.getContext("2d");
13
13
  * @extends Sprite
14
14
  */
15
15
  export class TextSprite extends Sprite {
16
- /**
17
- * Creates a new TextSprite.
18
- * @param {string} [text=""] The text to display.
19
- */
20
- constructor(text = "") {
21
- super();
22
- this._text = text;
23
- this._fontFamily = FontFamily.Monospace;
24
- this._fontSize = 16;
25
- this.fontColor = Color.White;
26
- this.lineWidth = 0;
27
- this.shadowBlur = 0;
28
- this.shadowColor = null;
29
- this.shadowOffsetX = 0;
30
- this.shadowOffsetY = 0;
31
- this.strokeStyle = null;
32
- this._updateFont();
33
- }
34
-
35
- /**
36
- * Renders the text sprite.
37
- * @param {CanvasRenderingContext2D} context The rendering context.
38
- */
39
- render(context) {
40
- if (Sprite.prototype.render.call(this, context)) {
41
- context.fillStyle = this.fontColor;
42
- context.font = this._font;
43
- context.textBaseline = "top";
44
-
45
- if (this.shadowColor) {
46
- context.shadowColor = this.shadowColor;
47
- context.shadowOffsetX = this.shadowOffsetX;
48
- context.shadowOffsetY = this.shadowOffsetY;
49
- context.shadowBlur = this.shadowBlur;
50
- }
51
-
52
- context.fillText(
53
- this._text,
54
- this.left + this.scene.x,
55
- this.bottom + this.scene.y,
56
- this.width
57
- );
58
-
59
- if (this.shadowColor) {
60
- context.shadowColor = null;
61
- context.shadowOffsetX = 0;
62
- context.shadowOffsetY = 0;
63
- context.shadowBlur = 0;
64
- }
65
-
66
- if (this.strokeStyle) {
67
- context.lineWidth = this.lineWidth;
68
- context.strokeStyle = this.strokeStyle;
69
- context.strokeText(
70
- this._text,
71
- this.left + this.scene.x,
72
- this.bottom + this.scene.y,
73
- this.width
74
- );
75
- context.lineWidth = 0;
76
- context.strokeStyle = null;
77
- }
78
- }
79
- }
80
-
81
- /**
82
- * Sets the font color.
83
- * @param {string} fontColor The font color.
84
- * @returns {TextSprite} This text sprite.
85
- */
86
- setFontColor(fontColor) {
87
- this.fontColor = fontColor;
88
- return this;
89
- }
90
-
91
- /**
92
- * Sets the font family.
93
- * @param {string} fontFamily The font family.
94
- * @returns {TextSprite} This text sprite.
95
- */
96
- setFontFamily(fontFamily) {
97
- this._fontFamily = fontFamily;
98
- this._updateFont();
99
- return this;
100
- }
101
-
102
- /**
103
- * Sets the font size.
104
- * @param {number} fontSize The font size.
105
- * @returns {TextSprite} This text sprite.
106
- */
107
- setFontSize(fontSize) {
108
- this._fontSize = fontSize;
109
- this.height = fontSize;
110
- this._updateFont();
111
- return this;
112
- }
113
-
114
- /**
115
- * Sets the line width.
116
- * @param {number} lineWidth The line width.
117
- * @returns {TextSprite} This text sprite.
118
- */
119
- setLineWidth(lineWidth) {
120
- this.lineWidth = lineWidth;
121
- return this;
122
- }
123
-
124
- /**
125
- * Sets the shadow blur.
126
- * @param {number} shadowBlur The shadow blur.
127
- * @returns {TextSprite} This text sprite.
128
- */
129
- setShadowBlur(shadowBlur) {
130
- this.shadowBlur = shadowBlur;
131
- return this;
132
- }
133
-
134
- /**
135
- * Sets the shadow color.
136
- * @param {string} shadowColor The shadow color.
137
- * @returns {TextSprite} This text sprite.
138
- */
139
- setShadowColor(shadowColor) {
140
- this.shadowColor = shadowColor;
141
- return this;
142
- }
143
-
144
- /**
145
- * Sets the shadow offset x.
146
- * @param {number} shadowOffsetX The shadow offset x.
147
- * @returns {TextSprite} This text sprite.
148
- */
149
- setShadowOffsetX(shadowOffsetX) {
150
- this.shadowOffsetX = shadowOffsetX;
151
- return this;
152
- }
153
-
154
- /**
155
- * Sets the shadow offset y.
156
- * @param {number} shadowOffsetY The shadow offset y.
157
- * @returns {TextSprite} This text sprite.
158
- */
159
- setShadowOffsetY(shadowOffsetY) {
160
- this.shadowOffsetY = shadowOffsetY;
161
- return this;
162
- }
163
-
164
- /**
165
- * Sets the stroke style.
166
- * @param {string} strokeStyle The stroke style.
167
- * @returns {TextSprite} This text sprite.
168
- */
169
- setStrokeStyle(strokeStyle) {
170
- this.strokeStyle = strokeStyle;
171
- return this;
172
- }
173
-
174
- /**
175
- * Sets the text.
176
- * @param {string} text The text.
177
- * @returns {TextSprite} This text sprite.
178
- */
179
- setText(text) {
180
- this._text = text;
181
- this._updateFont();
182
- return this;
183
- }
184
-
185
- /**
186
- * The font family.
187
- * @type {string}
188
- */
189
- get fontFamily() {
190
- return this._fontFamily;
191
- }
192
-
193
- /**
194
- * The font size.
195
- * @type {number}
196
- */
197
- get fontSize() {
198
- return this._fontSize;
199
- }
200
-
201
- /**
202
- * The text.
203
- * @type {string}
204
- */
205
- get text() {
206
- return this._text;
207
- }
208
-
209
- /**
210
- * The font family.
211
- * @type {string}
212
- */
213
- set fontFamily(fontFamily) {
214
- this.setFontFamily(fontFamily);
215
- }
216
-
217
- /**
218
- * The font size.
219
- * @type {number}
220
- */
221
- set fontSize(fontSize) {
222
- this.setFontSize(fontSize);
223
- }
224
-
225
- /**
226
- * The text.
227
- * @type {string}
228
- */
229
- set text(text) {
230
- this.setText(text);
231
- }
232
-
233
- /**
234
- * Updates the font.
235
- * @private
236
- */
237
- _updateFont() {
238
- this._font = `${this._fontSize}px ${this._fontFamily}`;
239
- hiddenContext.textBaseline = "top";
240
- hiddenContext.font = this._font;
241
- const measurement = hiddenContext.measureText(this._text);
242
- this.width = measurement.width;
243
- }
16
+ /**
17
+ * Creates a new TextSprite.
18
+ * @param {string} [text=""] The text to display.
19
+ */
20
+ constructor(text = "") {
21
+ super();
22
+ this._text = text;
23
+ this._fontFamily = FontFamily.Monospace;
24
+ this._fontSize = 16;
25
+ this.fontColor = Color.White;
26
+ this.lineWidth = 0;
27
+ this.shadowBlur = 0;
28
+ this.shadowColor = null;
29
+ this.shadowOffsetX = 0;
30
+ this.shadowOffsetY = 0;
31
+ this.strokeStyle = null;
32
+ this._updateFont();
33
+ }
34
+
35
+ /**
36
+ * Renders the text sprite.
37
+ * @param {CanvasRenderingContext2D} context The rendering context.
38
+ */
39
+ render(context) {
40
+ if (Sprite.prototype.render.call(this, context)) {
41
+ context.fillStyle = this.fontColor;
42
+ context.font = this._font;
43
+ context.textBaseline = "top";
44
+
45
+ if (this.shadowColor) {
46
+ context.shadowColor = this.shadowColor;
47
+ context.shadowOffsetX = this.shadowOffsetX;
48
+ context.shadowOffsetY = this.shadowOffsetY;
49
+ context.shadowBlur = this.shadowBlur;
50
+ }
51
+
52
+ context.fillText(
53
+ this._text,
54
+ this.left + this.scene.x,
55
+ this.bottom + this.scene.y,
56
+ this.width
57
+ );
58
+
59
+ if (this.shadowColor) {
60
+ context.shadowColor = null;
61
+ context.shadowOffsetX = 0;
62
+ context.shadowOffsetY = 0;
63
+ context.shadowBlur = 0;
64
+ }
65
+
66
+ if (this.strokeStyle) {
67
+ context.lineWidth = this.lineWidth;
68
+ context.strokeStyle = this.strokeStyle;
69
+ context.strokeText(
70
+ this._text,
71
+ this.left + this.scene.x,
72
+ this.bottom + this.scene.y,
73
+ this.width
74
+ );
75
+ context.lineWidth = 0;
76
+ context.strokeStyle = null;
77
+ }
78
+ }
79
+ }
80
+
81
+ /**
82
+ * Sets the font color.
83
+ * @param {string} fontColor The font color.
84
+ * @returns {TextSprite} This text sprite.
85
+ */
86
+ setFontColor(fontColor) {
87
+ this.fontColor = fontColor;
88
+ return this;
89
+ }
90
+
91
+ /**
92
+ * Sets the font family.
93
+ * @param {string} fontFamily The font family.
94
+ * @returns {TextSprite} This text sprite.
95
+ */
96
+ setFontFamily(fontFamily) {
97
+ this._fontFamily = fontFamily;
98
+ this._updateFont();
99
+ return this;
100
+ }
101
+
102
+ /**
103
+ * Sets the font size.
104
+ * @param {number} fontSize The font size.
105
+ * @returns {TextSprite} This text sprite.
106
+ */
107
+ setFontSize(fontSize) {
108
+ this._fontSize = fontSize;
109
+ this.height = fontSize;
110
+ this._updateFont();
111
+ return this;
112
+ }
113
+
114
+ /**
115
+ * Sets the line width.
116
+ * @param {number} lineWidth The line width.
117
+ * @returns {TextSprite} This text sprite.
118
+ */
119
+ setLineWidth(lineWidth) {
120
+ this.lineWidth = lineWidth;
121
+ return this;
122
+ }
123
+
124
+ /**
125
+ * Sets the shadow blur.
126
+ * @param {number} shadowBlur The shadow blur.
127
+ * @returns {TextSprite} This text sprite.
128
+ */
129
+ setShadowBlur(shadowBlur) {
130
+ this.shadowBlur = shadowBlur;
131
+ return this;
132
+ }
133
+
134
+ /**
135
+ * Sets the shadow color.
136
+ * @param {string} shadowColor The shadow color.
137
+ * @returns {TextSprite} This text sprite.
138
+ */
139
+ setShadowColor(shadowColor) {
140
+ this.shadowColor = shadowColor;
141
+ return this;
142
+ }
143
+
144
+ /**
145
+ * Sets the shadow offset x.
146
+ * @param {number} shadowOffsetX The shadow offset x.
147
+ * @returns {TextSprite} This text sprite.
148
+ */
149
+ setShadowOffsetX(shadowOffsetX) {
150
+ this.shadowOffsetX = shadowOffsetX;
151
+ return this;
152
+ }
153
+
154
+ /**
155
+ * Sets the shadow offset y.
156
+ * @param {number} shadowOffsetY The shadow offset y.
157
+ * @returns {TextSprite} This text sprite.
158
+ */
159
+ setShadowOffsetY(shadowOffsetY) {
160
+ this.shadowOffsetY = shadowOffsetY;
161
+ return this;
162
+ }
163
+
164
+ /**
165
+ * Sets the stroke style.
166
+ * @param {string} strokeStyle The stroke style.
167
+ * @returns {TextSprite} This text sprite.
168
+ */
169
+ setStrokeStyle(strokeStyle) {
170
+ this.strokeStyle = strokeStyle;
171
+ return this;
172
+ }
173
+
174
+ /**
175
+ * Sets the text.
176
+ * @param {string} text The text.
177
+ * @returns {TextSprite} This text sprite.
178
+ */
179
+ setText(text) {
180
+ this._text = text;
181
+ this._updateFont();
182
+ return this;
183
+ }
184
+
185
+ /**
186
+ * The font family.
187
+ * @type {string}
188
+ */
189
+ get fontFamily() {
190
+ return this._fontFamily;
191
+ }
192
+
193
+ /**
194
+ * The font size.
195
+ * @type {number}
196
+ */
197
+ get fontSize() {
198
+ return this._fontSize;
199
+ }
200
+
201
+ /**
202
+ * The text.
203
+ * @type {string}
204
+ */
205
+ get text() {
206
+ return this._text;
207
+ }
208
+
209
+ /**
210
+ * The font family.
211
+ * @type {string}
212
+ */
213
+ set fontFamily(fontFamily) {
214
+ this.setFontFamily(fontFamily);
215
+ }
216
+
217
+ /**
218
+ * The font size.
219
+ * @type {number}
220
+ */
221
+ set fontSize(fontSize) {
222
+ this.setFontSize(fontSize);
223
+ }
224
+
225
+ /**
226
+ * The text.
227
+ * @type {string}
228
+ */
229
+ set text(text) {
230
+ this.setText(text);
231
+ }
232
+
233
+ /**
234
+ * Updates the font.
235
+ * @private
236
+ */
237
+ _updateFont() {
238
+ this._font = `${this._fontSize}px ${this._fontFamily}`;
239
+ hiddenContext.textBaseline = "top";
240
+ hiddenContext.font = this._font;
241
+ const measurement = hiddenContext.measureText(this._text);
242
+ this.width = measurement.width;
243
+ }
244
244
  }
package/src/Touch.mjs CHANGED
@@ -3,50 +3,50 @@
3
3
  import { Point } from "./Point.mjs";
4
4
 
5
5
  export class Touch extends Point {
6
- constructor(event) {
7
- super();
8
- event.preventDefault();
9
- this._isDown = true;
10
- this.updateCoordinates(event);
6
+ constructor(event) {
7
+ super();
8
+ event.preventDefault();
9
+ this._isDown = true;
10
+ this.updateCoordinates(event);
11
11
 
12
- addEventListener(
13
- "touchend",
14
- (event) => {
15
- event.preventDefault();
16
- this._isDown = false;
17
- this.updateCoordinates(event);
18
- },
19
- false
20
- );
12
+ addEventListener(
13
+ "touchend",
14
+ (event) => {
15
+ event.preventDefault();
16
+ this._isDown = false;
17
+ this.updateCoordinates(event);
18
+ },
19
+ false
20
+ );
21
21
 
22
- addEventListener(
23
- "touchmove",
24
- (event) => {
25
- event.preventDefault();
26
- this.updateCoordinates(event);
27
- },
28
- false
29
- );
22
+ addEventListener(
23
+ "touchmove",
24
+ (event) => {
25
+ event.preventDefault();
26
+ this.updateCoordinates(event);
27
+ },
28
+ false
29
+ );
30
30
 
31
- addEventListener(
32
- "touchstart",
33
- () => {
34
- event.preventDefault();
35
- this._isDown = true;
36
- this.updateCoordinates(event);
37
- },
38
- false
39
- );
40
- }
31
+ addEventListener(
32
+ "touchstart",
33
+ () => {
34
+ event.preventDefault();
35
+ this._isDown = true;
36
+ this.updateCoordinates(event);
37
+ },
38
+ false
39
+ );
40
+ }
41
41
 
42
- get command() {
43
- return this._isDown;
44
- }
42
+ get command() {
43
+ return this._isDown;
44
+ }
45
45
 
46
- updateCoordinates(event) {
47
- const TOUCHES = event.changedTouches;
48
- const TOUCH = TOUCHES[0];
49
- this.x = TOUCH.pageX;
50
- this.y = TOUCH.pageY;
51
- }
46
+ updateCoordinates(event) {
47
+ const TOUCHES = event.changedTouches;
48
+ const TOUCH = TOUCHES[0];
49
+ this.x = TOUCH.pageX;
50
+ this.y = TOUCH.pageY;
51
+ }
52
52
  }