diff --git a/assets/tilesets/testLoadTileset.tsx b/assets/tilesets/testLoadTileset.tsx new file mode 100644 index 0000000..7184af4 --- /dev/null +++ b/assets/tilesets/testLoadTileset.tsx @@ -0,0 +1,12 @@ + + + + + + + + + + + + diff --git a/assets/tilesets/testLoadTilesetTile.tsx b/assets/tilesets/testLoadTilesetTile.tsx new file mode 100644 index 0000000..564171e --- /dev/null +++ b/assets/tilesets/testLoadTilesetTile.tsx @@ -0,0 +1,13 @@ + + + + + + + + + + + + + diff --git a/tiled.go b/tiled.go index dbfc924..fb0195b 100644 --- a/tiled.go +++ b/tiled.go @@ -43,6 +43,20 @@ func LoadFile(fileName string, options ...LoaderOption) (*Map, error) { return l.LoadFile(fileName) } +// LoadTilesetFromReader loads a tileset from an io.Reader. +// baseDir is used to locate relative paths to additional tileset data; default +// is currend directory if empty. +func LoadTilesetFromReader(baseDir string, r io.Reader, options ...LoaderOption) (*Tileset, error) { + l := newLoader(options...) + return l.LoadTilesetFromReader(baseDir, r) +} + +// LoadTilesetFile loads a tiled map in TSX format from a file. +func LoadTilesetFile(fileName string, options ...LoaderOption) (*Tileset, error) { + l := newLoader(options...) + return l.LoadTilesetFile(fileName) +} + // loader provides configuration on how TMX maps and resources are loaded. type loader struct { // A FileSystem that is used for loading TMX files and any external @@ -106,3 +120,30 @@ func (l *loader) LoadFile(fileName string) (*Map, error) { dir := filepath.Dir(fileName) return l.LoadReader(dir, f) } + +// LoadTilesetFile loads a tileset in TSX format from a file. +func (l *loader) LoadTilesetFile(fileName string) (*Tileset, error) { + f, err := l.open(fileName) + if err != nil { + return nil, err + } + defer f.Close() + + dir := filepath.Dir(fileName) + return l.LoadTilesetFromReader(dir, f) +} + +// LoadTilesetFromReader loads a .tsx file into a Tileset structure +func (l *loader) LoadTilesetFromReader(baseDir string, r io.Reader) (*Tileset, error) { + d := xml.NewDecoder(r) + + t := &Tileset{ + baseDir: baseDir, + } + if err := d.Decode(t); err != nil { + return nil, err + } + + t.SourceLoaded = true + return t, nil +} diff --git a/tmx_tileset_test.go b/tmx_tileset_test.go index 2a4ea68..f5921b7 100644 --- a/tmx_tileset_test.go +++ b/tmx_tileset_test.go @@ -24,6 +24,8 @@ package tiled import ( "image" + "os" + "path/filepath" "testing" "github.com/stretchr/testify/assert" @@ -167,3 +169,124 @@ func TestGetTileRect(t *testing.T) { } } + +var testLoadTilesetFile = &Tileset{ + baseDir: ".", + Columns: 64, + FirstGID: 0, + Image: &Image{ + Format: "", + Data: nil, + Height: 3040, + Width: 2048, + Source: "ProjectUtumno_full.png", + Trans: nil, + }, + Margin: 0, + Name: "ProjectUtumno_full", + Properties: Properties{ + { + Name: "testTilesetProperty", + Value: "valueOfTilesetProperty", + }, + }, + Source: "", + SourceLoaded: true, + Spacing: 0, + TerrainTypes: nil, + TileCount: 6080, + TileHeight: 32, + TileOffset: nil, + TileWidth: 32, + TiledVersion: "1.2.3", + Tiles: []*TilesetTile{ + { + ID: 116, + Type: "door", + Animation: nil, + Image: nil, + ObjectGroups: nil, + Probability: 0, + Properties: Properties{ + { + Name: "testTileProperty", + Type: "int", + Value: "7", + }, + }, + Terrain: "", + }, + }, + Version: "1.2", +} + +var testLoadTilesetTileFile = &TilesetTile{ + ID: 464, + Animation: []*AnimationFrame{ + { + Duration: 500, + TileID: 75, + }, + { + Duration: 500, + TileID: 76, + }, + }, + Image: nil, + ObjectGroups: []*ObjectGroup{ + { + ID: 0, + Color: nil, + DrawOrder: "index", + Name: "", + Objects: []*Object{ + { + GID: 0, + Ellipses: nil, + Height: 6.125, + ID: 1, + Name: "", + PolyLines: nil, + Polygons: nil, + Properties: nil, + Rotation: 0, + Text: nil, + Type: "", + Visible: true, + Width: 32.375, + X: -0.25, + Y: 17.75, + }, + }, + OffsetX: 0, + OffsetY: 0, + Opacity: 1, + Properties: nil, + Visible: true, + }, + }, +} + +func TestLoadTileset(t *testing.T) { + tsxFile, err := os.Open(filepath.Join(GetAssetsDirectory(), "tilesets/testLoadTileset.tsx")) + assert.Nil(t, err) + defer tsxFile.Close() + + tsx, err := LoadTilesetFromReader(".", tsxFile) + assert.Nil(t, err) + + assert.Equal(t, testLoadTilesetFile, tsx) +} + +func TestLoadTile(t *testing.T) { + tsxFile, err := os.Open(filepath.Join(GetAssetsDirectory(), "tilesets/testLoadTilesetTile.tsx")) + assert.Nil(t, err) + defer tsxFile.Close() + + tsx, err := LoadTilesetFromReader(".", tsxFile) + assert.Nil(t, err) + assert.Len(t, tsx.Tiles, 1) + + tile := tsx.Tiles[0] + assert.Equal(t, testLoadTilesetTileFile, tile) +}