-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathebitsx.go
58 lines (49 loc) · 1.56 KB
/
ebitsx.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
package ebitmx
import (
"fmt"
"io/fs"
"io/ioutil"
"math"
"os"
)
// EbitenTileset is a friendly representation of a TSX Tileset
type EbitenTileset struct {
TileWidth int
TileHeight int
TilesetWidth int
TilesetHeight int
Tiles []Tile
}
// GetEbitenTileset returns a simplified TSX Tileset, based on a file on disk
func GetEbitenTileset(path string) (*EbitenTileset, error) {
return GetTilesetFromFS(os.DirFS("."), path)
}
// GetTilesetFromFS allows you to pass in the file system used to find the desired file
// This is useful for Go's v1.16 embed package which makes it simple to embed assets into
// your binary and accessible via the embed.FS which is compatible with the fs.FS interface
func GetTilesetFromFS(fileSystem fs.FS, path string) (*EbitenTileset, error) {
tsxFile, err := fileSystem.Open(path)
if err != nil {
return nil, fmt.Errorf("error opening TSX file %s: %v", path, err)
}
defer tsxFile.Close()
bytes, err := ioutil.ReadAll(tsxFile)
if err != nil {
return nil, fmt.Errorf("error reading TSX file %s: %v", path, err)
}
tsxTileset, err := ParseTSX(bytes)
if err != nil {
return nil, fmt.Errorf("error parsing TSX file %s: %v", path, err)
}
return transformMapToEbitenTileset(tsxTileset)
}
func transformMapToEbitenTileset(tsx *Tileset) (*EbitenTileset, error) {
ebitenMap := &EbitenTileset{
TileWidth: tsx.TileWidth,
TileHeight: tsx.TileHeight,
TilesetWidth: tsx.Columns,
TilesetHeight: int(math.Ceil(float64(tsx.TileCount) / float64(tsx.Columns))),
Tiles: tsx.Tiles,
}
return ebitenMap, nil
}