WIP: bounds checking
This commit is contained in:
4
Makefile
4
Makefile
@@ -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
|
||||||
|
|
||||||
|
@@ -79,8 +79,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
state := state{
|
state := state{
|
||||||
zoom: 1.0,
|
zoom: 0.5,
|
||||||
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++ {
|
||||||
@@ -171,7 +171,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,17 +197,26 @@ 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(cellToPix(float64(x), float64(y)))
|
||||||
|
iso.Translate(float64(-e.state.origin.X), float64(-e.state.origin.Y))
|
||||||
// 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
|
iso.Scale(e.state.zoom, e.state.zoom) // apply current zoom factor
|
||||||
|
|
||||||
|
// Check if we're going to draw far outside the viewport, cancel if so
|
||||||
|
minX, minY := iso.Apply(cellWidth, cellHeight)
|
||||||
|
if minX < 0.0 || minY < 0.0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
screenW, screenL := screen.Size()
|
||||||
|
maxX, maxY := iso.Apply(float64(screenW), float64(screenL))
|
||||||
|
if int(maxX*e.state.zoom) > screenW || int(maxY*e.state.zoom) > screenL {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
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 {
|
||||||
return err
|
return err
|
||||||
@@ -236,9 +244,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
|
||||||
|
Reference in New Issue
Block a user