diff options
| author | Martin Kagamino Lehoux <martin@lehoux.net> | 2025-07-25 11:41:19 +0200 |
|---|---|---|
| committer | Martin Kagamino Lehoux <martin@lehoux.net> | 2025-07-25 11:41:19 +0200 |
| commit | 563b9ee704a14322ed4805896c4f8ca3fa5df03a (patch) | |
| tree | 302580bbc98354d5286ffd19c12fd40729e4696f /selection | |
| parent | 500df7635bf1b24f19bec5dede929389bb1b179c (diff) | |
feat: Selection with priority
Diffstat (limited to 'selection')
| -rw-r--r-- | selection/selection.go | 91 |
1 files changed, 91 insertions, 0 deletions
diff --git a/selection/selection.go b/selection/selection.go new file mode 100644 index 0000000..2755110 --- /dev/null +++ b/selection/selection.go @@ -0,0 +1,91 @@ +package selection + +import ( + "age_of_empires/ecs" + "age_of_empires/physics" + "math" + + "github.com/hajimehoshi/ebiten/v2" +) + +const HALO_WIDTH = 10 + +type Priority int + +const ( + Unit Priority = iota + Building +) + +type GlobalSelection struct { + IsActive bool + Start physics.Point +} + +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], bounds physics.Rectangle, selection *ecs.Component[Selection]) { + if !selection.IsEnabled { + return + } + selection.Value.IsSelected = false + if m.Bounds.Overlaps(bounds) { + 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], bounds physics.Rectangle, selection *ecs.Component[Selection]) { + if !selection.IsEnabled { + return + } + selection.Value.IsSelected = false + if s.HasSelected || !s.Cursor.In(bounds) { + return + } + selection.Value.IsSelected = true +} |
