description and source-codefunction 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)
}