extra-game-loop 0.3.4 → 0.3.5
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/README.md +4 -4
- package/lib/game-loop.d.ts +8 -8
- package/lib/game-loop.js +5 -4
- package/lib/game-loop.js.map +1 -1
- package/package.json +1 -1
- package/src/game-loop.ts +12 -12
package/README.md
CHANGED
|
@@ -37,10 +37,10 @@ class GameLoop<FixedDeltaTime extends number = number> {
|
|
|
37
37
|
constructor(options: {
|
|
38
38
|
fixedDeltaTime: FixedDeltaTime /* ms */
|
|
39
39
|
maximumDeltaTime: number /* ms */
|
|
40
|
-
update
|
|
41
|
-
fixedUpdate
|
|
42
|
-
lateUpdate
|
|
43
|
-
render
|
|
40
|
+
update?: (deltaTime: number /* ms */) => void
|
|
41
|
+
fixedUpdate?: (deltaTime: FixedDeltaTime /* ms */) => void
|
|
42
|
+
lateUpdate?: (deltaTime: number /* ms */, alpha: number) => void
|
|
43
|
+
render?: (alpha: number /* [0, 1) */) => void
|
|
44
44
|
})
|
|
45
45
|
|
|
46
46
|
start(): void
|
package/lib/game-loop.d.ts
CHANGED
|
@@ -1,19 +1,19 @@
|
|
|
1
1
|
interface IGameLoopOptions<FixedDeltaTime extends number = number> {
|
|
2
2
|
fixedDeltaTime: FixedDeltaTime;
|
|
3
3
|
maximumDeltaTime: number;
|
|
4
|
-
update
|
|
5
|
-
fixedUpdate
|
|
6
|
-
lateUpdate
|
|
7
|
-
render
|
|
4
|
+
update?: (deltaTime: number) => void;
|
|
5
|
+
fixedUpdate?: (deltaTime: FixedDeltaTime) => void;
|
|
6
|
+
lateUpdate?: (deltaTime: number, alpha: number) => void;
|
|
7
|
+
render?: (alpha: number) => void;
|
|
8
8
|
}
|
|
9
9
|
export declare class GameLoop<FixedDeltaTime extends number> {
|
|
10
10
|
private readonly fsm;
|
|
11
11
|
private readonly fixedDeltaTime;
|
|
12
12
|
private readonly maximumDeltaTime;
|
|
13
|
-
private readonly update
|
|
14
|
-
private readonly fixedUpdate
|
|
15
|
-
private readonly lateUpdate
|
|
16
|
-
private readonly render
|
|
13
|
+
private readonly update?;
|
|
14
|
+
private readonly fixedUpdate?;
|
|
15
|
+
private readonly lateUpdate?;
|
|
16
|
+
private readonly render?;
|
|
17
17
|
private requstId?;
|
|
18
18
|
private lastTimestamp?;
|
|
19
19
|
private deltaTimeAccumulator;
|
package/lib/game-loop.js
CHANGED
|
@@ -56,15 +56,16 @@ class GameLoop {
|
|
|
56
56
|
}
|
|
57
57
|
}
|
|
58
58
|
nextFrame(deltaTime) {
|
|
59
|
+
var _a, _b, _c, _d;
|
|
59
60
|
this.deltaTimeAccumulator = Math.min(this.deltaTimeAccumulator + deltaTime, this.maximumDeltaTime);
|
|
60
|
-
this.update(deltaTime);
|
|
61
|
+
(_a = this.update) === null || _a === void 0 ? void 0 : _a.call(this, deltaTime);
|
|
61
62
|
while (this.deltaTimeAccumulator >= this.fixedDeltaTime) {
|
|
62
|
-
this.fixedUpdate(this.fixedDeltaTime);
|
|
63
|
+
(_b = this.fixedUpdate) === null || _b === void 0 ? void 0 : _b.call(this, this.fixedDeltaTime);
|
|
63
64
|
this.deltaTimeAccumulator -= this.fixedDeltaTime;
|
|
64
65
|
}
|
|
65
66
|
const alpha = this.deltaTimeAccumulator / this.fixedDeltaTime;
|
|
66
|
-
this.lateUpdate(deltaTime, alpha);
|
|
67
|
-
this.render(alpha);
|
|
67
|
+
(_c = this.lateUpdate) === null || _c === void 0 ? void 0 : _c.call(this, deltaTime, alpha);
|
|
68
|
+
(_d = this.render) === null || _d === void 0 ? void 0 : _d.call(this, alpha);
|
|
68
69
|
}
|
|
69
70
|
}
|
|
70
71
|
exports.GameLoop = GameLoop;
|
package/lib/game-loop.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"game-loop.js","sourceRoot":"","sources":["../src/game-loop.ts"],"names":[],"mappings":";;;AAAA,yCAAyE;AACzE,iDAA4C;AAwC5C,IA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AI,CAAC,MAAM,CAAC,CAAA;QAErB,oBAAoB,CAAC,IAAI,CAAC,QAAS,CAAC,CAAA;QAEpC,OAAO,IAAI,CAAC,QAAQ,CAAA;QACpB,OAAO,IAAI,CAAC,aAAa,CAAA;QACzB,IAAI,CAAC,aAAa,GAAG,CAAC,CAAA;IACxB,CAAC;IAED,iBAAiB;QACf,IAAI,IAAI,CAAC,GAAG,CAAC,OAAO,CAAC,KAAK,CAAC,OAAO,CAAC,EAAE;YACnC,OAAO,IAAI,CAAC,aAAa,KAAK,CAAC;gBAC1B,CAAC,CAAC,IAAI,GAAG,IAAI,CAAC,aAAa;gBAC3B,CAAC,CAAC,CAAC,CAAA;SACT;aAAM;YACL,OAAO,CAAC,CAAA;SACT;IACH,CAAC;IAuBD,SAAS,CAAC,SAAiB;;QACzB,IAAI,CAAC,oBAAoB,GAAG,IAAI,CAAC,GAAG,CAClC,IAAI,CAAC,oBAAoB,GAAG,SAAS,EACrC,IAAI,CAAC,gBAAgB,CACtB,CAAA;QAED,MAAA,IAAI,CAAC,MAAM,qDAAG,SAAS,CAAC,CAAA;QAExB,OAAO,IAAI,CAAC,oBAAoB,IAAI,IAAI,CAAC,cAAc,EAAE;YACvD,MAAA,IAAI,CAAC,WAAW,qDAAG,IAAI,CAAC,cAAc,CAAC,CAAA;YACvC,IAAI,CAAC,oBAAoB,IAAI,IAAI,CAAC,cAAc,CAAA;SACjD;QAED,MAAM,KAAK,GAAG,IAAI,CAAC,oBAAoB,GAAG,IAAI,CAAC,cAAc,CAAA;QAC7D,MAAA,IAAI,CAAC,UAAU,qDAAG,SAAS,EAAE,KAAK,CAAC,CAAA;QACnC,MAAA,IAAI,CAAC,MAAM,qDAAG,KAAK,CAAC,CAAA;IACtB,CAAC;CACF;AA5FD,4BA4FC"}
|
package/package.json
CHANGED
package/src/game-loop.ts
CHANGED
|
@@ -18,25 +18,25 @@ interface IGameLoopOptions<FixedDeltaTime extends number = number> {
|
|
|
18
18
|
* 每帧运行一次, 运行早于fixedUpdate.
|
|
19
19
|
* 在此处理用户输入等与物理计算无关的操作, 在update里改变的状态直到下一物理帧时才会反应.
|
|
20
20
|
*/
|
|
21
|
-
update
|
|
21
|
+
update?: (deltaTime: number) => void
|
|
22
22
|
|
|
23
23
|
/**
|
|
24
24
|
* 每物理帧运行一次, 运行晚于update, 早于lateUpdate.
|
|
25
25
|
* deltaTime固定等于fixedDeltaTime.
|
|
26
26
|
* 一帧中可能运行零次, 一次, 或数十次, 不应在此处理物理计算以外的繁重操作.
|
|
27
27
|
*/
|
|
28
|
-
fixedUpdate
|
|
28
|
+
fixedUpdate?: (deltaTime: FixedDeltaTime) => void
|
|
29
29
|
|
|
30
30
|
/**
|
|
31
31
|
* 每帧运行一次, 运行晚于fixedUpdate, 能获得alpha.
|
|
32
32
|
* 出于各种原因, 你可能会想使用它.
|
|
33
33
|
*/
|
|
34
|
-
lateUpdate
|
|
34
|
+
lateUpdate?: (deltaTime: number, alpha: number) => void
|
|
35
35
|
|
|
36
36
|
/**
|
|
37
37
|
* 每帧运行一次, 总是运行在fixedUpdate和udpate之后.
|
|
38
38
|
*/
|
|
39
|
-
render
|
|
39
|
+
render?: (alpha: number) => void
|
|
40
40
|
}
|
|
41
41
|
|
|
42
42
|
enum State {
|
|
@@ -57,10 +57,10 @@ export class GameLoop<FixedDeltaTime extends number> {
|
|
|
57
57
|
private readonly fsm = new FiniteStateMachine(schema, State.Stopped)
|
|
58
58
|
private readonly fixedDeltaTime: FixedDeltaTime
|
|
59
59
|
private readonly maximumDeltaTime: number
|
|
60
|
-
private readonly update
|
|
61
|
-
private readonly fixedUpdate
|
|
62
|
-
private readonly lateUpdate
|
|
63
|
-
private readonly render
|
|
60
|
+
private readonly update?: (deltaTime: number) => void
|
|
61
|
+
private readonly fixedUpdate?: (fixedDeltaTime: FixedDeltaTime) => void
|
|
62
|
+
private readonly lateUpdate?: (deltaTime: number, alpha: number) => void
|
|
63
|
+
private readonly render?: (alpha: number) => void
|
|
64
64
|
private requstId?: number
|
|
65
65
|
private lastTimestamp?: number
|
|
66
66
|
private deltaTimeAccumulator = 0
|
|
@@ -134,15 +134,15 @@ export class GameLoop<FixedDeltaTime extends number> {
|
|
|
134
134
|
, this.maximumDeltaTime
|
|
135
135
|
)
|
|
136
136
|
|
|
137
|
-
this.update(deltaTime)
|
|
137
|
+
this.update?.(deltaTime)
|
|
138
138
|
|
|
139
139
|
while (this.deltaTimeAccumulator >= this.fixedDeltaTime) {
|
|
140
|
-
this.fixedUpdate(this.fixedDeltaTime)
|
|
140
|
+
this.fixedUpdate?.(this.fixedDeltaTime)
|
|
141
141
|
this.deltaTimeAccumulator -= this.fixedDeltaTime
|
|
142
142
|
}
|
|
143
143
|
|
|
144
144
|
const alpha = this.deltaTimeAccumulator / this.fixedDeltaTime
|
|
145
|
-
this.lateUpdate(deltaTime, alpha)
|
|
146
|
-
this.render(alpha)
|
|
145
|
+
this.lateUpdate?.(deltaTime, alpha)
|
|
146
|
+
this.render?.(alpha)
|
|
147
147
|
}
|
|
148
148
|
}
|