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
77 lines (66 loc) · 2 KB
/
CellTerrain.js
File metadata and controls
77 lines (66 loc) · 2 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
// Generated by CoffeeScript 2.5.1
var CellTerrain,
modulo = function(a, b) { return (+a % (b = +b) + b) % b; };
CellTerrain = class CellTerrain {
constructor(options) {
this.cellSize = options.cellSize;
this.cells = {};
this.neighbours = [[-1, 0, 0], [1, 0, 0], [0, -1, 0], [0, 1, 0], [0, 0, -1], [0, 0, 1]];
}
vec3(x, y, 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];
}
getBuffer(x, y, z) {
console.log(this.cells[this.vec3(x, y, z)]);
}
};
export {
CellTerrain
};