1+ import {CellTerrain} from ' ./CellTerrain.js'
2+
13class TerrainManager
24 constructor : (options )->
35 @toxelSize = options .toxelSize
@@ -6,7 +8,9 @@ class TerrainManager
68 @blocksMapping = options .blocksMapping
79 @cellSize = options .cellSize
810 @models = options .models
9- @world = {}
11+ @cellTerrain = new CellTerrain {
12+ cellSize : @cellSize
13+ }
1014 console .log " WORKER: TerrainManager started!"
1115 getToxel : (x ,y )->
1216 x -= 1
@@ -30,8 +34,9 @@ class TerrainManager
3034 z = parseInt z
3135 return " #{ x} :#{ y} :#{ z} "
3236 genBlockFace : (type ,voxel )->
33- blockName = @blocks [voxel][ " faces " ][type]
37+
3438 try
39+ blockName = @blocks [voxel][" faces" ][type]
3540 toxX = @blocksMapping [blockName][" x" ]
3641 toxY = @blocksMapping [blockName][" y" ]
3742 catch e
@@ -123,18 +128,18 @@ class TerrainManager
123128 pos = [cellX* @cellSize + i,cellY* @cellSize + j,cellZ* @cellSize + k]
124129 voxel = @ getVoxel pos...
125130 if voxel
126- if @blocks [voxel].isBlock
127- if not @blocks [@ getVoxel (pos[0 ]+ 1 ,pos[1 ],pos[2 ])].isBlock
131+ if @blocks [voxel] is undefined or @blocks [voxel] .isBlock
132+ if not ( @blocks [@ getVoxel (pos[0 ]+ 1 ,pos[1 ],pos[2 ])] is undefined or @blocks [ @ getVoxel (pos[ 0 ] + 1 ,pos[ 1 ],pos[ 2 ])] .isBlock )
128133 addFace " nx" ,pos,voxel
129- if not @blocks [@ getVoxel (pos[0 ]- 1 ,pos[1 ],pos[2 ])].isBlock
134+ if not ( @blocks [@ getVoxel (pos[0 ]- 1 ,pos[1 ],pos[2 ])] is undefined or @blocks [ @ getVoxel (pos[ 0 ] - 1 ,pos[ 1 ],pos[ 2 ])] .isBlock )
130135 addFace " px" ,pos,voxel
131- if not @blocks [@ getVoxel (pos[0 ],pos[1 ]- 1 ,pos[2 ])].isBlock
136+ if not ( @blocks [@ getVoxel (pos[0 ],pos[1 ]- 1 ,pos[2 ])] is undefined or @blocks [ @ getVoxel (pos[ 0 ],pos[ 1 ] - 1 ,pos[ 2 ])] .isBlock )
132137 addFace " ny" ,pos,voxel
133- if not @blocks [@ getVoxel (pos[0 ],pos[1 ]+ 1 ,pos[2 ])].isBlock
138+ if not ( @blocks [@ getVoxel (pos[0 ],pos[1 ]+ 1 ,pos[2 ])] is undefined or @blocks [ @ getVoxel (pos[ 0 ],pos[ 1 ] + 1 ,pos[ 2 ])] .isBlock )
134139 addFace " py" ,pos,voxel
135- if not @blocks [@ getVoxel (pos[0 ],pos[1 ],pos[2 ]+ 1 )].isBlock
140+ if not ( @blocks [@ getVoxel (pos[0 ],pos[1 ],pos[2 ]+ 1 )] is undefined or @blocks [ @ getVoxel (pos[ 0 ],pos[ 1 ],pos[ 2 ] + 1 )] .isBlock )
136141 addFace " pz" ,pos,voxel
137- if not @blocks [@ getVoxel (pos[0 ],pos[1 ],pos[2 ]- 1 )].isBlock
142+ if not ( @blocks [@ getVoxel (pos[0 ],pos[1 ],pos[2 ]- 1 )] is undefined or @blocks [ @ getVoxel (pos[ 0 ],pos[ 1 ],pos[ 2 ] - 1 )] .isBlock )
138143 addFace " nz" ,pos,voxel
139144 else
140145 geo = @models [@blocks [voxel].model ]
@@ -145,14 +150,10 @@ class TerrainManager
145150 uvs
146151 }
147152 setVoxel : (voxelX ,voxelY ,voxelZ ,value )->
148- @world [ @ vec3 ( voxelX,voxelY,voxelZ)] = value
153+ @cellTerrain . setVoxel voxelX,voxelY,voxelZ, value
149154 return
150155 getVoxel : (voxelX ,voxelY ,voxelZ )->
151- voxel = @world [@ vec3 (voxelX,voxelY,voxelZ)]
152- if voxel is undefined
153- return 0
154- else
155- return voxel
156+ return @cellTerrain .getVoxel voxelX,voxelY,voxelZ
156157
157158addEventListener " message" , (e )->
158159 fn = handlers[e .data .type ]
@@ -183,4 +184,6 @@ handlers={
183184 cell : terrain .genCellGeo data...
184185 info : data
185186 }
187+ setCell : (data )->
188+ terrain .cellTerrain .cells [" #{ data[0 ]} :#{ data[1 ]} :#{ data[2 ]} " ]= data[3 ]
186189}
0 commit comments