diff options
| author | Martin Kagamino Lehoux <martin@lehoux.net> | 2023-10-06 07:04:35 +0200 |
|---|---|---|
| committer | Martin Kagamino Lehoux <martin@lehoux.net> | 2023-10-06 07:04:35 +0200 |
| commit | bf546b1b4c92d3d358a85b6b760dad01bc95ea7c (patch) | |
| tree | 898680c3e7b5c32ff206cbd075023b15f3356f4e | |
| parent | 7b805f2bcc3b25c9077ec39fc0693091a4e05b0a (diff) | |
feat(selection): Use circle for units
| -rw-r--r-- | image.go | 21 | ||||
| -rw-r--r-- | main.go | 12 | ||||
| -rw-r--r-- | selection.go | 10 |
3 files changed, 33 insertions, 10 deletions
@@ -4,10 +4,29 @@ import ( "image/color" "github.com/hajimehoshi/ebiten/v2" + "github.com/hajimehoshi/ebiten/v2/vector" ) -func NewColorImage(size Point, color color.Color) *ebiten.Image { +func NewFilledRectangleImage(size Point, color color.Color) *ebiten.Image { image := ebiten.NewImage(size.X, size.Y) image.Fill(color) return image } + +func NewStrokeRectangleImage(size Point, strokeWidth int, color color.Color) *ebiten.Image { + image := ebiten.NewImage(size.X, size.Y) + vector.StrokeRect(image, 0, 0, float32(size.X), float32(size.Y), float32(strokeWidth), color, true) + return image +} + +func NewFilledCircleImage(width int, color color.Color) *ebiten.Image { + image := ebiten.NewImage((width), (width)) + vector.DrawFilledCircle(image, float32(width/2), float32(width/2), float32(width/2), color, true) + return image +} + +func NewStrokeCircleImage(width int, strokeWidth int, color color.Color) *ebiten.Image { + image := ebiten.NewImage((width), (width)) + vector.StrokeCircle(image, float32(width/2), float32(width/2), float32(width/2), float32(strokeWidth), color, true) + return image +} @@ -132,19 +132,21 @@ func main() { ebiten.SetWindowSize(640, 480) ebiten.SetWindowTitle("Age of Empire") game := &Game{} - ironImage := NewColorImage(Point{100, 100}, color.RGBA{0x80, 0x80, 0x80, 0xff}) + ironImage := NewFilledRectangleImage(Point{100, 100}, color.RGBA{0x80, 0x80, 0x80, 0xff}) + ironHaloImage := NewStrokeRectangleImage(Point{110, 110}, SELECTION_HALO_SIZE, color.RGBA{0xff, 0x00, 0x00, 0xff}) ironMine := Entity{ Position: C(Point{1000, 1000}), Image: C(ironImage), - Selection: C(Selection{IsSelected: false}), + Selection: C(Selection{IsSelected: false, Halo: ironHaloImage}), } game.Entities = append(game.Entities, &ironMine) var order Order - personImage := NewColorImage(Point{100, 100}, color.RGBA{0xff, 0xff, 0xff, 0xff}) + personImage := NewFilledCircleImage(100, color.RGBA{0xff, 0xff, 0xff, 0xff}) + personSelectionHalo := NewStrokeCircleImage(110, SELECTION_HALO_SIZE, color.RGBA{0xff, 0x00, 0x00, 0xff}) person1 := Entity{ Position: C(Point{2000, 2000}), Image: C(personImage), - Selection: C(Selection{IsSelected: false}), + Selection: C(Selection{IsSelected: false, Halo: personSelectionHalo}), Move: C(Move{IsActive: false}), Order: C(order), } @@ -152,7 +154,7 @@ func main() { person2 := Entity{ Position: C(Point{2200, 2200}), Image: C(personImage), - Selection: C(Selection{IsSelected: false}), + Selection: C(Selection{IsSelected: false, Halo: personSelectionHalo}), Move: C(Move{IsActive: false}), Order: C(order), } diff --git a/selection.go b/selection.go index 730570a..23e98aa 100644 --- a/selection.go +++ b/selection.go @@ -1,15 +1,15 @@ package main import ( - "image/color" - "github.com/hajimehoshi/ebiten/v2" - "github.com/hajimehoshi/ebiten/v2/vector" "golang.org/x/exp/slog" ) +const SELECTION_HALO_SIZE = 10 + type Selection struct { IsSelected bool + Halo *ebiten.Image } func (e *Entity) SelectMultiple(cursor Point, selection GlobalSelection) { @@ -38,7 +38,9 @@ func (e *Entity) SelectSingle(cursor Point, canBeSelected bool) bool { func DrawSelection(screen *ebiten.Image, e *Entity) { if e.Image.IsEnabled && e.Position.IsEnabled && e.Selection.IsEnabled { if e.Selection.Value.IsSelected { - vector.StrokeRect(screen, float32(e.Position.Value.X-5), float32(e.Position.Value.Y-5), 110, 110, 10, color.RGBA{0xff, 0x00, 0x00, 0xff}, false) + opt := &ebiten.DrawImageOptions{} + opt.GeoM.Translate(float64(e.Position.Value.X-SELECTION_HALO_SIZE/2), float64(e.Position.Value.Y-SELECTION_HALO_SIZE/2)) + screen.DrawImage(e.Selection.Value.Halo, opt) } } } |
