-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
31 lines (27 loc) · 1.05 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
var leveljs = require('level-js')
var crunch = require('voxel-crunch')
module.exports = VoxelLevel
function VoxelLevel(db) {
if (!(this instanceof VoxelLevel)) return new VoxelLevel(db)
this.db = db
return true
}
VoxelLevel.prototype.load = function(worldName, chunkPosition, dimensions, cb) {
var chunkLength = dimensions[0] * dimensions[1] * dimensions[2]
var chunkIndex = chunkPosition.join('|') + '|' + chunkLength
this.db.sublevel(worldName).get(chunkIndex, { valueEncoding: 'binary' }, function(err, rle) {
if (err) return cb(err)
var voxels = new Uint8Array(chunkLength)
crunch.decode(rle, voxels)
cb(false, {position: chunkPosition, voxels: voxels, dimensions: dimensions})
})
}
VoxelLevel.prototype.store = function(worldName, chunk, cb) {
var rle = crunch.encode(chunk.voxels)
var key = chunk.position.join('|')
key += '|' + chunk.voxels.length
if (rle.length === 80) return cb('empty chunk', rle.length)
this.db.sublevel(worldName).put(key, rle, { valueEncoding: 'binary' }, function(err) {
cb(err, rle.length)
})
}