-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtypes.go
More file actions
167 lines (152 loc) · 2.68 KB
/
types.go
File metadata and controls
167 lines (152 loc) · 2.68 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
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package main
import (
"math"
rl "github.com/gen2brain/raylib-go/raylib"
)
const (
chunkWidth = 16
chunkHeight = 256
sectionHeight = 16
sectionCount = chunkHeight / sectionHeight
)
const (
blockAir byte = iota
blockGrass
blockStone
blockDirt
blockCobblestone
blockSand
blockLeaves
blockWater
blockLava
blockGravel
blockTorch
blockCoalOre
blockIronOre
blockGoldOre
blockDiamondOre
blockLapisOre
blockBedrock
blockLog
blockPlank
blockGlass
blockGlowstone
blockObsidian
blockDiamondBlock
blockGoldBlock
blockIronBlock
blockCoalBlock
blockLogBirch
blockLogSpruce
blockLeavesBirch
blockLeavesSpruce
blockPlankOak
blockPlankBirch
blockPlankSpruce
blockSandstone
blockCactus
blockDeadBush
blockSnow
blockIce
blockRose
blockDandelion
blockTallGrass
blockCraftingTable
)
const (
// Tools (Start at 100 to avoid block collision)
itemWoodPickaxe byte = 100 + iota
itemStonePickaxe
itemIronPickaxe
itemDiamondPickaxe
itemGoldPickaxe
itemWoodShovel
itemStoneShovel
itemIronShovel
itemDiamondShovel
itemGoldShovel
itemWoodAxe
itemStoneAxe
itemIronAxe
itemDiamondAxe
itemGoldAxe
// Resources
itemCoal
itemIronIngot
itemGoldIngot
itemDiamond
itemStick
)
type hitInfo struct {
x int
y int
z int
normal rl.Vector3
distance float32
hit bool
}
type blockFaces struct {
Top string
Bottom string
North string
South string
East string
West string
}
type faceMesh struct {
mesh rl.Mesh
vertices []float32
normals []float32
texcoords []float32
indices []uint16
}
func inBounds(x, y, z int) bool {
return x >= 0 && x < chunkWidth &&
y >= 0 && y < chunkHeight &&
z >= 0 && z < chunkWidth
}
func divFloor(v, d int) int {
if d == 0 {
return 0
}
if v >= 0 {
return v / d
}
return -(((-v) + d - 1) / d)
}
func modFloor(v, d int) int {
if d == 0 {
return 0
}
m := v % d
if m < 0 {
m += d
}
return m
}
func findHit(blocks *[chunkWidth][chunkHeight][chunkWidth]byte, ray rl.Ray) hitInfo {
best := hitInfo{distance: float32(math.MaxFloat32)}
for x := 0; x < chunkWidth; x++ {
for y := 0; y < chunkHeight; y++ {
for z := 0; z < chunkWidth; z++ {
if blocks[x][y][z] == blockAir {
continue
}
min := rl.NewVector3(float32(x)-0.5, float32(y)-0.5, float32(z)-0.5)
max := rl.NewVector3(float32(x)+0.5, float32(y)+0.5, float32(z)+0.5)
collision := rl.GetRayCollisionBox(ray, rl.BoundingBox{Min: min, Max: max})
if collision.Hit && collision.Distance < best.distance {
best = hitInfo{
x: x,
y: y,
z: z,
normal: collision.Normal,
distance: collision.Distance,
hit: true,
}
}
}
}
}
return best
}