aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMartin Kagamino Lehoux <martin@lehoux.net>2025-07-07 09:39:29 +0200
committerMartin Kagamino Lehoux <martin@lehoux.net>2025-07-07 09:39:29 +0200
commit2b05b9d8aa47f6cc9d768c6758f1cca4e8721a4c (patch)
tree17b59634ce20f42111e30cd199a77960a386008a
parentb3e70bf4cf7e790fa8b5239c05b6c7b917f19635 (diff)
Regroup drawing
-rw-r--r--draw.go33
-rw-r--r--entity.go9
-rw-r--r--main.go7
-rw-r--r--resource.go4
-rw-r--r--selection.go11
5 files changed, 39 insertions, 25 deletions
diff --git a/draw.go b/draw.go
new file mode 100644
index 0000000..9af35f5
--- /dev/null
+++ b/draw.go
@@ -0,0 +1,33 @@
+package main
+
+import (
+ "fmt"
+ "image/color"
+
+ "github.com/hajimehoshi/ebiten/v2"
+ "github.com/hajimehoshi/ebiten/v2/text/v2"
+)
+
+func (e *Entity) Draw(g *Game, screen *ebiten.Image) {
+ 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)
+ if e.Selection.IsEnabled && e.Selection.Value.IsSelected {
+ opt := &ebiten.DrawImageOptions{}
+ opt.GeoM.Translate(float64(e.Position.Value.X-SELECTION_HALO_WIDTH/2), float64(e.Position.Value.Y-SELECTION_HALO_WIDTH/2))
+ screen.DrawImage(e.Selection.Value.Halo, opt)
+ if e.ResourceSource.IsEnabled {
+ resourceText := fmt.Sprintf("%d", e.ResourceSource.Value.Remaining)
+ op := &text.DrawOptions{}
+ op.GeoM.Translate(float64(e.Position.Value.X+5), float64(e.Position.Value.Y+30))
+ op.ColorScale.ScaleWithColor(color.White)
+ text.Draw(screen, resourceText, &text.GoTextFace{
+ Source: g.FaceSource,
+ Size: 40,
+ }, op)
+ }
+ }
+}
diff --git a/entity.go b/entity.go
index a9fd0ff..b784c04 100644
--- a/entity.go
+++ b/entity.go
@@ -24,12 +24,3 @@ func (e Entity) Bounds() physics.Rectangle {
Max: e.Position.Value.Add(e.Image.Value.Bounds().Size()),
}
}
-
-func Draw(screen *ebiten.Image, e *Entity) {
- 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 aef9968..09eb16d 100644
--- a/main.go
+++ b/main.go
@@ -165,10 +165,7 @@ func (g *Game) Draw(screen *ebiten.Image) {
cursor := physics.Point{X: x, Y: y}
screen.Fill(soilColor)
for _, e := range g.Entities {
- Draw(screen, e)
- }
- for _, e := range g.Entities {
- DrawSelection(screen, e)
+ e.Draw(g, screen)
}
for _, e := range g.Entities {
DrawMove(screen, e)
@@ -209,10 +206,12 @@ func main() {
kcore.Expect(err, "failed to create font source")
game.FaceSource = s
ironImage := NewFilledRectangleImage(physics.Point{X: 100, Y: 100}, color.RGBA{0x80, 0x80, 0x80, 0xff})
+ ironSelectionHalo := NewStrokeRectangleImage(physics.Point{X: 110, Y: 110}, SELECTION_HALO_WIDTH, color.RGBA{0xff, 0x00, 0x00, 0xff})
ironMine := Entity{
Position: ecs.C(physics.Point{X: 1000, Y: 1000}),
Image: ecs.C(ironImage),
ResourceSource: ecs.C(ResourceSource{Remaining: 1000}),
+ Selection: ecs.C(Selection{IsSelected: false, Halo: ironSelectionHalo}),
}
game.Entities = append(game.Entities, &ironMine)
storageImage := NewFilledRectangleImage(physics.Point{X: 100, Y: 100}, color.RGBA{0x00, 0x00, 0xff, 0xff})
diff --git a/resource.go b/resource.go
index 1639cb2..d52a438 100644
--- a/resource.go
+++ b/resource.go
@@ -1,6 +1,8 @@
package main
-import "time"
+import (
+ "time"
+)
type ResourceSource struct {
Remaining int
diff --git a/selection.go b/selection.go
index ef270c6..496d7f6 100644
--- a/selection.go
+++ b/selection.go
@@ -38,14 +38,3 @@ func (e *Entity) SelectSingle(cursor physics.Point, canBeSelected bool) bool {
slog.Info("entity selected", slog.String("position", e.Position.Value.String()))
return true
}
-
-func DrawSelection(screen *ebiten.Image, e *Entity) {
- if !e.Image.IsEnabled || !e.Position.IsEnabled || !e.Selection.IsEnabled {
- return
- }
- if e.Selection.Value.IsSelected {
- opt := &ebiten.DrawImageOptions{}
- opt.GeoM.Translate(float64(e.Position.Value.X-SELECTION_HALO_WIDTH/2), float64(e.Position.Value.Y-SELECTION_HALO_WIDTH/2))
- screen.DrawImage(e.Selection.Value.Halo, opt)
- }
-}