Files
ordoor/internal/scenario/draw.go

219 lines
5.2 KiB
Go
Raw Normal View History

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
geo := s.geoForCam()
geo.Translate(cellWidthHalf, 0)
geo.Scale(s.Zoom, s.Zoom)
geo.Invert()
cX, cY := ebiten.CursorPosition()
x, y := geo.Apply(float64(cX), float64(cY))
2020-04-18 11:44:05 +01:00
screenPos := CartPt{
X: x,
Y: y,
2020-04-18 11:44:05 +01:00
}
// FIXME: adjust for Z level
s.selectedCell = screenPos.ToISO()
2020-04-18 11:44:05 +01:00
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
// FIXME: we don't cope with zoom very neatly here
sw, sh := screen.Size()
2020-04-18 11:44:05 +01:00
topLeft := CartPt{
2020-06-13 15:07:32 +01:00
X: float64(s.Viewpoint.X) - (2 * cellWidth / s.Zoom), // Ensure all visible cells are rendered
Y: float64(s.Viewpoint.Y) - (2 * cellHeight / s.Zoom),
2020-04-18 11:44:05 +01:00
}.ToISO()
2020-04-18 11:44:05 +01:00
bottomRight := CartPt{
2020-06-13 15:07:32 +01:00
X: float64(s.Viewpoint.X) + (float64(sw) / s.Zoom) + (2 * cellHeight / s.Zoom),
Y: float64(s.Viewpoint.Y) + (float64(sh) / s.Zoom) + (5 * cellHeight / s.Zoom), // Z dimension requires it
2020-04-18 11:44:05 +01:00
}.ToISO()
// 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 := []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 := IsoPt{X: float64((a + b) / 2), Y: float64((a - b) / 2)}
ipt := image.Pt(int(pt.X), int(pt.Y))
if !ipt.In(s.area.Rect) {
continue
}
toDraw = append(toDraw, pt)
}
}
sort.Slice(toDraw, func(i, j int) bool {
iPix := toDraw[i].ToCart()
jPix := toDraw[j].ToCart()
if iPix.Y < jPix.Y {
return true
}
if iPix.Y == jPix.Y {
return iPix.X < jPix.X
}
return false
})
2020-06-13 13:42:26 +01:00
counter := 0
for _, pt := range toDraw {
for z := 0; z <= s.ZIdx; z++ {
2020-06-13 13:42:26 +01:00
if err := s.renderCell(int(pt.X), int(pt.Y), z, screen, &counter); err != nil {
return err
}
}
}
2020-04-18 12:23:03 +01:00
// Finally, draw cursor chrome
// FIXME: it looks like we might need to do this in normal painting order...
2020-04-18 12:23:03 +01:00
spr, err := s.specials.Sprite(0)
if err != nil {
return err
}
2020-04-18 12:23:03 +01:00
op := ebiten.DrawImageOptions{}
geo := s.geoForCoords(int(s.selectedCell.X), int(s.selectedCell.Y), 0)
op.GeoM = geo
op.GeoM.Translate(-209, -332)
op.GeoM.Translate(float64(spr.Rect.Min.X), float64(spr.Rect.Min.Y))
op.GeoM.Scale(s.Zoom, s.Zoom)
2020-04-18 12:23:03 +01:00
if err := screen.DrawImage(spr.Image, &op); err != nil {
return err
}
2020-06-13 15:07:32 +01:00
x1, y1 := geo.Apply(0, 0)
ebitenutil.DebugPrintAt(
screen,
fmt.Sprintf("(%d,%d)", int(s.selectedCell.X), int(s.selectedCell.Y)),
int(x1),
int(y1),
)
2020-04-18 12:23:03 +01:00
2020-06-13 13:42:26 +01:00
ebitenutil.DebugPrintAt(screen, fmt.Sprintf("Sprites: %v", counter), 0, 16)
/*
2020-06-13 15:07:32 +01:00
// debug: draw a square around the selected cell
x2, y2 := geo.Apply(cellWidth, cellHeight)
ebitenutil.DrawLine(screen, x1, y1, x2, y1, colornames.Green) // top line
ebitenutil.DrawLine(screen, x1, y1, x1, y2, colornames.Green) // left line
ebitenutil.DrawLine(screen, x2, y1, x2, y2, colornames.Green) // right line
ebitenutil.DrawLine(screen, x1, y2, x2, y2, colornames.Green) // bottom line
*/
2020-04-18 12:23:03 +01:00
return nil
}
func (s *Scenario) geoForCam() ebiten.GeoM {
2020-04-18 12:23:03 +01:00
geo := ebiten.GeoM{}
geo.Translate(-float64(s.Viewpoint.X), -float64(s.Viewpoint.Y))
return geo
}
func (s *Scenario) geoForCoords(x, y, z int) ebiten.GeoM {
geo := s.geoForCam()
2020-04-18 12:23:03 +01:00
pix := IsoPt{X: float64(x), Y: float64(y)}.ToCart()
geo.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
2020-04-18 12:23:03 +01:00
geo.Translate(0.0, -float64(z*48.0)) // offset for Z index
return geo
}
2020-06-13 13:42:26 +01:00
func (s *Scenario) renderCell(x, y, z int, screen *ebiten.Image, counter *int) error {
2020-04-18 12:23:03 +01:00
sprites, err := s.area.SpritesForCell(x, y, z)
if err != nil {
return err
}
// TODO: iso.Scale(e.state.zoom, e.state.zoom) // apply current zoom factor
2020-04-18 12:23:03 +01:00
iso := s.geoForCoords(x, y, z)
// 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?
iso.Translate(-209, -332)
for _, spr := range sprites {
2020-06-13 13:42:26 +01:00
*counter = *counter + 1
2020-04-17 22:45:02 +01:00
op := ebiten.DrawImageOptions{GeoM: iso}
2020-04-18 12:23:03 +01:00
op.GeoM.Translate(float64(spr.Rect.Min.X), float64(spr.Rect.Min.Y))
// Zoom has to come last
op.GeoM.Scale(s.Zoom, s.Zoom)
2020-04-17 22:45:02 +01:00
if err := screen.DrawImage(spr.Image, &op); err != nil {
return err
}
}
return nil
}
const (
cellWidth = 128.0
cellHeight = 63.0
cellWidthHalf = cellWidth / 2.0
cellHeightHalf = cellHeight / 2.0
)
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,
}
}