-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
202 lines (159 loc) · 5.37 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import EventEmitter from "./events"
import Renderer from "./renderer"
import { defer } from "./utils"
export const events = {
READY: "ready",
START: "start",
STOP: "stop",
TICK: "tick",
STEP: "step",
PRERENDER: "pre-render",
RENDER: "render",
POSTRENDER: "post-render",
}
/*
Suki
connects our primary renderer, an event emitter, a game loop, and such
*/
class Suki {
constructor() {
const that = this
this.running = false
this.isReady = false
this.events = new EventEmitter()
this.renderer = new Renderer()
this.$ = this.renderer
this.canvas = this.renderer.canvas
this.stats = {
skippedFrames: 0,
fps: () => {
return 1000/that.time().lastRenderDelta
},
}
this.renderer.addCanvasToDOM()
// add our hook for watching for if the document is ready
let isReadyCheck = () => {
if (document.readyState == "complete") {
defer(() => {
that.isReady = true
that.events.trigger(events.READY)
})
return true
}
else return false
}
if (!isReadyCheck()) {
document.onreadystatechange = isReadyCheck
}
}
whenReady(fn) {
if (this.isReady) fn()
else this.events.on(events.READY, fn)
return this
}
time() {
return null
}
start(fps = 60) {
// note that fps is constrained by the browser,
// thus it may be more apt to call it a 'render interval'
// that said, it does still determine the frameskip threshold,
let fpms = 1000/fps
console.log(`Stepping every ${fpms.toFixed(2)}ms, ${fps} frames per second`)
this.events.trigger(events.START, this.renderer, this)
this.running = true
const that = this
const now = () => Date.now()
const start = now()
// if a step takes longer than this we skip the frame
// At 60FPS, this would be 12.5ms to process a 'step'
const frameskipDeltaThreshold = fpms * 0.8
// our time reference, provided to all renders and steps
// using a constant reference helps V8's JITC create an optimized class early.
const time = {
id: null,
now: start,
start: start,
ticks: 0,
delta: null,
elapsed: 0,
lastCalled: start,
stepDuration: 0,
lastRenderDelta: 0,
}
let _now = 0
let lastRender = start
this.time = () => time
// everything in this function is extremely performance-sensitive
// but, it's also where all the magic happens
const tick = e => {
time.ticks += 1
if (!that.running) {
if (time.id != null) window.cancelAnimationFrame(time.id)
time.id = null
return
}
that.events.trigger(events.TICK, time)
// update time object with the new time and deltas
_now = now()
time.now = _now
time.delta = (_now - time.lastCalled)
time.elapsed += time.delta
// perform a step
that.events.trigger(events.STEP, time)
// after a step we measure the time it took to determine if too much processing
// occurred for an immediate render, a la, frame sklpping
_now = now()
time.stepDuration = _now - time.now
time.lastRenderDelta = _now - lastRender
if (time.stepDuration > frameskipDeltaThreshold) {
that.stats.skippedFrames += 1
time.elapsed -= time.delta
time.lastCalled = _now
}
else {
lastRender = _now
that.events.trigger(events.PRERENDER, time, that.renderer, that)
that.events.trigger(events.RENDER, time, that.renderer, that)
time.lastCalled = _now
that.events.trigger(events.POSTRENDER, time, that.renderer, that)
}
time.id = window.requestAnimationFrame(tick)
}
// we ride
time.id = window.requestAnimationFrame(tick)
return this
}
stop() {
this.running = false
this.events.trigger(events.STOP, this.renderer, this)
}
}
/*
Amazing things seem to be happening!
*/
export const suki = new Suki()
/*
Subsystem
Subsystems are how we create modules that hook into the game loop
*/
export const SubSystem = (superclass = Object) => class SukiAttachedApp extends superclass {
constructor() {
super()
this.data = null
}
mount() {
if (this.tick) suki.events.on(events.TICK, this.tick, this.data)
if (this.step) suki.events.on(events.STEP, this.step, this.data)
if (this.preRender) suki.events.on(events.PRERENDER, this.preRender, this.data)
if (this.render) suki.events.on(events.RENDER, this.render, this.data)
if (this.postRender) suki.events.on(events.POSTRENDER, this.postRender, this.data)
}
unmount() {
if (this.tick) suki.events.off(events.TICK, this.tick, this.data)
if (this.step) suki.events.off(events.STEP, this.step, this.data)
if (this.preRender) suki.events.off(events.PRERENDER, this.preRender, this.data)
if (this.render) suki.events.off(events.RENDER, this.render, this.data)
if (this.postRender) suki.events.off(events.POSTRENDER, this.postRender, this.data)
}
}