UNPKG

minecraft.js

Version:

Minecraft data serialization/deserialization and networking

68 lines (58 loc) 1.94 kB
var constants = require(__dirname + '/constants.js'), Vector2 = require(__dirname + '/vector2.js'), Vector3 = require(__dirname + '/vector3.js'), Block = require(__dirname + '/block.js'); /** @constructor */ var Chunk = module.exports = function(x, z) { this.blocks = new Buffer(Math.pow(constants.CHUNK_SIZE, 2) * constants.CHUNK_HEIGHT); this.heightMap = []; this.entities = []; this.tileEntities = []; this.tileTicks = []; this.empty = true; this.lastUpdate = 0; this.position = new Vector2(x, z); this.terrainPopulated = false; for(var i = 0; i < 16; i++) { this.heightMap[i] = []; for(var j = 0; j < 16; j++) { this.heightMap[i][j] = 0; } } }; Chunk.prototype.forEachBlock = function(userFunction) { for(var i = 0; i < constants.CHUNK_SIZE; i++) { for(var j = 0; j < constants.CHUNK_SIZE; j++) { for(var k = 0; k < constants.CHUNK_HEIGHT; k++) { var args = { pos: new Vector3(i, k, j), block: this.get(i, k, j) }; userFunction(args, this); } } } }; Chunk.prototype.get = Chunk.prototype.getBlock = function(x, y, z) { var blockId = Block.getId(x, y, z); return new Block({ value: this.blocks.readUInt8(blockId) //TODO: get other values }); }; Chunk.prototype.getTop = function(x, z, yStart) { for(var i = yStart || (constants.CHUNK_HEIGHT - 1); i >= 0; i++) { var block = this.get(x, i, z); if(block.value != 0) return block; } }; Chunk.prototype.set = Chunk.prototype.setBlock = function(x, y, z, value) { if(value != undefined) { var blockId = Block.getId(x, y, z); this.empty = false; this.blocks.writeUInt8(value.value, blockId); //TODO: set other values } };