mani-game-engine 1.0.0-pre.62 → 1.0.0-pre.63
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/lib/clock.d.ts +3 -1
- package/lib/clock.js +11 -8
- package/lib/clock.js.map +1 -1
- package/package.json +1 -1
- package/src/clock.ts +128 -124
package/lib/clock.d.ts
CHANGED
|
@@ -13,6 +13,7 @@ export type Timeout = {
|
|
|
13
13
|
};
|
|
14
14
|
export type GameClockOptions = Partial<typeof defaultOptions>;
|
|
15
15
|
export interface Clock {
|
|
16
|
+
readonly gameTime: number;
|
|
16
17
|
onPrepare: Signal<unknown>;
|
|
17
18
|
onEarlyUpdate: Signal<OnEarlyUpdateParams>;
|
|
18
19
|
onUpdate: Signal<OnUpdateParams>;
|
|
@@ -26,13 +27,14 @@ export interface Clock {
|
|
|
26
27
|
clearTimeout(timeoutId: number): boolean;
|
|
27
28
|
}
|
|
28
29
|
export declare class GameClock implements Clock {
|
|
30
|
+
get gameTime(): number;
|
|
29
31
|
private nextTimeoutId;
|
|
30
32
|
private timeouts;
|
|
31
33
|
private isRunning;
|
|
32
34
|
private requestId;
|
|
33
35
|
private currentTime;
|
|
34
36
|
private accumulator;
|
|
35
|
-
private
|
|
37
|
+
private _gameTime;
|
|
36
38
|
private readonly _fixedDeltaTime;
|
|
37
39
|
private update;
|
|
38
40
|
private readonly maxFrameTime;
|
package/lib/clock.js
CHANGED
|
@@ -10,6 +10,9 @@ const defaultOptions = {
|
|
|
10
10
|
timeScale: 1,
|
|
11
11
|
};
|
|
12
12
|
class GameClock {
|
|
13
|
+
get gameTime() {
|
|
14
|
+
return this._gameTime;
|
|
15
|
+
}
|
|
13
16
|
constructor(options) {
|
|
14
17
|
this.nextTimeoutId = 0;
|
|
15
18
|
this.timeouts = new Map();
|
|
@@ -17,7 +20,7 @@ class GameClock {
|
|
|
17
20
|
this.requestId = 0;
|
|
18
21
|
this.currentTime = 0;
|
|
19
22
|
this.accumulator = 0;
|
|
20
|
-
this.
|
|
23
|
+
this._gameTime = 0;
|
|
21
24
|
this.update = (time) => {
|
|
22
25
|
const newTime = time * 0.001;
|
|
23
26
|
if (!this.isRunning) {
|
|
@@ -32,9 +35,9 @@ class GameClock {
|
|
|
32
35
|
this.accumulator += frameTime;
|
|
33
36
|
let isMultipleFixedUpdate = false;
|
|
34
37
|
this.onPrepare.dispatch();
|
|
35
|
-
this.onEarlyUpdate.dispatch({ time: this.
|
|
38
|
+
this.onEarlyUpdate.dispatch({ time: this._gameTime, deltaTime: frameTime });
|
|
36
39
|
for (const [id, timeOut] of this.timeouts) {
|
|
37
|
-
if (this.
|
|
40
|
+
if (this._gameTime >= timeOut.callTime) {
|
|
38
41
|
timeOut.callback();
|
|
39
42
|
if (timeOut.recall) {
|
|
40
43
|
timeOut.callTime += timeOut.recall;
|
|
@@ -48,13 +51,13 @@ class GameClock {
|
|
|
48
51
|
if (isMultipleFixedUpdate) {
|
|
49
52
|
this.onPrepare.dispatch();
|
|
50
53
|
}
|
|
51
|
-
this.onFixedUpdate.dispatch({ time: this.
|
|
54
|
+
this.onFixedUpdate.dispatch({ time: this._gameTime, deltaTime: this._fixedDeltaTime });
|
|
52
55
|
isMultipleFixedUpdate = true;
|
|
53
56
|
this.accumulator -= this._fixedDeltaTime;
|
|
54
|
-
this.
|
|
57
|
+
this._gameTime += this._fixedDeltaTime;
|
|
55
58
|
}
|
|
56
59
|
const alpha = this.accumulator / this._fixedDeltaTime;
|
|
57
|
-
this.onUpdate.dispatch({ time: this.
|
|
60
|
+
this.onUpdate.dispatch({ time: this._gameTime + alpha * this._fixedDeltaTime, deltaTime: frameTime, alpha: alpha });
|
|
58
61
|
if (this.isRunning) {
|
|
59
62
|
this.requestId = requestAnimationFrame(this.update);
|
|
60
63
|
}
|
|
@@ -99,13 +102,13 @@ class GameClock {
|
|
|
99
102
|
return this;
|
|
100
103
|
}
|
|
101
104
|
setTimeout(callback, delay) {
|
|
102
|
-
const timeOut = { callback, callTime: this.
|
|
105
|
+
const timeOut = { callback, callTime: this._gameTime + delay };
|
|
103
106
|
this.nextTimeoutId++;
|
|
104
107
|
this.timeouts.set(this.nextTimeoutId, timeOut);
|
|
105
108
|
return this.nextTimeoutId;
|
|
106
109
|
}
|
|
107
110
|
setInterval(callback, delay) {
|
|
108
|
-
const timeOut = { callback, callTime: this.
|
|
111
|
+
const timeOut = { callback, callTime: this._gameTime + delay, recall: delay };
|
|
109
112
|
this.nextTimeoutId++;
|
|
110
113
|
this.timeouts.set(this.nextTimeoutId, timeOut);
|
|
111
114
|
return this.nextTimeoutId;
|
package/lib/clock.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"clock.js","sourceRoot":"","sources":["../src/clock.ts"],"names":[],"mappings":";;;AAAA,6CAAmC;AAGnC,MAAM,cAAc,GAAG,OAAO,CAAC;AAI/B,MAAM,cAAc,GAAG;
|
|
1
|
+
{"version":3,"file":"clock.js","sourceRoot":"","sources":["../src/clock.ts"],"names":[],"mappings":";;;AAAA,6CAAmC;AAGnC,MAAM,cAAc,GAAG,OAAO,CAAC;AAI/B,MAAM,cAAc,GAAG;IACrB,SAAS,EAAE,IAAI;IACf,cAAc,EAAE,CAAC,GAAG,EAAE;IACtB,YAAY,EAAE,IAAI;IAClB,SAAS,EAAE,CAAC;CACb,CAAC;AAwBF,MAAa,SAAS;IACpB,IAAI,QAAQ;QACV,OAAO,IAAI,CAAC,SAAS,CAAC;IACxB,CAAC;IAiED,YAAY,OAA0B;QA/D9B,kBAAa,GAAG,CAAC,CAAC;QAClB,aAAQ,GAAG,IAAI,GAAG,EAAmB,CAAC;QACtC,cAAS,GAAG,KAAK,CAAC;QAClB,cAAS,GAAG,CAAC,CAAC;QACd,gBAAW,GAAG,CAAC,CAAC;QAChB,gBAAW,GAAG,CAAC,CAAC;QAChB,cAAS,GAAG,CAAC,CAAC;QAGd,WAAM,GAAG,CAAC,IAAY,EAAE,EAAE;YAChC,MAAM,OAAO,GAAG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KAAa;QAC3C,MAAM,OAAO,GAAY,EAAC,QAAQ,EAAE,QAAQ,EAAE,IAAI,CAAC,SAAS,GAAG,KAAK,EAAE,MAAM,EAAE,KAAK,EAAC,CAAC;QACrF,IAAI,CAAC,aAAa,EAAE,CAAC;QACrB,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,IAAI,CAAC,aAAa,EAAE,OAAO,CAAC,CAAC;QAC/C,OAAO,IAAI,CAAC,aAAa,CAAC;IAC5B,CAAC;IAED,YAAY,CAAC,SAAiB;QAC5B,OAAO,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC;IACzC,CAAC;CACF;AAlID,8BAkIC"}
|
package/package.json
CHANGED
package/src/clock.ts
CHANGED
|
@@ -6,158 +6,162 @@ const MIN_TIME_SCALE = 0.00001;
|
|
|
6
6
|
// type OnUpdateParams = { time: number, deltaTime: number, alpha: number; };
|
|
7
7
|
|
|
8
8
|
const defaultOptions = {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
9
|
+
autoStart: true,
|
|
10
|
+
fixedDeltaTime: 1 / 60,
|
|
11
|
+
maxFrameTime: 0.25,
|
|
12
|
+
timeScale: 1,
|
|
13
13
|
};
|
|
14
14
|
|
|
15
15
|
export type Timeout = {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
16
|
+
recall?: number;
|
|
17
|
+
callback: Function;
|
|
18
|
+
callTime: number;
|
|
19
19
|
};
|
|
20
20
|
export type GameClockOptions = Partial<typeof defaultOptions>;
|
|
21
21
|
|
|
22
22
|
export interface Clock {
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
23
|
+
readonly gameTime: number;
|
|
24
|
+
onPrepare: Signal<unknown>;
|
|
25
|
+
onEarlyUpdate: Signal<OnEarlyUpdateParams>;
|
|
26
|
+
onUpdate: Signal<OnUpdateParams>;
|
|
27
|
+
onFixedUpdate: Signal<OnFixedUpdateParams>;
|
|
28
|
+
timeScale: number;
|
|
29
|
+
start(): this;
|
|
30
|
+
stop(): this;
|
|
31
|
+
dispose(): void;
|
|
32
|
+
setTimeout(callback: Function, delay: number): number;
|
|
33
|
+
setInterval(callback: Function, delay: number): number;
|
|
34
|
+
clearTimeout(timeoutId: number): boolean;
|
|
35
35
|
}
|
|
36
36
|
|
|
37
37
|
export class GameClock implements Clock {
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
38
|
+
get gameTime(): number {
|
|
39
|
+
return this._gameTime;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
private nextTimeoutId = 0;
|
|
43
|
+
private timeouts = new Map<number, Timeout>();
|
|
44
|
+
private isRunning = false;
|
|
45
|
+
private requestId = 0;
|
|
46
|
+
private currentTime = 0;
|
|
47
|
+
private accumulator = 0;
|
|
48
|
+
private _gameTime = 0;
|
|
49
|
+
|
|
50
|
+
private readonly _fixedDeltaTime: number;
|
|
51
|
+
private update = (time: number) => {
|
|
52
|
+
const newTime = time * 0.001;
|
|
53
|
+
if (!this.isRunning) {
|
|
54
|
+
this.currentTime = newTime;
|
|
55
|
+
this.isRunning = true;
|
|
56
|
+
}
|
|
53
57
|
|
|
54
|
-
|
|
58
|
+
let frameTime = (newTime - this.currentTime) * this.timeScale;
|
|
55
59
|
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
60
|
+
if (frameTime > this.maxFrameTime) {
|
|
61
|
+
frameTime = this.maxFrameTime;
|
|
62
|
+
}
|
|
59
63
|
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
64
|
+
this.currentTime = newTime;
|
|
65
|
+
this.accumulator += frameTime;
|
|
66
|
+
let isMultipleFixedUpdate = false;
|
|
63
67
|
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
for (const [id, timeOut] of this.timeouts) {
|
|
68
|
-
if (this.gameTime >= timeOut.callTime) {
|
|
69
|
-
timeOut.callback();
|
|
70
|
-
if (timeOut.recall) {
|
|
71
|
-
timeOut.callTime += timeOut.recall;
|
|
72
|
-
} else {
|
|
73
|
-
this.timeouts.delete(id);
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
}
|
|
68
|
+
this.onPrepare.dispatch();
|
|
69
|
+
this.onEarlyUpdate.dispatch({time: this._gameTime, deltaTime: frameTime});
|
|
77
70
|
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
this.gameTime += this._fixedDeltaTime;
|
|
86
|
-
}
|
|
87
|
-
const alpha = this.accumulator / this._fixedDeltaTime;
|
|
88
|
-
this.onUpdate.dispatch({time: this.gameTime + alpha * this._fixedDeltaTime, deltaTime: frameTime, alpha: alpha});
|
|
89
|
-
if (this.isRunning) {
|
|
90
|
-
this.requestId = requestAnimationFrame(this.update);
|
|
71
|
+
for (const [id, timeOut] of this.timeouts) {
|
|
72
|
+
if (this._gameTime >= timeOut.callTime) {
|
|
73
|
+
timeOut.callback();
|
|
74
|
+
if (timeOut.recall) {
|
|
75
|
+
timeOut.callTime += timeOut.recall;
|
|
76
|
+
} else {
|
|
77
|
+
this.timeouts.delete(id);
|
|
91
78
|
}
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
private readonly maxFrameTime: number;
|
|
95
|
-
|
|
96
|
-
readonly onPrepare = new Signal();
|
|
97
|
-
readonly onEarlyUpdate = new Signal<OnEarlyUpdateParams>();
|
|
98
|
-
readonly onUpdate = new Signal<OnUpdateParams>();
|
|
99
|
-
readonly onFixedUpdate = new Signal<OnFixedUpdateParams>();
|
|
100
|
-
|
|
101
|
-
constructor(options?: GameClockOptions) {
|
|
102
|
-
const opts = {...defaultOptions, ...options} as Required<GameClockOptions>;
|
|
103
|
-
this._fixedDeltaTime = opts.fixedDeltaTime;
|
|
104
|
-
this.maxFrameTime = opts.maxFrameTime;
|
|
105
|
-
this.timeScale = opts.timeScale;
|
|
106
|
-
|
|
107
|
-
opts.autoStart && this.start();
|
|
79
|
+
}
|
|
108
80
|
}
|
|
109
81
|
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
this.
|
|
113
|
-
|
|
114
|
-
|
|
82
|
+
while (this.accumulator >= this._fixedDeltaTime) {
|
|
83
|
+
if (isMultipleFixedUpdate) {
|
|
84
|
+
this.onPrepare.dispatch();
|
|
85
|
+
}
|
|
86
|
+
this.onFixedUpdate.dispatch({time: this._gameTime, deltaTime: this._fixedDeltaTime});
|
|
87
|
+
isMultipleFixedUpdate = true;
|
|
88
|
+
this.accumulator -= this._fixedDeltaTime;
|
|
89
|
+
this._gameTime += this._fixedDeltaTime;
|
|
90
|
+
}
|
|
91
|
+
const alpha = this.accumulator / this._fixedDeltaTime;
|
|
92
|
+
this.onUpdate.dispatch({time: this._gameTime + alpha * this._fixedDeltaTime, deltaTime: frameTime, alpha: alpha});
|
|
93
|
+
if (this.isRunning) {
|
|
94
|
+
this.requestId = requestAnimationFrame(this.update);
|
|
115
95
|
}
|
|
96
|
+
};
|
|
116
97
|
|
|
117
|
-
|
|
98
|
+
private readonly maxFrameTime: number;
|
|
118
99
|
|
|
119
|
-
|
|
100
|
+
readonly onPrepare = new Signal();
|
|
101
|
+
readonly onEarlyUpdate = new Signal<OnEarlyUpdateParams>();
|
|
102
|
+
readonly onUpdate = new Signal<OnUpdateParams>();
|
|
103
|
+
readonly onFixedUpdate = new Signal<OnFixedUpdateParams>();
|
|
120
104
|
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
105
|
+
constructor(options?: GameClockOptions) {
|
|
106
|
+
const opts = {...defaultOptions, ...options} as Required<GameClockOptions>;
|
|
107
|
+
this._fixedDeltaTime = opts.fixedDeltaTime;
|
|
108
|
+
this.maxFrameTime = opts.maxFrameTime;
|
|
109
|
+
this.timeScale = opts.timeScale;
|
|
124
110
|
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
}
|
|
111
|
+
opts.autoStart && this.start();
|
|
112
|
+
}
|
|
128
113
|
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
}
|
|
114
|
+
dispose(): void {
|
|
115
|
+
this.onPrepare.detachAll();
|
|
116
|
+
this.onEarlyUpdate.detachAll();
|
|
117
|
+
this.onUpdate.detachAll();
|
|
118
|
+
this.onFixedUpdate.detachAll();
|
|
119
|
+
}
|
|
136
120
|
|
|
137
|
-
|
|
138
|
-
if (!this.isRunning) {
|
|
139
|
-
return this;
|
|
140
|
-
}
|
|
141
|
-
this.isRunning = false;
|
|
142
|
-
cancelAnimationFrame(this.requestId);
|
|
143
|
-
return this;
|
|
144
|
-
}
|
|
121
|
+
get fixedDeltaTime(): number { return this._fixedDeltaTime; }
|
|
145
122
|
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
123
|
+
private _timeScale = 1;
|
|
124
|
+
|
|
125
|
+
get timeScale(): number {
|
|
126
|
+
return this._timeScale;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
set timeScale(value: number) {
|
|
130
|
+
this._timeScale = Math.max(value, MIN_TIME_SCALE);
|
|
131
|
+
}
|
|
152
132
|
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
this.timeouts.set(this.nextTimeoutId, timeOut);
|
|
157
|
-
return this.nextTimeoutId;
|
|
133
|
+
start(): this {
|
|
134
|
+
if (this.isRunning) {
|
|
135
|
+
return this;
|
|
158
136
|
}
|
|
137
|
+
this.requestId = requestAnimationFrame(this.update);
|
|
138
|
+
return this;
|
|
139
|
+
}
|
|
159
140
|
|
|
160
|
-
|
|
161
|
-
|
|
141
|
+
stop(): this {
|
|
142
|
+
if (!this.isRunning) {
|
|
143
|
+
return this;
|
|
162
144
|
}
|
|
145
|
+
this.isRunning = false;
|
|
146
|
+
cancelAnimationFrame(this.requestId);
|
|
147
|
+
return this;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
setTimeout(callback: Function, delay: number) {
|
|
151
|
+
const timeOut: Timeout = {callback, callTime: this._gameTime + delay};
|
|
152
|
+
this.nextTimeoutId++;
|
|
153
|
+
this.timeouts.set(this.nextTimeoutId, timeOut);
|
|
154
|
+
return this.nextTimeoutId;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
setInterval(callback: Function, delay: number) {
|
|
158
|
+
const timeOut: Timeout = {callback, callTime: this._gameTime + delay, recall: delay};
|
|
159
|
+
this.nextTimeoutId++;
|
|
160
|
+
this.timeouts.set(this.nextTimeoutId, timeOut);
|
|
161
|
+
return this.nextTimeoutId;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
clearTimeout(timeoutId: number) {
|
|
165
|
+
return this.timeouts.delete(timeoutId);
|
|
166
|
+
}
|
|
163
167
|
}
|