-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcamera.go
137 lines (109 loc) · 3.6 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
package main
import (
"math"
"github.com/hajimehoshi/ebiten/v2"
)
/*
Use this somewhere:
mapScreenWidth := float64(g.level.width*tileSize) * g.camera.GetZoom()
mapScreenHeight := float64(g.level.height*tileSize) * g.camera.GetZoom()
if mapScreenWidth < screenWidth {
g.view.offsetX = (screenWidth - mapScreenWidth) / 2
} else {
g.view.offsetX = math.Max(-(mapScreenWidth - screenWidth), math.Min(0, screenWidth/2-cameraFocusEntiy.worldPosX*g.camera.GetZoom()))
}
if mapScreenHeight < screenHeight {
g.view.offsetY = (screenHeight - mapScreenHeight) / 2
} else {
g.view.offsetY = math.Max(-(mapScreenHeight - screenHeight), math.Min(0, screenHeight/2-cameraFocusEntiy.worldPosY*g.camera.GetZoom()))
}
*/
type ICamera interface {
TargetPosition(Vec2f)
TargetEntity(*ILivingEntity)
TargetCursor(*TileCursor)
Update(Vec2f)
GetZoom() float64
SetZoom(float64)
GetCurrentPosition() Vec2f
WorldToScreen2(Vec2f) Vec2f
}
type CameraTargetType uint8
const (
CAMERA_TARGET_POSITION CameraTargetType = 1
CAMERA_TARGET_ENTITY CameraTargetType = 2
CAMERA_TARGET_CURSOR CameraTargetType = 3
)
type Camera struct {
zoom float64
currentWorldPos Vec2f
targetWorldPos Vec2f
targetEntity ILivingEntity
targetCursor *TileCursor
targetType CameraTargetType
tickAnimStarted int
}
func (c *Camera) TargetPosition(pos Vec2f) {
c.targetWorldPos = pos
c.targetType = CAMERA_TARGET_POSITION
c.tickAnimStarted = tickCounter
}
func (c *Camera) TargetEntity(e ILivingEntity) {
c.targetEntity = e
c.targetType = CAMERA_TARGET_ENTITY
c.tickAnimStarted = tickCounter
}
func (c *Camera) TargetCursor(cur *TileCursor) {
c.targetCursor = cur
c.targetType = CAMERA_TARGET_CURSOR
c.tickAnimStarted = tickCounter
}
func EaseOutQuad(factor float64) float64 {
return 1 - (1-factor)*(1-factor)
}
func (c *Camera) Update() {
var duration int = ebiten.MaxTPS() * 1
if c.targetType == CAMERA_TARGET_POSITION {
c.currentWorldPos = c.targetWorldPos
} else if c.targetType == CAMERA_TARGET_ENTITY && c.targetEntity != nil {
animIteration := tickCounter - c.tickAnimStarted
entityPos := c.targetEntity.GetWorldPos()
if animIteration <= duration {
animFactor := float64(animIteration) / float64(duration)
delta := entityPos.Translate(c.currentWorldPos.Negate()).Scale(animFactor)
c.currentWorldPos = c.currentWorldPos.Translate(delta)
} else {
c.currentWorldPos = entityPos
}
} else if c.targetType == CAMERA_TARGET_CURSOR {
animIteration := tickCounter - c.tickAnimStarted
cursorPos := Vec2f{float64(c.targetCursor.x), float64(c.targetCursor.y)}.Scale(tileSize).Translate(Vec2f{tileSize / 2, tileSize / 2})
if animIteration <= duration {
animFactor := float64(animIteration) / float64(duration)
delta := cursorPos.Translate(c.currentWorldPos.Negate()).Scale(animFactor)
c.currentWorldPos = c.currentWorldPos.Translate(delta)
} else {
c.currentWorldPos = cursorPos
}
}
}
func (c *Camera) GetCurrentPosition() Vec2f {
return c.currentWorldPos
}
func (c *Camera) WorldToScreen2(pos Vec2f) Vec2f {
screenCenter := Vec2f{screenWidth / 2, screenHeight / 2}
cameraWorldPos := c.currentWorldPos
entityWorldPos := pos
cameraZoom := c.zoom
var screenPos Vec2f
screenPos = cameraWorldPos.Subtract(entityWorldPos)
screenPos = screenPos.Scale(cameraZoom)
screenPos = screenCenter.Subtract(screenPos)
return screenPos
}
func (c *Camera) SetZoom(zoom float64) {
c.zoom = math.Max(1.0, zoom)
}
func (c *Camera) GetZoom() float64 {
return c.zoom
}