2020-04-11 00:13:28 +01:00
|
|
|
package scenario
|
|
|
|
|
|
|
|
import (
|
2020-04-18 00:12:15 +01:00
|
|
|
"fmt"
|
2020-04-11 00:13:28 +01:00
|
|
|
"image"
|
|
|
|
"sort"
|
|
|
|
|
|
|
|
"github.com/hajimehoshi/ebiten"
|
2020-04-18 00:12:15 +01:00
|
|
|
"github.com/hajimehoshi/ebiten/ebitenutil"
|
2020-04-11 00:13:28 +01:00
|
|
|
)
|
|
|
|
|
2020-04-18 00:12:15 +01:00
|
|
|
type CartPt struct {
|
|
|
|
X float64
|
|
|
|
Y float64
|
|
|
|
}
|
|
|
|
|
|
|
|
type IsoPt struct {
|
|
|
|
X float64
|
|
|
|
Y float64
|
|
|
|
}
|
|
|
|
|
2020-04-11 00:13:28 +01:00
|
|
|
func (s *Scenario) Update(screenX, screenY int) error {
|
|
|
|
s.tick += 1
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Scenario) Draw(screen *ebiten.Image) error {
|
|
|
|
// Bounds clipping
|
|
|
|
// http://www.java-gaming.org/index.php?topic=24922.0
|
|
|
|
// https://stackoverflow.com/questions/892811/drawing-isometric-game-worlds
|
|
|
|
// https://gamedev.stackexchange.com/questions/25896/how-do-i-find-which-isometric-tiles-are-inside-the-cameras-current-view
|
|
|
|
|
|
|
|
sw, sh := screen.Size()
|
|
|
|
|
2020-04-18 00:12:15 +01:00
|
|
|
topLeft := CartPt{X: float64(s.Viewpoint.X), Y: float64(s.Viewpoint.Y)}.ToISO()
|
2020-04-11 00:13:28 +01:00
|
|
|
topLeft.X -= 5 // Ensure we paint to every visible section of the screeen.
|
|
|
|
topLeft.X -= 5 // FIXME: haxxx
|
|
|
|
|
2020-04-18 00:12:15 +01:00
|
|
|
bottomRight := CartPt{X: float64(s.Viewpoint.X + sw), Y: float64(s.Viewpoint.Y + sh)}.ToISO()
|
2020-04-11 00:13:28 +01:00
|
|
|
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.
|
2020-04-18 00:12:15 +01:00
|
|
|
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++ {
|
2020-04-11 00:13:28 +01:00
|
|
|
if b&1 != a&1 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-04-18 00:12:15 +01:00
|
|
|
pt := IsoPt{X: float64((a + b) / 2), Y: float64((a - b) / 2)}
|
|
|
|
ipt := image.Pt(int(pt.X), int(pt.Y))
|
2020-04-11 00:13:28 +01:00
|
|
|
|
2020-04-18 00:12:15 +01:00
|
|
|
if !ipt.In(s.area.Rect) {
|
2020-04-11 00:13:28 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
toDraw = append(toDraw, pt)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Slice(toDraw, func(i, j int) bool {
|
2020-04-18 00:12:15 +01:00
|
|
|
iPix := toDraw[i].ToCart()
|
|
|
|
jPix := toDraw[j].ToCart()
|
2020-04-11 00:13:28 +01:00
|
|
|
|
|
|
|
if iPix.Y < jPix.Y {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
if iPix.Y == jPix.Y {
|
|
|
|
return iPix.X < jPix.X
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
})
|
|
|
|
|
|
|
|
counter := map[string]int{}
|
|
|
|
for _, pt := range toDraw {
|
|
|
|
for z := 0; z <= s.ZIdx; z++ {
|
2020-04-18 00:12:15 +01:00
|
|
|
if err := s.renderCell(pt, z, screen, counter); err != nil {
|
2020-04-11 00:13:28 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//log.Printf("%#+v", counter)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-04-18 00:12:15 +01:00
|
|
|
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)
|
2020-04-11 00:13:28 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
iso := ebiten.GeoM{}
|
|
|
|
iso.Translate(-float64(s.Viewpoint.X), -float64(s.Viewpoint.Y))
|
|
|
|
|
2020-04-18 00:12:15 +01:00
|
|
|
pix := pos.ToCart()
|
|
|
|
iso.Translate(pix.X, pix.Y)
|
2020-04-11 00:13:28 +01:00
|
|
|
|
|
|
|
// Taking the Z index away *seems* to draw the object in the correct place.
|
|
|
|
// FIXME: There are some artifacts, investigate more
|
|
|
|
iso.Translate(0.0, -float64(z*48.0)) // offset for Z index
|
|
|
|
|
|
|
|
// TODO: iso.Scale(e.state.zoom, e.state.zoom) // apply current zoom factor
|
|
|
|
|
|
|
|
for _, spr := range sprites {
|
|
|
|
// if _, ok := counter[spr.ID]; !ok {
|
|
|
|
// counter[spr.ID] = 0
|
|
|
|
// }
|
|
|
|
// counter[spr.ID] = counter[spr.ID] + 1
|
2020-04-17 22:45:02 +01:00
|
|
|
op := ebiten.DrawImageOptions{GeoM: iso}
|
2020-04-18 00:12:15 +01:00
|
|
|
|
|
|
|
// 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))
|
2020-04-11 00:13:28 +01:00
|
|
|
|
2020-04-17 22:45:02 +01:00
|
|
|
if err := screen.DrawImage(spr.Image, &op); err != nil {
|
2020-04-11 00:13:28 +01:00
|
|
|
return err
|
|
|
|
}
|
2020-04-18 00:12:15 +01:00
|
|
|
|
|
|
|
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))
|
|
|
|
}
|
|
|
|
|
2020-04-11 00:13:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
2020-04-18 00:12:15 +01:00
|
|
|
cellWidth = 128
|
2020-04-11 00:13:28 +01:00
|
|
|
cellHeight = 64
|
2020-04-18 00:12:15 +01:00
|
|
|
|
|
|
|
cellWidthHalf = cellWidth / 2
|
|
|
|
cellHeightHalf = cellHeight / 2
|
2020-04-11 00:13:28 +01:00
|
|
|
)
|
|
|
|
|
2020-04-18 00:12:15 +01:00
|
|
|
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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2020-04-11 00:13:28 +01:00
|
|
|
// Doesn't take the camera or Z level into account
|
|
|
|
func cellToPix(pt image.Point) image.Point {
|
|
|
|
return image.Pt(
|
2020-04-18 00:12:15 +01:00
|
|
|
(pt.X-pt.Y)*cellWidthHalf,
|
|
|
|
(pt.X+pt.Y)*cellHeightHalf,
|
2020-04-11 00:13:28 +01:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Doesn't take the camera or Z level into account
|
|
|
|
func pixToCell(pt image.Point) image.Point {
|
2020-04-18 00:12:15 +01:00
|
|
|
fX := pt.X
|
|
|
|
fY := pt.Y
|
2020-04-11 00:13:28 +01:00
|
|
|
return image.Pt(
|
2020-04-18 00:12:15 +01:00
|
|
|
// (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)),
|
|
|
|
|
|
|
|
|
2020-04-11 00:13:28 +01:00
|
|
|
)
|
2020-04-18 00:12:15 +01:00
|
|
|
}*/
|