-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathcamera.go
61 lines (50 loc) · 1.78 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
package main
import (
"math"
"github.com/go-gl/mathgl/mgl32"
"github.com/shurcooL/eX0/eX0-go/packet"
)
type CameraI interface {
CalculateForFrame(gameMoment)
ModelView() mgl32.Mat4
}
// FreeCamera is a camera that is not attached to any player. It can be scolled around the level.
type FreeCamera struct {
pos [2]float32
zoom float32
}
func (*FreeCamera) CalculateForFrame(gameMoment) {}
func (c *FreeCamera) ModelView() mgl32.Mat4 {
s := c.Scale()
return mgl32.Translate3D(float32(components.view.windowSize[0])/2, float32(components.view.windowSize[1])/2, 0).
Mul4(mgl32.Scale3D(s, s, s)).
Mul4(mgl32.Translate3D(c.pos[0], c.pos[1], 0))
}
func (c *FreeCamera) Scale() float32 {
return float32(math.Pow(1.0+0.1*float64(c.zoom), 3))
}
// PlayerCamera is a camera attached to a player.
type PlayerCamera struct {
logic *logic
playerID uint8
pos playerPosVel
}
// CalculateForFrame calculates camera position for frame at gameMoment.
// c.logic.playersStateMu must be held.
func (c *PlayerCamera) CalculateForFrame(gameMoment gameMoment) {
ps := c.logic.playersState[c.playerID]
if (ps.conn != nil && ps.conn.JoinStatus < IN_GAME) || ps.Team == packet.Spectator {
return
}
// TODO: Consider using same position as calculated for all players
// during "Calculate player positions for this frame" step,
// instead of our own copy (which might not match).
// This is better now that we're using same gameMoment
// and locking playersStateMu outside.
c.pos = ps.InterpolatedOrDead(gameMoment, c.playerID)
}
func (c *PlayerCamera) ModelView() mgl32.Mat4 {
return mgl32.Translate3D(float32(components.view.windowSize[0])/2, float32(components.view.windowSize[1])/20, 0).
Mul4(mgl32.HomogRotate3DZ(c.pos.Z)).
Mul4(mgl32.Translate3D(-c.pos.X, -c.pos.Y, 0))
}