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
98 lines (86 loc) · 2.74 KB
/
CellTerrain.js
File metadata and controls
98 lines (86 loc) · 2.74 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
var modulo = function (a, b) {
return ((+a % (b = +b)) + b) % b;
};
var CellTerrain = class CellTerrain {
constructor(options) {
this.cellSize = options.cellSize;
this.cells = {};
this.blocksDef = options.blocksDef;
}
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 Uint32Array(
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(x, y, z) {
return this.cells[this.vec3(x, y, z)];
}
setCell(cellX, cellY, cellZ, buffer) {
return (this.cells[this.vec3(cellX, cellY, cellZ)] = buffer);
}
getBlock(blockX, blockY, blockZ) {
var stateId = this.getVoxel(blockX, blockY, blockZ);
var def = this.blocksDef[stateId];
if (def !== void 0) {
return {
name: def[0],
stateId,
boundingBox: def[1] === 1 ? "block" : "empty",
transparent: def[2],
};
} else {
return false;
}
}
};
export { CellTerrain };