-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtmx.go
92 lines (74 loc) · 1.81 KB
/
tmx.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package zelduh
import (
"fmt"
"io/ioutil"
"strconv"
"strings"
"github.com/deanobob/tmxreader"
"github.com/faiface/pixel"
)
// Load loads the tmx files
func Load(tilemapFiles []string, tilemapDir string) map[string]tmxreader.TmxMap {
tmxMapData := map[string]tmxreader.TmxMap{}
for _, name := range tilemapFiles {
path := fmt.Sprintf("%s%s.tmx", tilemapDir, name)
tmxMapData[name] = parseTmxFile(path)
}
return tmxMapData
}
func parseTmxFile(filename string) tmxreader.TmxMap {
raw, err := ioutil.ReadFile(filename)
if err != nil {
panic(err)
}
tmxMap, err := tmxreader.Parse(raw)
if err != nil {
panic(err)
}
return tmxMap
}
type mapDrawData struct {
Rect pixel.Rect
SpriteID int
}
// MapData represents data for one map
type MapData struct {
Name string
Data []mapDrawData
}
// BuildMapDrawData builds draw data and stores it in a map
func BuildMapDrawData(dir string, files []string, tileSize float64) map[string]MapData {
// load all TMX file data for each map
tmxMapData := Load(files, dir)
all := map[string]MapData{}
for mapName, mapData := range tmxMapData {
md := MapData{
Name: mapName,
Data: []mapDrawData{},
}
layers := mapData.Layers
for _, layer := range layers {
records := ParseCSV(strings.TrimSpace(layer.Data.Value) + ",")
for row := 0; row <= len(records); row++ {
if len(records) > row {
for col := 0; col < len(records[row])-1; col++ {
y := float64(11-row) * tileSize
x := float64(col) * tileSize
record := records[row][col]
spriteID, err := strconv.Atoi(record)
if err != nil {
panic(err)
}
mrd := mapDrawData{
Rect: pixel.R(x, y, x+tileSize, y+tileSize),
SpriteID: spriteID,
}
md.Data = append(md.Data, mrd)
}
}
}
all[mapName] = md
}
}
return all
}