level

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Sep 15, 2020 License: GPL-3.0 Imports: 6 Imported by: 0

Documentation

Overview

Package level deals with the reading, organisation and rendering of a level on screen. The following is an example of a level file, stored as JSON:

{
    "version": "1",
    "levels": [
        {
            "name": "level 1",
            "layers": [
                {
                    "name": "background",
                    "image": {
                        "path": "",
                        "extra": {}
                    },
                    "grid": {
                        "assets": {
                            "path": "",
                            "quantity": 5,
                            "offset": {
                                "x": 0,
                                "y": 0
                            }
                            "width": 16,
                            "height": 16
                        },
                        "tiles": [
                            {
                                "asset": 0,
                                "x": 10,
                                "y": 10,
                                "extra": ""
                            }
                        ],
                        "extra": {}
                    },
                    "bounds": [
                        {
                            "type": "box",
                            "dimensions": {
                                "x": 10,
                                "y": 10,
                                "width": 20,
                                "height": 20
                            },
                            "extra": ""
                        }
                    ]
                }
            ],
            "extra": ""
        }
    ]
}

Index

Constants

View Source
const (
	ErrorWrongFileFormat     = "Loaded levels file is not a valid JSON"
	ErrorVersionNotSupported = "Version \"%s\" not supported"
	ErrorNoLevels            = "Levels file must have at least one level declared, none found"
)

Returned errors

Variables

This section is empty.

Functions

func Deserialize

func Deserialize(r io.Reader) (map[string]Level, error)

Deserialize validates a levels file and returns its information as a []Level

Types

type Grid

type Grid struct {
	TileWidth  float64
	TileHeight float64
	Assets     []*pixel.Sprite
	Tiles      []GridTile
}

Grid divides the screen in tiles of tileWidth * tileHeight size

func NewGrid

func NewGrid(w, h float64) *Grid

NewGrid returns a new Grid instance

func (*Grid) ToPixels

func (g *Grid) ToPixels(coords pixel.Vec) pixel.Vec

ToPixels transforms grid coordinates of a tile to its center coordinates in pixels.

type GridAssets

type GridAssets struct {
	Path     string
	Quantity int
	Offset   struct {
		X float64
		Y float64
	}
	Width  float64
	Height float64
}

type GridTile

type GridTile struct {
	Asset  int
	Coords pixel.Vec
}

GridTile holds data of a single tile

type Layer

type Layer struct {
	Grid *Grid
	// contains filtered or unexported fields
}

Layer contains the different structs a layer can hold and show on screen

type Level

type Level struct {
	Limits pixel.Rect
	Layers map[string]Layer
}

Level holds the information needed to build a level

func (Level) Draw

func (l Level) Draw(target pixel.Target)

Draw renders the level following layers order

type LevelsFile

type LevelsFile struct {
	Version string
	Levels  map[string]struct {
		Limits struct {
			Min pixel.Vec
			Max pixel.Vec
		}
		Layers map[string]struct {
			Image struct {
				Path string
			}
			Grid struct {
				Assets GridAssets
				Tiles  []struct {
					Asset int
					X     float64
					Y     float64
				}
			}
		}
	}
}

LevelsFile is the serialized form of Levels

Jump to

Keyboard shortcuts

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