forked from michaljaz/webmc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCellTerrain.js
More file actions
110 lines (94 loc) · 2.9 KB
/
CellTerrain.js
File metadata and controls
110 lines (94 loc) · 2.9 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// Generated by CoffeeScript 2.5.1
var CellTerrain,
modulo = function(a, b) { return (+a % (b = +b) + b) % b; };
import {
Block
} from './../build/Block.js';
CellTerrain = class CellTerrain {
constructor(options) {
this.cellSize = options.cellSize;
this.cells = {};
this.biomes = {};
this.loadedBlocks = {};
}
vec3(x, y, z) {
x = parseInt(x);
y = parseInt(y);
z = parseInt(z);
return `${x}:${y}:${z}`;
}
computeVoxelOffset(voxelX, voxelY, voxelZ) {
var x, y, z;
x = modulo(voxelX, this.cellSize) | 0;
y = modulo(voxelY, this.cellSize) | 0;
z = modulo(voxelZ, this.cellSize) | 0;
return y * this.cellSize * this.cellSize + z * this.cellSize + x;
}
computeCellForVoxel(voxelX, voxelY, voxelZ) {
var cellX, cellY, cellZ;
cellX = Math.floor(voxelX / this.cellSize);
cellY = Math.floor(voxelY / this.cellSize);
cellZ = Math.floor(voxelZ / this.cellSize);
return [cellX, cellY, cellZ];
}
addCellForVoxel(voxelX, voxelY, voxelZ) {
var cell, cellId;
cellId = this.vec3(...this.computeCellForVoxel(voxelX, voxelY, voxelZ));
cell = this.cells[cellId];
if (!cell) {
cell = new Uint8Array(this.cellSize * this.cellSize * this.cellSize);
this.cells[cellId] = cell;
}
return cell;
}
getCellForVoxel(voxelX, voxelY, voxelZ) {
var cellId;
cellId = this.vec3(...this.computeCellForVoxel(voxelX, voxelY, voxelZ));
return this.cells[cellId];
}
setVoxel(voxelX, voxelY, voxelZ, value) {
var cell, voff;
cell = this.getCellForVoxel(voxelX, voxelY, voxelZ);
if (!cell) {
cell = this.addCellForVoxel(voxelX, voxelY, voxelZ);
}
voff = this.computeVoxelOffset(voxelX, voxelY, voxelZ);
cell[voff] = value;
}
getVoxel(voxelX, voxelY, voxelZ) {
var cell, voff;
cell = this.getCellForVoxel(voxelX, voxelY, voxelZ);
if (!cell) {
return 0;
}
voff = this.computeVoxelOffset(voxelX, voxelY, voxelZ);
return cell[voff];
}
getCell(cellX, cellY, cellZ) {
return this.cells[this.vec3(x, y, z)];
}
setCell(cellX, cellY, cellZ, buffer) {
return this.cells[this.vec3(cellX, cellY, cellZ)] = buffer;
}
setBiome(cellX, cellY, cellZ, biome) {
return this.biomes[this.vec3(cellX, cellY, cellZ)] = biome;
}
getBlockBiome(blockX, blockY, blockZ) {
var biome, voff;
biome = this.getCellForVoxel(blockX, blockY, blockZ);
if (!biome) {
return null;
}
voff = this.computeVoxelOffset(blockX, blockY, blockZ);
return biome[voff];
}
getBlock(blockX, blockY, blockZ) {
if (this.loadedBlocks[this.getVoxel(blockX, blockY, blockZ)] === void 0) {
this.loadedBlocks[this.getVoxel(blockX, blockY, blockZ)] = new Block.fromStateId(this.getVoxel(blockX, blockY, blockZ), this.getBlockBiome(blockX, blockY, blockZ));
}
return this.loadedBlocks[this.getVoxel(blockX, blockY, blockZ)];
}
};
export {
CellTerrain
};