From 5af6361f22d727a4abacd724cf9152e67f73328a Mon Sep 17 00:00:00 2001 From: Martin Kagamino Lehoux Date: Wed, 9 Jul 2025 07:39:39 +0200 Subject: Export draw logic --- README.md | 4 ++-- draw.go | 34 ++++++++++++++++++++++++++++++++++ main.go | 42 ------------------------------------------ 3 files changed, 36 insertions(+), 44 deletions(-) diff --git a/README.md b/README.md index b81d5f9..c013a54 100644 --- a/README.md +++ b/README.md @@ -4,19 +4,19 @@ ## TODO -- atm, stops moving if someone settles at destination - spawn may register a target, on spawn unit will do as a right click on target (move, gather, ...) - time must come from engine, not now - selection priority: selecting units + building -> select only units - see the current action (debug?) - keyboard config -- spawn order - bigger map - move the map view +- ecs array: on delete, replace by last and update id->index map ## Bugs - select A, gather, move to B, (it stops), gather (no storage) - if B is < 1 cell away, it will go and merge with A +- stops moving if someone settles at destination ## Dependencies diff --git a/draw.go b/draw.go index 90c1134..ba4f651 100644 --- a/draw.go +++ b/draw.go @@ -1,11 +1,13 @@ package main import ( + "age_of_empires/physics" "fmt" "image/color" "github.com/hajimehoshi/ebiten/v2" "github.com/hajimehoshi/ebiten/v2/text/v2" + "github.com/hajimehoshi/ebiten/v2/vector" ) func (e *Entity) Draw(g *Game, screen *ebiten.Image) { @@ -39,5 +41,37 @@ func (e *Entity) Draw(g *Game, screen *ebiten.Image) { Size: 40, }, op) } + if e.Move.Value.IsActive { + last := e.Position.Value + dx := +e.Image.Value.Bounds().Dx() / 2 + dy := +e.Image.Value.Bounds().Dy() / 2 + for _, point := range e.Move.Value.Path { + vector.StrokeLine(screen, float32(last.X+dx), float32(last.Y+dy), float32(point.X+dx), float32(point.Y+dy), 10.0, color.RGBA{256 * 3 / 16, 256 * 3 / 16, 256 * 3 / 16, 256 / 4}, true) + last = point + } + } + } +} + +func (g *Game) Draw(screen *ebiten.Image) { + x, y := ebiten.CursorPosition() + cursor := physics.Point{X: x, Y: y} + screen.Fill(soilColor) + 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) } + bannerHeight := float32(200) + vector.DrawFilledRect(screen, 0, 0, float32(3200), bannerHeight, color.White, true) + + resourceText := fmt.Sprintf("Resources: %d", g.ResourceAmount) + op := &text.DrawOptions{} + op.GeoM.Translate(25, 25) + op.ColorScale.ScaleWithColor(color.Black) + text.Draw(screen, resourceText, &text.GoTextFace{ + Source: g.FaceSource, + Size: 100, + }, op) } diff --git a/main.go b/main.go index 122e95a..afb0bee 100644 --- a/main.go +++ b/main.go @@ -4,7 +4,6 @@ import ( "age_of_empires/ecs" "age_of_empires/physics" "bytes" - "fmt" "image" "image/color" _ "image/jpeg" @@ -16,7 +15,6 @@ import ( "github.com/hajimehoshi/ebiten/v2" "github.com/hajimehoshi/ebiten/v2/inpututil" "github.com/hajimehoshi/ebiten/v2/text/v2" - "github.com/hajimehoshi/ebiten/v2/vector" "github.com/martinlehoux/kagamigo/kcore" "golang.org/x/exp/slog" "golang.org/x/image/font/gofont/goregular" @@ -45,20 +43,6 @@ type Game struct { UnitBuilder EntityBuilder } -func DrawMove(screen *ebiten.Image, e *Entity) { - if e.Move.IsEnabled && e.Position.IsEnabled && e.Selection.IsEnabled { - if e.Selection.Value.IsSelected && e.Move.Value.IsActive { - last := e.Position.Value - dx := +e.Image.Value.Bounds().Dx() / 2 - dy := +e.Image.Value.Bounds().Dy() / 2 - for _, point := range e.Move.Value.Path { - vector.StrokeLine(screen, float32(last.X+dx), float32(last.Y+dy), float32(point.X+dx), float32(point.Y+dy), 10.0, color.RGBA{256 * 3 / 16, 256 * 3 / 16, 256 * 3 / 16, 256 / 4}, true) - last = point - } - } - } -} - func (g *Game) getMoveMap() physics.MoveMap { blocked := map[physics.Point]bool{} for _, e := range g.Entities { @@ -172,32 +156,6 @@ func (g *Game) Update() error { return nil } -func (g *Game) Draw(screen *ebiten.Image) { - x, y := ebiten.CursorPosition() - cursor := physics.Point{X: x, Y: y} - screen.Fill(soilColor) - for _, e := range g.Entities { - e.Draw(g, screen) - } - for _, e := range g.Entities { - DrawMove(screen, e) - } - 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) - } - bannerHeight := float32(200) - vector.DrawFilledRect(screen, 0, 0, float32(3200), bannerHeight, color.White, true) - - resourceText := fmt.Sprintf("Resources: %d", g.ResourceAmount) - op := &text.DrawOptions{} - op.GeoM.Translate(25, 25) - op.ColorScale.ScaleWithColor(color.Black) - text.Draw(screen, resourceText, &text.GoTextFace{ - Source: g.FaceSource, - Size: 100, - }, op) -} - func (g *Game) Layout(outsideWidth, outsideHeight int) (screenWidth, screenHeight int) { return 3200, 2400 } -- cgit v1.3