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
|
package selection
import (
"image/color"
"math"
"age_of_empires/ecs"
"age_of_empires/physics"
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/vector"
)
const HaloWidth = 10
type Priority int
const (
Unit Priority = iota
Building
)
type GlobalSelection struct {
isActive bool
start physics.Point
}
func (s GlobalSelection) IsActive(cursor physics.Point) bool {
return s.isActive && physics.Distance(s.start, cursor) > 10
}
func (s *GlobalSelection) Clear() {
s.isActive = false
s.start = physics.Point{}
}
func (s *GlobalSelection) Start(point physics.Point) {
s.isActive = true
s.start = point
}
func (s *GlobalSelection) Select(cursor physics.Point) Multiple {
s.isActive = false
return NewMultiple(s.start, cursor)
}
func (s *GlobalSelection) Draw(dst *ebiten.Image, cursor physics.Point) {
vector.StrokeRect(dst, float32(s.start.X), float32(s.start.Y), float32(cursor.X-s.start.X), float32(cursor.Y-s.start.Y), 10.0, color.RGBA{256 * 3 / 16, 256 * 3 / 16, 256 * 3 / 16, 256 / 4}, true)
}
type Selection struct {
IsSelected bool
Halo *ebiten.Image
Priority Priority
}
type Multiple struct {
Bounds physics.Rectangle
Preselected []*ecs.Component[Selection]
}
func NewMultiple(start physics.Point, end physics.Point) Multiple {
return Multiple{
Bounds: physics.Rectangle{Min: start, Max: end}.Canon(),
Preselected: []*ecs.Component[Selection]{},
}
}
func (m *Multiple) Preselect(position ecs.Component[physics.Point], relBounds ecs.Component[physics.Rectangle], selection *ecs.Component[Selection]) {
if !selection.IsEnabled || !position.IsEnabled || !relBounds.IsEnabled {
return
}
selection.Value.IsSelected = false
if m.Bounds.Overlaps(physics.Translate(relBounds.Value, position.Value)) {
m.Preselected = append(m.Preselected, selection)
}
}
func (m *Multiple) Select() {
if len(m.Preselected) == 0 {
return
}
highestPriority := Priority(math.MaxInt)
for _, selection := range m.Preselected {
if selection.Value.Priority < highestPriority {
highestPriority = selection.Value.Priority
}
}
for _, selection := range m.Preselected {
if selection.Value.Priority == highestPriority {
selection.Value.IsSelected = true
}
}
}
type Single struct {
Cursor physics.Point
HasSelected bool
}
func NewSingle(cursor physics.Point) Single {
return Single{
Cursor: cursor,
HasSelected: false,
}
}
func (s *Single) Select(position ecs.Component[physics.Point], relBounds ecs.Component[physics.Rectangle], selection *ecs.Component[Selection]) {
if !selection.IsEnabled || !position.IsEnabled || !relBounds.IsEnabled {
return
}
selection.Value.IsSelected = false
if s.HasSelected || !s.Cursor.In(physics.Translate(relBounds.Value, position.Value)) {
return
}
selection.Value.IsSelected = true
}
|