forked from michaljaz/webmc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCellTerrain.coffee
More file actions
64 lines (63 loc) · 1.74 KB
/
CellTerrain.coffee
File metadata and controls
64 lines (63 loc) · 1.74 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
class CellTerrain
constructor: (options)->
@cellSize=options.cellSize
@cells={}
@blocksDef=options.blocksDef
vec3: (x,y,z)->
x=parseInt x
y=parseInt y
z=parseInt z
return "#{x}:#{y}:#{z}"
computeVoxelOffset: (voxelX,voxelY,voxelZ) ->
x=voxelX %% @cellSize|0
y=voxelY %% @cellSize|0
z=voxelZ %% @cellSize|0
return y*@cellSize*@cellSize+z*@cellSize+x;
computeCellForVoxel: (voxelX,voxelY,voxelZ) ->
cellX = Math.floor voxelX / @cellSize
cellY = Math.floor voxelY / @cellSize
cellZ = Math.floor voxelZ / @cellSize
return [cellX,cellY,cellZ]
addCellForVoxel:(voxelX,voxelY,voxelZ)->
cellId=@vec3(@computeCellForVoxel(voxelX,voxelY,voxelZ)...)
cell=@cells[cellId]
if not cell
cell=new Uint32Array @cellSize*@cellSize*@cellSize
@cells[cellId]=cell
return cell
getCellForVoxel:(voxelX,voxelY,voxelZ)->
cellId=@vec3(@computeCellForVoxel(voxelX, voxelY, voxelZ)...)
return @cells[cellId]
setVoxel:(voxelX,voxelY,voxelZ,value)->
cell=@getCellForVoxel voxelX,voxelY,voxelZ
if not cell
cell=@addCellForVoxel voxelX,voxelY,voxelZ
voff=@computeVoxelOffset voxelX,voxelY,voxelZ
cell[voff]=value
return
getVoxel:(voxelX,voxelY,voxelZ)->
cell=@getCellForVoxel voxelX,voxelY,voxelZ
if not cell
return 0
voff=@computeVoxelOffset voxelX,voxelY,voxelZ
return cell[voff]
getCell:(cellX,cellY,cellZ)->
return @cells[@vec3(x,y,z)]
setCell:(cellX,cellY,cellZ,buffer)->
@cells[@vec3(cellX,cellY,cellZ)]=buffer
getBlock:(blockX,blockY,blockZ)->
stateId=@getVoxel(blockX,blockY,blockZ)
def=@blocksDef[stateId]
if def isnt undefined
if def[1] is 1
boundingBox="block"
else
boundingBox="empty"
return {
name:def[0]
stateId
boundingBox
}
else
return false
export {CellTerrain}