ebitmx

package module
v0.0.0-...-c183f62 Latest Latest
Warning

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

Go to latest
Published: Mar 28, 2021 License: MIT Imports: 8 Imported by: 1

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"`
	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     [][]int
}

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
}

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"`
	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 []Tileset `xml:"tileset"`
	Layers []Layer `xml:"layer"`
}

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 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"`
}

type Tile

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

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

Directories

Path Synopsis
examples module

Jump to

Keyboard shortcuts

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