Skip to content

Commit

Permalink
introduce game.arrayType && game.blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
max-mapper committed Mar 5, 2013
1 parent 077735d commit 9ca141f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
16 changes: 15 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ function Game(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

Expand Down Expand Up @@ -229,6 +229,20 @@ Game.prototype.blockPosition = function(pos) {
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)
Expand Down
7 changes: 7 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,13 @@ gameTest(function createBlock(game, t) {
t.equal(game.createBlock(pos), true)
})

gameTest(function blocks(game, t) {
var pos = [6,6,6]
game.setBlock(pos, 1)
var blocks = game.blocks([5,5,5], [7,7,7]).voxels
t.equal(!!blocks[7], true)
})

gameTest(function raycastVoxels(game, t) {
var pos = [50, 50, 50]
game.setBlock(pos, 1)
Expand Down

0 comments on commit 9ca141f

Please sign in to comment.