aboutsummaryrefslogtreecommitdiff
path: root/draw.go
diff options
context:
space:
mode:
authorMartin Kagamino Lehoux <martin@lehoux.net>2026-05-07 10:31:42 +0200
committerMartin Kagamino Lehoux <martin@lehoux.net>2026-05-07 10:31:42 +0200
commitb2ca89d8193d98aa21e9637257c3e7a7216b9bd8 (patch)
treee87be16d5a92ad88070f3c96976174cab683f571 /draw.go
parent2e8da8cb6948664a77fa73cb0c66e0cc27419dc5 (diff)
Add viewport/camera system with pan, zoom, and world-space cursor conversion
- New camera.go: Camera struct with arrow-key pan, scroll-wheel zoom toward cursor, middle-mouse drag, exponential velocity decay, and world-bounds clamping - Layout() now returns screen dimensions (960x640) instead of world size so the camera GeoM transform drives all rendering - All world entities, halos, text, path lines and spawn target converted via camera transform in draw.go; banners remain in screen space - selection.go: separate startScreen field so the drag-select box draws in screen space while selection logic stays in world space - Banner heights and font sizes scaled down to fit the 640px logical screen - Three new integration tests: ScreenToWorld round-trip, bounds clamping, zoom-toward-cursor stability Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Diffstat (limited to 'draw.go')
-rw-r--r--draw.go38
1 files changed, 25 insertions, 13 deletions
diff --git a/draw.go b/draw.go
index baa8a08..d035985 100644
--- a/draw.go
+++ b/draw.go
@@ -20,10 +20,13 @@ func (g *Game) font(size float64) *text.GoTextFace {
}
func (g *Game) Draw(screen *ebiten.Image) {
- x, y := ebiten.CursorPosition()
- cursor := physics.Point{X: x, Y: y}
+ sx, sy := ebiten.CursorPosition()
+ cursor := g.Camera.ScreenToWorld(sx, sy)
+ screenCursor := physics.Point{X: sx, Y: sy}
screen.Fill(soilColor)
+ camGeoM := g.Camera.GeoM()
+
required := ecs.CM_Image | ecs.CM_Position
for i, mask := range g.Mask {
if mask&required == required {
@@ -31,18 +34,21 @@ func (g *Game) Draw(screen *ebiten.Image) {
image := g.Image[i]
op := &ebiten.DrawImageOptions{}
op.GeoM.Translate(float64(position.X), float64(position.Y))
+ op.GeoM.Concat(camGeoM)
screen.DrawImage(image, op)
if mask&ecs.CM_Selectable == ecs.CM_Selectable {
selectable := g.Selectable[i]
if selectable.IsSelected {
opt := &ebiten.DrawImageOptions{}
opt.GeoM.Translate(float64(position.X-selection.HaloWidth/2), float64(position.Y-selection.HaloWidth/2))
+ opt.GeoM.Concat(camGeoM)
screen.DrawImage(selectable.Halo, opt)
if mask&ecs.CM_ResourceSource == ecs.CM_ResourceSource {
source := g.ResourceSource[i]
resourceText := fmt.Sprintf("%d", source.Remaining)
op := &text.DrawOptions{}
op.GeoM.Translate(float64(position.X+5), float64(position.Y+30))
+ op.GeoM.Concat(camGeoM)
op.ColorScale.ScaleWithColor(color.White)
text.Draw(screen, resourceText, g.font(40), op)
}
@@ -51,20 +57,26 @@ func (g *Game) Draw(screen *ebiten.Image) {
spawnText := fmt.Sprintf("%d", len(spawn.Requests))
op := &text.DrawOptions{}
op.GeoM.Translate(float64(position.X+5), float64(position.Y+70))
+ op.GeoM.Concat(camGeoM)
op.ColorScale.ScaleWithColor(color.White)
text.Draw(screen, spawnText, g.font(40), op)
if spawn.SpawnTarget.Ok {
spawnTarget := spawn.SpawnTarget.Value
- vector.DrawFilledCircle(screen, float32(spawnTarget.X+50), float32(spawnTarget.Y+50), 30, Red, true)
+ ssx, ssy := g.Camera.WorldToScreen(spawnTarget)
+ offset := 50 * float32(g.Camera.Zoom)
+ vector.DrawFilledCircle(screen, float32(ssx)+offset, float32(ssy)+offset, 30*float32(g.Camera.Zoom), Red, true)
}
}
if mask&ecs.CM_Move == ecs.CM_Move {
move := g.Move[i]
- dx := +image.Bounds().Dx() / 2
- dy := +image.Bounds().Dy() / 2
+ dx := image.Bounds().Dx() / 2
+ dy := image.Bounds().Dy() / 2
last := position
+ strokeW := 10.0 * float32(g.Camera.Zoom)
for _, point := range move.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)
+ lsx, lsy := g.Camera.WorldToScreen(physics.Point{X: last.X + dx, Y: last.Y + dy})
+ psx, psy := g.Camera.WorldToScreen(physics.Point{X: point.X + dx, Y: point.Y + dy})
+ vector.StrokeLine(screen, float32(lsx), float32(lsy), float32(psx), float32(psy), strokeW, color.RGBA{256 * 3 / 16, 256 * 3 / 16, 256 * 3 / 16, 256 / 4}, true)
last = point
}
}
@@ -73,7 +85,7 @@ func (g *Game) Draw(screen *ebiten.Image) {
}
}
if g.GlobalSelection.IsActive(cursor) {
- g.GlobalSelection.Draw(screen, cursor)
+ g.GlobalSelection.Draw(screen, screenCursor)
}
drawTopBanner(screen, g)
@@ -85,18 +97,18 @@ func (g *Game) Draw(screen *ebiten.Image) {
func drawTopBanner(screen *ebiten.Image, g *Game) {
screenBounds := screen.Bounds()
- bannerHeight := float32(200)
+ bannerHeight := float32(50)
vector.DrawFilledRect(screen, 0, 0, float32(screenBounds.Dx()), bannerHeight, color.White, true)
resourceText := fmt.Sprintf("Resources: %d", g.ResourceAmount)
op := &text.DrawOptions{}
- op.GeoM.Translate(25, 25)
+ op.GeoM.Translate(8, 6)
op.ColorScale.ScaleWithColor(color.Black)
- text.Draw(screen, resourceText, g.font(100), op)
+ text.Draw(screen, resourceText, g.font(30), op)
}
func drawBottomBanner(screen *ebiten.Image, g *Game) {
screenBounds := screen.Bounds()
- bannerHeight := float32(200)
+ bannerHeight := float32(40)
bannerTop := screenBounds.Dy() - int(bannerHeight)
vector.DrawFilledRect(screen, 0, float32(bannerTop), float32(screenBounds.Dx()), bannerHeight, color.White, true)
@@ -109,7 +121,7 @@ func drawBottomBanner(screen *ebiten.Image, g *Game) {
}
op := &text.DrawOptions{}
- op.GeoM.Translate(25, float64(float32(bannerTop)+25))
+ op.GeoM.Translate(8, float64(float32(bannerTop)+6))
op.ColorScale.ScaleWithColor(color.Black)
- text.Draw(screen, shortcuts, g.font(60), op)
+ text.Draw(screen, shortcuts, g.font(22), op)
}