diff options
| author | Martin Kagamino Lehoux <martin@lehoux.net> | 2025-09-25 22:15:45 +0200 |
|---|---|---|
| committer | Martin Kagamino Lehoux <martin@lehoux.net> | 2025-09-25 22:15:45 +0200 |
| commit | bd750fa2605eecd14ab821f630a7c1b51c48c982 (patch) | |
| tree | d6974c0d5bf1ffad274dce070a5ce979d887425d | |
| parent | 4f2217d74d1fa39235ffd9e3d378099d20c5cc56 (diff) | |
Improve selection code
| -rw-r--r-- | draw.go | 9 | ||||
| -rw-r--r-- | main.go | 28 | ||||
| -rw-r--r-- | selection/selection.go | 32 |
3 files changed, 52 insertions, 17 deletions
@@ -1,11 +1,12 @@ package main import ( - "age_of_empires/physics" - "age_of_empires/selection" "fmt" "image/color" + "age_of_empires/physics" + "age_of_empires/selection" + "github.com/hajimehoshi/ebiten/v2" "github.com/hajimehoshi/ebiten/v2/text/v2" "github.com/hajimehoshi/ebiten/v2/vector" @@ -94,8 +95,8 @@ func (g *Game) Draw(screen *ebiten.Image) { for _, e := range g.Entities { e.Draw(g, screen) } - if g.Selection.IsActive { - vector.StrokeRect(screen, float32(g.Selection.Start.X), float32(g.Selection.Start.Y), float32(cursor.X-g.Selection.Start.X), float32(cursor.Y-g.Selection.Start.Y), 10.0, color.RGBA{256 * 3 / 16, 256 * 3 / 16, 256 * 3 / 16, 256 / 4}, true) + if g.Selection.IsActive(cursor) { + g.Selection.Draw(screen, cursor) } drawTopBanner(screen, g) @@ -1,9 +1,6 @@ package main import ( - "age_of_empires/ecs" - "age_of_empires/physics" - "age_of_empires/selection" "bytes" "image" "image/color" @@ -12,6 +9,10 @@ import ( "runtime/pprof" "time" + "age_of_empires/ecs" + "age_of_empires/physics" + "age_of_empires/selection" + "github.com/hajimehoshi/ebiten/v2" "github.com/hajimehoshi/ebiten/v2/inpututil" "github.com/hajimehoshi/ebiten/v2/text/v2" @@ -59,10 +60,17 @@ func (g *Game) entityAt(position physics.Point) *Entity { return nil } +const ( + KeySpawnRequest = ebiten.KeyS + KeyPatrolRequest = ebiten.KeyA // TODO: Should be Q +) + func (g *Game) updateSelecting(cursor physics.Point, moveMap physics.MoveMap) { + if inpututil.IsKeyJustReleased(ebiten.KeyEscape) { + g.Selection.Clear() + } if inpututil.IsMouseButtonJustPressed(ebiten.MouseButtonLeft) { - g.Selection.Start = cursor - g.Selection.IsActive = true + g.Selection.Start(cursor) } if inpututil.IsMouseButtonJustReleased(ebiten.MouseButtonRight) { destination := cursor.Div(100).Mul(100) @@ -75,8 +83,8 @@ func (g *Game) updateSelecting(cursor physics.Point, moveMap physics.MoveMap) { } } if inpututil.IsMouseButtonJustReleased(ebiten.MouseButtonLeft) { - if g.Selection.IsActive && physics.Distance(g.Selection.Start, cursor) > 10 { - selector := selection.NewMultiple(g.Selection.Start, cursor) + if g.Selection.IsActive(cursor) { + selector := g.Selection.Select(cursor) for _, e := range g.Entities { selector.Preselect(e.Position, e.RelBounds, &e.Selection) } @@ -87,9 +95,9 @@ func (g *Game) updateSelecting(cursor physics.Point, moveMap physics.MoveMap) { selector.Select(e.Position, e.RelBounds, &e.Selection) } } - g.Selection.IsActive = false + g.Selection.Clear() } - if inpututil.IsKeyJustReleased(ebiten.KeyS) { + if inpututil.IsKeyJustReleased(KeySpawnRequest) { for _, e := range g.Entities { if !e.Selection.IsEnabled || !e.Selection.Value.IsSelected || !e.Spawn.IsEnabled { continue @@ -118,7 +126,7 @@ func (g *Game) Update() error { g.CurrentAction = Selecting slog.Info("selecting action") } - if inpututil.IsKeyJustReleased(ebiten.KeyA) { // Should be Q + if inpututil.IsKeyJustReleased(KeyPatrolRequest) { g.CurrentAction = Patrolling slog.Info("patrolling action") } diff --git a/selection/selection.go b/selection/selection.go index eb12c91..d4b8cd9 100644 --- a/selection/selection.go +++ b/selection/selection.go @@ -1,11 +1,14 @@ package selection import ( + "image/color" + "math" + "age_of_empires/ecs" "age_of_empires/physics" - "math" "github.com/hajimehoshi/ebiten/v2" + "github.com/hajimehoshi/ebiten/v2/vector" ) const HaloWidth = 10 @@ -18,8 +21,31 @@ const ( ) type GlobalSelection struct { - IsActive bool - Start physics.Point + 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 { |
