extra-game-loop 0.3.3 → 0.3.4

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/package.json CHANGED
@@ -1,10 +1,11 @@
1
1
  {
2
2
  "name": "extra-game-loop",
3
- "version": "0.3.3",
3
+ "version": "0.3.4",
4
4
  "description": "",
5
5
  "keywords": [],
6
6
  "files": [
7
- "lib"
7
+ "lib",
8
+ "src"
8
9
  ],
9
10
  "main": "lib/index.js",
10
11
  "types": "lib/index.d.ts",
@@ -0,0 +1,148 @@
1
+ import { FiniteStateMachine, IFiniteStateMachineSchema } from 'extra-fsm'
2
+ import { assert } from '@blackglory/prelude'
3
+
4
+ interface IGameLoopOptions<FixedDeltaTime extends number = number> {
5
+ /**
6
+ * 物理帧一帧经过的时间, 可以通过`1000/fps`得来, 或物理引擎指示的最小deltaTime.
7
+ */
8
+ fixedDeltaTime: FixedDeltaTime
9
+
10
+ /**
11
+ * 将渲染帧的deltaTime拆分成物理帧时, 允许的最大渲染帧deltaTime,
12
+ * 如果实际的渲染帧deltaTime超过此值, 则它会被重置为此值.
13
+ * 这可以在性能较差的设备上解决因物理帧执行需要带时间过长而导致渲染帧率无限下降的"死亡螺旋"问题.
14
+ */
15
+ maximumDeltaTime: number
16
+
17
+ /**
18
+ * 每帧运行一次, 运行早于fixedUpdate.
19
+ * 在此处理用户输入等与物理计算无关的操作, 在update里改变的状态直到下一物理帧时才会反应.
20
+ */
21
+ update: (deltaTime: number) => void
22
+
23
+ /**
24
+ * 每物理帧运行一次, 运行晚于update, 早于lateUpdate.
25
+ * deltaTime固定等于fixedDeltaTime.
26
+ * 一帧中可能运行零次, 一次, 或数十次, 不应在此处理物理计算以外的繁重操作.
27
+ */
28
+ fixedUpdate: (deltaTime: FixedDeltaTime) => void
29
+
30
+ /**
31
+ * 每帧运行一次, 运行晚于fixedUpdate, 能获得alpha.
32
+ * 出于各种原因, 你可能会想使用它.
33
+ */
34
+ lateUpdate: (deltaTime: number, alpha: number) => void
35
+
36
+ /**
37
+ * 每帧运行一次, 总是运行在fixedUpdate和udpate之后.
38
+ */
39
+ render: (alpha: number) => void
40
+ }
41
+
42
+ enum State {
43
+ Stopped = 'stopped'
44
+ , Running = 'running'
45
+ }
46
+
47
+ type Event =
48
+ | 'start'
49
+ | 'stop'
50
+
51
+ const schema: IFiniteStateMachineSchema<State, Event> = {
52
+ [State.Stopped]: { start: State.Running }
53
+ , [State.Running]: { stop: State.Stopped }
54
+ }
55
+
56
+ export class GameLoop<FixedDeltaTime extends number> {
57
+ private readonly fsm = new FiniteStateMachine(schema, State.Stopped)
58
+ private readonly fixedDeltaTime: FixedDeltaTime
59
+ private readonly maximumDeltaTime: number
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
+ private requstId?: number
65
+ private lastTimestamp?: number
66
+ private deltaTimeAccumulator = 0
67
+ private lastDeltaTime = 0
68
+
69
+ constructor(options: IGameLoopOptions<FixedDeltaTime>) {
70
+ this.fixedDeltaTime = options.fixedDeltaTime
71
+ this.maximumDeltaTime = options.maximumDeltaTime
72
+ assert(
73
+ this.maximumDeltaTime >= this.fixedDeltaTime
74
+ , 'maximumDeltaTime must be greater than or equal to fixedDeltaTime'
75
+ )
76
+
77
+ this.update = options.update
78
+ this.fixedUpdate = options.fixedUpdate
79
+ this.lateUpdate = options.lateUpdate
80
+ this.render = options.render
81
+ }
82
+
83
+ start(): void {
84
+ this.fsm.send('start')
85
+
86
+ this.lastTimestamp = performance.now()
87
+ this.loop()
88
+ }
89
+
90
+ stop(): void {
91
+ this.fsm.send('stop')
92
+
93
+ cancelAnimationFrame(this.requstId!)
94
+
95
+ delete this.requstId
96
+ delete this.lastTimestamp
97
+ this.lastDeltaTime = 0
98
+ }
99
+
100
+ getFramesOfSecond(): number {
101
+ if (this.fsm.matches(State.Running)) {
102
+ return this.lastDeltaTime !== 0
103
+ ? 1000 / this.lastDeltaTime
104
+ : 0
105
+ } else {
106
+ return 0
107
+ }
108
+ }
109
+
110
+ private loop = (): void => {
111
+ // requestAnimationFrame的实现存在陷阱, 它提供的timestamp参数有时会比`performance.now()`早.
112
+ // 因此主动调用`performance.now()`来获取时间戳.
113
+ // https://stackoverflow.com/questions/50895206/exact-time-of-display-requestanimationframe-usage-and-timeline
114
+ const timestamp = performance.now()
115
+ const deltaTime = timestamp - this.lastTimestamp!
116
+ this.lastDeltaTime = deltaTime
117
+ this.lastTimestamp = timestamp
118
+
119
+ this.nextFrame(deltaTime)
120
+
121
+ this.requstId = requestAnimationFrame(this.loop)
122
+ }
123
+
124
+ /**
125
+ * nextFrameUpdateFirst依序做以下几件事:
126
+ * 1. 响应用户输入, 为这一帧的游戏世界做出非物理方面的更新, 例如角色转向, 改变武器瞄准角度等.
127
+ * 2. 响应游戏世界从上一帧更新后到这一帧更新之前发生的物理变化.
128
+ * 注意, 如果渲染帧率比物理帧率快, 且运行性能良好, 则物理帧不一定会在此帧更新.
129
+ * 3. 渲染经过更新后的最新状态, 渲染器可以根据alpha参数执行插值渲染.
130
+ */
131
+ nextFrame(deltaTime: number): void {
132
+ this.deltaTimeAccumulator = Math.min(
133
+ this.deltaTimeAccumulator + deltaTime
134
+ , this.maximumDeltaTime
135
+ )
136
+
137
+ this.update(deltaTime)
138
+
139
+ while (this.deltaTimeAccumulator >= this.fixedDeltaTime) {
140
+ this.fixedUpdate(this.fixedDeltaTime)
141
+ this.deltaTimeAccumulator -= this.fixedDeltaTime
142
+ }
143
+
144
+ const alpha = this.deltaTimeAccumulator / this.fixedDeltaTime
145
+ this.lateUpdate(deltaTime, alpha)
146
+ this.render(alpha)
147
+ }
148
+ }
package/src/index.ts ADDED
@@ -0,0 +1 @@
1
+ export * from './game-loop'