commit 9e499efa47b8b2ce1634f4a61dbd78716390077e
parent e59ae6596780d1c575d0d33d1903605041c5bfb7
Author: bsandro <brian.drosan@gmail.com>
Date: Sun, 3 Oct 2021 05:14:34 +0300
find first tileset gid for render
Diffstat:
M | main.go | | | 56 | +++++++++++++++++++++++++++++++------------------------- |
1 file changed, 31 insertions(+), 25 deletions(-)
diff --git a/main.go b/main.go
@@ -8,35 +8,36 @@ import (
"image"
_ "image/png"
"log"
- "os"
_ "math"
+ "os"
)
const (
- flipHoriz uint32 = 0b10000000000000000000000000000000
- flipVert uint32 = 0b01000000000000000000000000000000
- flipDiag uint32 = 0b00100000000000000000000000000000
- screenW = 1024
- screenH = 768
- tileSize = 16
- tileXCount = 40
+ flipHoriz uint32 = 0b10000000000000000000000000000000
+ flipVert uint32 = 0b01000000000000000000000000000000
+ flipDiag uint32 = 0b00100000000000000000000000000000
+ screenW = 1024
+ screenH = 768
+ tileSize = 16
+ tileXCount = 40
)
type Gamemap struct {
Height int `json:height`
Width int `json:width`
Layers []struct {
- Type string `json:type`
- Id int `json:id`
+ Type string `json:type`
+ Id int `json:id`
Data []uint32 `json:data`
- Height int `json:height`
- Width int `json:width`
- X int `json:x`
- Y int `json:y`
+ Height int `json:height`
+ Width int `json:width`
+ X int `json:x`
+ Y int `json:y`
} `json:layers`
Tilesets []struct {
- Firstgid uint `json:firstgid`
+ Firstgid uint32 `json:firstgid`
Source string `json:source`
+ Name string `json:name`
} `json:tilesets`
}
@@ -64,33 +65,38 @@ func init() {
}
}
+func findFirstGid(gid uint32) uint32 {
+ var ret uint32 = 0
+ for _, tileset := range gamemap.Tilesets {
+ if gid >= tileset.Firstgid && ret < tileset.Firstgid {
+ ret = tileset.Firstgid
+ }
+ }
+ return ret
+}
+
func (g *Game) Update() error {
return nil
}
func (g *Game) Draw(screen *ebiten.Image) {
- fmt.Println("------------------------------------------------------------------------------------------------------")
-
for _, layer := range gamemap.Layers {
if layer.Type == "tilelayer" {
for i, t := range layer.Data {
if t > 0 {
- t_orig := t
is_flipped_hor := (t & flipHoriz) >> 31
is_flipped_ver := (t & flipVert) >> 30
is_flipped_diag := (t & flipDiag) >> 29
-
- t &= ^(flipHoriz | flipVert | flipDiag)
- t1 := int(t - 1) // tileset.firstgid
- if t1 == 113 || t1 == 35 {
- fmt.Printf("(%d) %32b -> %b (%d %d %d)\n", t1, t_orig, t, is_flipped_hor, is_flipped_ver, is_flipped_diag)
+ t &= ^(flipHoriz | flipVert | flipDiag) // cleanup flags
+ first_gid := findFirstGid(t)
+ if first_gid == 0 {
+ log.Fatal("Invalid tile gid:", t)
}
+ t1 := int(t - first_gid)
op := &ebiten.DrawImageOptions{}
op.GeoM.Translate(-float64(tileSize)/2, -float64(tileSize)/2)
if is_flipped_diag == 1 {
ret := ebiten.GeoM{}
- //op.GeoM.Scale(-1, -1)
- //op.GeoM.Rotate(-float64(math.Pi / 2))
ret.SetElement(0, 0, op.GeoM.Element(1, 0))
ret.SetElement(0, 1, op.GeoM.Element(1, 1))
ret.SetElement(0, 2, op.GeoM.Element(1, 2))