add offset to tiles and make storage sparse
This commit is contained in:
parent
f5bd131297
commit
c1e9570403
2 changed files with 30 additions and 27 deletions
|
@ -18,8 +18,9 @@ type Tilemap struct {
|
|||
Disabled bool
|
||||
Hidden bool
|
||||
ID
|
||||
Map [][]Tile
|
||||
Map map[image.Point]Tile
|
||||
Ersatz bool // "fake wall"
|
||||
Offset image.Point // world coordinates
|
||||
Src ImageRef
|
||||
TileSize int
|
||||
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
|
||||
// coordinates, that gives the full range of tiles to test.
|
||||
sm1 := t.TileSize - 1
|
||||
r = r.Sub(t.Offset)
|
||||
min := r.Min.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++ {
|
||||
row := t.Map[j]
|
||||
for i := min.X; i <= max.X && i < len(row); i++ {
|
||||
if row[i] == nil {
|
||||
for j := min.Y; j <= max.Y; j++ {
|
||||
for i := min.X; i <= max.X; i++ {
|
||||
if t.Map[image.Pt(i, j)] == nil {
|
||||
continue
|
||||
}
|
||||
if r.Overlaps(image.Rect(i*t.TileSize, j*t.TileSize, (i+1)*t.TileSize, (j+1)*t.TileSize)) {
|
||||
|
@ -60,13 +61,12 @@ func (t *Tilemap) Draw(screen *ebiten.Image, opts ebiten.DrawImageOptions) {
|
|||
w, _ := src.Size()
|
||||
og := opts.GeoM
|
||||
var geom ebiten.GeoM
|
||||
for j, row := range t.Map {
|
||||
for i, tile := range row {
|
||||
for p, tile := range t.Map {
|
||||
if tile == nil {
|
||||
continue
|
||||
}
|
||||
geom.Reset()
|
||||
geom.Translate(float64(i*t.TileSize), float64(j*t.TileSize))
|
||||
geom.Translate(float64(p.X*t.TileSize+t.Offset.X), float64(p.Y*t.TileSize+t.Offset.Y))
|
||||
geom.Concat(og)
|
||||
opts.GeoM = geom
|
||||
|
||||
|
@ -76,22 +76,19 @@ func (t *Tilemap) Draw(screen *ebiten.Image, opts ebiten.DrawImageOptions) {
|
|||
screen.DrawImage(src, &opts)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Update calls Update on any tiles that are Updaters, e.g. AnimatedTile.
|
||||
func (t *Tilemap) Update() error {
|
||||
if t.Disabled {
|
||||
return nil
|
||||
}
|
||||
for _, row := range t.Map {
|
||||
for _, tile := range row {
|
||||
for _, tile := range t.Map {
|
||||
if u, ok := tile.(Updater); ok {
|
||||
if err := u.Update(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
|
|
8
main.go
8
main.go
|
@ -23,7 +23,7 @@ func main() {
|
|||
engine.AssetFS = assets
|
||||
// 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)},
|
||||
{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},
|
||||
|
@ -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(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{
|
||||
ID: "level_1",
|
||||
|
|
Loading…
Reference in a new issue