1
+ window . World = class {
2
+
3
+ static get TOTAL_HEIGHT ( ) {
4
+ return ChunkSection . SIZE * 16 - 1 ;
5
+ }
6
+
7
+ constructor ( ) {
8
+ this . group = new THREE . Object3D ( ) ;
9
+ this . chunks = [ ] ;
10
+
11
+ for ( let x = - 16 ; x < 16 ; x ++ ) {
12
+ for ( let z = - 16 ; z < 16 ; z ++ ) {
13
+ this . setBlockAt ( x , 0 , z , 1 ) ;
14
+ }
15
+ }
16
+ }
17
+
18
+ setBlockAt ( x , y , z , type ) {
19
+ let chunkSection = this . getChunkAtBlock ( x , y , z ) ;
20
+ if ( chunkSection != null ) {
21
+ chunkSection . setBlockAt ( x & 15 , y & 15 , z & 15 , type ) ;
22
+ }
23
+
24
+ this . blockChanged ( x , y , z ) ;
25
+ }
26
+
27
+ getChunkAt ( x , z ) {
28
+ let zArray = this . chunks [ x ] ;
29
+ if ( typeof zArray === 'undefined' ) {
30
+ zArray = this . chunks [ x ] = [ ] ;
31
+ }
32
+
33
+ let chunk = zArray [ z ] ;
34
+ if ( typeof chunk === 'undefined' ) {
35
+ chunk = new Chunk ( x , z ) ;
36
+ this . chunks [ x ] [ z ] = chunk ;
37
+ this . group . add ( chunk . group ) ;
38
+ }
39
+ return chunk ;
40
+ }
41
+
42
+ blockChanged ( x , y , z ) {
43
+ this . setDirty ( x - 1 , y - 1 , z - 1 , x + 1 , y + 1 , z + 1 ) ;
44
+ }
45
+
46
+ setDirty ( minX , minY , minZ , maxX , maxY , maxZ ) {
47
+ // To chunk coordinates
48
+ minX = minX >> 4 ;
49
+ maxX = maxX >> 4 ;
50
+ minY = minY >> 4 ;
51
+ maxY = maxY >> 4 ;
52
+ minZ = minZ >> 4 ;
53
+ maxZ = maxZ >> 4 ;
54
+
55
+ // Minimum and maximum y
56
+ minY = Math . max ( 0 , minY ) ;
57
+ maxY = Math . min ( 15 , maxY ) ;
58
+
59
+ for ( let x = minX ; x <= maxX ; x ++ ) {
60
+ for ( let y = minY ; y <= maxY ; y ++ ) {
61
+ for ( let z = minZ ; z <= maxZ ; z ++ ) {
62
+ this . getChunkAt ( x , y , z ) . queueForRebuild ( ) ;
63
+ }
64
+ }
65
+ }
66
+ }
67
+
68
+ getChunkAtBlock ( x , y , z ) {
69
+ let chunk = this . getChunkAt ( x >> 4 , z >> 4 ) ;
70
+ return y < 0 || y > World . TOTAL_HEIGHT ? null : chunk . getSection ( y >> 4 ) ;
71
+ }
72
+
73
+
74
+ }
0 commit comments