extra-game-loop 0.3.5 → 0.3.6
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 +24 -0
- package/lib/game-loop-lite.d.ts +19 -0
- package/lib/game-loop-lite.js +58 -0
- package/lib/game-loop-lite.js.map +1 -0
- package/lib/index.d.ts +1 -0
- package/lib/index.js +1 -0
- package/lib/index.js.map +1 -1
- package/package.json +1 -1
- package/src/game-loop-lite.ts +88 -0
- package/src/game-loop.ts +3 -3
- package/src/index.ts +1 -0
package/README.md
CHANGED
|
@@ -53,3 +53,27 @@ class GameLoop<FixedDeltaTime extends number = number> {
|
|
|
53
53
|
nextFrame(deltaTime: number): void
|
|
54
54
|
}
|
|
55
55
|
```
|
|
56
|
+
|
|
57
|
+
### GameLoopLite
|
|
58
|
+
```ts
|
|
59
|
+
class GameLoopLite {
|
|
60
|
+
constructor(options: {
|
|
61
|
+
update?: (deltaTime: number /* ms */) => void
|
|
62
|
+
render?: () => void
|
|
63
|
+
})
|
|
64
|
+
|
|
65
|
+
start(): void
|
|
66
|
+
stop(): void
|
|
67
|
+
getFramesOfSecond(): number
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* This method allows you to manually advance to the next frame.
|
|
71
|
+
*/
|
|
72
|
+
nextFrame(deltaTime: number): void
|
|
73
|
+
}
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
The lite version of `GameLoop` for situations where the physics system is not needed.
|
|
77
|
+
|
|
78
|
+
Basically, it can be seen as an OOP wrapper for `requestAnimationFrame`,
|
|
79
|
+
so it can be used for non-game projects as well.
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
interface IGameLoopLiteOptions {
|
|
2
|
+
update?: (deltaTime: number) => void;
|
|
3
|
+
render?: () => void;
|
|
4
|
+
}
|
|
5
|
+
export declare class GameLoopLite {
|
|
6
|
+
private readonly fsm;
|
|
7
|
+
private readonly update?;
|
|
8
|
+
private readonly render?;
|
|
9
|
+
private requstId?;
|
|
10
|
+
private lastTimestamp?;
|
|
11
|
+
private lastDeltaTime;
|
|
12
|
+
constructor(options: IGameLoopLiteOptions);
|
|
13
|
+
start(): void;
|
|
14
|
+
stop(): void;
|
|
15
|
+
getFramesOfSecond(): number;
|
|
16
|
+
private loop;
|
|
17
|
+
nextFrame(deltaTime: number): void;
|
|
18
|
+
}
|
|
19
|
+
export {};
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.GameLoopLite = void 0;
|
|
4
|
+
const extra_fsm_1 = require("extra-fsm");
|
|
5
|
+
var State;
|
|
6
|
+
(function (State) {
|
|
7
|
+
State["Stopped"] = "stopped";
|
|
8
|
+
State["Running"] = "running";
|
|
9
|
+
})(State || (State = {}));
|
|
10
|
+
const schema = {
|
|
11
|
+
[State.Stopped]: { start: State.Running },
|
|
12
|
+
[State.Running]: { stop: State.Stopped }
|
|
13
|
+
};
|
|
14
|
+
class GameLoopLite {
|
|
15
|
+
constructor(options) {
|
|
16
|
+
this.fsm = new extra_fsm_1.FiniteStateMachine(schema, State.Stopped);
|
|
17
|
+
this.lastDeltaTime = 0;
|
|
18
|
+
this.loop = () => {
|
|
19
|
+
const timestamp = performance.now();
|
|
20
|
+
const deltaTime = timestamp - this.lastTimestamp;
|
|
21
|
+
this.lastDeltaTime = deltaTime;
|
|
22
|
+
this.lastTimestamp = timestamp;
|
|
23
|
+
this.nextFrame(deltaTime);
|
|
24
|
+
this.requstId = requestAnimationFrame(this.loop);
|
|
25
|
+
};
|
|
26
|
+
this.update = options.update;
|
|
27
|
+
this.render = options.render;
|
|
28
|
+
}
|
|
29
|
+
start() {
|
|
30
|
+
this.fsm.send('start');
|
|
31
|
+
this.lastTimestamp = performance.now();
|
|
32
|
+
this.loop();
|
|
33
|
+
}
|
|
34
|
+
stop() {
|
|
35
|
+
this.fsm.send('stop');
|
|
36
|
+
cancelAnimationFrame(this.requstId);
|
|
37
|
+
delete this.requstId;
|
|
38
|
+
delete this.lastTimestamp;
|
|
39
|
+
this.lastDeltaTime = 0;
|
|
40
|
+
}
|
|
41
|
+
getFramesOfSecond() {
|
|
42
|
+
if (this.fsm.matches(State.Running)) {
|
|
43
|
+
return this.lastDeltaTime !== 0
|
|
44
|
+
? 1000 / this.lastDeltaTime
|
|
45
|
+
: 0;
|
|
46
|
+
}
|
|
47
|
+
else {
|
|
48
|
+
return 0;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
nextFrame(deltaTime) {
|
|
52
|
+
var _a, _b;
|
|
53
|
+
(_a = this.update) === null || _a === void 0 ? void 0 : _a.call(this, deltaTime);
|
|
54
|
+
(_b = this.render) === null || _b === void 0 ? void 0 : _b.call(this);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
exports.GameLoopLite = GameLoopLite;
|
|
58
|
+
//# sourceMappingURL=game-loop-lite.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"game-loop-lite.js","sourceRoot":"","sources":["../src/game-loop-lite.ts"],"names":[],"mappings":";;;AAAA,yCAAyE;AAczE,IAAK,KAGJ;AAHD,WAAK,KAAK;IACR,4BAAmB,CAAA;IACnB,4BAAmB,CAAA;AACrB,CAAC,EAHI,KAAK,KAAL,KAAK,QAGT;AAMD,MAAM,MAAM,GAA4C;IACtD,CAAC,KAAK,CAAC,OAAO,CAAC,EAAE,EAAE,KAAK,EAAE,KAAK,CAAC,OAAO,EAAE;IACzC,CAAC,KAAK,CAAC,OAAO,CAAC,EAAE,EAAE,IAAI,EAAE,KAAK,CAAC,OAAO,EAAE;CACzC,CAAA;AAED,MAAa,YAAY;IAQvB,YAAY,OAA6B;QAPxB,QAAG,GAAG,IAAI,8BAAkB,CAAC,MAAM,EAAE,KAAK,CAAC,OAAO,CAAC,CAAA;QAK5D,kBAAa,GAAG,CAAC,CAAA;QAkCjB,SAAI,GAAG,GAAS,EAAE;YAIxB,MAAM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QAExB,MAAA,IAAI,CAAC,MAAM,oDAAI,CAAA;IACjB,CAAC;CACF;AA3DD,oCA2DC"}
|
package/lib/index.d.ts
CHANGED
package/lib/index.js
CHANGED
|
@@ -15,4 +15,5 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
17
|
__exportStar(require("./game-loop"), exports);
|
|
18
|
+
__exportStar(require("./game-loop-lite"), exports);
|
|
18
19
|
//# sourceMappingURL=index.js.map
|
package/lib/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,8CAA2B"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,8CAA2B;AAC3B,mDAAgC"}
|
package/package.json
CHANGED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import { FiniteStateMachine, IFiniteStateMachineSchema } from 'extra-fsm'
|
|
2
|
+
|
|
3
|
+
interface IGameLoopLiteOptions {
|
|
4
|
+
/**
|
|
5
|
+
* 每帧运行一次, 总是最早运行.
|
|
6
|
+
*/
|
|
7
|
+
update?: (deltaTime: number) => void
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* 每帧运行一次, 总是最晚运行.
|
|
11
|
+
*/
|
|
12
|
+
render?: () => void
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
enum State {
|
|
16
|
+
Stopped = 'stopped'
|
|
17
|
+
, Running = 'running'
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
type Event =
|
|
21
|
+
| 'start'
|
|
22
|
+
| 'stop'
|
|
23
|
+
|
|
24
|
+
const schema: IFiniteStateMachineSchema<State, Event> = {
|
|
25
|
+
[State.Stopped]: { start: State.Running }
|
|
26
|
+
, [State.Running]: { stop: State.Stopped }
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export class GameLoopLite {
|
|
30
|
+
private readonly fsm = new FiniteStateMachine(schema, State.Stopped)
|
|
31
|
+
private readonly update?: (deltaTime: number) => void
|
|
32
|
+
private readonly render?: () => void
|
|
33
|
+
private requstId?: number
|
|
34
|
+
private lastTimestamp?: number
|
|
35
|
+
private lastDeltaTime = 0
|
|
36
|
+
|
|
37
|
+
constructor(options: IGameLoopLiteOptions) {
|
|
38
|
+
this.update = options.update
|
|
39
|
+
this.render = options.render
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
start(): void {
|
|
43
|
+
this.fsm.send('start')
|
|
44
|
+
|
|
45
|
+
this.lastTimestamp = performance.now()
|
|
46
|
+
this.loop()
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
stop(): void {
|
|
50
|
+
this.fsm.send('stop')
|
|
51
|
+
|
|
52
|
+
cancelAnimationFrame(this.requstId!)
|
|
53
|
+
|
|
54
|
+
delete this.requstId
|
|
55
|
+
delete this.lastTimestamp
|
|
56
|
+
this.lastDeltaTime = 0
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
getFramesOfSecond(): number {
|
|
60
|
+
if (this.fsm.matches(State.Running)) {
|
|
61
|
+
return this.lastDeltaTime !== 0
|
|
62
|
+
? 1000 / this.lastDeltaTime
|
|
63
|
+
: 0
|
|
64
|
+
} else {
|
|
65
|
+
return 0
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
private loop = (): void => {
|
|
70
|
+
// requestAnimationFrame的实现存在陷阱, 它提供的timestamp参数有时会比`performance.now()`早.
|
|
71
|
+
// 因此主动调用`performance.now()`来获取时间戳.
|
|
72
|
+
// https://stackoverflow.com/questions/50895206/exact-time-of-display-requestanimationframe-usage-and-timeline
|
|
73
|
+
const timestamp = performance.now()
|
|
74
|
+
const deltaTime = timestamp - this.lastTimestamp!
|
|
75
|
+
this.lastDeltaTime = deltaTime
|
|
76
|
+
this.lastTimestamp = timestamp
|
|
77
|
+
|
|
78
|
+
this.nextFrame(deltaTime)
|
|
79
|
+
|
|
80
|
+
this.requstId = requestAnimationFrame(this.loop)
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
nextFrame(deltaTime: number): void {
|
|
84
|
+
this.update?.(deltaTime)
|
|
85
|
+
|
|
86
|
+
this.render?.()
|
|
87
|
+
}
|
|
88
|
+
}
|
package/src/game-loop.ts
CHANGED
|
@@ -15,7 +15,7 @@ interface IGameLoopOptions<FixedDeltaTime extends number = number> {
|
|
|
15
15
|
maximumDeltaTime: number
|
|
16
16
|
|
|
17
17
|
/**
|
|
18
|
-
* 每帧运行一次,
|
|
18
|
+
* 每帧运行一次, 总是最早运行.
|
|
19
19
|
* 在此处理用户输入等与物理计算无关的操作, 在update里改变的状态直到下一物理帧时才会反应.
|
|
20
20
|
*/
|
|
21
21
|
update?: (deltaTime: number) => void
|
|
@@ -34,7 +34,7 @@ interface IGameLoopOptions<FixedDeltaTime extends number = number> {
|
|
|
34
34
|
lateUpdate?: (deltaTime: number, alpha: number) => void
|
|
35
35
|
|
|
36
36
|
/**
|
|
37
|
-
* 每帧运行一次,
|
|
37
|
+
* 每帧运行一次, 总是最晚运行.
|
|
38
38
|
*/
|
|
39
39
|
render?: (alpha: number) => void
|
|
40
40
|
}
|
|
@@ -122,7 +122,7 @@ export class GameLoop<FixedDeltaTime extends number> {
|
|
|
122
122
|
}
|
|
123
123
|
|
|
124
124
|
/**
|
|
125
|
-
*
|
|
125
|
+
* nextFrame依序做以下几件事:
|
|
126
126
|
* 1. 响应用户输入, 为这一帧的游戏世界做出非物理方面的更新, 例如角色转向, 改变武器瞄准角度等.
|
|
127
127
|
* 2. 响应游戏世界从上一帧更新后到这一帧更新之前发生的物理变化.
|
|
128
128
|
* 注意, 如果渲染帧率比物理帧率快, 且运行性能良好, 则物理帧不一定会在此帧更新.
|
package/src/index.ts
CHANGED