-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinput.go
More file actions
317 lines (296 loc) · 8.41 KB
/
Copy pathinput.go
File metadata and controls
317 lines (296 loc) · 8.41 KB
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
311
312
313
314
315
316
317
package main
import (
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/inpututil"
)
// eventMsg is a transient announcement drawn at the top of the screen
// (war declarations, conquests, surrenders...).
type eventMsg struct {
text string
ttl float64
}
// banner queues a world event announcement.
func (g *Game) banner(text string) {
g.banners = append(g.banners, eventMsg{text: text, ttl: 6})
if len(g.banners) > 6 {
g.banners = g.banners[1:]
}
}
func (g *Game) updateBanners(dt float64) {
kept := g.banners[:0]
for _, b := range g.banners {
b.ttl -= dt
if b.ttl > 0 {
kept = append(kept, b)
}
}
g.banners = kept
}
// handleInput processes selection and orders:
//
// Left click — select the unit under the cursor (Shift adds)
// Left drag (empty) — rubber-band select player units in the box
// Left drag (on unit) — ONE-TIME move order for the selection
// Right drag / click — PERSISTENT order for the selection (stays until C)
// Left+Right drag — DEFENCE LINE: the selection lines up along the
// line you draw (hold both buttons, drag, release)
// C — cancel orders (selection, or all of yours)
// X — declare war on the country under the cursor
func (g *Game) handleInput() {
mx, my := ebiten.CursorPosition()
if inpututil.IsKeyJustPressed(ebiten.KeyC) {
g.cancelSelectedOrders()
return
}
if inpututil.IsKeyJustPressed(ebiten.KeyX) {
g.declareWarOnHovered()
return
}
leftDown := ebiten.IsMouseButtonPressed(ebiten.MouseButtonLeft)
rightDown := ebiten.IsMouseButtonPressed(ebiten.MouseButtonRight)
// Defence-line order: with units selected, hold left+right and drag to
// draw a line; the selection deploys evenly along it once both buttons
// are released. The preview keeps following the cursor while one button
// is still held, and the gesture ends without committing if it was never
// completed (both buttons pressed together first).
if g.dragMode == dragLine {
if !leftDown && !rightDown {
g.finishLineOrder(mx, my)
g.dragMode = dragNone
}
return
}
if leftDown && rightDown && len(g.selected) > 0 {
g.dragMode = dragLine
g.dragStartX, g.dragStartY = mx, my
return
}
if inpututil.IsMouseButtonJustPressed(ebiten.MouseButtonLeft) {
g.dragStartX, g.dragStartY = mx, my
if u := g.playerUnitAt(mx, my); u != nil {
if ebiten.IsKeyPressed(ebiten.KeyShift) {
g.addSelected(u)
} else {
g.selected = []*Unit{u}
}
g.dragMode = dragMove
} else {
g.dragMode = dragSelect
}
}
if inpututil.IsMouseButtonJustReleased(ebiten.MouseButtonLeft) {
dx, dy := mx-g.dragStartX, my-g.dragStartY
if g.dragMode == dragSelect {
if dx*dx+dy*dy < 36 { // plain click: select under cursor or clear
if u := g.playerUnitAt(mx, my); u != nil {
if ebiten.IsKeyPressed(ebiten.KeyShift) {
g.addSelected(u)
} else {
g.selected = []*Unit{u}
}
} else {
g.selected = nil
}
} else {
g.selectBox(g.dragStartX, g.dragStartY, mx, my)
}
} else if g.dragMode == dragMove && dx*dx+dy*dy >= 36 {
wx, wy := g.camera.ScreenToWorld(float64(mx), float64(my))
g.issueOrders(g.selected, wx, wy, false)
}
g.dragMode = dragNone
}
// right button: persistent orders (right-click also selects the unit
// under the cursor if nothing is selected yet)
if inpututil.IsMouseButtonJustPressed(ebiten.MouseButtonRight) {
g.dragStartX, g.dragStartY = mx, my
if len(g.selected) == 0 {
if u := g.playerUnitAt(mx, my); u != nil {
g.selected = []*Unit{u}
}
}
}
if inpututil.IsMouseButtonJustReleased(ebiten.MouseButtonRight) {
if len(g.selected) > 0 {
wx, wy := g.camera.ScreenToWorld(float64(mx), float64(my))
g.issueOrders(g.selected, wx, wy, true)
}
}
}
// addSelected adds u to the selection if it isn't already there.
func (g *Game) addSelected(u *Unit) {
for _, s := range g.selected {
if s == u {
return
}
}
g.selected = append(g.selected, u)
}
// playerUnitAt returns the player's unit under screen coordinates, or nil.
func (g *Game) playerUnitAt(sx, sy int) *Unit {
m := g.gameMap
if m == nil || len(g.units) == 0 {
return nil
}
wx, wy := g.camera.ScreenToWorld(float64(sx), float64(sy))
tol := 14.0 / g.camera.Zoom // screen px tolerance scaled to world
tol2 := tol * tol
var best *Unit
bestD := tol2
for _, u := range g.units {
if u.Owner != m.PlayerCountry || u.HP <= 0 {
continue
}
dx, dy := u.X-wx, u.Y-wy
if d := dx*dx + dy*dy; d <= bestD {
bestD, best = d, u
}
}
return best
}
// selectBox selects every player unit inside the screen rectangle.
func (g *Game) selectBox(x0, y0, x1, y1 int) {
m := g.gameMap
if m == nil {
return
}
if x0 > x1 {
x0, x1 = x1, x0
}
if y0 > y1 {
y0, y1 = y1, y0
}
wx0, wy0 := g.camera.ScreenToWorld(float64(x0), float64(y0))
wx1, wy1 := g.camera.ScreenToWorld(float64(x1), float64(y1))
g.selected = nil
for _, u := range g.units {
if u.Owner != m.PlayerCountry || u.HP <= 0 {
continue
}
if u.X >= wx0 && u.X <= wx1 && u.Y >= wy0 && u.Y <= wy1 {
g.selected = append(g.selected, u)
}
}
}
// issueOrders gives every selected unit a move order. One-time orders target
// the exact point; persistent orders scatter each unit around it.
func (g *Game) issueOrders(units []*Unit, wx, wy float64, persistent bool) {
for _, u := range units {
if u.HP <= 0 {
continue
}
g.orderUnit(u, wx, wy, persistent)
}
}
// finishLineOrder commits the defence line the player is drawing: the line
// runs from where the gesture started (screen coords) to the current cursor,
// converted to world space for issueLineOrder.
func (g *Game) finishLineOrder(mx, my int) {
if len(g.selected) == 0 {
return
}
wx0, wy0 := g.camera.ScreenToWorld(float64(g.dragStartX), float64(g.dragStartY))
wx1, wy1 := g.camera.ScreenToWorld(float64(mx), float64(my))
g.issueLineOrder(g.selected, wx0, wy0, wx1, wy1)
}
// cancelSelectedOrders clears orders of the selection (or of every player
// unit when nothing is selected).
func (g *Game) cancelSelectedOrders() {
m := g.gameMap
if m == nil {
return
}
if len(g.selected) == 0 {
for _, u := range g.units {
if u.Owner == m.PlayerCountry {
u.Order = nil
}
}
return
}
for _, u := range g.selected {
u.Order = nil
}
}
// declareWarOnHovered declares war on the country under the cursor (X key).
func (g *Game) declareWarOnHovered() {
m := g.gameMap
if m == nil {
return
}
mx, my := ebiten.CursorPosition()
wx, wy := g.camera.ScreenToWorld(float64(mx), float64(my))
tx, ty := int(wx)/tileSize, int(wy)/tileSize
if tx < 0 || tx >= m.Width || ty < 0 || ty >= m.Height {
return
}
oc := m.Owner[ty][tx]
if oc < 0 || oc == m.PlayerCountry {
g.banner("No valid target to declare war on.")
return
}
if g.eliminated[oc] {
g.banner("That nation no longer exists.")
return
}
if g.atWarBetween(m.PlayerCountry, oc) {
g.banner("You are already at war with " + g.name(oc) + ".")
return
}
if g.pendingOutgoing(m.PlayerCountry) {
g.banner("You already have a war declaration pending.")
return
}
g.declareWar(m.PlayerCountry, oc)
}
// pendingOutgoing reports whether country i has its own declaration pending
// (incoming declarations from others don't block counter-declarations, which
// take effect immediately as a mutual war).
func (g *Game) pendingOutgoing(i int) bool {
for _, d := range g.decls {
if d.a == i {
return true
}
}
return false
}
// hoveredForeignCountry returns the non-player, non-eliminated country index
// under the cursor, or -1 (used for the HUD declare-war hint).
func (g *Game) hoveredForeignCountry() int {
m := g.gameMap
if m == nil {
return -1
}
mx, my := ebiten.CursorPosition()
wx, wy := g.camera.ScreenToWorld(float64(mx), float64(my))
tx, ty := int(wx)/tileSize, int(wy)/tileSize
if tx < 0 || tx >= m.Width || ty < 0 || ty >= m.Height {
return -1
}
oc := m.Owner[ty][tx]
if oc < 0 || oc == m.PlayerCountry || g.eliminated[oc] {
return -1
}
return oc
}
// selectionCentroid returns the average position of the selected units.
func (g *Game) selectionCentroid() (float64, float64, bool) {
if len(g.selected) == 0 {
return 0, 0, false
}
var sx, sy float64
for _, u := range g.selected {
sx += u.X
sy += u.Y
}
return sx / float64(len(g.selected)), sy / float64(len(g.selected)), true
}
// isSelected reports whether u is currently selected.
func (g *Game) isSelected(u *Unit) bool {
for _, s := range g.selected {
if s == u {
return true
}
}
return false
}