/
camera.go
310 lines (253 loc) · 6.34 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
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
package engi
import (
"sync"
"github.com/Lealen/engi/ecs"
"github.com/go-gl/mathgl/mgl32"
)
var (
MinZoom float32 = 0.25
MaxZoom float32 = 3
)
func GetCamera() *cameraSystem {
return cam
}
// CameraSystem is a System that manages the state of the Camera
type cameraSystem struct {
*ecs.System
x, y, z float32
tracking *ecs.Entity // The entity that is currently being followed
}
func (cameraSystem) Type() string {
return "cameraSystem"
}
func (cam *cameraSystem) New(*ecs.World) {
cam.System = ecs.NewSystem()
cam.x = WorldBounds.Max.X / 2
cam.y = WorldBounds.Max.Y / 2
cam.z = 1
cam.AddEntity(ecs.NewEntity([]string{cam.Type()}))
Mailbox.Listen("CameraMessage", func(msg Message) {
cammsg, ok := msg.(CameraMessage)
if !ok {
return
}
if cammsg.Incremental {
switch cammsg.Axis {
case XAxis:
cam.moveX(cammsg.Value)
case YAxis:
cam.moveY(cammsg.Value)
case ZAxis:
cam.zoom(cammsg.Value)
}
} else {
switch cammsg.Axis {
case XAxis:
cam.moveToX(cammsg.Value)
case YAxis:
cam.moveToY(cammsg.Value)
case ZAxis:
cam.zoomTo(cammsg.Value)
}
}
})
}
func (cam *cameraSystem) FollowEntity(entity *ecs.Entity) {
cam.tracking = entity
var space *SpaceComponent
if _, ok := cam.tracking.ComponentFast(space).(*SpaceComponent); !ok {
cam.tracking = nil
return
}
}
func (cam *cameraSystem) moveX(value float32) {
cam.moveToX(cam.x + value)
}
func (cam *cameraSystem) moveY(value float32) {
cam.moveToY(cam.y + value)
}
func (cam *cameraSystem) zoom(value float32) {
cam.zoomTo(cam.z + value)
}
func (cam *cameraSystem) moveToX(location float32) {
cam.x = mgl32.Clamp(location, WorldBounds.Min.X, WorldBounds.Max.X)
}
func (cam *cameraSystem) moveToY(location float32) {
cam.y = mgl32.Clamp(location, WorldBounds.Min.X, WorldBounds.Max.Y)
}
func (cam *cameraSystem) zoomTo(zoomLevel float32) {
cam.z = mgl32.Clamp(zoomLevel, MinZoom, MaxZoom)
}
func (cam *cameraSystem) X() float32 {
return cam.x
}
func (cam *cameraSystem) Y() float32 {
return cam.y
}
func (cam *cameraSystem) Z() float32 {
return cam.z
}
func (cam *cameraSystem) Update(entity *ecs.Entity, dt float32) {
if cam.tracking == nil {
return
}
var space *SpaceComponent
var ok bool
if space, ok = cam.tracking.ComponentFast(space).(*SpaceComponent); !ok {
return
}
cam.centerCam(space.Position.X+space.Width/2, space.Position.Y+space.Height/2, cam.z)
}
func (cam *cameraSystem) centerCam(x, y, z float32) {
cam.moveToX(x)
cam.moveToY(y)
cam.zoomTo(z)
}
// CameraAxis is the axis at which the Camera can/has to move
type CameraAxis uint8
const (
XAxis CameraAxis = iota
YAxis
ZAxis
)
// CameraMessage is a message that can be sent to the Camera (and other Systemers), to indicate movement
type CameraMessage struct {
Axis CameraAxis
Value float32
Incremental bool
}
func (CameraMessage) Type() string {
return "CameraMessage"
}
// KeyboardScroller is a Systemer that allows for scrolling when certain keys are pressed
type KeyboardScroller struct {
*ecs.System
scrollSpeed float32
upKeys []Key
leftKeys []Key
downKeys []Key
rightKeys []Key
keysMu sync.RWMutex
isSetup bool
}
func (*KeyboardScroller) Type() string {
return "KeyboardScroller"
}
func (c *KeyboardScroller) New(*ecs.World) {
if !c.isSetup {
c.System = ecs.NewSystem()
c.isSetup = true
}
}
func (c *KeyboardScroller) Update(entity *ecs.Entity, dt float32) {
c.keysMu.RLock()
defer c.keysMu.RUnlock()
for _, upKey := range c.upKeys {
if Keys.Get(upKey).Down() {
Mailbox.Dispatch(CameraMessage{YAxis, -c.scrollSpeed * dt, true})
break
}
}
for _, rightKey := range c.rightKeys {
if Keys.Get(rightKey).Down() {
Mailbox.Dispatch(CameraMessage{XAxis, c.scrollSpeed * dt, true})
break
}
}
for _, downKey := range c.downKeys {
if Keys.Get(downKey).Down() {
Mailbox.Dispatch(CameraMessage{YAxis, c.scrollSpeed * dt, true})
break
}
}
for _, leftKey := range c.leftKeys {
if Keys.Get(leftKey).Down() {
Mailbox.Dispatch(CameraMessage{XAxis, -c.scrollSpeed * dt, true})
break
}
}
}
func (c *KeyboardScroller) BindKeyboard(up, right, down, left Key) {
c.keysMu.Lock()
defer c.keysMu.Unlock()
c.upKeys = append(c.upKeys, up)
c.rightKeys = append(c.rightKeys, right)
c.downKeys = append(c.downKeys, down)
c.leftKeys = append(c.leftKeys, left)
}
func NewKeyboardScroller(scrollSpeed float32, up, right, down, left Key) *KeyboardScroller {
kbs := &KeyboardScroller{
scrollSpeed: scrollSpeed,
}
kbs.New(nil)
kbs.BindKeyboard(up, right, down, left)
kbs.AddEntity(ecs.NewEntity([]string{kbs.Type()}))
return kbs
}
// EdgeScroller is a Systemer that allows for scrolling when the mouse is near the edges
type EdgeScroller struct {
*ecs.System
scrollSpeed float32
margin float64
isSetup bool
}
func (*EdgeScroller) Type() string {
return "EdgeScroller"
}
func (c *EdgeScroller) New(*ecs.World) {
if !c.isSetup {
c.System = ecs.NewSystem()
c.isSetup = true
}
}
func (c *EdgeScroller) Update(entity *ecs.Entity, dt float32) {
curX, curY := window.GetCursorPos()
maxX, maxY := window.GetSize()
if curX < c.margin {
Mailbox.Dispatch(CameraMessage{XAxis, -c.scrollSpeed * dt, true})
} else if curX > float64(maxX)-c.margin {
Mailbox.Dispatch(CameraMessage{XAxis, c.scrollSpeed * dt, true})
}
if curY < c.margin {
Mailbox.Dispatch(CameraMessage{YAxis, -c.scrollSpeed * dt, true})
} else if curY > float64(maxY)-c.margin {
Mailbox.Dispatch(CameraMessage{YAxis, c.scrollSpeed * dt, true})
}
}
func NewEdgeScroller(scrollSpeed float32, margin float64) *EdgeScroller {
es := &EdgeScroller{
scrollSpeed: scrollSpeed,
margin: margin,
}
es.New(nil)
es.AddEntity(ecs.NewEntity([]string{es.Type()}))
return es
}
// MouseZoomer is a Systemer that allows for zooming when the scroll wheel is used
type MouseZoomer struct {
*ecs.System
zoomSpeed float32
isSetup bool
}
func (*MouseZoomer) Type() string {
return "MouseZoomer"
}
func (c *MouseZoomer) New(*ecs.World) {
if !c.isSetup {
c.System = ecs.NewSystem()
c.isSetup = true
}
}
func (c *MouseZoomer) Update(entity *ecs.Entity, dt float32) {
if Mouse.ScrollY != 0 {
Mailbox.Dispatch(CameraMessage{ZAxis, Mouse.ScrollY * c.zoomSpeed, true})
}
}
func NewMouseZoomer(zoomSpeed float32) *MouseZoomer {
es := &MouseZoomer{
zoomSpeed: zoomSpeed,
}
es.New(nil)
es.AddEntity(ecs.NewEntity([]string{es.Type()}))
return es
}