-
Notifications
You must be signed in to change notification settings - Fork 0
/
flatland.js
65 lines (48 loc) · 1.78 KB
/
flatland.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
'use strict';
var ndarray = require('ndarray');
module.exports = function(game, opts) {
return new Flatland(game, opts);
};
module.exports.pluginInfo = {
loadAfter: ['voxel-registry']
};
function Flatland(game, opts) {
this.game = game;
this.registry = game.plugins.get('voxel-registry');
if (!this.registry) throw new Error('voxel-flatland requires voxel-registry plugin');
this.block = opts.block;
if (!this.block) throw new Error('voxel-flatland requires block option');
this.enable();
}
Flatland.prototype.enable = function() {
this.game.voxels.on('missingChunk', this.onMissingChunk = this.missingChunk.bind(this));
};
Flatland.prototype.disable = function() {
this.game.voxels.removeListener('missingChunk', this.onMissingChunk);
};
Flatland.prototype.missingChunk = function(position) {
console.log('missingChunk',position);
if (position[1] > 0) return; // everything above y=0 is air
var blockIndex = this.registry.getBlockIndex(this.block);
if (!blockIndex) {
throw new Error('voxel-flatland unable to find block of name: '+this.block);
};
var width = this.game.chunkSize;
var pad = this.game.chunkPad;
var arrayType = this.game.arrayType;
var buffer = new ArrayBuffer((width+pad) * (width+pad) * (width+pad) * arrayType.BYTES_PER_ELEMENT);
var voxelsPadded = ndarray(new arrayType(buffer), [width+pad, width+pad, width+pad]);
var h = pad >> 1;
var voxels = voxelsPadded.lo(h,h,h).hi(width,width,width);
for (var x = 0; x < this.game.chunkSize; ++x) {
for (var z = 0; z < this.game.chunkSize; ++z) {
for (var y = 0; y < this.game.chunkSize; ++y) {
voxels.set(x,y,z, blockIndex);
}
}
}
var chunk = voxelsPadded;
chunk.position = position;
console.log('about to showChunk',chunk);
this.game.showChunk(chunk);
};