forked from icexin/gocraft
/
camera.go
220 lines (185 loc) · 3.92 KB
/
camera.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
package player
import (
"github.com/artheus/go-minecraft/core/ctx"
. "github.com/artheus/go-minecraft/core/types"
. "github.com/artheus/go-minecraft/math/f32"
"github.com/faiface/mainthread"
"github.com/go-gl/glfw/v3.2/glfw"
"github.com/go-gl/mathgl/mgl32"
)
type CameraMovement int
const (
MoveForward CameraMovement = iota
MoveBackward
MoveLeft
MoveRight
MoveJump
)
type Camera struct {
ctx *ctx.Context
pos mgl32.Vec3
up mgl32.Vec3
right mgl32.Vec3
front mgl32.Vec3
wfront mgl32.Vec3
prevtime float64
velocityY float32
rotateX, rotateY float32
Sens float32
flying bool
}
func NewCamera(ctx *ctx.Context, pos mgl32.Vec3) *Camera {
c := &Camera{
ctx: ctx,
pos: pos,
front: mgl32.Vec3{0, 0, -1},
rotateY: 0,
rotateX: -90,
Sens: 0.14,
flying: false,
}
c.updateAngles()
return c
}
func (c *Camera) Restore(state PlayerState) {
c.pos = mgl32.Vec3{state.X, state.Y, state.Z}
c.rotateX = state.Rx
c.rotateY = state.Ry
c.updateAngles()
}
func (c *Camera) State() PlayerState {
return PlayerState{
X: c.pos.X(),
Y: c.pos.Y(),
Z: c.pos.Z(),
Rx: c.rotateX,
Ry: c.rotateY,
}
}
func (c *Camera) Matrix() mgl32.Mat4 {
return mgl32.LookAtV(c.pos, c.pos.Add(c.front), c.up)
}
func (c *Camera) SetPos(pos mgl32.Vec3) {
c.pos = pos
}
func (c *Camera) Pos() mgl32.Vec3 {
return c.pos
}
func (c *Camera) Front() mgl32.Vec3 {
return c.front
}
func (c *Camera) FlipFlying() {
c.flying = !c.flying
}
func (c *Camera) Flying() bool {
return c.flying
}
func (c *Camera) OnAngleChange(dx, dy float32) {
if mgl32.Abs(dx) > 200 || mgl32.Abs(dy) > 200 {
return
}
c.rotateX += dx * c.Sens
c.rotateY += dy * c.Sens
if c.rotateY > 89 {
c.rotateY = 89
}
if c.rotateY < -89 {
c.rotateY = -89
}
c.updateAngles()
}
func (c *Camera) MovementEventLoop() {
running := true
subscriber := c.ctx.EventPipe().Subscriber()
go c.GravityLoop()
for running {
select {
case <-c.ctx.Context().Done():
running = false
break
case evt := <-subscriber.Get():
if move, ok := evt.Object().(*EventMove); ok {
c.OnMoveChange(move.Move, move.Delta)
}
}
}
}
func (c *Camera) GravityLoop() {
var running = true
for running {
select {
case <- c.ctx.Context().Done():
running = false
break
default:
if c.flying {
continue
}
var dt float64
mainthread.Call(func() {
now := glfw.GetTime()
dt = now - c.prevtime
c.prevtime = now
if dt > 0.02 {
dt = 0.02
}
})
c.velocityY -= float32(dt * 20)
if c.velocityY < -50 {
c.velocityY = -50
}
y := c.pos.Y()
ny := Round(c.pos.Y())
const pad = 0.25
head := Vec3{
X: Round(c.pos.X()),
Y: ny,
Z: Round(c.pos.Z()),
}
feet := head.Down()
if c.ctx.Game().World().Block(feet.Down()).Obstacle && y < ny && ny-y > pad && c.velocityY < 0 {
c.velocityY = 0 //c.pos.Y() - ny - pad
}
c.pos = c.pos.Add(mgl32.Vec3{0, c.velocityY*float32(dt), 0})
}
}
}
func (c *Camera) OnMoveChange(dir CameraMovement, delta float32) {
if c.flying {
delta = 5 * delta
}
switch dir {
case MoveForward:
if c.flying {
c.pos = c.pos.Add(c.front.Mul(delta))
} else {
c.pos = c.pos.Add(c.wfront.Mul(delta))
}
case MoveBackward:
if c.flying {
c.pos = c.pos.Sub(c.front.Mul(delta))
} else {
c.pos = c.pos.Sub(c.wfront.Mul(delta))
}
case MoveLeft:
c.pos = c.pos.Sub(c.right.Mul(delta))
case MoveRight:
c.pos = c.pos.Add(c.right.Mul(delta))
case MoveJump:
c.velocityY = delta
}
pos := c.Pos()
pos, _ = c.ctx.Game().World().Collide(pos)
c.SetPos(pos)
}
func (c *Camera) updateAngles() {
front := mgl32.Vec3{
Cos(Radian(c.rotateY)) * Cos(Radian(c.rotateX)),
Sin(Radian(c.rotateY)),
Cos(Radian(c.rotateY)) * Sin(Radian(c.rotateX)),
}
c.front = front.Normalize()
c.right = c.front.Cross(mgl32.Vec3{0, 1, 0}).Normalize()
c.up = c.right.Cross(c.front).Normalize()
c.wfront = mgl32.Vec3{0, 1, 0}.Cross(c.right).Normalize()
}