add offset to tiles and make storage sparse

This commit is contained in:
Josh Deprez 2021-08-15 16:14:50 +10:00
parent f5bd131297
commit c1e9570403
2 changed files with 30 additions and 27 deletions

View file

@ -18,8 +18,9 @@ type Tilemap struct {
Disabled bool Disabled bool
Hidden bool Hidden bool
ID ID
Map [][]Tile Map map[image.Point]Tile
Ersatz bool // "fake wall" Ersatz bool // "fake wall"
Offset image.Point // world coordinates
Src ImageRef Src ImageRef
TileSize int TileSize int
ZPos ZPos
@ -34,13 +35,13 @@ func (t *Tilemap) CollidesWith(r image.Rectangle) bool {
// If we round down r.Min, and round up r.Max, to the nearest tile // If we round down r.Min, and round up r.Max, to the nearest tile
// coordinates, that gives the full range of tiles to test. // coordinates, that gives the full range of tiles to test.
sm1 := t.TileSize - 1 sm1 := t.TileSize - 1
r = r.Sub(t.Offset)
min := r.Min.Div(t.TileSize) min := r.Min.Div(t.TileSize)
max := r.Max.Add(image.Pt(sm1, sm1)).Div(t.TileSize) max := r.Max.Add(image.Pt(sm1, sm1)).Div(t.TileSize)
for j := min.Y; j <= max.Y && j < len(t.Map); j++ { for j := min.Y; j <= max.Y; j++ {
row := t.Map[j] for i := min.X; i <= max.X; i++ {
for i := min.X; i <= max.X && i < len(row); i++ { if t.Map[image.Pt(i, j)] == nil {
if row[i] == nil {
continue continue
} }
if r.Overlaps(image.Rect(i*t.TileSize, j*t.TileSize, (i+1)*t.TileSize, (j+1)*t.TileSize)) { if r.Overlaps(image.Rect(i*t.TileSize, j*t.TileSize, (i+1)*t.TileSize, (j+1)*t.TileSize)) {
@ -60,21 +61,19 @@ func (t *Tilemap) Draw(screen *ebiten.Image, opts ebiten.DrawImageOptions) {
w, _ := src.Size() w, _ := src.Size()
og := opts.GeoM og := opts.GeoM
var geom ebiten.GeoM var geom ebiten.GeoM
for j, row := range t.Map { for p, tile := range t.Map {
for i, tile := range row { if tile == nil {
if tile == nil { continue
continue
}
geom.Reset()
geom.Translate(float64(i*t.TileSize), float64(j*t.TileSize))
geom.Concat(og)
opts.GeoM = geom
s := tile.TileIndex() * t.TileSize
sx, sy := s%w, (s/w)*t.TileSize
src := src.SubImage(image.Rect(sx, sy, sx+t.TileSize, sy+t.TileSize)).(*ebiten.Image)
screen.DrawImage(src, &opts)
} }
geom.Reset()
geom.Translate(float64(p.X*t.TileSize+t.Offset.X), float64(p.Y*t.TileSize+t.Offset.Y))
geom.Concat(og)
opts.GeoM = geom
s := tile.TileIndex() * t.TileSize
sx, sy := s%w, (s/w)*t.TileSize
src := src.SubImage(image.Rect(sx, sy, sx+t.TileSize, sy+t.TileSize)).(*ebiten.Image)
screen.DrawImage(src, &opts)
} }
} }
@ -83,12 +82,10 @@ func (t *Tilemap) Update() error {
if t.Disabled { if t.Disabled {
return nil return nil
} }
for _, row := range t.Map { for _, tile := range t.Map {
for _, tile := range row { if u, ok := tile.(Updater); ok {
if u, ok := tile.(Updater); ok { if err := u.Update(); err != nil {
if err := u.Update(); err != nil { return err
return err
}
} }
} }
} }

View file

@ -23,7 +23,7 @@ func main() {
engine.AssetFS = assets engine.AssetFS = assets
// engine.AnimDefs set in game/anims.go // engine.AnimDefs set in game/anims.go
tiles := [][]engine.Tile{ denseTiles := [][]engine.Tile{
{engine.StaticTile(9), nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, engine.StaticTile(9)}, {engine.StaticTile(9), nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, engine.StaticTile(9)},
{nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, &engine.AnimatedTile{AnimRef: engine.AnimRef{Key: "red_tiles"}}, nil, nil, nil, nil, nil, nil, nil, nil, nil}, {nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, &engine.AnimatedTile{AnimRef: engine.AnimRef{Key: "red_tiles"}}, nil, nil, nil, nil, nil, nil, nil, nil, nil},
{nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, &engine.AnimatedTile{AnimRef: engine.AnimRef{Key: "red_tiles"}}, nil, nil, nil}, {nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, &engine.AnimatedTile{AnimRef: engine.AnimRef{Key: "red_tiles"}}, nil, nil, nil},
@ -40,6 +40,12 @@ func main() {
{engine.StaticTile(7), engine.StaticTile(7), engine.StaticTile(7), engine.StaticTile(7), &engine.AnimatedTile{AnimRef: engine.AnimRef{Key: "red_tiles"}}, engine.StaticTile(7), engine.StaticTile(7), engine.StaticTile(7), engine.StaticTile(7), engine.StaticTile(7), engine.StaticTile(7), engine.StaticTile(7), engine.StaticTile(7), &engine.AnimatedTile{AnimRef: engine.AnimRef{Key: "green_tiles"}}, engine.StaticTile(7), engine.StaticTile(7), engine.StaticTile(7), engine.StaticTile(7), engine.StaticTile(7), engine.StaticTile(7)}, {engine.StaticTile(7), engine.StaticTile(7), engine.StaticTile(7), engine.StaticTile(7), &engine.AnimatedTile{AnimRef: engine.AnimRef{Key: "red_tiles"}}, engine.StaticTile(7), engine.StaticTile(7), engine.StaticTile(7), engine.StaticTile(7), engine.StaticTile(7), engine.StaticTile(7), engine.StaticTile(7), engine.StaticTile(7), &engine.AnimatedTile{AnimRef: engine.AnimRef{Key: "green_tiles"}}, engine.StaticTile(7), engine.StaticTile(7), engine.StaticTile(7), engine.StaticTile(7), engine.StaticTile(7), engine.StaticTile(7)},
{engine.StaticTile(9), engine.StaticTile(7), engine.StaticTile(7), engine.StaticTile(7), engine.StaticTile(7), engine.StaticTile(7), engine.StaticTile(7), engine.StaticTile(7), engine.StaticTile(7), engine.StaticTile(7), engine.StaticTile(7), engine.StaticTile(7), engine.StaticTile(7), engine.StaticTile(7), engine.StaticTile(7), engine.StaticTile(7), engine.StaticTile(7), engine.StaticTile(7), engine.StaticTile(7), engine.StaticTile(9)}, {engine.StaticTile(9), engine.StaticTile(7), engine.StaticTile(7), engine.StaticTile(7), engine.StaticTile(7), engine.StaticTile(7), engine.StaticTile(7), engine.StaticTile(7), engine.StaticTile(7), engine.StaticTile(7), engine.StaticTile(7), engine.StaticTile(7), engine.StaticTile(7), engine.StaticTile(7), engine.StaticTile(7), engine.StaticTile(7), engine.StaticTile(7), engine.StaticTile(7), engine.StaticTile(7), engine.StaticTile(9)},
} }
tiles := make(map[image.Point]engine.Tile)
for j, row := range denseTiles {
for i, tile := range row {
tiles[image.Pt(i, j)] = tile
}
}
level1 := &engine.Scene{ level1 := &engine.Scene{
ID: "level_1", ID: "level_1",