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
|
package main
import (
"math"
"age_of_empires/physics"
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/inpututil"
)
const (
ScreenWidth = 960
ScreenHeight = 640
WorldWidth = 3200
WorldHeight = 2400
cameraZoomMin = 0.25
cameraZoomMax = 3.0
cameraZoomStep = 0.10
cameraScrollSpeed = 400.0
cameraDrag = 8.0
)
type Camera struct {
X, Y float64
Zoom float64
vx, vy float64
dragActive bool
dragStartScreenX, dragStartScreenY int
dragStartCamX, dragStartCamY float64
}
func (c *Camera) WorldToScreen(p physics.Point) (float64, float64) {
sx := (float64(p.X)-c.X)*c.Zoom + ScreenWidth/2
sy := (float64(p.Y)-c.Y)*c.Zoom + ScreenHeight/2
return sx, sy
}
func (c *Camera) ScreenToWorld(sx, sy int) physics.Point {
zoom := c.Zoom
if zoom == 0 {
zoom = 1
}
wx := float64(sx-ScreenWidth/2)/zoom + c.X
wy := float64(sy-ScreenHeight/2)/zoom + c.Y
return physics.Point{X: int(math.Round(wx)), Y: int(math.Round(wy))}
}
func (c *Camera) GeoM() ebiten.GeoM {
var m ebiten.GeoM
m.Translate(-c.X, -c.Y)
m.Scale(c.Zoom, c.Zoom)
m.Translate(ScreenWidth/2, ScreenHeight/2)
return m
}
func (c *Camera) clampToBounds() {
halfW := (ScreenWidth / 2.0) / c.Zoom
halfH := (ScreenHeight / 2.0) / c.Zoom
minX, maxX := halfW, float64(WorldWidth)-halfW
if minX > maxX {
minX = WorldWidth / 2
maxX = WorldWidth / 2
}
minY, maxY := halfH, float64(WorldHeight)-halfH
if minY > maxY {
minY = WorldHeight / 2
maxY = WorldHeight / 2
}
if c.X < minX {
c.X = minX
if c.vx < 0 {
c.vx = 0
}
}
if c.X > maxX {
c.X = maxX
if c.vx > 0 {
c.vx = 0
}
}
if c.Y < minY {
c.Y = minY
if c.vy < 0 {
c.vy = 0
}
}
if c.Y > maxY {
c.Y = maxY
if c.vy > 0 {
c.vy = 0
}
}
}
func (c *Camera) Update(dt float64) {
zoom := c.Zoom
if zoom == 0 {
zoom = 1
}
// Scroll wheel zoom toward cursor
_, wy := ebiten.Wheel()
if wy != 0 {
mx, my := ebiten.CursorPosition()
// cursor world pos before zoom
wx0 := float64(mx-ScreenWidth/2)/zoom + c.X
wy0 := float64(my-ScreenHeight/2)/zoom + c.Y
newZoom := math.Max(cameraZoomMin, math.Min(cameraZoomMax, zoom+wy*cameraZoomStep))
c.Zoom = newZoom
zoom = newZoom
// translate so cursor stays fixed
wx1 := float64(mx-ScreenWidth/2)/zoom + c.X
wy1 := float64(my-ScreenHeight/2)/zoom + c.Y
c.X -= wx1 - wx0
c.Y -= wy1 - wy0
}
// Middle-mouse drag
mx, my := ebiten.CursorPosition()
if inpututil.IsMouseButtonJustPressed(ebiten.MouseButtonMiddle) {
c.dragActive = true
c.dragStartScreenX = mx
c.dragStartScreenY = my
c.dragStartCamX = c.X
c.dragStartCamY = c.Y
}
if c.dragActive {
if ebiten.IsMouseButtonPressed(ebiten.MouseButtonMiddle) {
c.X = c.dragStartCamX - float64(mx-c.dragStartScreenX)/zoom
c.Y = c.dragStartCamY - float64(my-c.dragStartScreenY)/zoom
} else {
c.dragActive = false
}
}
// Arrow key pan (velocity-based)
dx, dy := 0.0, 0.0
if ebiten.IsKeyPressed(ebiten.KeyArrowLeft) {
dx -= 1
}
if ebiten.IsKeyPressed(ebiten.KeyArrowRight) {
dx += 1
}
if ebiten.IsKeyPressed(ebiten.KeyArrowUp) {
dy -= 1
}
if ebiten.IsKeyPressed(ebiten.KeyArrowDown) {
dy += 1
}
if dx != 0 && dy != 0 {
inv := 1.0 / math.Sqrt2
dx *= inv
dy *= inv
}
c.vx += dx * cameraScrollSpeed
c.vy += dy * cameraScrollSpeed
// Apply velocity and exponential drag
c.X += c.vx * dt
c.Y += c.vy * dt
decay := math.Exp(-cameraDrag * dt)
c.vx *= decay
c.vy *= decay
c.clampToBounds()
}
|