ebitmx

package module
v0.0.0-...-1021de5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 21, 2022 License: MIT Imports: 14 Imported by: 0

README

Ebitmx

Ebitmx is a super simple parser to help render TMX maps when using Ebiten for your games.

Please do not use this library in a production environment, this is under heavy development and there will be breaking changes.

This library parses a TMX file and returns a struct like the following, with the basic fields for you to render your map inside the Draw() function of the Ebiten main loop.

type EbitenMap struct {
	TileWidth  int      // The width of the tile
	TileHeight int      // The height of the tile
	MapHeight  int      // The number of tiles in Height
	MapWidth   int      // The number of tiles in Width
	Layers     [][]int  // Layers
}

Quick Start


// Load the tiles image
tiles, _, err := ebitenutil.NewImageFromFile("overworld.png")
if err != nil {
	fmt.Println(err)
	os.Exit(2)
}

// Load the information of the tmx file
myMap, err := ebitmx.GetEbitenMap("map.tmx")
if err != nil {
	fmt.Println(err)
	os.Exit(2)
}

// Ready to draw! Check the examples for more info

Examples

Check the example code in the examples folder.

Run it by

$ cd examples
$ go run main.go

A super simple map with 2 layers should load:

alt text

Roadmap (not in order)

  • Getters for simplicity
  • Improve docs and tests
  • Collision maps
  • Out of the box "Draw()" method for simplicity (if you don't want to think, just call Draw from your code, passing ebiten "screen")
  • Renderorder
  • Infinite maps

License

MIT License

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Data

type Data struct {
	XMLName     xml.Name `xml:"data"`
	Encoding    string   `xml:"encoding,attr"`
	Compression string   `xml:"compression,attr"`
	Raw         []byte   `xml:",innerxml"`
}

Data represents the data inside a Layer

type EbitenMap

type EbitenMap struct {
	TileWidth    int
	TileHeight   int
	MapHeight    int
	MapWidth     int
	Layers       [][]uint32
	TilesetInfos []TilesetInfo
	ObjectGroup  []ObjectGroup
}

EbitenMap is the transformed representation of a TMX map in the simplest way possible for Ebiten to understand and render

func GetEbitenMap

func GetEbitenMap(path string) (*EbitenMap, error)

GetEbitenMap returns a map that Ebiten can understand based on a TMX file. Note that some data might be lost, as Ebiten does not require too much information to render a map

func GetEbitenMapFromFS

func GetEbitenMapFromFS(fileSystem fs.FS, path string) (*EbitenMap, error)

GetEbitenMapFromFS 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

type EbitenTileset

type EbitenTileset struct {
	TileWidth     int
	TileHeight    int
	TilesetWidth  int
	TilesetHeight int
	Tiles         []Tile
	ImageSrc      string
}

EbitenTileset is a friendly representation of a TSX Tileset

func GetEbitenTileset

func GetEbitenTileset(path string) (*EbitenTileset, error)

GetEbitenTileset returns a simplified TSX Tileset, based on a file on disk

func GetTilesetFromFS

func GetTilesetFromFS(fileSystem fs.FS, path string) (*EbitenTileset, error)

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

type Image

type Image struct {
	XMLName xml.Name `xml:"image"`
	Format  string   `xml:"format"`
	Source  string   `xml:"source,attr"`
	Width   int      `xml:"width,attr"`
	Height  int      `xml:"height,attr"`
}

type Layer

type Layer struct {
	XMLName xml.Name `xml:"layer"`
	ID      string   `xml:"id,attr"`
	Name    string   `xml:"name,attr"`
	Data    Data     `xml:"data"`
	Width   int      `xml:"width,attr"`
	Height  int      `xml:"height,attr"`
}

Layer represents a layer in the TMX map file

type Map

type Map struct {
	XMLName      xml.Name `xml:"map"`
	Version      string   `xml:"version,attr"`
	TiledVersion string   `xml:"tiledversion,attr"`
	Orientation  string   `xml:"orientation,attr"`
	RenderOrder  string   `xml:"renderorder,attr"`
	Width        int      `xml:"width,attr"`
	Height       int      `xml:"height,attr"`
	TileWidth    int      `xml:"tilewidth,attr"`
	TilHeight    int      `xml:"tileheight,attr"`
	Infinite     bool     `xml:"infinite,attr"`

	//Tileset []TilesetInfos `xml:"TilesetInfo"`
	Layers      []Layer       `xml:"layer"`
	Tileset     []TilesetInfo `xml:"tileset"`
	ObjectGroup []ObjectGroup `xml:"objectgroup"`
}

Map is the representation of a map in a TMX file

func ParseTMX

func ParseTMX(bytes []byte) (*Map, error)

ParseTMX parses a TMX file and returns a Map For now, we only allow XML format

type Object

type Object struct {
	Id         int        `xml:"id,attr"`
	Name       string     `xml:"name,attr"`
	Type       string     `xml:"type,attr"`
	X          float64    `xml:"x,attr"`
	Y          float64    `xml:"y,attr"`
	Width      float64    `xml:"width,attr"`
	Height     float64    `xml:"height,attr"`
	Properties Properties `xml:"properties"`
}

type ObjectGroup

type ObjectGroup struct {
	Id      int      `xml:"id,attr"`
	Name    string   `xml:"name,attr"`
	Objects []Object `xml:"object"`
}

type Properties

type Properties struct {
	XMLName    xml.Name   `xml:"properties"`
	Properties []Property `xml:"property"`
}

type Property

type Property struct {
	XMLName xml.Name `xml:"property"`
	Name    string   `xml:"name,attr"`
	Type    string   `xml:"type,attr"`
	Value   string   `xml:"value,attr"`
	Values  string   `xml:",innerxml"`
}

type Tile

type Tile struct {
	XMLName     xml.Name      `xml:"tile"`
	Id          int           `xml:"id,attr"`
	Type        string        `xml:"type,attr"`
	Properties  Properties    `xml:"properties"`
	ObjectGroup []ObjectGroup `xml:"objectgroup"`
}

type Tileset

type Tileset struct {
	XMLName    xml.Name `xml:"tileset"`
	Version    string   `xml:"version,attr"`
	Name       string   `xml:"name,attr"`
	TileWidth  int      `xml:"tilewidth,attr"`
	TileHeight int      `xml:"tileheight,attr"`
	TileCount  int      `xml:"tilecount,attr"`
	Columns    int      `xml:"columns,attr"`
	Image      Image    `xml:"image"`
	Tiles      []Tile   `xml:"tile"`
}

Tileset represents a set of tiles in a TMX, or a TSX file

func ParseTSX

func ParseTSX(bytes []byte) (*Tileset, error)

ParseTSX parses a TSX file and returns a Tileset For now, we only allow XML format

type TilesetInfo

type TilesetInfo struct {
	FirstGid uint32 `xml:"firstgid,attr"`
	Source   string `xml:"source,attr"`
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL