-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTerrain.js
89 lines (74 loc) · 2.98 KB
/
Terrain.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
const terrainBoundaries = {
lowerX: 0,
upperX: 32,
lowerY: 0,
upperY: 32,
lowerZ: 0,
upperZ: 32
};
export default class Terrain {
constructor(renderDistanceChunks) {
this.horizontalRenderDistance = renderDistanceChunks * 32; // TODO: Change 32 to a constant variable
}
init(centerPosition) {
const terrainBoundaries = this.getTerrainBoundariesFromPosition(centerPosition);
this.loadTerrain(terrainBoundaries);
this.prevCenterChunk = 0; // TODO: call getChunkID() from chunk class
}
update(centerPosition) {
const currentChunk = 0; // TODO: getChunkID()
if (this.prevCenterChunk !== currentChunk) {
const terrainBoundaries = this.getTerrainBoundariesFromPosition(centerPosition);
this.unloadTerrain(terrainBoundaries);
this.loadTerrain(terrainBoundaries);
this.prevCenterChunk = currentChunk;
}
}
loadTerrain(terrainBoundaries) {
const { lowerX, upperX, lowerY, upperY, lowerZ, upperZ } = boundaries;
for (let x = lowerX; x < upperX; x += 32) {
for (let y = lowerY; y < upperY; y += 32) {
for (let z = lowerZ; z < upperZ; z += 32) {
// TODO: generate chunk at (x, y, z)
}
}
}
}
unloadTerrain(terrainBoundaries) {
const { lowerX, upperX, lowerY, upperY, lowerZ, upperZ } = boundaries;
/*
for (const chunk of loadedChunks) {
const chunkWorldOriginPosition = chunk.getWorldOriginPosition();
if (
chunkWorldOriginPosition.x < lowerX ||
chunkWorldOriginPosition.x > upperX ||
chunkWorldOriginPosition.y < lowerY ||
chunkWorldOriginPosition.y > upperY ||
chunkWorldOriginPosition.z < lowerZ ||
chunkWorldOriginPosition.z > upperZ
) {
const removedMeshes = this.chunksManager.removeChunk(chunk.getId());
// remove the chunk meshes from the scene
for (const mesh of removedMeshes) {
if (mesh) {
this.scene.remove(mesh);
}
}
}
*/
}
getTerrainBoundaries(position) {
const centerChunkX = this.roundToNearestHorizontalChunk(position.x);
const centerChunkZ = this.roundToNearestHorizontalChunk(position.z);
const lowerX = centerChunkX - this.horizontalRenderDistance;
const upperX = centerChunkX + this.horizontalRenderDistance;
const lowerZ = centerChunkZ - this.horizontalRenderDistance;
const upperZ = centerChunkZ + this.horizontalRenderDistance;
const upperY = 256; // change value for max world height
const lowerY = 0; // change value for min world height
return { lowerX, upperX, lowerY, upperY, lowerZ, upperZ };
}
roundToNearestHorizontalChunk(num) {
return Math.round(num / 32) * 32;
}
}