tilepix

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

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

Go to latest
Published: Jan 16, 2020 License: MIT Imports: 19 Imported by: 0

README

Build Status Go Report Card GitHub GoDoc

TilePixLogo TilePix

TilePix is a complementary library, designed to be used with the Pixel library (see below for more details on Pixel). TilePix was born out of Ludum Dare; having found that a vast amount of very limited time during the Ludum Dare weekends was used planing out map layouts, and defining collision or activation areas. TilePix should make those activities a trivially short amount of time.

Pixel

This library is a complement to the Pixel 2D games library, and is largely inspired by it. A huge thanks to faiface for one, giving us access to such a fantastic library; and two, providing the inspiration for this library!

TilePix would not have been possible without the great amount of care and effort that has been put into Pixel.

Pixel is subject to the MIT licence.

Stability

TilePix is a work-in-progress project; as such, expect bugs and missing features. If you notice a bug or a feature you feel is missing, please consider contributing - simply (and correctly) raising issues is just as valuable as writing code!

Releases

The aim is that releases on this library will fairly regular, and well planned. You can use Go modules with TilePix if you want version security.

Example

Here is a very basic example of using the library. It is advisable to view the excellent Pixel tutorials before trying to understand this package, as TilePix is very Pixel centric.

package main

import (
	"image/color"

	// We must use blank imports for any image formats in the tileset image sources.
	// You will get an error if a blank import is not made; TilePix does not import
	// specific image formats, that is the responsibility of the calling code.
	_ "image/png"

	"github.com/bcvery1/tilepix"

	"github.com/faiface/pixel"
	"github.com/faiface/pixel/pixelgl"
)

func run() {
	cfg := pixelgl.WindowConfig{
		Title: "TilePix",
		Bounds: pixel.R(0, 0, 640, 320),
		VSync: true,
	}

	win, err := pixelgl.NewWindow(cfg)
	if err != nil {
		panic(err)
	}

	// Load and initialise the map.
	m, err := tilepix.ReadFile("myMap.tmx")
	if err != nil {
		panic(err)
	}

	for !win.Closed() {
		win.Clear(color.White)

		// Draw all layers to the window.
		if err := m.DrawAll(win, color.White, pixel.IM); err != nil {
			panic(err)
		}

		win.Update()
	}
}

func main() {
	pixelgl.Run(run)
}

Futher examples can be found in the examples directory.

Contributing

Thanks for considering contributing to TilePix; for details on how you can contribute, please consult the contribution guide.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrUnknownEncoding       = errors.New("tmx: invalid encoding scheme")
	ErrUnknownCompression    = errors.New("tmx: invalid compression method")
	ErrInvalidDecodedDataLen = errors.New("tmx: invalid decoded data length")
	ErrInvalidGID            = errors.New("tmx: invalid GID")
	ErrInvalidObjectType     = errors.New("tmx: the object type requested does not match this object")
	ErrInvalidPointsField    = errors.New("tmx: invalid points string")
	ErrInfiniteMap           = errors.New("tmx: infinite maps are not currently supported")
)

Errors which are returned from various places in the package.

View Source
var (
	// NilTile is a tile with no tile set.  Will be skipped over when drawing.
	NilTile = &DecodedTile{Nil: true}
)

Functions

This section is empty.

Types

type Data

type Data struct {
	Encoding    string `xml:"encoding,attr"`
	Compression string `xml:"compression,attr"`
	RawData     []byte `xml:",innerxml"`
	// DataTiles is only used when layer encoding is XML.
	DataTiles []*DataTile `xml:"tile"`
}

Data is a TMX file structure holding data.

func (*Data) String

func (d *Data) String() string

type DataTile

type DataTile struct {
	GID GID `xml:"gid,attr"`
}

DataTile is a tile from a data object.

type DecodedTile

type DecodedTile struct {
	ID             ID
	Tileset        *Tileset
	HorizontalFlip bool
	VerticalFlip   bool
	DiagonalFlip   bool
	Nil            bool
	// contains filtered or unexported fields
}

DecodedTile is a convenience struct, which stores the decoded data from a Tile.

func (*DecodedTile) Draw

func (t *DecodedTile) Draw(ind, columns, numRows int, ts *Tileset, target pixel.Target, offset pixel.Vec)

Draw will draw the tile to the target provided. This will calculate the sprite from the provided tileset and set the DecodedTiles' internal `sprite` property; this is so it is only calculated the first time.

