@@ -4,8 +4,8 @@ var modulo = function (a, b) {
44
55class ChunkTerrain {
66 constructor ( options ) {
7- this . cellSize = 16 ;
8- this . cells = { } ;
7+ this . chunkSize = 16 ;
8+ this . chunks = { } ;
99 this . blocksDef = options . blocksDef ;
1010 }
1111
@@ -17,66 +17,61 @@ class ChunkTerrain {
1717 }
1818
1919 computeVoxelOffset ( voxelX , voxelY , voxelZ ) {
20- var x , y , z ;
21- x = modulo ( voxelX , this . cellSize ) | 0 ;
22- y = modulo ( voxelY , this . cellSize ) | 0 ;
23- z = modulo ( voxelZ , this . cellSize ) | 0 ;
24- return y * this . cellSize * this . cellSize + z * this . cellSize + x ;
20+ var x = modulo ( voxelX , this . chunkSize ) | 0 ;
21+ var y = modulo ( voxelY , this . chunkSize ) | 0 ;
22+ var z = modulo ( voxelZ , this . chunkSize ) | 0 ;
23+ return y * this . chunkSize * this . chunkSize + z * this . chunkSize + x ;
2524 }
2625
27- computeCellForVoxel ( voxelX , voxelY , voxelZ ) {
28- var cellX , cellY , cellZ ;
29- cellX = Math . floor ( voxelX / this . cellSize ) ;
30- cellY = Math . floor ( voxelY / this . cellSize ) ;
31- cellZ = Math . floor ( voxelZ / this . cellSize ) ;
32- return [ cellX , cellY , cellZ ] ;
26+ computeChunkForVoxel ( voxelX , voxelY , voxelZ ) {
27+ return [
28+ Math . floor ( voxelX / this . chunkSize ) ,
29+ Math . floor ( voxelY / this . chunkSize ) ,
30+ Math . floor ( voxelZ / this . chunkSize ) ,
31+ ] ;
3332 }
3433
35- addCellForVoxel ( voxelX , voxelY , voxelZ ) {
36- var cell , cellId ;
37- cellId = this . vec3 ( ...this . computeCellForVoxel ( voxelX , voxelY , voxelZ ) ) ;
38- cell = this . cells [ cellId ] ;
34+ addChunkForVoxel ( voxelX , voxelY , voxelZ ) {
35+ var cellId = this . vec3 (
36+ ...this . computeChunkForVoxel ( voxelX , voxelY , voxelZ )
37+ ) ;
38+ var cell = this . chunks [ cellId ] ;
3939 if ( ! cell ) {
4040 cell = new Uint32Array (
41- this . cellSize * this . cellSize * this . cellSize
41+ this . chunkSize * this . chunkSize * this . chunkSize
4242 ) ;
43- this . cells [ cellId ] = cell ;
43+ this . chunks [ cellId ] = cell ;
4444 }
4545 return cell ;
4646 }
4747
48- getCellForVoxel ( voxelX , voxelY , voxelZ ) {
49- var cellId ;
50- cellId = this . vec3 ( ...this . computeCellForVoxel ( voxelX , voxelY , voxelZ ) ) ;
51- return this . cells [ cellId ] ;
48+ getChunkForVoxel ( voxelX , voxelY , voxelZ ) {
49+ var cellId = this . vec3 (
50+ ...this . computeChunkForVoxel ( voxelX , voxelY , voxelZ )
51+ ) ;
52+ return this . chunks [ cellId ] ;
5253 }
5354
5455 setVoxel ( voxelX , voxelY , voxelZ , value ) {
55- var cell , voff ;
56- cell = this . getCellForVoxel ( voxelX , voxelY , voxelZ ) ;
56+ var cell = this . getChunkForVoxel ( voxelX , voxelY , voxelZ ) ;
5757 if ( ! cell ) {
58- cell = this . addCellForVoxel ( voxelX , voxelY , voxelZ ) ;
58+ cell = this . addChunkForVoxel ( voxelX , voxelY , voxelZ ) ;
5959 }
60- voff = this . computeVoxelOffset ( voxelX , voxelY , voxelZ ) ;
60+ var voff = this . computeVoxelOffset ( voxelX , voxelY , voxelZ ) ;
6161 cell [ voff ] = value ;
6262 }
6363
6464 getVoxel ( voxelX , voxelY , voxelZ ) {
65- var cell , voff ;
66- cell = this . getCellForVoxel ( voxelX , voxelY , voxelZ ) ;
65+ var cell = this . getChunkForVoxel ( voxelX , voxelY , voxelZ ) ;
6766 if ( ! cell ) {
6867 return 0 ;
6968 }
70- voff = this . computeVoxelOffset ( voxelX , voxelY , voxelZ ) ;
69+ var voff = this . computeVoxelOffset ( voxelX , voxelY , voxelZ ) ;
7170 return cell [ voff ] ;
7271 }
7372
74- getCell ( x , y , z ) {
75- return this . cells [ this . vec3 ( x , y , z ) ] ;
76- }
77-
78- setCell ( cellX , cellY , cellZ , buffer ) {
79- return ( this . cells [ this . vec3 ( cellX , cellY , cellZ ) ] = buffer ) ;
73+ setChunk ( chunkX , chunkY , chunkZ , buffer ) {
74+ this . chunks [ this . vec3 ( chunkX , chunkY , chunkZ ) ] = buffer ;
8075 }
8176
8277 getBlock ( blockX , blockY , blockZ ) {
@@ -93,6 +88,10 @@ class ChunkTerrain {
9388 return false ;
9489 }
9590 }
91+
92+ reset ( ) {
93+ this . chunks = { } ;
94+ }
9695}
9796
9897export { ChunkTerrain } ;
0 commit comments