diff --git a/cmd/view-map/main.go b/cmd/view-map/main.go index ac4872e..292f8e3 100644 --- a/cmd/view-map/main.go +++ b/cmd/view-map/main.go @@ -6,6 +6,7 @@ import ( "log" "math" "os" + "sort" "github.com/hajimehoshi/ebiten" @@ -54,6 +55,7 @@ func main() { state := state{ zoom: 1.0, origin: image.Point{0, 3000}, // FIXME: haxxx + zIdx: 1, } env := &env{ area: area, @@ -76,7 +78,7 @@ func main() { win.OnMouseWheel(env.changeZoom) for i := 0; i <= 6; i++ { - win.OnKeyUp(ebiten.Key1+ebiten.Key(i), env.setZIdx(i)) + win.OnKeyUp(ebiten.Key1+ebiten.Key(i), env.setZIdx(i+1)) } if err := win.Run(env.Update, env.Draw); err != nil { @@ -103,34 +105,49 @@ func (e *env) Draw(screen *ebiten.Image) error { sw, sh := screen.Size() - topLeftX, topLeftY := pixToCell( - float64(e.state.origin.X), - float64(e.state.origin.Y), - ) - topLeftX -= 1 // Otherwise we miss half a cell on alternate rows on the left + topLeft := pixToCell(e.state.origin) + topLeft.X -= 1 // Otherwise we miss half a cell on alternate rows on the left - bottomRightX, bottomRightY := pixToCell( - float64(e.state.origin.X+sw), - float64(e.state.origin.Y+sh), - ) + bottomRight := pixToCell(image.Pt(e.state.origin.X+sw, e.state.origin.Y+sh)) // X+Y is constant for all tiles in a column // X-Y is constant for all tiles in a row - for a := int(topLeftX + topLeftY); a <= int(bottomRightX+bottomRightY); a++ { - for b := int(topLeftX - topLeftY); b <= int(bottomRightX-bottomRightY); b++ { + // However, the drawing order is odd unless we reorder explicitly. + toDraw := []image.Point{} + for a := topLeft.X + topLeft.Y; a <= bottomRight.X+bottomRight.Y; a++ { + for b := topLeft.X - topLeft.Y; b <= bottomRight.X-bottomRight.Y; b++ { if b&1 != a&1 { continue } - x := (a + b) / 2 - y := (a - b) / 2 + pt := image.Pt((a+b)/2, (a-b)/2) - if !image.Pt(x, y).In(e.area.Rect) { + if !pt.In(e.area.Rect) { continue } + toDraw = append(toDraw, pt) + } + } - for z := 0; z <= e.state.zIdx; z++ { - e.renderCell(x, y, z, screen) + sort.Slice(toDraw, func(i, j int) bool { + iPix := cellToPix(toDraw[i]) + jPix := cellToPix(toDraw[j]) + + if iPix.Y < jPix.Y { + return true + } + + if iPix.Y == jPix.Y { + return iPix.X < jPix.X + } + + return false + }) + + for _, pt := range toDraw { + for z := 0; z <= e.state.zIdx; z++ { + if err := e.renderCell(pt.X, pt.Y, z, screen); err != nil { + return err } } } @@ -147,8 +164,12 @@ func (e *env) renderCell(x, y, z int, screen *ebiten.Image) error { iso := ebiten.GeoM{} iso.Translate(-float64(e.state.origin.X), -float64(e.state.origin.Y)) - fx, fy := cellToPix(float64(x), float64(y)) - iso.Translate(fx, fy) + pix := cellToPix(image.Pt(x, y)) + iso.Translate(float64(pix.X), float64(pix.Y)) + + if e.step%30 == 0 { + // log.Printf("x=%v y=%v z=%v", pix.X-e.state.origin.X, pix.Y-e.state.origin.Y, z) + } // Taking the Z index away *seems* to draw the object in the correct place. // FIXME: There are some artifacts, investigate more @@ -189,11 +210,17 @@ const ( ) // Doesn't take the camera or Z level into account -func cellToPix(x, y float64) (float64, float64) { - return (x - y) * cellWidth, (x + y) * cellHeight / 2.0 +func cellToPix(pt image.Point) image.Point { + return image.Pt( + (pt.X-pt.Y)*cellWidth, + (pt.X+pt.Y)*cellHeight/2, + ) } // Doesn't take the camera or Z level into account -func pixToCell(x, y float64) (float64, float64) { - return y/cellHeight + x/(cellWidth*2.0), y/cellHeight - x/(cellWidth*2.0) +func pixToCell(pt image.Point) image.Point { + return image.Pt( + pt.Y/cellHeight+pt.X/(cellWidth*2), + pt.Y/cellHeight-pt.X/(cellWidth*2), + ) }