func (*DecodedTile) IsNil

func (t *DecodedTile) IsNil() bool

IsNil returns whether this tile is nil. If so, it means there is nothing set for the tile, and should be skipped in drawing.

func (DecodedTile) Position

func (t DecodedTile) Position(ind int, ts *Tileset) pixel.Vec

Position returns the relative game position.

func (*DecodedTile) String

func (t *DecodedTile) String() string

type GID

type GID uint32

GID is a global tile ID. Tiles can use GID or ID.

type ID

type ID uint32

ID is a tile ID. Tiles can use GID or ID.

type Image

type Image struct {
	Source string `xml:"source,attr"`
	Trans  string `xml:"trans,attr"`
	Width  int    `xml:"width,attr"`
	Height int    `xml:"height,attr"`
	// contains filtered or unexported fields
}

Image is a TMX file structure which referencing an image file, with associated properies.

func (*Image) String

func (i *Image) String() string

type ImageLayer

type ImageLayer struct {
	Locked  bool    `xml:"locked,attr"`
	Name    string  `xml:"name,attr"`
	OffSetX float64 `xml:"offsetx,attr"`
	OffSetY float64 `xml:"offsety,attr"`
	Opacity float64 `xml:"opacity,attr"`
	Image   *Image  `xml:"image"`
	// contains filtered or unexported fields
}

ImageLayer is a TMX file structure which references an image layer, with associated properties.

func (*ImageLayer) Draw

func (im *ImageLayer) Draw(target pixel.Target, mat pixel.Matrix) error

Draw will draw the image layer to the target provided, shifted with the provided matrix.

func (*ImageLayer) String

func (im *ImageLayer) String() string

type Map

type Map struct {
	Version     string `xml:"title,attr"`
	Orientation string `xml:"orientation,attr"`
	// Width is the number of tiles - not the width in pixels
	Width int `xml:"width,attr"`
	// Height is the number of tiles - not the height in pixels
	Height       int            `xml:"height,attr"`
	TileWidth    int            `xml:"tilewidth,attr"`
	TileHeight   int            `xml:"tileheight,attr"`
	Properties   []*Property    `xml:"properties>property"`
	Tilesets     []*Tileset     `xml:"tileset"`
	TileLayers   []*TileLayer   `xml:"layer"`
	ObjectGroups []*ObjectGroup `xml:"objectgroup"`
	Infinite     bool           `xml:"infinite,attr"`
	ImageLayers  []*ImageLayer  `xml:"imagelayer"`

	Canvas_ *pixelgl.Canvas
	// contains filtered or unexported fields
}

Map is a TMX file structure representing the map as a whole.

func Read

func Read(r io.Reader, dir string) (*Map, error)

Read will read, decode and initialise a Tiled Map from a data reader.

func ReadFile

func ReadFile(filePath string) (*Map, error)

ReadFile will read, decode and initialise a Tiled Map from a file path.

func (*Map) Bounds

func (m *Map) Bounds() pixel.Rect

Bounds will return a pixel rectangle representing the width-height in pixels.

func (*Map) Centre

func (m *Map) Centre() pixel.Vec

Centre will return a pixel vector reprensenting the center of the bounds.

func (*Map) DrawAll

func (m *Map) DrawAll(target pixel.Target, clearColour color.Color, mat pixel.Matrix) error

DrawAll will draw all tile layers and image layers to the target. Tile layers are first draw to their own `pixel.Batch`s for efficiency. All layers are drawn to a `pixel.Canvas` before being drawn to the target.

- target - The target to draw layers to. - clearColour - The colour to clear the maps' canvas before drawing. - mat - The matrix to draw the canvas to the target with.

func (*Map) GenerateTileObjectLayer

func (m *Map) GenerateTileObjectLayer() error

GenerateTileObjectLayer will create an object layer which contains all objects as defined by individual tiles.

func (*Map) GetImageLayerByName

func (m *Map) GetImageLayerByName(name string) *ImageLayer

GetImageLayerByName returns a Map's ImageLayer by its name

func (*Map) GetObjectByName

func (m *Map) GetObjectByName(name string) []*Object

GetObjectByName returns the Maps' Objects by their name

func (*Map) GetObjectLayerByName

func (m *Map) GetObjectLayerByName(name string) *ObjectGroup

GetObjectLayerByName returns a Map's ObjectGroup by its name

func (*Map) GetTileLayerByName

func (m *Map) GetTileLayerByName(name string) *TileLayer

