From 56b3d85afaf260be93e46e2b930f79ade461789b Mon Sep 17 00:00:00 2001 From: Martin Kagamino Lehoux Date: Fri, 4 Jul 2025 23:55:19 +0200 Subject: Simple refactor --- entity.go | 9 +++++---- main.go | 10 +++++----- selection.go | 32 +++++++++++++++++--------------- 3 files changed, 27 insertions(+), 24 deletions(-) diff --git a/entity.go b/entity.go index 8d6dfb9..a9fd0ff 100644 --- a/entity.go +++ b/entity.go @@ -26,9 +26,10 @@ func (e Entity) Bounds() physics.Rectangle { } func Draw(screen *ebiten.Image, e *Entity) { - if e.Image.IsEnabled && e.Position.IsEnabled { - op := &ebiten.DrawImageOptions{} - op.GeoM.Translate(float64(e.Position.Value.X), float64(e.Position.Value.Y)) - screen.DrawImage(e.Image.Value, op) + if !e.Image.IsEnabled || !e.Position.IsEnabled { + return } + op := &ebiten.DrawImageOptions{} + op.GeoM.Translate(float64(e.Position.Value.X), float64(e.Position.Value.Y)) + screen.DrawImage(e.Image.Value, op) } diff --git a/main.go b/main.go index 3aea932..aef9968 100644 --- a/main.go +++ b/main.go @@ -35,11 +35,11 @@ type GlobalSelection struct { } type Game struct { - Entities []*Entity - CurrentAction Action - Selection GlobalSelection + Entities []*Entity + CurrentAction Action + Selection GlobalSelection ResourceAmount int - FaceSource *text.GoTextFaceSource + FaceSource *text.GoTextFaceSource } func DrawMove(screen *ebiten.Image, e *Entity) { @@ -186,7 +186,7 @@ func (g *Game) Draw(screen *ebiten.Image) { text.Draw(screen, resourceText, &text.GoTextFace{ Source: g.FaceSource, Size: 100, - } , op) + }, op) } func (g *Game) Layout(outsideWidth, outsideHeight int) (screenWidth, screenHeight int) { diff --git a/selection.go b/selection.go index d9120d2..329f6d7 100644 --- a/selection.go +++ b/selection.go @@ -15,26 +15,28 @@ type Selection struct { } func (e *Entity) SelectMultiple(cursor physics.Point, selection GlobalSelection) { - if e.Selection.IsEnabled { - e.Selection.Value.IsSelected = false - selectionBounds := physics.Rectangle{Min: selection.Start, Max: cursor}.Canon() - if selectionBounds.Overlaps(e.Bounds()) { - e.Selection.Value.IsSelected = true - slog.Info("entity selected", slog.String("position", e.Position.Value.String())) - } + if !e.Selection.IsEnabled { + return + } + e.Selection.Value.IsSelected = false + selectionBounds := physics.Rectangle{Min: selection.Start, Max: cursor}.Canon() + if selectionBounds.Overlaps(e.Bounds()) { + e.Selection.Value.IsSelected = true + slog.Info("entity selected", slog.String("position", e.Position.Value.String())) } } func (e *Entity) SelectSingle(cursor physics.Point, canBeSelected bool) bool { - if e.Selection.IsEnabled { - e.Selection.Value.IsSelected = false - if canBeSelected && cursor.In(e.Bounds()) { - e.Selection.Value.IsSelected = true - slog.Info("entity selected", slog.String("position", e.Position.Value.String())) - return true - } + if !e.Selection.IsEnabled { + return false + } + e.Selection.Value.IsSelected = false + if !canBeSelected || cursor.In(e.Bounds()) { + return false } - return false + e.Selection.Value.IsSelected = true + slog.Info("entity selected", slog.String("position", e.Position.Value.String())) + return true } func DrawSelection(screen *ebiten.Image, e *Entity) { -- cgit v1.3