-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
camera.go
244 lines (201 loc) · 5.23 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
/*
Copyright 2018 V4 Games
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package core
import (
"fmt"
"log"
"strconv"
jsoniter "github.com/json-iterator/go"
rl "github.com/zaklaus/raylib-go/raylib"
"github.com/zaklaus/raylib-go/raymath"
)
const (
// CameraModeStatic stationary camera
CameraModeStatic = 1
// CameraModeFollow camera following an object
CameraModeFollow = 2
// CameraModeLerp camera transiting between two objects
CameraModeLerp = 3
)
type camera struct {
Follow *Object
Start *Object
End *Object
Speed float32
Progress float32
Zoom float32
TargetZoom float32
ZoomSpeed float32
Mode int
First bool
FollowName string
}
type cameraData struct {
Follow string
Start string
End string
Speed float32
Progress float32
TargetZoom float32
ZoomSpeed float32
Mode int
First bool
}
// NewCamera game camera
func (c *Object) NewCamera() {
c.Update = updateCamera
c.Finish = finishCamera
c.Zoom = 1
c.TargetZoom = c.Zoom
c.ZoomSpeed = 0.8
c.DebugVisible = false
c.First = true
followName := c.Meta.Properties.GetString("follow")
if followName == "" {
c.FollowName = "player"
} else {
c.FollowName = followName
}
c.Mode = CameraModeFollow
strMode := c.Meta.Properties.GetString("mode")
if strMode != "" {
c.SetCameraMode(strMode)
}
spd, _ := strconv.ParseFloat(c.Meta.Properties.GetString("speed"), 32)
if spd == 0 {
spd = 1
}
c.Serialize = func(o *Object) string {
val, _ := jsoniter.MarshalToString(&cameraData{
Follow: o.FollowName,
Start: "",
End: "",
Speed: o.Speed,
Progress: o.Progress,
TargetZoom: o.TargetZoom,
ZoomSpeed: o.ZoomSpeed,
Mode: o.Mode,
First: o.First,
})
return val
}
c.Deserialize = func(o *Object, v string) {
var dat cameraData
jsoniter.UnmarshalFromString(v, &dat)
/* o.Follow = dat.Follow
o.Start = dat.Start
o.End = dat.End */
o.Speed = dat.Speed
o.Progress = dat.Progress
o.TargetZoom = dat.TargetZoom
o.ZoomSpeed = dat.ZoomSpeed
o.Mode = dat.Mode
o.First = dat.First
o.FollowName = dat.Follow
}
c.Speed = float32(spd)
MainCamera = c
c.Draw = func(o *Object) {
if !DebugMode || !o.DebugVisible {
return
}
mode := "static"
switch o.Mode {
case 2:
mode = "follow"
case 3:
mode = "lerp"
}
rl.DrawCircle(int32(o.Position.X), int32(o.Position.Y), 2, rl.White)
DrawTextCentered(fmt.Sprintf("%s", o.Name), int32(o.Position.X), int32(o.Position.Y)+5, 10, rl.White)
DrawTextCentered(fmt.Sprintf("Mode: %s", mode), int32(o.Position.X), int32(o.Position.Y)+15, 10, rl.White)
}
}
func finishCamera(c *Object) {
if c.Mode == CameraModeFollow {
c.Follow, _ = c.world.FindObject(c.FollowName)
if c.First && c.Follow != nil {
c.Position = c.Follow.Position
}
} else if c.Mode == CameraModeLerp {
c.Start, _ = c.world.FindObject(c.Meta.Properties.GetString("start"))
c.End, _ = c.world.FindObject(c.Meta.Properties.GetString("end"))
}
}
func updateCamera(c *Object, dt float32) {
var dest rl.Vector2
CanSave = BitsSet(CanSave, IsSequenceHappening)
if c.Mode == CameraModeFollow {
if c.Follow == nil {
log.Println("Camera object follows nil reference.")
return
}
if c.Follow == LocalPlayer {
CanSave = BitsClear(CanSave, IsSequenceHappening)
}
dest = c.Follow.Position
} else if c.Mode == CameraModeLerp {
if c.Start == nil || c.End == nil {
log.Println("Camera object lerps between nil references.")
return
}
if c.First {
c.Position = c.Start.Position
}
dest = c.End.Position
} else {
dest = c.Position
}
if !c.First || c.Mode == CameraModeLerp {
t := c.Speed
if c.Mode == CameraModeLerp {
vd := raymath.Vector2Distance(dest, c.Position)
if c.Progress > 1 {
c.Progress = 1
c.Mode = CameraModeStatic
if c.Target != nil {
c.Target.Trigger(c.Target, c)
}
}
if t > vd {
t = vd
}
c.Position = Vector2Lerp(c.Start.Position, dest, c.Progress)
c.Progress += t * dt
} else {
c.Position = Vector2Lerp(c.Position, dest, t)
}
} else {
c.Position = dest
}
c.Zoom = ScalarLerp(c.Zoom, c.TargetZoom, c.ZoomSpeed*dt)
c.First = false
}
// SetCameraZoom overrides camera zoom
func (c *Object) SetCameraZoom(t float32) {
c.Zoom = t
c.TargetZoom = t
}
// SetCameraMode sets the camera behavior mode
func (c *Object) SetCameraMode(strMode string) {
switch strMode {
default:
fallthrough
case "static":
c.Mode = 1
case "follow":
c.Mode = 2
case "lerp":
c.Mode = 3
}
}