GetTileLayerByName returns a Map's TileLayer by its name

func (*Map) String

func (m *Map) String() string

type Object

type Object struct {
	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"`
	GID        ID          `xml:"gid,attr"`
	ID         ID          `xml:"id,attr"`
	Visible    bool        `xml:"visible,attr"`
	Polygon    *Polygon    `xml:"polygon"`
	PolyLine   *PolyLine   `xml:"polyline"`
	Properties []*Property `xml:"properties>property"`
	Ellipse    *struct{}   `xml:"ellipse"`
	Point      *struct{}   `xml:"point"`
	// contains filtered or unexported fields
}

Object is a TMX file struture holding a specific Tiled object.

func (*Object) GetEllipse

func (o *Object) GetEllipse() (pixel.Circle, error)

GetEllipse will return a pixel.Circle representation of this object relative to the map (the co-ordinates will match those as drawn in Tiled). If the object type is not `EllipseObj` this function will return `pixel.C(pixel.ZV, 0)` and an error.

Because there is no pixel geometry code for irregular ellipses, this function will average the width and height of the ellipse object from the TMX file, and return a regular circle about the centre of the ellipse.

func (*Object) GetPoint

func (o *Object) GetPoint() (pixel.Vec, error)

GetPoint will return a pixel.Vec representation of this object relative to the map (the co-ordinates will match those as drawn in Tiled). If the object type is not `PointObj` this function will return `pixel.ZV` and an error.

func (*Object) GetPolyLine

func (o *Object) GetPolyLine() ([]pixel.Vec, error)

GetPolyLine will return a pixel.Vec slice representation of this object relative to the map (the co-ordinates will match those as drawn in Tiled). If the object type is not `PolylineObj` this function will return `nil` and an error.

func (*Object) GetPolygon

func (o *Object) GetPolygon() ([]pixel.Vec, error)

GetPolygon will return a pixel.Vec slice representation of this object relative to the map (the co-ordinates will match those as drawn in Tiled). If the object type is not `PolygonObj` this function will return `nil` and an error.

func (*Object) GetRect

func (o *Object) GetRect() (pixel.Rect, error)

GetRect will return a pixel.Rect representation of this object relative to the map (the co-ordinates will match those as drawn in Tiled). If the object type is not `RectangleObj` this function will return `pixel.R(0, 0, 0, 0)` and an error.

func (*Object) GetTile

func (o *Object) GetTile() (*DecodedTile, error)

GetTile will return the object decoded into a DecodedTile struct. If this object is not a DecodedTile, this function will return `nil` and an error.

func (*Object) GetType

func (o *Object) GetType() ObjectType

GetType will return the ObjectType constant type of this object.

func (*Object) String

func (o *Object) String() string

type ObjectGroup

type ObjectGroup struct {
	Name       string      `xml:"name,attr"`
	Color      string      `xml:"color,attr"`
	OffSetX    float64     `xml:"offsetx,attr"`
	OffSetY    float64     `xml:"offsety,attr"`
	Opacity    float32     `xml:"opacity,attr"`
	Visible    bool        `xml:"visible,attr"`
	Properties []*Property `xml:"properties>property"`
	Objects    []*Object   `xml:"object"`
	// contains filtered or unexported fields
}

ObjectGroup is a TMX file structure holding a Tiled ObjectGroup.

func (*ObjectGroup) GetObjectByName

func (og *ObjectGroup) GetObjectByName(name string) []*Object

GetObjectByName returns the ObjectGroups' Objects by their name

func (*ObjectGroup) String

func (og *ObjectGroup) String() string

type ObjectType

type ObjectType int

ObjectType is used to represent the types an object can be.

const (
	EllipseObj ObjectType = iota
	PolygonObj
	PolylineObj
	RectangleObj
	PointObj
	TileObj
)

These are the currently supported object types.

func (ObjectType) String

func (o ObjectType) String() string

type Point

type Point struct {
	X int
	Y int
	// contains filtered or unexported fields
}

Point is a TMX file structure holding a Tiled Point object.

func (*Point) String

func (p *Point) String() string

func (*Point) V

func (p *Point) V() pixel.Vec

V converts the Tiled Point to a Pixel Vector.

type PolyLine

type PolyLine struct {
	Points string `xml:"points,attr"`
	// contains filtered or unexported fields
}

PolyLine is a TMX file structure representing a Tiled Polyline.

func (*PolyLine) Decode

