commit ec1f27b656d72a1de6a57a83656cde18c6308122
parent 0c407d734f17931ab22e4b3a6a1003c1b1613421
Author: bsandro <brian.drosan@gmail.com>
Date:   Thu, 14 Oct 2021 02:03:05 +0300
Tiles grouped by coordinates and sorted(?) by layer
Diffstat:
2 files changed, 34 insertions(+), 17 deletions(-)
diff --git a/gamemap.go b/gamemap.go
@@ -33,19 +33,18 @@ type Tile struct {
 	Options                    *ebiten.DrawImageOptions
 	Image                      *ebiten.Image
 	Gid                        uint32
-	x, y                       float64 // position coordinates
+	x, y                       float64 // absolute position coordinates
+	LayerId                    int32
 }
 
 type Layer struct {
 	Type   string   `json:type`
-	Id     int      `json:id`
+	Id     int32    `json:id`
 	Data   []uint32 `json:data`
-	Height int      `json:height`
-	Width  int      `json:width`
-	X      int      `json:x`
-	Y      int      `json:y`
-
-	Tiles []Tile
+	Height int32    `json:height`
+	Width  int32    `json:width`
+	X      int32    `json:x`
+	Y      int32    `json:y`
 }
 
 type Gamemap struct {
@@ -57,12 +56,14 @@ type Gamemap struct {
 	Tilesets   []*Tileset `json:tilesets`
 
 	RealWidth, RealHeight float64
+
+	Tiles map[uint64][]*Tile
 }
 
 func (l *Layer) Init(gamemap *Gamemap) error {
 	for i, t := range l.Data {
 		if t > 0 {
-			var tile Tile
+			var tile *Tile = &Tile{}
 			// first 3 bits in that uint value are flags
 			tile.FlipHor = ((t & flipHoriz) >> 31) == 1
 			tile.FlipVer = ((t & flipVert) >> 30) == 1
@@ -97,13 +98,19 @@ func (l *Layer) Init(gamemap *Gamemap) error {
 			// shifting picture back after rotating the matrix
 			tile.Options.GeoM.Translate(float64(t_width)/2, float64(t_height)/2)
 			// setting tile coordinates on the map
-			tile.x = float64((uint32(i) % gamemap.Width) * t_width)
-			tile.y = float64((uint32(i) / gamemap.Width) * t_height)
+			tile_x := uint32(i) % gamemap.Width
+			tile_y := uint32(i) / gamemap.Width
+			tile.x = float64(tile_x * t_width)
+			tile.y = float64(tile_y * t_height)
 			tile.Options.GeoM.Translate(tile.x, tile.y)
 			sx := int((tile.Gid % tileset.Columns) * t_width)
 			sy := int((tile.Gid / tileset.Columns) * t_height)
+			tile.LayerId = l.Id
 			tile.Image = tileset.Atlas.SubImage(image.Rect(sx, sy, sx+int(t_width), sy+int(t_height))).(*ebiten.Image)
-			l.Tiles = append(l.Tiles, tile)
+			// transforming tile coordinates into a single number
+			tile_key := gamemap.GetTileKey(tile_x, tile_y)
+			tiles := gamemap.Tiles[tile_key]
+			gamemap.Tiles[tile_key] = append(tiles, tile)
 		}
 	}
 
@@ -131,6 +138,8 @@ func (m *Gamemap) Init(filename string) error {
 		return err
 	}
 
+	m.Tiles = make(map[uint64][]*Tile)
+
 	for _, tileset := range m.Tilesets {
 		if err := tileset.Init(); err != nil {
 			return err
@@ -163,6 +172,14 @@ func (m *Gamemap) findTileset(gid uint32) *Tileset {
 	return ret
 }
 
+func (m *Gamemap) GetTileKey(x, y uint32) uint64 {
+	// first 4 bytes are x, second 4 bytes - y.
+	var ret uint64 = 0
+	ret |= uint64(x) << 32
+	ret |= uint64(y)
+	return ret
+}
+
 func (t *Tile) IsOnScreen(cam *Camera, gamemap *Gamemap) bool {
 	var left float64 = cam.x - float64(gamemap.TileWidth)
 	var right float64 = cam.x + float64(gamemap.TileWidth) + cam.width
diff --git a/main.go b/main.go
@@ -68,14 +68,14 @@ func (g *Game) Update() error {
 }
 
 func (g *Game) Draw(screen *ebiten.Image) {
-	for _, layer := range g.Map.Layers {
-		for _, t := range layer.Tiles {
-			if t.IsOnScreen(&g.Cam, g.Map) {
+	for _, tiles := range g.Map.Tiles {
+		for _, tile := range tiles {
+			if tile.IsOnScreen(&g.Cam, g.Map) {
 				// render options for that tile are being copied into opts so the cam
 				// movement transformations are not dragged back into the tile itself.
-				opts := *t.Options
+				opts := *tile.Options
 				opts.GeoM.Translate(-g.Cam.x, -g.Cam.y)
-				screen.DrawImage(t.Image, &opts)
+				screen.DrawImage(tile.Image, &opts)
 			}
 		}
 	}