Reorder drawn objects

This commit is contained in:
2020-03-20 00:03:03 +00:00
parent 576ac0570d
commit e7d9083e6b

View File

@@ -6,6 +6,7 @@ import (
"log" "log"
"math" "math"
"os" "os"
"sort"
"github.com/hajimehoshi/ebiten" "github.com/hajimehoshi/ebiten"
@@ -54,6 +55,7 @@ func main() {
state := state{ state := state{
zoom: 1.0, zoom: 1.0,
origin: image.Point{0, 3000}, // FIXME: haxxx origin: image.Point{0, 3000}, // FIXME: haxxx
zIdx: 1,
} }
env := &env{ env := &env{
area: area, area: area,
@@ -76,7 +78,7 @@ func main() {
win.OnMouseWheel(env.changeZoom) win.OnMouseWheel(env.changeZoom)
for i := 0; i <= 6; i++ { 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 { 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() sw, sh := screen.Size()
topLeftX, topLeftY := pixToCell( topLeft := pixToCell(e.state.origin)
float64(e.state.origin.X), topLeft.X -= 1 // Otherwise we miss half a cell on alternate rows on the left
float64(e.state.origin.Y),
)
topLeftX -= 1 // Otherwise we miss half a cell on alternate rows on the left
bottomRightX, bottomRightY := pixToCell( bottomRight := pixToCell(image.Pt(e.state.origin.X+sw, e.state.origin.Y+sh))
float64(e.state.origin.X+sw),
float64(e.state.origin.Y+sh),
)
// X+Y is constant for all tiles in a column // X+Y is constant for all tiles in a column
// X-Y is constant for all tiles in a row // X-Y is constant for all tiles in a row
for a := int(topLeftX + topLeftY); a <= int(bottomRightX+bottomRightY); a++ { // However, the drawing order is odd unless we reorder explicitly.
for b := int(topLeftX - topLeftY); b <= int(bottomRightX-bottomRightY); b++ { 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 { if b&1 != a&1 {
continue continue
} }
x := (a + b) / 2 pt := image.Pt((a+b)/2, (a-b)/2)
y := (a - b) / 2
if !image.Pt(x, y).In(e.area.Rect) { if !pt.In(e.area.Rect) {
continue continue
} }
toDraw = append(toDraw, pt)
}
}
for z := 0; z <= e.state.zIdx; z++ { sort.Slice(toDraw, func(i, j int) bool {
e.renderCell(x, y, z, screen) 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 := ebiten.GeoM{}
iso.Translate(-float64(e.state.origin.X), -float64(e.state.origin.Y)) iso.Translate(-float64(e.state.origin.X), -float64(e.state.origin.Y))
fx, fy := cellToPix(float64(x), float64(y)) pix := cellToPix(image.Pt(x, y))
iso.Translate(fx, fy) 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. // Taking the Z index away *seems* to draw the object in the correct place.
// FIXME: There are some artifacts, investigate more // FIXME: There are some artifacts, investigate more
@@ -189,11 +210,17 @@ const (
) )
// Doesn't take the camera or Z level into account // Doesn't take the camera or Z level into account
func cellToPix(x, y float64) (float64, float64) { func cellToPix(pt image.Point) image.Point {
return (x - y) * cellWidth, (x + y) * cellHeight / 2.0 return image.Pt(
(pt.X-pt.Y)*cellWidth,
(pt.X+pt.Y)*cellHeight/2,
)
} }
// Doesn't take the camera or Z level into account // Doesn't take the camera or Z level into account
func pixToCell(x, y float64) (float64, float64) { func pixToCell(pt image.Point) image.Point {
return y/cellHeight + x/(cellWidth*2.0), y/cellHeight - x/(cellWidth*2.0) return image.Pt(
pt.Y/cellHeight+pt.X/(cellWidth*2),
pt.Y/cellHeight-pt.X/(cellWidth*2),
)
} }