func (p *PolyLine) Decode() ([]*Point, error)

Decode will return a slice of points which make up this polyline.

func (*PolyLine) String

func (p *PolyLine) String() string

type Polygon

type Polygon struct {
	Points string `xml:"points,attr"`
	// contains filtered or unexported fields
}

Polygon is a TMX file structure representing a Tiled Polygon.

func (*Polygon) Decode

func (p *Polygon) Decode() ([]*Point, error)

Decode will return a slice of points which make up this polygon.

func (*Polygon) String

func (p *Polygon) String() string

type Property

type Property struct {
	Name  string `xml:"name,attr"`
	Value string `xml:"value,attr"`
	// contains filtered or unexported fields
}

Property is a TMX file structure which holds a Tiled property.

func (*Property) String

func (p *Property) String() string

type Tile

type Tile struct {
	ID    ID     `xml:"id,attr"`
	Image *Image `xml:"image"`
	// ObjectGroup is set if objects have been added to individual sprites in Tiled.
	ObjectGroup *ObjectGroup `xml:"objectgroup,omitempty"`
	// contains filtered or unexported fields
}

Tile is a TMX file structure which holds a Tiled tile.

func (*Tile) String

func (t *Tile) String() string

type TileLayer

type TileLayer struct {
	Name       string      `xml:"name,attr"`
	Opacity    float32     `xml:"opacity,attr"`
	OffSetX    float64     `xml:"offsetx,attr"`
	OffSetY    float64     `xml:"offsety,attr"`
	Visible    bool        `xml:"visible,attr"`
	Properties []*Property `xml:"properties>property"`
	Data       Data        `xml:"data"`
	// DecodedTiles is the attribute you should use instead of `Data`.
	// Tile entry at (x,y) is obtained using l.DecodedTiles[y*map.Width+x].
	DecodedTiles []*DecodedTile
	// Tileset is only set when the layer uses a single tileset and NilLayer is false.
	Tileset *Tileset
	// Empty should be set when all entries of the layer are NilTile.
	Empty bool

	Batch_     *pixel.Batch
	IsDirty    bool
	StaticBool bool
	// contains filtered or unexported fields
}

TileLayer is a TMX file structure which can hold any type of Tiled layer.

func (*TileLayer) Batch

func (l *TileLayer) Batch() (*pixel.Batch, error)

Batch returns the batch with the picture data from the tileset associated with this layer.

func (*TileLayer) Draw

func (l *TileLayer) Draw(target pixel.Target) error

Draw will use the TileLayers' batch to draw all tiles within the TileLayer to the target.

func (*TileLayer) SetDirty

func (l *TileLayer) SetDirty(newVal bool)

SetDirty will update the TileLayers' `dirty` property. If true, this will cause the TileLayers' batch be cleared and re-drawn next time `TileLayer.Draw` is called.

func (*TileLayer) SetStatic

func (l *TileLayer) SetStatic(newVal bool)

SetStatic will update the TileLayers' `static` property. If false, this will set the dirty property to true each time after `TileLayer.Draw` is called, so that the layer is drawn everytime.

func (*TileLayer) String

func (l *TileLayer) String() string

type Tileset

type Tileset struct {
	FirstGID   GID         `xml:"firstgid,attr"`
	Source     string      `xml:"source,attr"`
	Name       string      `xml:"name,attr"`
	TileWidth  int         `xml:"tilewidth,attr"`
	TileHeight int         `xml:"tileheight,attr"`
	Spacing    int         `xml:"spacing,attr"`
	Margin     int         `xml:"margin,attr"`
	Properties []*Property `xml:"properties>property"`
	Image      *Image      `xml:"image"`
	Tiles      []*Tile     `xml:"tile"`
	Tilecount  int         `xml:"tilecount,attr"`
	Columns    int         `xml:"columns,attr"`
	// contains filtered or unexported fields
}

Tileset is a TMX file structure which represents a Tiled Tileset

func (Tileset) GenerateTileObjectLayer

func (ts Tileset) GenerateTileObjectLayer(tileLayers []*TileLayer) ObjectGroup

GenerateTileObjectLayer will create a new ObjectGroup for the mapping of Objects to individual tiles.

func (*Tileset) String

func (ts *Tileset) String() string

func (Tileset) TileObjects

func (ts Tileset) TileObjects() map[ID]*ObjectGroup

TileObjects will return all ObjectGroups contained in Tiles.

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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