@vpmedia/phaser 1.94.0 → 1.96.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.
- package/CHANGELOG.md +41 -0
- package/package.json +11 -11
- package/src/phaser/core/animation.js +79 -56
- package/src/phaser/core/animation_manager.js +55 -55
- package/src/phaser/core/animation_parser.js +2 -2
- package/src/phaser/core/cache.js +154 -154
- package/src/phaser/core/device.js +2 -1
- package/src/phaser/core/device_util.js +27 -27
- package/src/phaser/core/dom.js +43 -43
- package/src/phaser/core/event_manager.js +63 -63
- package/src/phaser/core/factory.js +47 -47
- package/src/phaser/core/frame.js +30 -30
- package/src/phaser/core/frame_data.js +30 -28
- package/src/phaser/core/frame_util.js +8 -8
- package/src/phaser/core/game.js +35 -19
- package/src/phaser/core/input_pointer.js +18 -0
- package/src/phaser/core/loader.js +171 -171
- package/src/phaser/core/loader_parser.js +22 -22
- package/src/phaser/core/raf.js +1 -1
- package/src/phaser/core/scale_manager.js +3 -2
- package/src/phaser/core/scene.js +16 -10
- package/src/phaser/core/scene_manager.js +51 -43
- package/src/phaser/core/signal.js +56 -52
- package/src/phaser/core/sound.js +54 -54
- package/src/phaser/core/sound_manager.js +49 -49
- package/src/phaser/core/stage.js +16 -16
- package/src/phaser/core/time.js +54 -30
- package/src/phaser/core/timer.js +82 -64
- package/src/phaser/core/timer_event.js +9 -9
- package/src/phaser/core/tween.js +106 -90
- package/src/phaser/core/tween_data.js +30 -30
- package/src/phaser/core/tween_manager.js +32 -24
- package/src/phaser/core/world.js +4 -3
- package/src/phaser/display/bitmap_text.js +72 -54
- package/src/phaser/display/button.js +48 -47
- package/src/phaser/display/canvas/buffer.js +8 -8
- package/src/phaser/display/canvas/graphics.js +8 -8
- package/src/phaser/display/canvas/masker.js +5 -5
- package/src/phaser/display/canvas/pool.js +18 -18
- package/src/phaser/display/canvas/renderer.js +17 -16
- package/src/phaser/display/canvas/tinter.js +22 -22
- package/src/phaser/display/canvas/util.js +42 -42
- package/src/phaser/display/display_object.js +119 -108
- package/src/phaser/display/graphics.js +129 -112
- package/src/phaser/display/graphics_data.js +10 -10
- package/src/phaser/display/graphics_data_util.js +3 -3
- package/src/phaser/display/group.js +63 -50
- package/src/phaser/display/image.js +68 -50
- package/src/phaser/display/sprite_batch.js +2 -2
- package/src/phaser/display/sprite_util.js +19 -19
- package/src/phaser/display/text.js +217 -187
- package/src/phaser/display/webgl/abstract_filter.js +4 -4
- package/src/phaser/display/webgl/base_texture.js +9 -9
- package/src/phaser/display/webgl/blend_manager.js +7 -7
- package/src/phaser/display/webgl/earcut.js +95 -95
- package/src/phaser/display/webgl/earcut_node.js +4 -4
- package/src/phaser/display/webgl/fast_sprite_batch.js +15 -15
- package/src/phaser/display/webgl/filter_manager.js +9 -9
- package/src/phaser/display/webgl/filter_texture.js +10 -10
- package/src/phaser/display/webgl/graphics.js +37 -37
- package/src/phaser/display/webgl/graphics_data.js +4 -4
- package/src/phaser/display/webgl/mask_manager.js +6 -6
- package/src/phaser/display/webgl/render_texture.js +16 -16
- package/src/phaser/display/webgl/renderer.js +21 -20
- package/src/phaser/display/webgl/shader/complex.js +4 -4
- package/src/phaser/display/webgl/shader/fast.js +4 -4
- package/src/phaser/display/webgl/shader/normal.js +8 -8
- package/src/phaser/display/webgl/shader/primitive.js +4 -4
- package/src/phaser/display/webgl/shader/strip.js +4 -4
- package/src/phaser/display/webgl/shader_manager.js +9 -9
- package/src/phaser/display/webgl/sprite_batch.js +18 -18
- package/src/phaser/display/webgl/stencil_manager.js +16 -16
- package/src/phaser/display/webgl/texture.js +13 -13
- package/src/phaser/display/webgl/texture_util.js +8 -8
- package/src/phaser/display/webgl/util.js +25 -25
- package/src/phaser/geom/circle.js +74 -69
- package/src/phaser/geom/ellipse.js +33 -28
- package/src/phaser/geom/line.js +96 -93
- package/src/phaser/geom/matrix.js +61 -54
- package/src/phaser/geom/point.js +99 -96
- package/src/phaser/geom/polygon.js +27 -22
- package/src/phaser/geom/rectangle.js +137 -132
- package/src/phaser/geom/rounded_rectangle.js +12 -12
- package/src/phaser/geom/util/circle.js +33 -33
- package/src/phaser/geom/util/ellipse.js +5 -5
- package/src/phaser/geom/util/line.js +26 -26
- package/src/phaser/geom/util/matrix.js +8 -8
- package/src/phaser/geom/util/point.js +97 -97
- package/src/phaser/geom/util/polygon.js +4 -4
- package/src/phaser/geom/util/rectangle.js +74 -74
- package/src/phaser/geom/util/rounded_rectangle.js +4 -4
- package/src/phaser/util/math.js +81 -81
- package/types/phaser/core/animation.d.ts +79 -56
- package/types/phaser/core/animation.d.ts.map +1 -1
- package/types/phaser/core/animation_manager.d.ts +55 -55
- package/types/phaser/core/animation_parser.d.ts +3 -2
- package/types/phaser/core/animation_parser.d.ts.map +1 -1
- package/types/phaser/core/cache.d.ts +154 -154
- package/types/phaser/core/device.d.ts.map +1 -1
- package/types/phaser/core/dom.d.ts +51 -44
- package/types/phaser/core/dom.d.ts.map +1 -1
- package/types/phaser/core/event_manager.d.ts +63 -63
- package/types/phaser/core/factory.d.ts +47 -47
- package/types/phaser/core/frame.d.ts +30 -30
- package/types/phaser/core/frame_data.d.ts +28 -27
- package/types/phaser/core/frame_data.d.ts.map +1 -1
- package/types/phaser/core/game.d.ts +54 -21
- package/types/phaser/core/game.d.ts.map +1 -1
- package/types/phaser/core/input_pointer.d.ts +18 -0
- package/types/phaser/core/input_pointer.d.ts.map +1 -1
- package/types/phaser/core/loader.d.ts +175 -172
- package/types/phaser/core/loader.d.ts.map +1 -1
- package/types/phaser/core/loader_parser.d.ts.map +1 -1
- package/types/phaser/core/raf.d.ts +1 -1
- package/types/phaser/core/scale_manager.d.ts +6 -5
- package/types/phaser/core/scale_manager.d.ts.map +1 -1
- package/types/phaser/core/scene.d.ts +15 -9
- package/types/phaser/core/scene.d.ts.map +1 -1
- package/types/phaser/core/scene_manager.d.ts +51 -43
- package/types/phaser/core/scene_manager.d.ts.map +1 -1
- package/types/phaser/core/signal.d.ts +54 -51
- package/types/phaser/core/signal.d.ts.map +1 -1
- package/types/phaser/core/sound.d.ts +54 -54
- package/types/phaser/core/sound_manager.d.ts +49 -49
- package/types/phaser/core/stage.d.ts +10 -10
- package/types/phaser/core/time.d.ts +54 -30
- package/types/phaser/core/time.d.ts.map +1 -1
- package/types/phaser/core/timer.d.ts +82 -64
- package/types/phaser/core/timer.d.ts.map +1 -1
- package/types/phaser/core/timer_event.d.ts +9 -9
- package/types/phaser/core/tween.d.ts +106 -90
- package/types/phaser/core/tween.d.ts.map +1 -1
- package/types/phaser/core/tween_data.d.ts +30 -30
- package/types/phaser/core/tween_manager.d.ts +35 -67
- package/types/phaser/core/tween_manager.d.ts.map +1 -1
- package/types/phaser/core/world.d.ts +4 -3
- package/types/phaser/core/world.d.ts.map +1 -1
- package/types/phaser/display/bitmap_text.d.ts +76 -53
- package/types/phaser/display/bitmap_text.d.ts.map +1 -1
- package/types/phaser/display/button.d.ts +47 -46
- package/types/phaser/display/button.d.ts.map +1 -1
- package/types/phaser/display/canvas/buffer.d.ts +8 -8
- package/types/phaser/display/canvas/graphics.d.ts +3 -3
- package/types/phaser/display/canvas/graphics.d.ts.map +1 -1
- package/types/phaser/display/canvas/pool.d.ts +6 -3
- package/types/phaser/display/canvas/pool.d.ts.map +1 -1
- package/types/phaser/display/canvas/renderer.d.ts +18 -17
- package/types/phaser/display/canvas/renderer.d.ts.map +1 -1
- package/types/phaser/display/canvas/util.d.ts +6 -6
- package/types/phaser/display/canvas/util.d.ts.map +1 -1
- package/types/phaser/display/display_object.d.ts +119 -108
- package/types/phaser/display/display_object.d.ts.map +1 -1
- package/types/phaser/display/graphics.d.ts +118 -102
- package/types/phaser/display/graphics.d.ts.map +1 -1
- package/types/phaser/display/graphics_data.d.ts +10 -10
- package/types/phaser/display/group.d.ts +59 -47
- package/types/phaser/display/group.d.ts.map +1 -1
- package/types/phaser/display/image.d.ts +58 -45
- package/types/phaser/display/image.d.ts.map +1 -1
- package/types/phaser/display/text.d.ts +237 -188
- package/types/phaser/display/text.d.ts.map +1 -1
- package/types/phaser/display/webgl/abstract_filter.d.ts +4 -4
- package/types/phaser/display/webgl/base_texture.d.ts +10 -10
- package/types/phaser/display/webgl/base_texture.d.ts.map +1 -1
- package/types/phaser/display/webgl/blend_manager.d.ts +6 -6
- package/types/phaser/display/webgl/earcut.d.ts +95 -95
- package/types/phaser/display/webgl/earcut_node.d.ts +4 -4
- package/types/phaser/display/webgl/fast_sprite_batch.d.ts +15 -15
- package/types/phaser/display/webgl/filter_manager.d.ts +8 -8
- package/types/phaser/display/webgl/filter_texture.d.ts +10 -10
- package/types/phaser/display/webgl/graphics.d.ts +2 -2
- package/types/phaser/display/webgl/graphics.d.ts.map +1 -1
- package/types/phaser/display/webgl/graphics_data.d.ts +4 -4
- package/types/phaser/display/webgl/render_texture.d.ts +16 -16
- package/types/phaser/display/webgl/renderer.d.ts +21 -20
- package/types/phaser/display/webgl/renderer.d.ts.map +1 -1
- package/types/phaser/display/webgl/shader/complex.d.ts +4 -4
- package/types/phaser/display/webgl/shader/fast.d.ts +4 -4
- package/types/phaser/display/webgl/shader/normal.d.ts +8 -8
- package/types/phaser/display/webgl/shader/primitive.d.ts +4 -4
- package/types/phaser/display/webgl/shader/strip.d.ts +4 -4
- package/types/phaser/display/webgl/shader_manager.d.ts +8 -8
- package/types/phaser/display/webgl/sprite_batch.d.ts +17 -17
- package/types/phaser/display/webgl/stencil_manager.d.ts +15 -15
- package/types/phaser/display/webgl/texture.d.ts +12 -12
- package/types/phaser/display/webgl/texture_util.d.ts +4 -2
- package/types/phaser/display/webgl/texture_util.d.ts.map +1 -1
- package/types/phaser/geom/circle.d.ts +74 -69
- package/types/phaser/geom/circle.d.ts.map +1 -1
- package/types/phaser/geom/ellipse.d.ts +33 -28
- package/types/phaser/geom/ellipse.d.ts.map +1 -1
- package/types/phaser/geom/line.d.ts +96 -93
- package/types/phaser/geom/line.d.ts.map +1 -1
- package/types/phaser/geom/matrix.d.ts +61 -54
- package/types/phaser/geom/matrix.d.ts.map +1 -1
- package/types/phaser/geom/point.d.ts +99 -96
- package/types/phaser/geom/point.d.ts.map +1 -1
- package/types/phaser/geom/polygon.d.ts +29 -23
- package/types/phaser/geom/polygon.d.ts.map +1 -1
- package/types/phaser/geom/rectangle.d.ts +137 -132
- package/types/phaser/geom/rectangle.d.ts.map +1 -1
- package/types/phaser/geom/rounded_rectangle.d.ts +12 -12
- package/types/phaser/geom/util/point.d.ts +1 -1
- package/types/phaser/geom/util/point.d.ts.map +1 -1
- package/pnpm-workspace.yaml +0 -4
package/src/phaser/core/time.js
CHANGED
|
@@ -2,42 +2,66 @@ import { Timer } from './timer.js';
|
|
|
2
2
|
|
|
3
3
|
export class Time {
|
|
4
4
|
/**
|
|
5
|
-
*
|
|
6
|
-
* @param {import('./game.js').Game} game -
|
|
5
|
+
* Creates a new Time instance.
|
|
6
|
+
* @param {import('./game.js').Game} game - Reference to the Phaser Game instance.
|
|
7
7
|
*/
|
|
8
8
|
constructor(game) {
|
|
9
9
|
this.game = game;
|
|
10
|
+
/** @type {number} */
|
|
10
11
|
this.time = 0;
|
|
12
|
+
/** @type {number} */
|
|
11
13
|
this.prevTime = 0;
|
|
14
|
+
/** @type {number} */
|
|
12
15
|
this.now = 0;
|
|
16
|
+
/** @type {number} */
|
|
13
17
|
this.elapsed = 0;
|
|
18
|
+
/** @type {number} */
|
|
14
19
|
this.elapsedMS = 0;
|
|
20
|
+
/** @type {number} */
|
|
15
21
|
this.desiredFpsMult = 1 / 60;
|
|
22
|
+
/** @type {number} */
|
|
16
23
|
this._desiredFps = 60;
|
|
24
|
+
/** @type {number} */
|
|
17
25
|
this.suggestedFps = this.desiredFps;
|
|
26
|
+
/** @type {boolean} */
|
|
18
27
|
this.advancedTiming = false;
|
|
28
|
+
/** @type {number} */
|
|
19
29
|
this.frames = 0;
|
|
30
|
+
/** @type {number} */
|
|
20
31
|
this.fps = 0;
|
|
32
|
+
/** @type {number} */
|
|
21
33
|
this.fpsMin = 1000;
|
|
34
|
+
/** @type {number} */
|
|
22
35
|
this.fpsMax = 0;
|
|
36
|
+
/** @type {number} */
|
|
23
37
|
this.msMin = 1000;
|
|
38
|
+
/** @type {number} */
|
|
24
39
|
this.msMax = 0;
|
|
40
|
+
/** @type {number} */
|
|
25
41
|
this.pauseDuration = 0;
|
|
42
|
+
/** @type {number} */
|
|
26
43
|
this.timeToCall = 0;
|
|
44
|
+
/** @type {number} */
|
|
27
45
|
this.timeExpected = 0;
|
|
46
|
+
/** @type {Timer} */
|
|
28
47
|
this.events = new Timer(this.game, false);
|
|
48
|
+
/** @type {number} */
|
|
29
49
|
this._frameCount = 0;
|
|
50
|
+
/** @type {number} */
|
|
30
51
|
this._elapsedAccumulator = 0;
|
|
31
52
|
this._started = 0;
|
|
53
|
+
/** @type {number} */
|
|
32
54
|
this._timeLastSecond = 0;
|
|
55
|
+
/** @type {number} */
|
|
33
56
|
this._pauseStarted = 0;
|
|
57
|
+
/** @type {boolean} */
|
|
34
58
|
this._justResumed = false;
|
|
35
59
|
/** @type {Timer[]} */
|
|
36
60
|
this._timers = [];
|
|
37
61
|
}
|
|
38
62
|
|
|
39
63
|
/**
|
|
40
|
-
*
|
|
64
|
+
* Initializes the time manager and starts tracking time.
|
|
41
65
|
*/
|
|
42
66
|
boot() {
|
|
43
67
|
this._started = Date.now();
|
|
@@ -47,9 +71,9 @@ export class Time {
|
|
|
47
71
|
}
|
|
48
72
|
|
|
49
73
|
/**
|
|
50
|
-
*
|
|
51
|
-
* @param {Timer} timer -
|
|
52
|
-
* @returns {Timer}
|
|
74
|
+
* Adds a Timer to the Time manager.
|
|
75
|
+
* @param {Timer} timer - The Timer to add.
|
|
76
|
+
* @returns {Timer} The added Timer object.
|
|
53
77
|
*/
|
|
54
78
|
add(timer) {
|
|
55
79
|
this._timers.push(timer);
|
|
@@ -57,9 +81,9 @@ export class Time {
|
|
|
57
81
|
}
|
|
58
82
|
|
|
59
83
|
/**
|
|
60
|
-
*
|
|
61
|
-
* @param {boolean} autoDestroy -
|
|
62
|
-
* @returns {Timer}
|
|
84
|
+
* Creates a new Timer and adds it to the Time manager.
|
|
85
|
+
* @param {boolean} autoDestroy - Whether the timer should be automatically destroyed when it completes.
|
|
86
|
+
* @returns {Timer} The created Timer object.
|
|
63
87
|
*/
|
|
64
88
|
create(autoDestroy = true) {
|
|
65
89
|
const timer = new Timer(this.game, autoDestroy);
|
|
@@ -68,7 +92,7 @@ export class Time {
|
|
|
68
92
|
}
|
|
69
93
|
|
|
70
94
|
/**
|
|
71
|
-
*
|
|
95
|
+
* Removes all timers from the Time manager.
|
|
72
96
|
*/
|
|
73
97
|
removeAll() {
|
|
74
98
|
for (let i = 0; i < this._timers.length; i += 1) {
|
|
@@ -79,7 +103,7 @@ export class Time {
|
|
|
79
103
|
}
|
|
80
104
|
|
|
81
105
|
/**
|
|
82
|
-
*
|
|
106
|
+
* Refreshes the time tracking values.
|
|
83
107
|
*/
|
|
84
108
|
refresh() {
|
|
85
109
|
const previousDateNow = this.time;
|
|
@@ -90,8 +114,8 @@ export class Time {
|
|
|
90
114
|
}
|
|
91
115
|
|
|
92
116
|
/**
|
|
93
|
-
*
|
|
94
|
-
* @param {number} time -
|
|
117
|
+
* Updates the Time manager with a new timestamp.
|
|
118
|
+
* @param {number} time - The new timestamp to use for updating.
|
|
95
119
|
*/
|
|
96
120
|
update(time) {
|
|
97
121
|
const previousDateNow = this.time;
|
|
@@ -120,7 +144,7 @@ export class Time {
|
|
|
120
144
|
}
|
|
121
145
|
|
|
122
146
|
/**
|
|
123
|
-
*
|
|
147
|
+
* Updates all timers managed by the Time manager.
|
|
124
148
|
*/
|
|
125
149
|
updateTimers() {
|
|
126
150
|
let i = 0;
|
|
@@ -137,7 +161,7 @@ export class Time {
|
|
|
137
161
|
}
|
|
138
162
|
|
|
139
163
|
/**
|
|
140
|
-
*
|
|
164
|
+
* Updates the advanced timing values.
|
|
141
165
|
*/
|
|
142
166
|
updateAdvancedTiming() {
|
|
143
167
|
// count the number of time.update calls
|
|
@@ -163,7 +187,7 @@ export class Time {
|
|
|
163
187
|
}
|
|
164
188
|
|
|
165
189
|
/**
|
|
166
|
-
*
|
|
190
|
+
* Handles game pause event.
|
|
167
191
|
*/
|
|
168
192
|
gamePaused() {
|
|
169
193
|
this._pauseStarted = Date.now();
|
|
@@ -176,7 +200,7 @@ export class Time {
|
|
|
176
200
|
}
|
|
177
201
|
|
|
178
202
|
/**
|
|
179
|
-
*
|
|
203
|
+
* Handles game resume event.
|
|
180
204
|
*/
|
|
181
205
|
gameResumed() {
|
|
182
206
|
this.time = Date.now();
|
|
@@ -190,33 +214,33 @@ export class Time {
|
|
|
190
214
|
}
|
|
191
215
|
|
|
192
216
|
/**
|
|
193
|
-
*
|
|
194
|
-
* @returns {number}
|
|
217
|
+
* Gets the total elapsed time in seconds since the game started.
|
|
218
|
+
* @returns {number} The total elapsed time in seconds.
|
|
195
219
|
*/
|
|
196
220
|
totalElapsedSeconds() {
|
|
197
221
|
return (this.time - this._started) * 0.001;
|
|
198
222
|
}
|
|
199
223
|
|
|
200
224
|
/**
|
|
201
|
-
*
|
|
202
|
-
* @param {number} since -
|
|
203
|
-
* @returns {number}
|
|
225
|
+
* Gets the elapsed time in milliseconds since a given timestamp.
|
|
226
|
+
* @param {number} since - The timestamp to calculate elapsed time from.
|
|
227
|
+
* @returns {number} The elapsed time in milliseconds.
|
|
204
228
|
*/
|
|
205
229
|
elapsedSince(since) {
|
|
206
230
|
return this.time - since;
|
|
207
231
|
}
|
|
208
232
|
|
|
209
233
|
/**
|
|
210
|
-
*
|
|
211
|
-
* @param {number} since -
|
|
212
|
-
* @returns {number}
|
|
234
|
+
* Gets the elapsed time in seconds since a given timestamp.
|
|
235
|
+
* @param {number} since - The timestamp to calculate elapsed time from.
|
|
236
|
+
* @returns {number} The elapsed time in seconds.
|
|
213
237
|
*/
|
|
214
238
|
elapsedSecondsSince(since) {
|
|
215
239
|
return (this.time - since) * 0.001;
|
|
216
240
|
}
|
|
217
241
|
|
|
218
242
|
/**
|
|
219
|
-
*
|
|
243
|
+
* Resets the time tracking values.
|
|
220
244
|
*/
|
|
221
245
|
reset() {
|
|
222
246
|
this._started = this.time;
|
|
@@ -224,22 +248,22 @@ export class Time {
|
|
|
224
248
|
}
|
|
225
249
|
|
|
226
250
|
/**
|
|
227
|
-
*
|
|
251
|
+
* Destroys the Time manager and cleans up resources.
|
|
228
252
|
*/
|
|
229
253
|
destroy() {
|
|
230
254
|
this.reset();
|
|
231
255
|
}
|
|
232
256
|
|
|
233
257
|
/**
|
|
234
|
-
*
|
|
235
|
-
* @returns {number}
|
|
258
|
+
* Gets the desired frames per second.
|
|
259
|
+
* @returns {number} The desired frames per second.
|
|
236
260
|
*/
|
|
237
261
|
get desiredFps() {
|
|
238
262
|
return this._desiredFps;
|
|
239
263
|
}
|
|
240
264
|
|
|
241
265
|
/**
|
|
242
|
-
*
|
|
266
|
+
* Sets the desired frames per second.
|
|
243
267
|
*/
|
|
244
268
|
set desiredFps(value) {
|
|
245
269
|
this._desiredFps = value;
|
package/src/phaser/core/timer.js
CHANGED
|
@@ -3,43 +3,61 @@ import { TimerEvent } from './timer_event.js';
|
|
|
3
3
|
|
|
4
4
|
export class Timer {
|
|
5
5
|
/**
|
|
6
|
-
*
|
|
7
|
-
* @param {import('./game.js').Game} game -
|
|
8
|
-
* @param {boolean} autoDestroy -
|
|
6
|
+
* Creates a new Timer instance.
|
|
7
|
+
* @param {import('./game.js').Game} game - The game instance.
|
|
8
|
+
* @param {boolean} autoDestroy - Whether to automatically destroy the timer when it completes.
|
|
9
9
|
*/
|
|
10
10
|
constructor(game, autoDestroy = false) {
|
|
11
11
|
this.game = game;
|
|
12
|
+
/** @type {boolean} */
|
|
12
13
|
this.running = false;
|
|
14
|
+
/** @type {boolean} */
|
|
13
15
|
this.autoDestroy = autoDestroy;
|
|
16
|
+
/** @type {boolean} */
|
|
14
17
|
this.expired = false;
|
|
18
|
+
/** @type {number} */
|
|
15
19
|
this.elapsed = 0;
|
|
16
20
|
/** @type {TimerEvent[]} */
|
|
17
21
|
this.events = [];
|
|
22
|
+
/** @type {Signal} */
|
|
18
23
|
this.onComplete = new Signal();
|
|
24
|
+
/** @type {number} */
|
|
19
25
|
this.nextTick = 0;
|
|
26
|
+
/** @type {number} */
|
|
20
27
|
this.timeCap = 1000;
|
|
28
|
+
/** @type {boolean} */
|
|
21
29
|
this.paused = false;
|
|
30
|
+
/** @type {boolean} */
|
|
22
31
|
this._codePaused = false;
|
|
32
|
+
/** @type {number} */
|
|
23
33
|
this._started = 0;
|
|
34
|
+
/** @type {number} */
|
|
24
35
|
this._pauseStarted = 0;
|
|
36
|
+
/** @type {number} */
|
|
25
37
|
this._pauseTotal = 0;
|
|
38
|
+
/** @type {number} */
|
|
26
39
|
this._now = Date.now();
|
|
40
|
+
/** @type {number} */
|
|
27
41
|
this._len = 0;
|
|
42
|
+
/** @type {number} */
|
|
28
43
|
this._marked = 0;
|
|
44
|
+
/** @type {number} */
|
|
29
45
|
this._i = 0;
|
|
46
|
+
/** @type {number} */
|
|
30
47
|
this._diff = 0;
|
|
48
|
+
/** @type {number} */
|
|
31
49
|
this._newTick = 0;
|
|
32
50
|
}
|
|
33
51
|
|
|
34
52
|
/**
|
|
35
|
-
*
|
|
36
|
-
* @param {number} delay -
|
|
37
|
-
* @param {boolean} loop -
|
|
38
|
-
* @param {number} repeatCount -
|
|
39
|
-
* @param {Function} callback -
|
|
40
|
-
* @param {object} callbackContext -
|
|
41
|
-
* @param {...any} args -
|
|
42
|
-
* @returns {TimerEvent}
|
|
53
|
+
* Creates a new TimerEvent.
|
|
54
|
+
* @param {number} delay - The delay in milliseconds before the event fires.
|
|
55
|
+
* @param {boolean} loop - Whether the event should loop indefinitely.
|
|
56
|
+
* @param {number} repeatCount - The number of times to repeat the event (0 = infinite).
|
|
57
|
+
* @param {Function} callback - The function to call when the event fires.
|
|
58
|
+
* @param {object} callbackContext - The context in which to call the callback.
|
|
59
|
+
* @param {...any} args - Arguments to pass to the callback function.
|
|
60
|
+
* @returns {TimerEvent} The created TimerEvent.
|
|
43
61
|
*/
|
|
44
62
|
create(delay, loop, repeatCount, callback, callbackContext = null, args) {
|
|
45
63
|
const roundedDelay = Math.round(delay);
|
|
@@ -57,45 +75,45 @@ export class Timer {
|
|
|
57
75
|
}
|
|
58
76
|
|
|
59
77
|
/**
|
|
60
|
-
*
|
|
61
|
-
* @param {number} delay -
|
|
62
|
-
* @param {Function} callback -
|
|
63
|
-
* @param {object} callbackContext -
|
|
64
|
-
* @param {...any} args -
|
|
65
|
-
* @returns {TimerEvent}
|
|
78
|
+
* Creates a new TimerEvent that runs once.
|
|
79
|
+
* @param {number} delay - The delay in milliseconds before the event fires.
|
|
80
|
+
* @param {Function} callback - The function to call when the event fires.
|
|
81
|
+
* @param {object} callbackContext - The context in which to call the callback.
|
|
82
|
+
* @param {...any} args - Arguments to pass to the callback function.
|
|
83
|
+
* @returns {TimerEvent} The created TimerEvent.
|
|
66
84
|
*/
|
|
67
85
|
add(delay, callback, callbackContext = null, ...args) {
|
|
68
86
|
return this.create(delay, false, 0, callback, callbackContext, args);
|
|
69
87
|
}
|
|
70
88
|
|
|
71
89
|
/**
|
|
72
|
-
*
|
|
73
|
-
* @param {number} delay -
|
|
74
|
-
* @param {number} repeatCount -
|
|
75
|
-
* @param {Function} callback -
|
|
76
|
-
* @param {object} callbackContext -
|
|
77
|
-
* @param {...any} args -
|
|
78
|
-
* @returns {TimerEvent}
|
|
90
|
+
* Creates a new TimerEvent that repeats a specified number of times.
|
|
91
|
+
* @param {number} delay - The delay in milliseconds before the event fires.
|
|
92
|
+
* @param {number} repeatCount - The number of times to repeat the event (0 = infinite).
|
|
93
|
+
* @param {Function} callback - The function to call when the event fires.
|
|
94
|
+
* @param {object} callbackContext - The context in which to call the callback.
|
|
95
|
+
* @param {...any} args - Arguments to pass to the callback function.
|
|
96
|
+
* @returns {TimerEvent} The created TimerEvent.
|
|
79
97
|
*/
|
|
80
98
|
repeat(delay, repeatCount, callback, callbackContext = null, ...args) {
|
|
81
99
|
return this.create(delay, false, repeatCount, callback, callbackContext, args);
|
|
82
100
|
}
|
|
83
101
|
|
|
84
102
|
/**
|
|
85
|
-
*
|
|
86
|
-
* @param {number} delay -
|
|
87
|
-
* @param {Function} callback -
|
|
88
|
-
* @param {object} callbackContext -
|
|
89
|
-
* @param {...any} args -
|
|
90
|
-
* @returns {TimerEvent}
|
|
103
|
+
* Creates a new TimerEvent that loops indefinitely.
|
|
104
|
+
* @param {number} delay - The delay in milliseconds before the event fires.
|
|
105
|
+
* @param {Function} callback - The function to call when the event fires.
|
|
106
|
+
* @param {object} callbackContext - The context in which to call the callback.
|
|
107
|
+
* @param {...any} args - Arguments to pass to the callback function.
|
|
108
|
+
* @returns {TimerEvent} The created TimerEvent.
|
|
91
109
|
*/
|
|
92
110
|
loop(delay, callback, callbackContext = null, ...args) {
|
|
93
111
|
return this.create(delay, true, 0, callback, callbackContext, args);
|
|
94
112
|
}
|
|
95
113
|
|
|
96
114
|
/**
|
|
97
|
-
*
|
|
98
|
-
* @param {number} delay -
|
|
115
|
+
* Starts the timer.
|
|
116
|
+
* @param {number} delay - The delay in milliseconds before starting (optional).
|
|
99
117
|
*/
|
|
100
118
|
start(delay = 0) {
|
|
101
119
|
if (this.running) {
|
|
@@ -109,8 +127,8 @@ export class Timer {
|
|
|
109
127
|
}
|
|
110
128
|
|
|
111
129
|
/**
|
|
112
|
-
*
|
|
113
|
-
* @param {boolean} clearEvents -
|
|
130
|
+
* Stops the timer.
|
|
131
|
+
* @param {boolean} clearEvents - Whether to clear all events (default: true).
|
|
114
132
|
*/
|
|
115
133
|
stop(clearEvents = true) {
|
|
116
134
|
this.running = false;
|
|
@@ -120,9 +138,9 @@ export class Timer {
|
|
|
120
138
|
}
|
|
121
139
|
|
|
122
140
|
/**
|
|
123
|
-
*
|
|
124
|
-
* @param {TimerEvent | null | undefined} event -
|
|
125
|
-
* @returns {boolean}
|
|
141
|
+
* Removes a TimerEvent from the timer.
|
|
142
|
+
* @param {TimerEvent | null | undefined} event - The TimerEvent to remove.
|
|
143
|
+
* @returns {boolean} True if the event was removed, false otherwise.
|
|
126
144
|
*/
|
|
127
145
|
remove(event) {
|
|
128
146
|
for (let i = 0; i < this.events.length; i += 1) {
|
|
@@ -135,7 +153,7 @@ export class Timer {
|
|
|
135
153
|
}
|
|
136
154
|
|
|
137
155
|
/**
|
|
138
|
-
*
|
|
156
|
+
* Orders the timer events by their next tick time.
|
|
139
157
|
*/
|
|
140
158
|
order() {
|
|
141
159
|
if (this.events.length > 0) {
|
|
@@ -146,10 +164,10 @@ export class Timer {
|
|
|
146
164
|
}
|
|
147
165
|
|
|
148
166
|
/**
|
|
149
|
-
*
|
|
150
|
-
* @param {TimerEvent} a -
|
|
151
|
-
* @param {TimerEvent} b -
|
|
152
|
-
* @returns {number}
|
|
167
|
+
* Sorts TimerEvents by their tick time.
|
|
168
|
+
* @param {TimerEvent} a - First TimerEvent to compare.
|
|
169
|
+
* @param {TimerEvent} b - Second TimerEvent to compare.
|
|
170
|
+
* @returns {number} Comparison result (-1, 0, or 1).
|
|
153
171
|
*/
|
|
154
172
|
sortHandler(a, b) {
|
|
155
173
|
if (a.tick < b.tick) {
|
|
@@ -161,7 +179,7 @@ export class Timer {
|
|
|
161
179
|
}
|
|
162
180
|
|
|
163
181
|
/**
|
|
164
|
-
*
|
|
182
|
+
* Clears pending events from the timer.
|
|
165
183
|
*/
|
|
166
184
|
clearPendingEvents() {
|
|
167
185
|
this._i = this.events.length;
|
|
@@ -176,9 +194,9 @@ export class Timer {
|
|
|
176
194
|
}
|
|
177
195
|
|
|
178
196
|
/**
|
|
179
|
-
*
|
|
180
|
-
* @param {number} time -
|
|
181
|
-
* @returns {boolean}
|
|
197
|
+
* Updates the timer state at a given time.
|
|
198
|
+
* @param {number} time - The current time in milliseconds.
|
|
199
|
+
* @returns {boolean} True if the timer should continue running, false if it should be destroyed.
|
|
182
200
|
*/
|
|
183
201
|
update(time) {
|
|
184
202
|
if (this.paused) {
|
|
@@ -236,7 +254,7 @@ export class Timer {
|
|
|
236
254
|
}
|
|
237
255
|
|
|
238
256
|
/**
|
|
239
|
-
*
|
|
257
|
+
* Pauses the timer.
|
|
240
258
|
*/
|
|
241
259
|
pause() {
|
|
242
260
|
if (!this.running) {
|
|
@@ -251,7 +269,7 @@ export class Timer {
|
|
|
251
269
|
}
|
|
252
270
|
|
|
253
271
|
/**
|
|
254
|
-
*
|
|
272
|
+
* Internal pause method for the timer.
|
|
255
273
|
*/
|
|
256
274
|
_pause() {
|
|
257
275
|
if (this.paused || !this.running) {
|
|
@@ -262,8 +280,8 @@ export class Timer {
|
|
|
262
280
|
}
|
|
263
281
|
|
|
264
282
|
/**
|
|
265
|
-
*
|
|
266
|
-
* @param {number} baseTime -
|
|
283
|
+
* Adjusts timer events when time has jumped (e.g., when tab is switched).
|
|
284
|
+
* @param {number} baseTime - The time to adjust from.
|
|
267
285
|
*/
|
|
268
286
|
adjustEvents(baseTime) {
|
|
269
287
|
for (let i = 0; i < this.events.length; i += 1) {
|
|
@@ -286,7 +304,7 @@ export class Timer {
|
|
|
286
304
|
}
|
|
287
305
|
|
|
288
306
|
/**
|
|
289
|
-
*
|
|
307
|
+
* Resumes the timer.
|
|
290
308
|
*/
|
|
291
309
|
resume() {
|
|
292
310
|
if (!this.paused) {
|
|
@@ -301,7 +319,7 @@ export class Timer {
|
|
|
301
319
|
}
|
|
302
320
|
|
|
303
321
|
/**
|
|
304
|
-
*
|
|
322
|
+
* Internal resume method for the timer.
|
|
305
323
|
*/
|
|
306
324
|
_resume() {
|
|
307
325
|
if (this._codePaused) {
|
|
@@ -311,7 +329,7 @@ export class Timer {
|
|
|
311
329
|
}
|
|
312
330
|
|
|
313
331
|
/**
|
|
314
|
-
*
|
|
332
|
+
* Removes all events from the timer.
|
|
315
333
|
*/
|
|
316
334
|
removeAll() {
|
|
317
335
|
this.onComplete.removeAll();
|
|
@@ -321,7 +339,7 @@ export class Timer {
|
|
|
321
339
|
}
|
|
322
340
|
|
|
323
341
|
/**
|
|
324
|
-
*
|
|
342
|
+
* Destroys the timer and cleans up resources.
|
|
325
343
|
*/
|
|
326
344
|
destroy() {
|
|
327
345
|
this.onComplete.removeAll();
|
|
@@ -332,16 +350,16 @@ export class Timer {
|
|
|
332
350
|
}
|
|
333
351
|
|
|
334
352
|
/**
|
|
335
|
-
*
|
|
336
|
-
* @returns {number}
|
|
353
|
+
* Gets the next tick time for the timer.
|
|
354
|
+
* @returns {number} The next tick time in milliseconds.
|
|
337
355
|
*/
|
|
338
356
|
get next() {
|
|
339
357
|
return this.nextTick;
|
|
340
358
|
}
|
|
341
359
|
|
|
342
360
|
/**
|
|
343
|
-
*
|
|
344
|
-
* @returns {number}
|
|
361
|
+
* Gets the duration until the next event.
|
|
362
|
+
* @returns {number} The duration in milliseconds.
|
|
345
363
|
*/
|
|
346
364
|
get duration() {
|
|
347
365
|
if (this.running && this.nextTick > this._now) {
|
|
@@ -351,16 +369,16 @@ export class Timer {
|
|
|
351
369
|
}
|
|
352
370
|
|
|
353
371
|
/**
|
|
354
|
-
*
|
|
355
|
-
* @returns {number}
|
|
372
|
+
* Gets the number of active events in the timer.
|
|
373
|
+
* @returns {number} The number of events.
|
|
356
374
|
*/
|
|
357
375
|
get length() {
|
|
358
376
|
return this.events.length;
|
|
359
377
|
}
|
|
360
378
|
|
|
361
379
|
/**
|
|
362
|
-
*
|
|
363
|
-
* @returns {number}
|
|
380
|
+
* Gets the elapsed time since the timer started.
|
|
381
|
+
* @returns {number} The elapsed time in milliseconds.
|
|
364
382
|
*/
|
|
365
383
|
get ms() {
|
|
366
384
|
if (this.running) {
|
|
@@ -370,8 +388,8 @@ export class Timer {
|
|
|
370
388
|
}
|
|
371
389
|
|
|
372
390
|
/**
|
|
373
|
-
*
|
|
374
|
-
* @returns {number}
|
|
391
|
+
* Gets the elapsed time in seconds since the timer started.
|
|
392
|
+
* @returns {number} The elapsed time in seconds.
|
|
375
393
|
*/
|
|
376
394
|
get seconds() {
|
|
377
395
|
if (this.running) {
|
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
export class TimerEvent {
|
|
2
2
|
/**
|
|
3
|
-
*
|
|
4
|
-
* @param {import('./timer.js').Timer} timer -
|
|
5
|
-
* @param {number} delay -
|
|
6
|
-
* @param {number} tick -
|
|
7
|
-
* @param {number} repeatCount -
|
|
8
|
-
* @param {boolean} loop -
|
|
9
|
-
* @param {Function} callback -
|
|
10
|
-
* @param {object} callbackContext -
|
|
11
|
-
* @param {...any} args -
|
|
3
|
+
* Creates a new TimerEvent instance.
|
|
4
|
+
* @param {import('./timer.js').Timer} timer - Reference to the parent Timer.
|
|
5
|
+
* @param {number} delay - The delay (in milliseconds) before the event should occur.
|
|
6
|
+
* @param {number} tick - The tick (in milliseconds) at which the event should occur.
|
|
7
|
+
* @param {number} repeatCount - The number of times the event should repeat.
|
|
8
|
+
* @param {boolean} loop - Whether the event should loop.
|
|
9
|
+
* @param {Function} callback - The function to call when the event occurs.
|
|
10
|
+
* @param {object} callbackContext - The context in which to call the callback function.
|
|
11
|
+
* @param {...any} args - Arguments to pass to the callback function.
|
|
12
12
|
*/
|
|
13
13
|
constructor(timer, delay, tick, repeatCount, loop, callback, callbackContext, args) {
|
|
14
14
|
this.timer = timer;
|