From 6e70ddcb6080da93ca7cc8722fa403d6841e1627 Mon Sep 17 00:00:00 2001 From: Nick Thomas Date: Sat, 18 Apr 2020 00:12:15 +0100 Subject: [PATCH] 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. --- cmd/view-map/main.go | 15 +++++- go.mod | 1 + go.sum | 10 ++++ internal/assetstore/map.go | 5 ++ internal/scenario/draw.go | 91 ++++++++++++++++++++++++++-------- internal/scenario/draw_test.go | 21 ++++++++ internal/scenario/manage.go | 25 ++++++++++ internal/ui/window.go | 11 ++++ 8 files changed, 156 insertions(+), 23 deletions(-) create mode 100644 internal/scenario/draw_test.go create mode 100644 internal/scenario/manage.go diff --git a/cmd/view-map/main.go b/cmd/view-map/main.go index de8e5d0..80d9bb5 100644 --- a/cmd/view-map/main.go +++ b/cmd/view-map/main.go @@ -51,8 +51,6 @@ func main() { log.Fatal("Couldn't create window: %v", err) } - // TODO: click to view cell data - win.OnKeyUp(ebiten.KeyLeft, env.changeOrigin(-64, +0)) win.OnKeyUp(ebiten.KeyRight, env.changeOrigin(+64, +0)) win.OnKeyUp(ebiten.KeyUp, env.changeOrigin(+0, -64)) @@ -62,6 +60,8 @@ func main() { win.OnKeyUp(ebiten.Key1+ebiten.Key(i), env.setZIdx(i)) } + win.OnMouseClick(env.showCellData) + if err := win.Run(); err != nil { log.Fatal(err) } @@ -87,3 +87,14 @@ func (e *env) setZIdx(to int) func() { e.scenario.ZIdx = to } } + +func (e *env) showCellData() { + screenX, screenY := ebiten.CursorPosition() + viewX, viewY := e.scenario.Viewpoint.X+screenX, e.scenario.Viewpoint.Y+screenY + + log.Printf("Click registered at (%d,%d) screen, (%d,%d) virtual", screenX, screenY, viewX, viewY) + + cell, pos := e.scenario.CellAtCursor() + log.Printf("Viewpoint: %#+v z=%v", e.scenario.Viewpoint, e.scenario.ZIdx) + log.Printf("Cell under cursor: (%.2f,%.2f,%d): %#+v", pos.X, pos.Y, pos.Z, cell) +} diff --git a/go.mod b/go.mod index 54bb909..5879c86 100644 --- a/go.mod +++ b/go.mod @@ -9,6 +9,7 @@ require ( github.com/jfreymuth/oggvorbis v1.0.1 // indirect github.com/kr/text v0.2.0 // indirect github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect + github.com/stretchr/testify v1.5.1 golang.org/x/exp v0.0.0-20200331195152-e8c3332aa8e5 // indirect golang.org/x/image v0.0.0-20200119044424-58c23975cae1 golang.org/x/mobile v0.0.0-20200329125638-4c31acba0007 // indirect diff --git a/go.sum b/go.sum index 4d52779..dab54d8 100644 --- a/go.sum +++ b/go.sum @@ -3,6 +3,8 @@ github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo= github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= +github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/emef/bitfield v0.0.0-20170503144143-7d3f8f823065 h1:7QVNyw2v9R1qOvbe9vfeVJWWKCSnd2Ap+8l8/CtG9LM= github.com/emef/bitfield v0.0.0-20170503144143-7d3f8f823065/go.mod h1:uN4GbWHfit2ByfOKQ4K6fuLy1/Os2eLynsIrDvjiDgM= github.com/go-gl/glfw/v3.3/glfw v0.0.0-20191125211704-12ad95a8df72 h1:b+9H1GAsx5RsjvDFLoS5zkNBzIQMuVKUYQDmxU3N5XE= @@ -39,6 +41,11 @@ github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e h1:fD57ERR4JtEqsWbfPhv4DMiApHyliiK5xCTNVSPiaAs= github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno= github.com/pkg/browser v0.0.0-20180916011732-0a3d74bf9ce4/go.mod h1:4OwLy04Bl9Ef3GJJCoec+30X3LQs/0/m4HFRt/2LUSA= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4= +github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= @@ -91,7 +98,10 @@ golang.org/x/tools v0.0.0-20200117220505-0cba7a3a9ee9/go.mod h1:TB2adYChydJhpapK golang.org/x/tools v0.0.0-20200207183749-b753a1ba74fa/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo= gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f h1:BLraFXnmrev5lT+xlilqcH8XK9/i0At2xKjWk4p6zsU= gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw= +gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/internal/assetstore/map.go b/internal/assetstore/map.go index 71a58cb..0410d93 100644 --- a/internal/assetstore/map.go +++ b/internal/assetstore/map.go @@ -77,6 +77,11 @@ func (m *Map) LoadSprites() error { return nil } +// FIXME: get rid of this +func (m *Map) Cell(x, y, z int) maps.Cell { + return m.raw.Cells.At(x, y, z) +} + // SpritesForCell returns the sprites needed to correctly render this cell. // They should be rendered from first to last to get the correct ordering func (m *Map) SpritesForCell(x, y, z int) ([]*Sprite, error) { diff --git a/internal/scenario/draw.go b/internal/scenario/draw.go index 246f1c4..944f766 100644 --- a/internal/scenario/draw.go +++ b/internal/scenario/draw.go @@ -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)), + + ) -} +}*/ diff --git a/internal/scenario/draw_test.go b/internal/scenario/draw_test.go new file mode 100644 index 0000000..f6158ba --- /dev/null +++ b/internal/scenario/draw_test.go @@ -0,0 +1,21 @@ +package scenario + +import ( + "testing" + + "github.com/stretchr/testify/require" +) + +func TestCARTtoISOtoCART(t *testing.T) { + for i := 0; i < 120; i++ { + for j := 0; j < 120; j++ { + orig := IsoPt{X: float64(i), Y: float64(j)} + asPix := orig.ToCart() + andBack := asPix.ToISO() + + t.Logf("%v,%v: asPix = %v", i, j, asPix) + + require.Equal(t, orig, andBack) + } + } +} diff --git a/internal/scenario/manage.go b/internal/scenario/manage.go new file mode 100644 index 0000000..7e2ab8f --- /dev/null +++ b/internal/scenario/manage.go @@ -0,0 +1,25 @@ +package scenario + +import ( + // "image" + + "github.com/hajimehoshi/ebiten" + + "code.ur.gs/lupine/ordoor/internal/maps" +) + +type CellPoint struct { + IsoPt + Z int +} + +func (s *Scenario) CellAtCursor() (maps.Cell, CellPoint) { + x, y := ebiten.CursorPosition() + screenPos := CartPt{X: float64(s.Viewpoint.X + x), Y: float64(s.Viewpoint.Y + y)} + isoPos := screenPos.ToISO() + // Convert to cell coordinates. + // TODO: zoom support will need a camera + // FIXME: adjust for Z level + + return s.area.Cell(int(isoPos.X), int(isoPos.Y), 0), CellPoint{IsoPt: isoPos, Z: s.ZIdx} +} diff --git a/internal/ui/window.go b/internal/ui/window.go index f15d38c..b83ac23 100644 --- a/internal/ui/window.go +++ b/internal/ui/window.go @@ -35,6 +35,7 @@ type Window struct { Title string KeyUpHandlers map[ebiten.Key]func() MouseWheelHandler func(float64, float64) + MouseClickHandler func() // Allow the "game" to be switched out at any time game Game @@ -72,6 +73,10 @@ func (w *Window) OnMouseWheel(f func(x, y float64)) { w.MouseWheelHandler = f } +func (w *Window) OnMouseClick(f func()) { + w.MouseClickHandler = f +} + func (w *Window) Layout(_, _ int) (int, int) { return w.xRes, w.yRes } @@ -130,6 +135,12 @@ func (w *Window) Update(screen *ebiten.Image) (outErr error) { } } + if w.MouseClickHandler != nil { + if inpututil.IsMouseButtonJustPressed(ebiten.MouseButtonLeft) { + w.MouseClickHandler() + } + } + if ebiten.IsDrawingSkipped() { return nil }