Repository navigation
Expand file tree
/
Copy pathGO.js
More file actions
282 lines (225 loc) · 5.95 KB
/
Copy pathGO.js
File metadata and controls
282 lines (225 loc) · 5.95 KB
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
export const GO = function() { }
GO.Screen = { }
GO.config = {}
GO.config.fontName = 'sans'
/* how long to wait for the webfont before starting anyway, in ms */
GO.config.fontTimeout = 3000
/*
* Wait for the configured webfont, then run done().
*
* A canvas draws its text once: fillText silently falls back to a system
* font while the webfont is still loading, and nothing redraws when it
* finally arrives, so the title screen would keep the wrong typeface for
* as long as it is on screen. The wait is capped, because a missing or
* slow font file must not stop the game from starting - it just looks
* different.
*/
GO.waitForFont = function(done)
{
var family = this.config.fontName
if (!family || !document.fonts || !document.fonts.load) {
done()
return
}
var called = false
,timer
,finish = function() {
if (called) {
return
}
called = true
window.clearTimeout(timer)
done()
}
timer = window.setTimeout(finish, this.config.fontTimeout)
/* the size is irrelevant, but the shorthand needs one to parse */
document.fonts.load('16px ' + family).then(finish, finish)
}
/*
* Bring up the canvas and the audio context, preload the assets behind a
* progress bar, then start the game. Everything the loader waits for is
* optional in the sense that a failure only costs that one asset.
*/
GO.boot = function()
{
if (!this.initCanvas()) {
return false
}
this.Sound.init()
var self = this
,tasks = [ function(done) { self.waitForFont(done) } ]
.concat(this.Sound.loadTasks())
this.Loader.run(tasks, function() {
self.start()
})
}
GO.initCanvas = function()
{
this.canvas = document.getElementById('canvas')
if (!this.canvas || (this.canvas && !this.canvas.getContext)) {
return false
}
this.Screen.width = this.canvas.width
this.Screen.height = this.canvas.height
this.Screen.scale = 1
this.ctx = this.canvas.getContext('2d')
this.resize()
addEventListener('resize', function() {
GO.resize()
}, true)
return true
}
GO.start = function()
{
this.Event.init()
this.entityCount = 0
this.speed = 1
this.delta = 0
this.counter = 0
this.seconds = 0
this.tick = 0
this.tickn = 0
this.frameCounter = 0
this.fps = 0
this.fpsCounter = 0
this.fpsLastSec = 0
this.msg = ''
this.messages = new GO.LinkedList
this.scenes = { }
this.scene = false
this.handlers = new GO.LinkedList
/* the canvas and the audio context are already up, GO.boot needed
them to draw the loading bar and to decode the samples */
if (!this.ctx && !this.initCanvas()) {
return false
}
requestAnimationFrame(function() {
GO.loop()
})
if (this.init) {
this.init.call(this)
}
}
/*
* Fit the canvas to the browser window, keeping the aspect ratio of the
* logical resolution (GO.Screen.width x GO.Screen.height). The backing
* store is sized in device pixels for sharp rendering on high-DPI
* displays; a base transform maps logical coordinates onto it, so all
* game code keeps drawing in logical coordinates.
*/
GO.resize = function()
{
var availWidth = window.innerWidth - 2
var availHeight = window.innerHeight - 2
var scale = Math.min(availWidth / this.Screen.width,
availHeight / this.Screen.height)
var displayWidth = Math.floor(this.Screen.width * scale)
var displayHeight = Math.floor(this.Screen.height * scale)
var dpr = window.devicePixelRatio || 1
this.canvas.style.width = displayWidth + 'px'
this.canvas.style.height = displayHeight + 'px'
this.canvas.width = Math.round(displayWidth * dpr)
this.canvas.height = Math.round(displayHeight * dpr)
this.Screen.scale = displayWidth / this.Screen.width
this.ctx.setTransform(this.canvas.width / this.Screen.width, 0, 0,
this.canvas.height / this.Screen.height, 0, 0)
this.ctx.mozImageSmoothingEnabled = false
}
GO.setScene = function(scene)
{
if (this.scene.deactivate) {
this.scene.deactivate.call(this.scene)
}
this.scene = scene
if (this.scene.activate) {
this.scene.activate.call(this.scene)
}
}
GO.loop = function()
{
requestAnimationFrame(function() {
GO.loop()
})
var date = new Date
this.now = date.getTime()
if (this.pause) {
this.oldtime = this.now
return
}
if (this.oldtime) {
this.delta = ((this.now - this.oldtime) / 1000) * this.speed
/* cap the delta so the game does not jump ahead after the
tab was inactive (rAF stops firing in background tabs) */
if (this.delta > 0.1) {
this.delta = 0.1
}
}
this.oldtime = this.now
this.counter += this.delta
this.seconds = Math.round(this.counter)
this.frameCounter++
this.fpsCounter++
if (this.fpsLastSec != this.seconds) {
this.fps = this.fpsCounter * this.speed
this.fpsCounter = 0
this.fpsLastSec = this.seconds
}
this.tickn += this.delta
if (this.tickn > 60 / 1000) {
this.tick++
this.tickn = 0
}
this.Event.poll()
if (this.handlers.count) {
this.handlers.foreach(function(handler) {
return handler.process.call(handler)
}, this)
}
if (this.scene) {
this.scene.process.call(this.scene)
}
if (this.msg) {
/* the alignment has to be set explicitly, the canvas keeps
whatever the scene (or showFPS) left behind */
this.ctx.fillStyle = 'white'
this.ctx.font = '12px ' + this.config.fontName
this.ctx.textAlign = 'left'
this.ctx.textBaseline = 'alphabetic'
this.ctx.fillText(this.msg, 20, this.Screen.height - 20)
}
this.showFPS()
this.Event.frameReset()
}
GO.showFPS = function()
{
if (!this.config.showFPS) {
return
}
this.ctx.fillStyle = '#666'
this.ctx.font = '8px ' + this.config.fontName
this.ctx.textAlign = 'end'
this.ctx.textBaseline = 'top'
this.ctx.fillText(this.fps + ' fps', GO.Screen.width - 5, 5)
}
GO.showMsg = function(msg)
{
if (!this.msg) {
this.msg = msg
} else {
this.messages.push({ msg: msg })
}
if (this.messageTimer) {
return
}
this.messageTimer = new GO.Timer(1000, function() {
if (!GO.messages.count) {
GO.msg = ''
GO.messageTimer = false
return false
}
var mobj = GO.messages.first
GO.msg = mobj.msg
GO.messages.del(mobj)
}, this)
this.handlers.push(this.messageTimer)
}