/
game.go
287 lines (251 loc) · 7 KB
/
game.go
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
package main
import (
"flag"
"fmt"
_ "image/png"
"log"
"math"
"os"
"runtime"
"runtime/pprof"
"time"
"github.com/barnex/fmath"
"github.com/go-gl/glfw/v3.1/glfw"
)
var cpuprofile = flag.Bool("cpuprofile", false, "write cpu profile to file")
var heapprofile = flag.Bool("heapprofile", false, "write heap profile to file")
var debugtextures = flag.Bool("debugtextures", false, "write texture sheet to file")
type Player struct {
pos Vec3
gravity float32
yaw float32
pitch float32
}
type Average struct {
data []float64
pos int
}
func NewAverage(len int) Average {
return Average{make([]float64, len), 0}
}
func (a *Average) Push(v float64) {
a.data[a.pos] = v
a.pos++
if a.pos == len(a.data) {
a.pos = 0
}
}
func (a Average) Get() float64 {
var t float64
for i := 0; i < len(a.data); i++ {
t += a.data[i]
}
return t / float64(len(a.data))
}
var (
fps Average
player Player
movementX float32
movementZ float32
render Render
lastMx float64
lastMy float64
)
const DEG_RAD = math.Pi / 180
func onMouse(w *glfw.Window, button glfw.MouseButton, action glfw.Action, mod glfw.ModifierKey) {
if action == glfw.Press {
if button == glfw.MouseButtonLeft {
breakBlock()
} else if button == glfw.MouseButtonRight {
placeBlock()
}
}
}
func onResize(w *glfw.Window, width int, height int) {
render.Resize(int32(width), int32(height))
}
func onMove(w *glfw.Window, x float64, y float64) {
player.yaw += float32((x - lastMx) / 1000)
player.pitch += float32((y - lastMy) / 1000)
if player.pitch < -(math.Pi / 2) {
player.pitch = -(math.Pi / 2)
} else if player.pitch > (math.Pi / 2) {
player.pitch = (math.Pi / 2)
}
lastMx = x
lastMy = y
}
func (player Player) GetHoverCoords(w World) (Position, bool) {
stepX := -fmath.Sin(-player.yaw) * fmath.Cos(player.pitch) * 0.2
stepY := -fmath.Sin(player.pitch) * 0.2
stepZ := -fmath.Cos(-player.yaw) * fmath.Cos(player.pitch) * 0.2
bX := player.pos[0]
bY := player.pos[1] + EYE_HEIGHT
bZ := player.pos[2]
for stepCount := 100; stepCount > 0; stepCount-- {
if w.GetBlock(int(bX), int(bY), int(bZ)) != nil {
return Position{int(bX), int(bY), int(bZ)}, true
} else {
bX += stepX
bY += stepY
bZ += stepZ
}
}
return Position{}, false
}
func breakBlock() {
if pos, exists := player.GetHoverCoords(&w); exists {
w.SetBlock(pos.x, pos.y, pos.z, nil)
}
}
func placeBlock() {
stepX := -fmath.Sin(-player.yaw) * fmath.Cos(player.pitch) * 0.2
stepY := -fmath.Sin(player.pitch) * 0.2
stepZ := -fmath.Cos(-player.yaw) * fmath.Cos(player.pitch) * 0.2
bX := player.pos[0]
bY := player.pos[1] + EYE_HEIGHT
bZ := player.pos[2]
for stepCount := 100; stepCount > 0; stepCount-- {
if w.GetBlock(int(bX), int(bY), int(bZ)) != nil {
bX -= stepX
bY -= stepY
bZ -= stepZ
w.SetBlock(int(bX), int(bY), int(bZ), br.ByName("gold_block"))
return
} else {
bX += stepX
bY += stepY
bZ += stepZ
}
}
}
func onKey(w *glfw.Window, key glfw.Key, scancode int, action glfw.Action, mods glfw.ModifierKey) {
if key == glfw.KeyW {
if action == glfw.Release {
movementX = 0.0
} else {
movementX = 0.12
}
}
if key == glfw.KeyS {
if action == glfw.Release {
movementX = 0.0
} else {
movementX = -0.12
}
}
if key == glfw.KeyA {
if action == glfw.Release {
movementZ = 0.0
} else {
movementZ = -0.12
}
}
if key == glfw.KeyD {
if action == glfw.Release {
movementZ = 0.0
} else {
movementZ = 0.12
}
}
if key == glfw.KeySpace {
if action == glfw.Press {
player.gravity = 0.3
}
}
}
func init() {
// GLFW event handling must run on the main OS thread
runtime.LockOSThread()
}
var (
w WorldFlat
br BlockRegistry
)
func main() {
flag.Parse()
fps = NewAverage(256)
player.pos = Vec3{8, MAP_H + 16, 8}
br = NewBlockRegistry()
br.Register(&BlockSimple{name: "grass", textures: [6]string{"dirt.png", "grass.png", "grass_side.png", "grass_side.png", "grass_side.png", "grass_side.png"}})
br.Register(&BlockSimple{name: "dirt", textures: [6]string{"dirt.png", "dirt.png", "dirt.png", "dirt.png", "dirt.png", "dirt.png"}})
br.Register(&BlockSimple{name: "stone", textures: [6]string{"stone.png", "stone.png", "stone.png", "stone.png", "stone.png", "stone.png"}})
br.Register(&BlockSimple{name: "gold_block", textures: [6]string{"gold_block.png", "gold_block.png", "gold_block.png", "gold_block.png", "gold_block.png", "gold_block.png"}})
w = NewWorldFlat(br)
fmt.Printf("Loading...\n")
if err := glfw.Init(); err != nil {
log.Fatalln("failed to initialize glfw:", err)
}
defer glfw.Terminate()
glfw.WindowHint(glfw.ContextVersionMajor, 2)
glfw.WindowHint(glfw.ContextVersionMinor, 1)
window, err := glfw.CreateWindow(800, 600, "GM-M1-142, strona 12", nil, nil)
if err != nil {
panic(err)
}
window.MakeContextCurrent()
window.SetInputMode(glfw.CursorMode, glfw.CursorDisabled)
window.SetKeyCallback(onKey)
window.SetCursorPosCallback(onMove)
window.SetFramebufferSizeCallback(onResize)
window.SetMouseButtonCallback(onMouse)
//glfw.SwapInterval(0)
render.Init(800, 600, *debugtextures)
defer render.Deinit()
w.RegisterRenderListener(&render)
if *cpuprofile {
fmt.Printf("CPU profiling ON!")
f, err := os.Create("cpu.prof")
if err != nil {
log.Fatal(err)
}
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
}
for !window.ShouldClose() {
t := time.Now()
render.Render(&player, &w)
window.SwapBuffers()
glfw.PollEvents()
nanoTime := time.Since(t)
movementLX := movementX * float32(nanoTime) / (16 * 1000000)
movementLZ := movementZ * float32(nanoTime) / (16 * 1000000)
gravityLD := 0.5 * float32(nanoTime) / (1000 * 1000000)
fpsNow := float64(1000000000) / float64(nanoTime)
fps.Push(fpsNow)
//fmt.Printf("%.2f (%.2f) [%.2f %.2f %.2f]\n", fps.Get(), nanoTime, player.pos[0], player.pos[1], player.pos[2])
// player.pos[1] - player.pitch * movementLD,
// fmath.Cos(player.pitch)
// fall
if w.GetBlock(int(player.pos[0]), int(fmath.Ceil(player.pos[1])) - 1, int(player.pos[2])) == nil {
player.gravity -= gravityLD
} else if player.gravity < 0 {
player.gravity = 0
}
if player.gravity < 0 {
bY := player.pos[1]
for w.GetBlock(int(player.pos[0]), int(fmath.Ceil(player.pos[1])), int(player.pos[2])) == nil && player.pos[1] > (bY + player.gravity) {
player.pos[1] -= 0.0025
}
} else if player.gravity > 0 {
bY := player.pos[1]
for w.GetBlock(int(player.pos[0]), int(fmath.Ceil(player.pos[1])), int(player.pos[2])) == nil && player.pos[1] < (bY + player.gravity) {
player.pos[1] += 0.0025
}
}
nx := player.pos[0] - fmath.Sin(-player.yaw) * movementLX + fmath.Cos(-player.yaw) * movementLZ
nz := player.pos[2] - fmath.Cos(-player.yaw) * movementLX - fmath.Sin(-player.yaw) * movementLZ
if w.GetBlock(int(nx), int(fmath.Ceil(player.pos[1])), int(nz)) == nil {
player.pos[0] = nx
player.pos[2] = nz
}
}
if *heapprofile {
f, err := os.Create("heap.prof")
if err != nil {
log.Fatal(err)
}
pprof.WriteHeapProfile(f)
f.Close()
}
}