Fix some errors in iso->pix->iso conversions, add debugging

Maps now render a bit more properly, and our mouse position can be
correctly assigned to a cell. Kind of, mostly.
This commit is contained in:
2020-04-18 00:12:15 +01:00
parent 1e141a2fb9
commit 6e70ddcb60
8 changed files with 156 additions and 23 deletions

View File

@@ -1,12 +1,24 @@
package scenario
import (
"fmt"
"image"
"sort"
"github.com/hajimehoshi/ebiten"
"github.com/hajimehoshi/ebiten/ebitenutil"
)
type CartPt struct {
X float64
Y float64
}
type IsoPt struct {
X float64
Y float64
}
func (s *Scenario) Update(screenX, screenY int) error {
s.tick += 1
@@ -21,27 +33,28 @@ func (s *Scenario) Draw(screen *ebiten.Image) error {
sw, sh := screen.Size()
topLeft := pixToCell(s.Viewpoint)
topLeft := CartPt{X: float64(s.Viewpoint.X), Y: float64(s.Viewpoint.Y)}.ToISO()
topLeft.X -= 5 // Ensure we paint to every visible section of the screeen.
topLeft.X -= 5 // FIXME: haxxx
bottomRight := pixToCell(image.Pt(s.Viewpoint.X+sw, s.Viewpoint.Y+sh))
bottomRight := CartPt{X: float64(s.Viewpoint.X + sw), Y: float64(s.Viewpoint.Y + sh)}.ToISO()
bottomRight.X += 5
bottomRight.Y += 5
// X+Y is constant for all tiles in a column
// X-Y is constant for all tiles in a row
// 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++ {
toDraw := []IsoPt{}
for a := int(topLeft.X + topLeft.Y); a <= int(bottomRight.X+bottomRight.Y); a++ {
for b := int(topLeft.X - topLeft.Y); b <= int(bottomRight.X-bottomRight.Y); b++ {
if b&1 != a&1 {
continue
}
pt := image.Pt((a+b)/2, (a-b)/2)
pt := IsoPt{X: float64((a + b) / 2), Y: float64((a - b) / 2)}
ipt := image.Pt(int(pt.X), int(pt.Y))
if !pt.In(s.area.Rect) {
if !ipt.In(s.area.Rect) {
continue
}
toDraw = append(toDraw, pt)
@@ -49,8 +62,8 @@ func (s *Scenario) Draw(screen *ebiten.Image) error {
}
sort.Slice(toDraw, func(i, j int) bool {
iPix := cellToPix(toDraw[i])
jPix := cellToPix(toDraw[j])
iPix := toDraw[i].ToCart()
jPix := toDraw[j].ToCart()
if iPix.Y < jPix.Y {
return true
@@ -66,7 +79,7 @@ func (s *Scenario) Draw(screen *ebiten.Image) error {
counter := map[string]int{}
for _, pt := range toDraw {
for z := 0; z <= s.ZIdx; z++ {
if err := s.renderCell(pt.X, pt.Y, z, screen, counter); err != nil {
if err := s.renderCell(pt, z, screen, counter); err != nil {
return err
}
}
@@ -77,8 +90,8 @@ func (s *Scenario) Draw(screen *ebiten.Image) error {
return nil
}
func (s *Scenario) renderCell(x, y, z int, screen *ebiten.Image, counter map[string]int) error {
sprites, err := s.area.SpritesForCell(x, y, z)
func (s *Scenario) renderCell(pos IsoPt, z int, screen *ebiten.Image, counter map[string]int) error {
sprites, err := s.area.SpritesForCell(int(pos.X), int(pos.Y), z)
if err != nil {
return err
}
@@ -86,8 +99,8 @@ func (s *Scenario) renderCell(x, y, z int, screen *ebiten.Image, counter map[str
iso := ebiten.GeoM{}
iso.Translate(-float64(s.Viewpoint.X), -float64(s.Viewpoint.Y))
pix := cellToPix(image.Pt(x, y))
iso.Translate(float64(pix.X), float64(pix.Y))
pix := pos.ToCart()
iso.Translate(pix.X, pix.Y)
// Taking the Z index away *seems* to draw the object in the correct place.
// FIXME: There are some artifacts, investigate more
@@ -101,33 +114,69 @@ func (s *Scenario) renderCell(x, y, z int, screen *ebiten.Image, counter map[str
// }
// counter[spr.ID] = counter[spr.ID] + 1
op := ebiten.DrawImageOptions{GeoM: iso}
op.GeoM.Translate(float64(spr.XOffset), float64(spr.YOffset))
// FIXME: this fixed offset is found in jungtil.obj. Drawing with it
// means we put everywhere where the iso->pix conversion expects, but
// it's a bit nasty. Is there a better way?
op.GeoM.Translate(float64(spr.Rect.Min.X-209), float64(spr.Rect.Min.Y-322))
if err := screen.DrawImage(spr.Image, &op); err != nil {
return err
}
if z == 0 {
x, y := op.GeoM.Apply(0, 0)
ebitenutil.DebugPrintAt(screen, fmt.Sprintf("(%d,%d)", int(pos.X), int(pos.Y)), int(x), int(y))
}
}
return nil
}
const (
cellWidth = 64
cellWidth = 128
cellHeight = 64
cellWidthHalf = cellWidth / 2
cellHeightHalf = cellHeight / 2
)
func (p CartPt) ToISO() IsoPt {
return IsoPt{
X: (p.Y / cellHeight) + (p.X / cellWidth),
Y: (p.Y / cellHeight) - (p.X / cellWidth),
}
}
func (p IsoPt) ToCart() CartPt {
return CartPt{
X: (p.X - p.Y) * cellWidthHalf,
Y: (p.X + p.Y) * cellHeightHalf,
}
}
/*
// Doesn't take the camera or Z level into account
func cellToPix(pt image.Point) image.Point {
return image.Pt(
(pt.X-pt.Y)*cellWidth,
(pt.X+pt.Y)*cellHeight/2,
(pt.X-pt.Y)*cellWidthHalf,
(pt.X+pt.Y)*cellHeightHalf,
)
}
// Doesn't take the camera or Z level into account
func pixToCell(pt image.Point) image.Point {
fX := pt.X
fY := pt.Y
return image.Pt(
pt.Y/cellHeight+pt.X/(cellWidth*2),
pt.Y/cellHeight-pt.X/(cellWidth*2),
// (pt.X / cellWidthHalf + pt.Y / cellHeightHalf) / 2,
// (pt.Y / cellHeightHalf - (pt.Y / cellWidthHalf)) / 2,
// int(fY/cellHeight+fX/(cellWidth*2)),
// int(fY/cellHeight-fX/(cellWidth*2)),
//int((fY / cellHeight) + (fX / cellWidth)),
//int((-fX / cellWidth) + (fY / cellHeight)),
)
}
}*/