Пример #1
0
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
}
Пример #2
0
function Game(opts) {
  if (!(this instanceof Game)) return new Game(opts)
  var self = this
  if (!opts) opts = {}
  if (process.browser && this.notCapable()) return
  if (!('generateChunks' in opts)) opts.generateChunks = true
  this.generateChunks = opts.generateChunks
  this.axes = ['x', 'y', 'z']
  this.setConfigurablePositions(opts)
  this.configureChunkLoading(opts)
  this.THREE = THREE
  this.cubeSize = opts.cubeSize || 25
  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.playerHeight = opts.playerHeight || 1.62 // gets multiplied by cubeSize
  this.meshType = opts.meshType || 'surfaceMesh'
  this.mesher = opts.mesher || voxel.meshers.greedy
  this.materialType = opts.materialType || THREE.MeshLambertMaterial
  this.materialParams = opts.materialParams || {}
  this.items = []
  this.voxels = voxel(this)
  this.chunkGroups = voxelChunks(this)  
  this.height = typeof window === "undefined" ? 1 : window.innerHeight
  this.width = typeof window === "undefined" ? 1 : window.innerWidth
  this.scene = new THREE.Scene()
  this.camera = this.createCamera(this.scene)

  if (!opts.lightsDisabled) this.addLights(this.scene)
  this.skyColor = opts.skyColor || 0xBFD1E5
  this.fogScale = opts.fogScale || 1
  this.collideVoxels = collisions(
    this.getTileAtIJK.bind(this),
    this.cubeSize,
    [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], [this.cubeSize, this.cubeSize, this.cubeSize]), this.chunkSize)
  this.voxelRegion = regionChange(this.spatial, this.cubeSize)
  this.chunkRegion = regionChange(this.spatial, this.cubeSize * this.chunkSize)
  // contains chunks that has had an update this tick. Will be generated right before redrawing the frame
  this.chunksNeedsUpdate = {}

  this.materials = require('voxel-texture')({
    THREE: THREE,
    texturePath: opts.texturePath || './textures/',
    materialType: opts.materialType || THREE.MeshLambertMaterial,
    materialParams: opts.materialParams || {}
  })

  if (process.browser) {
    this.materials.load(opts.materials || [['grass', 'dirt', 'grass_dirt'], 'brick', 'dirt'])
  }

  if (this.generateChunks) {
    self.voxels.on('missingChunk', function(chunkPos) {
      var chunk = self.voxels.generateChunk(chunkPos[0], chunkPos[1], chunkPos[2])
      if (process.browser) self.showChunk(chunk)
    })
    this.voxels.requestMissingChunks(this.worldOrigin)
  }

  // client side only after this point
  if (!process.browser) return
  
  this.paused = true
  this.initializeRendering()
  for (var chunkIndex in this.voxels.chunks) {
    this.showChunk(this.voxels.chunks[chunkIndex])
  }

  // player control
  this.buttons = kb(document.body, opts.keybindings || this.defaultButtons)
  this.buttons.disable()
  this.optout = false
  this.interact = interact(this.element)
  this.interact
      .on('attain', this.onControlChange.bind(this, true))
      .on('release', this.onControlChange.bind(this, false))
      .on('opt-out', this.onControlOptOut.bind(this))

  opts.controls = opts.controls || {}
  opts.controls.onfire = this.onFire.bind(this)
  this.controls = control(this.buttons, opts.controls)
  this.items.push(this.controls)
  this.controlling = null
}