Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Load Tileset file independently of a Map #91

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions assets/tilesets/testLoadTileset.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<tileset version="1.2" tiledversion="1.2.3" name="ProjectUtumno_full" tilewidth="32" tileheight="32" tilecount="6080" columns="64">
<image source="ProjectUtumno_full.png" width="2048" height="3040"/>
<properties>
<property name="testTilesetProperty" value="valueOfTilesetProperty"/>
</properties>
<tile id="116" type="door">
<properties>
<property name="testTileProperty" type="int" value="7"/>
</properties>
</tile>
</tileset>
13 changes: 13 additions & 0 deletions assets/tilesets/testLoadTilesetTile.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<tileset version="1.2" tiledversion="1.2.3" name="ProjectUtumno_full" tilewidth="32" tileheight="32" tilecount="6080" columns="64">
<image source="ProjectUtumno_full.png" width="2048" height="3040"/>
<tile id="464">
<objectgroup draworder="index">
<object id="1" x="-0.25" y="17.75" width="32.375" height="6.125"/>
</objectgroup>
<animation>
<frame tileid="75" duration="500"/>
<frame tileid="76" duration="500"/>
</animation>
</tile>
</tileset>
41 changes: 41 additions & 0 deletions tiled.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
123 changes: 123 additions & 0 deletions tmx_tileset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ package tiled

import (
"image"
"os"
"path/filepath"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -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)
}