Bounds checking

This commit is contained in:
2019-12-31 01:38:39 +00:00
parent 9e3389c8ad
commit 73553cb8b0
2 changed files with 53 additions and 31 deletions

View File

@@ -1,6 +1,6 @@
srcfiles = Makefile $(shell find . -iname *.go) srcfiles = Makefile go.mod $(shell find . -iname *.go)
GOBUILD ?= go build GOBUILD ?= go build -tags ebitengl
all: loader palette-idx view-obj view-map view-menu view-minimap view-set wh40k all: loader palette-idx view-obj view-map view-menu view-minimap view-set wh40k

View File

@@ -80,7 +80,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
} }
env := &env{ env := &env{
gameMap: gameMap, gameMap: gameMap,
@@ -97,10 +97,10 @@ func main() {
// TODO: click to view cell data // TODO: click to view cell data
win.OnKeyUp(ebiten.KeyLeft, env.changeOrigin(+64, +0)) win.OnKeyUp(ebiten.KeyLeft, env.changeOrigin(-64, +0))
win.OnKeyUp(ebiten.KeyRight, env.changeOrigin(-64, +0)) win.OnKeyUp(ebiten.KeyRight, env.changeOrigin(+64, +0))
win.OnKeyUp(ebiten.KeyUp, env.changeOrigin(+0, +64)) win.OnKeyUp(ebiten.KeyUp, env.changeOrigin(+0, -64))
win.OnKeyUp(ebiten.KeyDown, env.changeOrigin(+0, -64)) win.OnKeyUp(ebiten.KeyDown, env.changeOrigin(+0, +64))
win.OnMouseWheel(env.changeZoom) win.OnMouseWheel(env.changeZoom)
for i := 0; i <= 6; i++ { for i := 0; i <= 6; i++ {
@@ -148,20 +148,42 @@ func (e *env) getSprite(palette []string, ref maps.ObjRef) (*conv.Sprite, error)
} }
func (e *env) Draw(screen *ebiten.Image) error { func (e *env) Draw(screen *ebiten.Image) error {
// TODO: we should be able to perform bounds clipping on these // Bounds clipping
minX := int(e.gameMap.MinWidth) // http://www.java-gaming.org/index.php?topic=24922.0
maxX := int(e.gameMap.MaxWidth) // https://stackoverflow.com/questions/892811/drawing-isometric-game-worlds
minY := int(e.gameMap.MinLength) // https://gamedev.stackexchange.com/questions/25896/how-do-i-find-which-isometric-tiles-are-inside-the-cameras-current-view
maxY := int(e.gameMap.MaxLength)
minZ := 0
maxZ := int(e.state.zIdx) + 1
for z := minZ; z < maxZ; z++ { sw, sh := screen.Size()
for y := minY; y < maxY; y++ {
for x := minX; x < maxX; x++ { topLeftX, topLeftY := pixToCell(
if err := e.renderCell(x, y, z, screen); err != nil { float64(e.state.origin.X),
return err float64(e.state.origin.Y),
)
topLeftX -= 1 // Otherwise we miss half a cell on alternate rows on the left
bottomRightX, bottomRightY := pixToCell(
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 row
for a := int(topLeftX + topLeftY); a <= int(bottomRightX+bottomRightY); a++ {
for b := int(topLeftX - topLeftY); b <= int(bottomRightX-bottomRightY); b++ {
if b&1 != a&1 {
continue
} }
x := (a + b) / 2
y := (a - b) / 2
if x < int(e.gameMap.MinWidth) || x >= int(e.gameMap.MaxWidth) ||
y < int(e.gameMap.MinLength) || y >= int(e.gameMap.MaxLength) {
continue
}
for z := 0; z <= e.state.zIdx; z++ {
e.renderCell(x, y, z, screen)
} }
} }
} }
@@ -171,7 +193,6 @@ func (e *env) Draw(screen *ebiten.Image) error {
func (e *env) renderCell(x, y, z int, screen *ebiten.Image) error { func (e *env) renderCell(x, y, z int, screen *ebiten.Image) error {
var sprites []*conv.Sprite var sprites []*conv.Sprite
cell := e.gameMap.Cells.At(x, y, z) cell := e.gameMap.Cells.At(x, y, z)
if spr, err := e.getSprite(e.set.Palette, cell.Surface); err != nil { if spr, err := e.getSprite(e.set.Palette, cell.Surface); err != nil {
@@ -198,16 +219,17 @@ func (e *env) renderCell(x, y, z int, screen *ebiten.Image) error {
sprites = append(sprites, spr) sprites = append(sprites, spr)
} }
iso := ebiten.GeoM{}
iso.Translate(-float64(e.state.origin.X), -float64(e.state.origin.Y))
fx, fy := cellToPix(float64(x), float64(y))
iso.Translate(fx, fy)
// 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
fx, fy := cellToPix(float64(x), float64(y))
fx += float64(e.state.origin.X)
fy += float64(e.state.origin.Y)
iso := ebiten.GeoM{}
iso.Translate(fx, fy)
iso.Translate(0.0, -float64(z*48.0)) // offset for Z index iso.Translate(0.0, -float64(z*48.0)) // offset for Z index
iso.Scale(e.state.zoom, e.state.zoom) // apply current zoom factor
// TODO: iso.Scale(e.state.zoom, e.state.zoom) // apply current zoom factor
for _, sprite := range sprites { for _, sprite := range sprites {
if err := screen.DrawImage(sprite.Image, &ebiten.DrawImageOptions{GeoM: iso}); err != nil { if err := screen.DrawImage(sprite.Image, &ebiten.DrawImageOptions{GeoM: iso}); err != nil {
@@ -236,9 +258,9 @@ func (e *env) setZIdx(to int) func() {
} }
} }
var ( const (
cellWidth = 64.0 cellWidth = 64
cellHeight = 64.0 cellHeight = 64
) )
// Doesn't take the camera or Z level into account // Doesn't take the camera or Z level into account