This repository has been archived by the owner on May 15, 2019. It is now read-only.
forked from max-mapper/voxel-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
753 lines (638 loc) · 21.7 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
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
var voxel = require('voxel')
var voxelMesh = require('voxel-mesh')
var ray = require('voxel-raycast')
var texture = require('voxel-texture')
var control = require('voxel-control')
var voxelView = require('voxel-view')
var THREE = require('three')
var Stats = require('./lib/stats')
var Detector = require('./lib/detector')
var inherits = require('inherits')
var path = require('path')
var EventEmitter = require('events').EventEmitter
if (process.browser) var interact = require('./lib/interact')
var requestAnimationFrame = require('raf')
var collisions = require('collide-3d-tilemap')
var aabb = require('aabb-3d')
var glMatrix = require('gl-matrix')
var vector = glMatrix.vec3
var SpatialEventEmitter = require('spatial-events')
var regionChange = require('voxel-region-change')
var kb = require('kb-controls')
var physical = require('voxel-physical')
var pin = require('pin-it')
var tic = require('tic')()
module.exports = Game
function Game(opts) {
if (!(this instanceof Game)) return new Game(opts)
var self = this
if (!opts) opts = {}
if (process.browser && this.notCapable(opts)) return
// is this a client or a headless server
this.isClient = Boolean( (typeof opts.isClient !== 'undefined') ? opts.isClient : process.browser )
if (!('generateChunks' in opts)) opts.generateChunks = true
this.generateChunks = opts.generateChunks
this.setConfigurablePositions(opts)
this.configureChunkLoading(opts)
this.setDimensions(opts)
this.THREE = THREE
this.vector = vector
this.glMatrix = glMatrix
this.arrayType = opts.arrayType || Uint8Array
this.cubeSize = 1 // backwards compat
this.chunkSize = opts.chunkSize || 32
// chunkDistance and removeDistance should not be set to the same thing
// as it causes lag when you go back and forth on a chunk boundary
this.chunkDistance = opts.chunkDistance || 2
this.removeDistance = opts.removeDistance || this.chunkDistance + 1
this.skyColor = opts.skyColor || 0xBFD1E5
this.antialias = opts.antialias
this.playerHeight = opts.playerHeight || 1.62
this.meshType = opts.meshType || 'surfaceMesh'
this.mesher = opts.mesher || voxel.meshers.culled
this.materialType = opts.materialType || THREE.MeshLambertMaterial
this.materialParams = opts.materialParams || {}
this.items = []
this.voxels = voxel(this)
this.scene = new THREE.Scene()
this.view = opts.view || new voxelView(THREE, {
width: this.width,
height: this.height,
skyColor: this.skyColor,
antialias: this.antialias
})
this.view.bindToScene(this.scene)
this.camera = this.view.getCamera()
if (!opts.lightsDisabled) this.addLights(this.scene)
this.fogScale = opts.fogScale || 32
if (!opts.fogDisabled) this.scene.fog = new THREE.Fog( this.skyColor, 0.00025, this.worldWidth() * this.fogScale )
this.collideVoxels = collisions(
this.getBlock.bind(this),
1,
[Infinity, Infinity, Infinity],
[-Infinity, -Infinity, -Infinity]
)
this.timer = this.initializeTimer((opts.tickFPS || 16))
this.paused = false
this.spatial = new SpatialEventEmitter
this.region = regionChange(this.spatial, aabb([0, 0, 0], [1, 1, 1]), this.chunkSize)
this.voxelRegion = regionChange(this.spatial, 1)
this.chunkRegion = regionChange(this.spatial, this.chunkSize)
this.asyncChunkGeneration = false
// contains chunks that has had an update this tick. Will be generated right before redrawing the frame
this.chunksNeedsUpdate = {}
// contains new chunks yet to be generated. Handled by game.loadPendingChunks
this.pendingChunks = []
this.materials = texture({
game: this,
texturePath: opts.texturePath || './textures/',
materialType: opts.materialType || THREE.MeshLambertMaterial,
materialParams: opts.materialParams || {},
materialFlatColor: opts.materialFlatColor === true
})
this.materialNames = opts.materials || [['grass', 'dirt', 'grass_dirt'], 'brick', 'dirt']
self.chunkRegion.on('change', function(newChunk) {
self.removeFarChunks()
})
if (this.isClient) this.materials.load(this.materialNames)
if (this.generateChunks) this.handleChunkGeneration()
// client side only after this point
if (!this.isClient) return
this.paused = true
this.initializeRendering(opts)
this.showAllChunks()
setTimeout(function() {
self.asyncChunkGeneration = 'asyncChunkGeneration' in opts ? opts.asyncChunkGeneration : true
}, 2000)
this.initializeControls(opts)
}
inherits(Game, EventEmitter)
// # External API
Game.prototype.voxelPosition = function(gamePosition) {
var _ = Math.floor
var p = gamePosition
var v = []
v[0] = _(p[0])
v[1] = _(p[1])
v[2] = _(p[2])
return v
}
Game.prototype.cameraPosition = function() {
return this.view.cameraPosition()
}
Game.prototype.cameraVector = function() {
return this.view.cameraVector()
}
Game.prototype.makePhysical = function(target, envelope, blocksCreation) {
var vel = this.terminalVelocity
envelope = envelope || [2/3, 1.5, 2/3]
var obj = physical(target, this.potentialCollisionSet(), envelope, {x: vel[0], y: vel[1], z: vel[2]})
obj.blocksCreation = !!blocksCreation
return obj
}
Game.prototype.addItem = function(item) {
if (!item.tick) {
var newItem = physical(
item.mesh,
this.itemCollisionSet(),
[item.size, item.size, item.size]
)
if (item.velocity) {
newItem.velocity.copy(item.velocity)
newItem.subjectTo(this.gravity)
}
newItem.repr = function() { return 'debris' }
newItem.mesh = item.mesh
newItem.blocksCreation = item.blocksCreation
item = newItem
}
this.items.push(item)
if (item.mesh) this.scene.add(item.mesh)
return this.items[this.items.length - 1]
}
Game.prototype.removeItem = function(item) {
var ix = this.items.indexOf(item)
if (ix < 0) return
this.items.splice(ix, 1)
if (item.mesh) this.scene.remove(item.mesh)
}
// only intersects voxels, not items (for now)
Game.prototype.raycast = // backwards compat
Game.prototype.raycastVoxels = function(start, direction, maxDistance, epilson) {
if (!start) return this.raycastVoxels(this.cameraPosition(), this.cameraVector(), 10)
var hitNormal = [0, 0, 0]
var hitPosition = [0, 0, 0]
var cp = start || this.cameraPosition()
var cv = direction || this.cameraVector()
var hitBlock = ray(this, cp, cv, maxDistance || 10.0, hitPosition, hitNormal, epilson || this.epilson)
if (hitBlock <= 0) return false
var adjacentPosition = [0, 0, 0]
var voxelPosition = this.voxelPosition(hitPosition)
vector.add(adjacentPosition, voxelPosition, hitNormal)
return {
position: hitPosition,
voxel: voxelPosition,
direction: direction,
value: hitBlock,
normal: hitNormal,
adjacent: adjacentPosition
}
}
Game.prototype.canCreateBlock = function(pos) {
pos = this.parseVectorArguments(arguments)
var floored = pos.map(function(i) { return Math.floor(i) })
var bbox = aabb(floored, [1, 1, 1])
for (var i = 0, len = this.items.length; i < len; ++i) {
var item = this.items[i]
var itemInTheWay = item.blocksCreation && item.aabb && bbox.intersects(item.aabb())
if (itemInTheWay) return false
}
return true
}
Game.prototype.createBlock = function(pos, val) {
if (typeof val === 'string') val = this.materials.find(val)
if (!this.canCreateBlock(pos)) return false
this.setBlock(pos, val)
return true
}
Game.prototype.setBlock = function(pos, val) {
if (typeof val === 'string') val = this.materials.find(val)
var old = this.voxels.voxelAtPosition(pos, val)
var c = this.voxels.chunkAtPosition(pos)
var chunk = this.voxels.chunks[c.join('|')]
if (!chunk) return// todo - does self.emit('missingChunk', c.join('|')) make sense here?
this.addChunkToNextUpdate(chunk)
this.spatial.emit('change-block', pos, old, val)
this.emit('setBlock', pos, val, old)
}
Game.prototype.getBlock = function(pos) {
pos = this.parseVectorArguments(arguments)
return this.voxels.voxelAtPosition(pos)
}
Game.prototype.blockPosition = function(pos) {
pos = this.parseVectorArguments(arguments)
var ox = Math.floor(pos[0])
var oy = Math.floor(pos[1])
var oz = Math.floor(pos[2])
return [ox, oy, oz]
}
Game.prototype.blocks = function(low, high, iterator) {
var l = low, h = high
var d = [ h[0]-l[0], h[1]-l[1], h[2]-l[2] ]
if (!iterator) var voxels = new this.arrayType(d[0]*d[1]*d[2])
var i = 0
for(var z=l[2]; z<h[2]; ++z)
for(var y=l[1]; y<h[1]; ++y)
for(var x=l[0]; x<h[0]; ++x, ++i) {
if (iterator) iterator(x, y, z, i)
else voxels[i] = this.voxels.voxelAtPosition([x, y, z])
}
if (!iterator) return {voxels: voxels, dims: d}
}
// backwards compat
Game.prototype.createAdjacent = function(hit, val) {
this.createBlock(hit.adjacent, val)
}
Game.prototype.appendTo = function (element) {
this.view.appendTo(element)
}
// # Defaults/options parsing
Game.prototype.gravity = [0, -0.0000036, 0]
Game.prototype.friction = 0.3
Game.prototype.epilson = 1e-8
Game.prototype.terminalVelocity = [0.9, 0.1, 0.9]
Game.prototype.defaultButtons = {
'W': 'forward'
, 'A': 'left'
, 'S': 'backward'
, 'D': 'right'
, '<up>': 'forward'
, '<left>': 'left'
, '<down>': 'backward'
, '<right>': 'right'
, '<mouse 1>': 'fire'
, '<mouse 3>': 'firealt'
, '<space>': 'jump'
, '<shift>': 'crouch'
, '<control>': 'alt'
}
// used in methods that have identity function(pos) {}
Game.prototype.parseVectorArguments = function(args) {
if (!args) return false
if (args[0] instanceof Array) return args[0]
return [args[0], args[1], args[2]]
}
Game.prototype.setConfigurablePositions = function(opts) {
var sp = opts.startingPosition
this.startingPosition = sp || [35, 1024, 35]
var wo = opts.worldOrigin
this.worldOrigin = wo || [0, 0, 0]
}
Game.prototype.setDimensions = function(opts) {
if (opts.container) this.container = opts.container
if (opts.container && opts.container.clientHeight) {
this.height = opts.container.clientHeight
} else {
this.height = typeof window === "undefined" ? 1 : window.innerHeight
}
if (opts.container && opts.container.clientWidth) {
this.width = opts.container.clientWidth
} else {
this.width = typeof window === "undefined" ? 1 : window.innerWidth
}
}
Game.prototype.notCapable = function(opts) {
var self = this
if( !Detector().webgl ) {
this.view = {
appendTo: function(el) {
el.appendChild(self.notCapableMessage())
}
}
return true
}
return false
}
Game.prototype.notCapableMessage = function() {
var wrapper = document.createElement('div')
wrapper.className = "errorMessage"
var a = document.createElement('a')
a.title = "You need WebGL and Pointer Lock (Chrome 23/Firefox 14) to play this game. Click here for more information."
a.innerHTML = a.title
a.href = "http://get.webgl.org"
wrapper.appendChild(a)
return wrapper
}
Game.prototype.onWindowResize = function() {
var width = window.innerWidth
var height = window.innerHeight
if (this.container) {
width = this.container.clientWidth
height = this.container.clientHeight
}
this.view.resizeWindow(width, height)
}
// # Physics/collision related methods
Game.prototype.control = function(target) {
this.controlling = target
return this.controls.target(target)
}
Game.prototype.potentialCollisionSet = function() {
return [{ collide: this.collideTerrain.bind(this) }]
}
Game.prototype.itemCollisionSet = function() {
return [{ collide: this.collideAvatar.bind(this) }]
}
/**
* Get the position of the player under control.
* If there is no player under control, return
* current position of the game's camera.
*
* @return {Array} an [x, y, z] tuple
*/
Game.prototype.playerPosition = function() {
var target = this.controls.target()
var position = target
? target.avatar.position
: this.camera.localToWorld(this.camera.position.clone())
return [position.x, position.y, position.z]
}
Game.prototype.playerAABB = function(position) {
var pos = position || this.playerPosition()
var lower = []
var upper = [1/2, this.playerHeight, 1/2]
var playerBottom = [1/4, this.playerHeight, 1/4]
vector.subtract(lower, pos, playerBottom)
var bbox = aabb(lower, upper)
return bbox
}
Game.prototype.collideTerrain = function(other, bbox, vec, resting) {
var self = this
var axes = ['x', 'y', 'z']
var vec3 = [vec.x, vec.y, vec.z]
this.collideVoxels(bbox, vec3, function hit(axis, tile, coords, dir, edge) {
if (!tile) return
if (Math.abs(vec3[axis]) < Math.abs(edge)) return
vec3[axis] = vec[axes[axis]] = edge
other.acceleration[axes[axis]] = 0
resting[axes[axis]] = dir
other.friction[axes[(axis + 1) % 3]] = other.friction[axes[(axis + 2) % 3]] = axis === 1 ? self.friction : 1
return true
})
}
Game.prototype.collideAvatar = function(other, bbox, vec, resting) {
var playerPos = aabb(this.playerPosition(), [1, 1, 1])
var intersects = bbox.intersects(playerPos)
if (intersects) {
this.emit('collision', other)
}
return intersects
}
// # Three.js specific methods
Game.prototype.addStats = function() {
stats = new Stats()
stats.domElement.style.position = 'absolute'
stats.domElement.style.bottom = '0px'
document.body.appendChild( stats.domElement )
}
Game.prototype.addLights = function(scene) {
var ambientLight, directionalLight
ambientLight = new THREE.AmbientLight(0xcccccc)
scene.add(ambientLight)
var light = new THREE.DirectionalLight( 0xffffff , 1)
light.position.set( 1, 1, 0.5 ).normalize()
scene.add( light )
}
// # Chunk related methods
Game.prototype.configureChunkLoading = function(opts) {
var self = this
if (!opts.generateChunks) return
if (!opts.generate) {
this.generate = function(x,y,z) {
return x*x+y*y+z*z <= 15*15 ? 1 : 0 // sphere world
}
} else {
this.generate = opts.generate
}
if (opts.generateVoxelChunk) {
this.generateVoxelChunk = opts.generateVoxelChunk
} else {
this.generateVoxelChunk = function(low, high) {
return voxel.generate(low, high, self.generate, self)
}
}
}
Game.prototype.worldWidth = function() {
return this.chunkSize * 2 * this.chunkDistance
}
Game.prototype.chunkToWorld = function(pos) {
return [
pos[0] * this.chunkSize,
pos[1] * this.chunkSize,
pos[2] * this.chunkSize
]
}
Game.prototype.removeFarChunks = function(playerPosition) {
var self = this
playerPosition = playerPosition || this.playerPosition()
var nearbyChunks = this.voxels.nearbyChunks(playerPosition, this.removeDistance).map(function(chunkPos) {
return chunkPos.join('|')
})
Object.keys(self.voxels.chunks).map(function(chunkIndex) {
if (nearbyChunks.indexOf(chunkIndex) > -1) return
var chunk = self.voxels.chunks[chunkIndex]
var mesh = self.voxels.meshes[chunkIndex]
var pendingIndex = self.pendingChunks.indexOf(chunkIndex)
if (pendingIndex !== -1) self.pendingChunks.splice(pendingIndex, 1)
if (!chunk) return
var chunkPosition = chunk.position
if (mesh) {
if (mesh.surfaceMesh) {
self.scene.remove(mesh.surfaceMesh)
mesh.surfaceMesh.geometry.dispose()
}
if (mesh.wireMesh) {
mesh.wireMesh.geometry.dispose()
self.scene.remove(mesh.wireMesh)
}
delete mesh.data
delete mesh.geometry
delete mesh.meshed
delete mesh.surfaceMesh
delete mesh.wireMesh
}
delete self.voxels.chunks[chunkIndex]
self.emit('removeChunk', chunkPosition)
})
self.voxels.requestMissingChunks(playerPosition)
}
Game.prototype.addChunkToNextUpdate = function(chunk) {
this.chunksNeedsUpdate[chunk.position.join('|')] = chunk
}
Game.prototype.updateDirtyChunks = function() {
var self = this
Object.keys(this.chunksNeedsUpdate).forEach(function showChunkAtIndex(chunkIndex) {
var chunk = self.chunksNeedsUpdate[chunkIndex]
self.emit('dirtyChunkUpdate', chunk)
self.showChunk(chunk)
})
this.chunksNeedsUpdate = {}
}
Game.prototype.loadPendingChunks = function(count) {
var pendingChunks = this.pendingChunks
if (!this.asyncChunkGeneration) {
count = pendingChunks.length
} else {
count = count || (pendingChunks.length * 0.1)
count = Math.max(1, Math.min(count, pendingChunks.length))
}
for (var i = 0; i < count; i += 1) {
var chunkPos = pendingChunks[i].split('|')
var chunk = this.voxels.generateChunk(chunkPos[0]|0, chunkPos[1]|0, chunkPos[2]|0)
if (this.isClient) this.showChunk(chunk)
}
if (count) pendingChunks.splice(0, count)
}
Game.prototype.getChunkAtPosition = function(pos) {
var chunkID = this.voxels.chunkAtPosition(pos).join('|')
var chunk = this.voxels.chunks[chunkID]
return chunk
}
Game.prototype.showAllChunks = function() {
for (var chunkIndex in this.voxels.chunks) {
this.showChunk(this.voxels.chunks[chunkIndex])
}
}
Game.prototype.showChunk = function(chunk) {
var chunkIndex = chunk.position.join('|')
var bounds = this.voxels.getBounds.apply(this.voxels, chunk.position)
var scale = new THREE.Vector3(1, 1, 1)
var mesh = voxelMesh(chunk, this.mesher, scale, this.THREE)
this.voxels.chunks[chunkIndex] = chunk
if (this.voxels.meshes[chunkIndex]) {
if (this.voxels.meshes[chunkIndex].surfaceMesh) this.scene.remove(this.voxels.meshes[chunkIndex].surfaceMesh)
if (this.voxels.meshes[chunkIndex].wireMesh) this.scene.remove(this.voxels.meshes[chunkIndex].wireMesh)
}
this.voxels.meshes[chunkIndex] = mesh
if (this.isClient) {
if (this.meshType === 'wireMesh') mesh.createWireMesh()
else mesh.createSurfaceMesh(this.materials.material)
this.materials.paint(mesh)
}
mesh.setPosition(bounds[0][0], bounds[0][1], bounds[0][2])
mesh.addToScene(this.scene)
this.emit('renderChunk', chunk)
return mesh
}
// # Debugging methods
Game.prototype.addMarker = function(position) {
var geometry = new THREE.SphereGeometry( 0.1, 10, 10 )
var material = new THREE.MeshPhongMaterial( { color: 0xffffff, shading: THREE.FlatShading } )
var mesh = new THREE.Mesh( geometry, material )
mesh.position.copy(position)
this.scene.add(mesh)
}
Game.prototype.addAABBMarker = function(aabb, color) {
var geometry = new THREE.CubeGeometry(aabb.width(), aabb.height(), aabb.depth())
var material = new THREE.MeshBasicMaterial({ color: color || 0xffffff, wireframe: true, transparent: true, opacity: 0.5, side: THREE.DoubleSide })
var mesh = new THREE.Mesh(geometry, material)
mesh.position.set(aabb.x0() + aabb.width() / 2, aabb.y0() + aabb.height() / 2, aabb.z0() + aabb.depth() / 2)
this.scene.add(mesh)
return mesh
}
Game.prototype.addVoxelMarker = function(x, y, z, color) {
var bbox = aabb([x, y, z], [1, 1, 1])
return this.addAABBMarker(bbox, color)
}
Game.prototype.pin = pin
// # Misc internal methods
Game.prototype.onControlChange = function(gained, stream) {
this.paused = false
if (!gained && !this.optout) {
this.buttons.disable()
return
}
this.buttons.enable()
stream.pipe(this.controls.createWriteRotationStream())
}
Game.prototype.onControlOptOut = function() {
this.optout = true
}
Game.prototype.onFire = function(state) {
this.emit('fire', this.controlling, state)
}
Game.prototype.setInterval = tic.interval.bind(tic)
Game.prototype.setTimeout = tic.timeout.bind(tic)
Game.prototype.tick = function(delta) {
for(var i = 0, len = this.items.length; i < len; ++i) {
this.items[i].tick(delta)
}
if (this.materials) this.materials.tick(delta)
if (this.pendingChunks.length) this.loadPendingChunks()
if (Object.keys(this.chunksNeedsUpdate).length > 0) this.updateDirtyChunks()
tic.tick(delta)
this.emit('tick', delta)
if (!this.controls) return
var playerPos = this.playerPosition()
this.spatial.emit('position', playerPos, playerPos)
}
Game.prototype.render = function(delta) {
this.view.render(this.scene)
}
Game.prototype.initializeTimer = function(rate) {
var self = this
var accum = 0
var now = 0
var last = null
var dt = 0
var wholeTick
self.frameUpdated = true
self.interval = setInterval(timer, 0)
return self.interval
function timer() {
if (self.paused) {
last = Date.now()
accum = 0
return
}
now = Date.now()
dt = now - (last || now)
last = now
accum += dt
if (accum < rate) return
wholeTick = ((accum / rate)|0)
if (wholeTick <= 0) return
wholeTick *= rate
self.tick(wholeTick)
accum -= wholeTick
self.frameUpdated = true
}
}
Game.prototype.initializeRendering = function(opts) {
var self = this
if (!opts.statsDisabled) self.addStats()
window.addEventListener('resize', self.onWindowResize.bind(self), false)
requestAnimationFrame(window).on('data', function(dt) {
self.emit('prerender', dt)
self.render(dt)
self.emit('postrender', dt)
})
if (typeof stats !== 'undefined') {
self.on('postrender', function() {
stats.update()
})
}
}
Game.prototype.initializeControls = function(opts) {
// player control
this.keybindings = opts.keybindings || this.defaultButtons
this.buttons = kb(document.body, this.keybindings)
this.buttons.disable()
this.optout = false
this.interact = interact(opts.interactElement || this.view.element, opts.interactMouseDrag)
this.interact
.on('attain', this.onControlChange.bind(this, true))
.on('release', this.onControlChange.bind(this, false))
.on('opt-out', this.onControlOptOut.bind(this))
this.hookupControls(this.buttons, opts)
}
Game.prototype.hookupControls = function(buttons, opts) {
opts = opts || {}
opts.controls = opts.controls || {}
opts.controls.onfire = this.onFire.bind(this)
this.controls = control(buttons, opts.controls)
this.items.push(this.controls)
this.controlling = null
}
Game.prototype.handleChunkGeneration = function() {
var self = this
this.voxels.on('missingChunk', function(chunkPos) {
self.pendingChunks.push(chunkPos.join('|'))
})
this.voxels.requestMissingChunks(this.worldOrigin)
this.loadPendingChunks(this.pendingChunks.length)
}
// teardown methods
Game.prototype.destroy = function() {
clearInterval(this.timer)
}