Documentation
¶
Index ¶
- Constants
- Variables
- func ChunkInZipfile(zf *zip.Reader, layer int, coord render.Point) bool
- func ChunksInZipfile(zf *zip.Reader, layer int) []render.Point
- func FromRelativeCoordinate(rel render.Point, chunkCoord render.Point, chunkSize uint8) render.Point
- func ListSystemLevels() ([]string, error)
- func RelativeCoordinate(abs render.Point, chunkCoord render.Point, chunkSize uint8) render.Point
- type Accessor
- type Actor
- type ActorMap
- type Base
- type Chunk
- func (c *Chunk) CachedBitmap(mask render.Color) image.Image
- func (c *Chunk) Delete(p render.Point) error
- func (c *Chunk) IsModified() bool
- func (c *Chunk) MarshalBinary() ([]byte, error)
- func (c *Chunk) Rect() render.Rect
- func (c *Chunk) Set(p render.Point, sw *Swatch) error
- func (c *Chunk) SetDirty()
- func (c *Chunk) SizePositive() render.Rect
- func (c *Chunk) Teardown() int
- func (c *Chunk) Texture(e render.Engine) render.Texturer
- func (c *Chunk) TextureMasked(e render.Engine, mask render.Color) render.Texturer
- func (c *Chunk) ToBitmap(mask render.Color) image.Image
- func (c *Chunk) ToZipfile(zf *zip.Writer, layer int, coord render.Point) error
- func (c *Chunk) UnmarshalBinary(b []byte) error
- func (c *Chunk) UnmarshalJSON(b []byte) error
- func (c *Chunk) Usage() float64
- type ChunkMap
- type Chunker
- func (c *Chunker) Bounds() (low, high render.Point)
- func (c *Chunker) CacheSize() int
- func (c *Chunker) ChunkCoordinate(abs render.Point) render.Point
- func (c *Chunker) ChunkFromZipfile(coord render.Point) (*Chunk, error)
- func (c *Chunker) ClearChunkCache()
- func (c *Chunker) Delete(p render.Point) error
- func (c *Chunker) DeleteRect(r render.Rect) error
- func (c *Chunker) FreeCaches() int
- func (c *Chunker) FreeChunk(p render.Point) bool
- func (c *Chunker) GCSize() int
- func (c *Chunker) Get(p render.Point) (*Swatch, error)
- func (c *Chunker) GetChunk(p render.Point) (*Chunk, bool)
- func (c *Chunker) Inflate(pal *Palette) error
- func (c *Chunker) IterCachedChunks() <-chan *Chunk
- func (c *Chunker) IterChunks() <-chan render.Point
- func (c *Chunker) IterChunksThemselves() <-chan *Chunk
- func (c *Chunker) IterPixels() <-chan Pixel
- func (c *Chunker) IterViewport(viewport render.Rect) <-chan Pixel
- func (c *Chunker) IterViewportChunks(viewport render.Rect) <-chan render.Point
- func (c *Chunker) MigrateZipfile(zf *zip.Writer) error
- func (c *Chunker) OptimizeChunkerAccessors()
- func (c *Chunker) Prerender()
- func (c *Chunker) PrerenderN(n int) (remaining int)
- func (c *Chunker) RawChunkFromZipfile(coord render.Point) (string, []byte, error)
- func (c *Chunker) Redraw()
- func (c *Chunker) Set(p render.Point, sw *Swatch) error
- func (c *Chunker) SetChunk(p render.Point, chunk *Chunk)
- func (c *Chunker) SetRect(r render.Rect, sw *Swatch) error
- func (c *Chunker) WorldSize() render.Rect
- func (c *Chunker) WorldSizePositive() render.Rect
- type File
- type FileSystem
- func (fs *FileSystem) Checksum(filename string) (string, error)
- func (fs *FileSystem) Delete(filename string)
- func (fs *FileSystem) Exists(filename string) bool
- func (fs *FileSystem) Get(filename string) ([]byte, error)
- func (fs *FileSystem) List() []string
- func (fs *FileSystem) ListPrefix(prefix string) []string
- func (fs *FileSystem) MigrateZipfile(zf *zip.Writer) error
- func (fs *FileSystem) Set(filename string, data []byte)
- func (fs *FileSystem) UnmarshalJSON(text []byte) error
- type GameRule
- type JSONChunk
- type Level
- func (m *Level) AsJSON() ([]byte, error)
- func (l *Level) DeleteFile(filename string) bool
- func (l *Level) DeleteFiles(prefix string) int
- func (l *Level) GetFile(filename string) ([]byte, error)
- func (lvl *Level) GetScreenshotImage(filename string) (image.Image, error)
- func (lvl *Level) GetScreenshotImageAsUIImage(filename string) (*ui.Image, error)
- func (lvl *Level) HasScreenshot() bool
- func (l *Level) Inflate()
- func (l *Level) ListFiles() []string
- func (l *Level) ListFilesAt(prefix string) []string
- func (m *Level) Loop() error
- func (m *Level) PruneLinks() int
- func (m *Level) ReloadZipfile(data []byte) error
- func (l *Level) ReplacePalette(pal *Palette)
- func (l *Level) SetFile(filename string, data []byte)
- func (m *Level) Teardown()
- func (m *Level) ToGzip() ([]byte, error)
- func (m *Level) ToJSON() ([]byte, error)
- func (m *Level) ToZipfile() ([]byte, error)
- func (m *Level) Vacuum() error
- func (m *Level) WriteFile(filename string) error
- func (m *Level) WriteJSON(filename string) error
- type MapAccessor
- func (a *MapAccessor) Delete(p render.Point) error
- func (a *MapAccessor) Get(p render.Point) (*Swatch, error)
- func (a *MapAccessor) Inflate(pal *Palette) error
- func (a *MapAccessor) Iter() <-chan Pixel
- func (a *MapAccessor) IterViewport(viewport render.Rect) <-chan Pixel
- func (a *MapAccessor) Len() int
- func (a *MapAccessor) MarshalBinary() ([]byte, error)
- func (a *MapAccessor) MarshalJSON() ([]byte, error)
- func (a *MapAccessor) Reset()
- func (a *MapAccessor) Set(p render.Point, sw *Swatch) error
- func (a *MapAccessor) UnmarshalBinary(compressed []byte) error
- func (a *MapAccessor) UnmarshalJSON(b []byte) error
- type Option
- type PageType
- type Palette
- type Pixel
- type RLEAccessor
- func (a *RLEAccessor) Delete(p render.Point) error
- func (a *RLEAccessor) FromMapAccessor(ma *MapAccessor) *RLEAccessor
- func (a *RLEAccessor) Get(p render.Point) (*Swatch, error)
- func (a *RLEAccessor) Inflate(pal *Palette) error
- func (a *RLEAccessor) Iter() <-chan Pixel
- func (a *RLEAccessor) IterViewport(viewport render.Rect) <-chan Pixel
- func (a *RLEAccessor) Len() int
- func (a *RLEAccessor) MarshalBinary() ([]byte, error)
- func (a *RLEAccessor) Set(p render.Point, sw *Swatch) error
- func (a *RLEAccessor) UnmarshalBinary(compressed []byte) error
- type Swatch
Constants ¶
const ( MapType uint64 = iota RLEType )
Types of chunks.
const DefaultChunkType = RLEType
Default chunk type for newly created chunks (was MapType).
const PaletteSizeLimit = 256
Palettes are limited to uint8 in length, to aid image compression.
Variables ¶
var ( DefaultPaletteNames = []string{ "Default", "Colored Pencil", "Blueprint", "Neon Bright", } DefaultPalettes = map[string]*Palette{ "Default": { Swatches: []*Swatch{ { Name: "solid", Color: render.MustHexColor("#777"), Solid: true, Pattern: "noise.png", }, { Name: "decoration", Color: render.MustHexColor("#CCC"), Pattern: "noise.png", }, { Name: "fire", Color: render.Red, Fire: true, Pattern: "marker.png", }, { Name: "water", Color: render.MustHexColor("#09F"), Water: true, Pattern: "bubbles.png", }, { Name: "semisolid", Color: render.MustHexColor("#AAA"), SemiSolid: true, Pattern: "ink.png", }, { Name: "hint", Color: render.MustHexColor("#F0F"), Pattern: "marker.png", }, }, }, "Colored Pencil": { Swatches: []*Swatch{ { Name: "darkstone", Color: render.MustHexColor("#777"), Pattern: "noise.png", Solid: true, }, { Name: "grass", Color: render.DarkGreen, Solid: true, Pattern: "noise.png", }, { Name: "dirt", Color: render.MustHexColor("#960"), Solid: true, Pattern: "noise.png", }, { Name: "stone", Color: render.Grey, Solid: true, Pattern: "noise.png", }, { Name: "sandstone", Color: render.RGBA(215, 114, 44, 255), Solid: true, Pattern: "perlin-noise.png", }, { Name: "planks", Color: render.MustHexColor("#B80"), SemiSolid: true, Pattern: "marker.png", }, { Name: "fire", Color: render.Red, Fire: true, Pattern: "marker.png", }, { Name: "water", Color: render.RGBA(0, 153, 255, 255), Water: true, Pattern: "bubbles.png", }, { Name: "ice", Color: render.MustHexColor("#099"), Solid: true, Slippery: true, Pattern: "marker.png", }, { Name: "hint", Color: render.MustHexColor("#F0F"), Pattern: "marker.png", }, }, }, "Neon Bright": { Swatches: []*Swatch{ { Name: "ground", Color: render.MustHexColor("#FFE"), Solid: true, Pattern: "noise.png", }, { Name: "grass green", Color: render.Green, Solid: true, Pattern: "noise.png", }, { Name: "fire", Color: render.MustHexColor("#F90"), Pattern: "marker.png", }, { Name: "electricity", Color: render.Yellow, SemiSolid: true, Pattern: "perlin.png", }, { Name: "water", Color: render.MustHexColor("#09F"), Pattern: "bubbles.png", }, { Name: "ice blue", Color: render.MustHexColor("#0FF"), Solid: true, Slippery: true, Pattern: "marker.png", }, { Name: "hint", Color: render.Magenta, Pattern: "marker.png", }, }, }, "Blueprint": { Swatches: []*Swatch{ { Name: "solid", Color: render.RGBA(254, 254, 254, 255), Solid: true, Pattern: "noise.png", }, { Name: "decoration", Color: render.Grey, Pattern: "noise.png", }, { Name: "fire", Color: render.RGBA(255, 80, 0, 255), Fire: true, Pattern: "bubbles.png", }, { Name: "water", Color: render.RGBA(0, 153, 255, 255), Water: true, Pattern: "bubbles.png", }, { Name: "ice", Color: render.MustHexColor("#FAF"), Solid: true, Slippery: true, Pattern: "marker.png", }, { Name: "electric", Color: render.RGBA(255, 255, 0, 255), SemiSolid: true, Pattern: "marker.png", }, { Name: "hint", Color: render.MustHexColor("#F0F"), Pattern: "marker.png", }, }, }, } )
Some choice of palettes.
var (
DefaultWallpaper = "notebook.png"
)
Useful variables.
Functions ¶
func ChunkInZipfile ¶
ChunkInZipfile tests whether the chunk exists in the zipfile.
func ChunksInZipfile ¶
ChunksInZipfile returns the list of chunk coordinates in a zipfile.
func FromRelativeCoordinate ¶
func FromRelativeCoordinate(rel render.Point, chunkCoord render.Point, chunkSize uint8) render.Point
FromRelativeCoordinate is the inverse of RelativeCoordinate.
With a chunk size of 128 and a relative coordinate like (8, 12), this function will return the absolute world coordinates based on your chunk.Point's placement in the level.
func ListSystemLevels ¶
ListSystemLevels returns a list of built-in levels.
func RelativeCoordinate ¶
RelativeCoordinate will translate from an absolute world coordinate, into one that is relative to fit inside of the chunk with the given chunk coordinate and size.
Example:
- With 128x128 chunks and a world coordinate of (280,-600) - The ChunkCoordinate would be (2,-4) which encompasses (256,-512) to (383,-639) - And relative inside that chunk, the pixel is at (24,)
Types ¶
type Accessor ¶
type Accessor interface { Inflate(*Palette) error Iter() <-chan Pixel IterViewport(viewport render.Rect) <-chan Pixel Get(render.Point) (*Swatch, error) Set(render.Point, *Swatch) error Delete(render.Point) error Len() int MarshalBinary() ([]byte, error) UnmarshalBinary([]byte) error }
Accessor provides a high-level API to interact with absolute pixel coordinates while abstracting away the details of how they're stored.
type Actor ¶
type Actor struct { Filename string `json:"filename"` // like "exit.doodad" Point render.Point `json:"point"` Links []string `json:"links,omitempty"` // IDs of linked actors Options map[string]*Option `json:"options,omitempty"` // contains filtered or unexported fields }
Actor is an instance of a Doodad in the level.
type ActorMap ¶
ActorMap holds the doodad information by their ID in the level data.
func (ActorMap) Add ¶
Add a new Actor to the map. If it doesn't already have an ID it will be given a random UUIDv4 ID.
type Base ¶
type Base struct { Version int `json:"version"` // File format version spec. GameVersion string `json:"gameVersion"` // Game version that created the level. Title string `json:"title"` Author string `json:"author"` Locked bool `json:"locked"` // v2 level format: zip files with external chunks. // (v0 was json text, v1 was gzip compressed json text). // The game must load levels created using the previous // formats, they will not have a Zipfile and will have // Chunkers in memory from their (gz) json. Zipfile *zip.Reader `json:"-"` // Every drawing type is able to embed other files inside of itself. Files *FileSystem `json:"files,omitempty"` }
Base provides the common struct keys that are shared between Levels and Doodads.
type Chunk ¶
type Chunk struct { Type uint64 // map vs. 2D array. Accessor // Values told to it from higher up, not stored in JSON. Point render.Point Size uint8 // contains filtered or unexported fields }
Chunk holds a single portion of the pixel canvas.
func (*Chunk) CachedBitmap ¶
CachedBitmap returns a cached render of the chunk as a bitmap image.
This is like Texture() but skips the step of actually producing an (SDL2) texture. The benefit of this is that you can call it from your non-main threads and offload the bitmap work into background tasks, then when SDL2 needs the Texture, the cached bitmap is immediately there saving time on the main thread.
func (*Chunk) Delete ¶
Delete proxies to the accessor and flags the texture as dirty and marks the chunk "Modified".
func (*Chunk) IsModified ¶
IsModified returns the chunk's Modified flag. This is most likely to occur in the Editor when the user is drawing onto the level. Modified chunks are not unloaded from memory ever, until they can be saved back to disk in the Zipfile format. During regular gameplay, chunks are loaded and unloaded as needed.
The modified flag is flipped on Set() or Delete() and is never unflipped. On file save, the Chunker is reloaded from scratch to hold chunks cached from zipfile members.
func (*Chunk) MarshalBinary ¶
MarshalBinary encodes a chunk to binary format.
The binary format consists of one Uvarint for the chunk Type and then followed by whatever binary representation that chunk type encodes its data with.
func (*Chunk) Set ¶
Set proxies to the accessor and flags the texture as dirty.
It also marks the chunk as "Modified" so it will be kept in memory until the drawing is next saved to disk and the chunk written out to the zipfile.
func (*Chunk) SetDirty ¶
func (c *Chunk) SetDirty()
SetDirty sets the `dirty` flag to true and forces the texture to be re-computed next frame.
func (*Chunk) SizePositive ¶
SizePositive returns the Size anchored to 0,0 with only positive coordinates.
func (*Chunk) Teardown ¶
Teardown the chunk and free (SDL2) texture memory in ways Go can not by itself. Returns the number of textures freed.
func (*Chunk) Texture ¶
Texture will return a cached texture for the rendering engine for this chunk's pixel data. If the cache is dirty it will be rebuilt in this func.
Texture cache can be disabled with balance.DisableChunkTextureCache=true.
func (*Chunk) TextureMasked ¶
TextureMasked returns a cached texture with the ColorMask applied.
func (*Chunk) ToBitmap ¶
ToBitmap exports the chunk's pixels as a bitmap image. NOT CACHED! This will always run the logic. Use Bitmap() if you want a cached bitmap image that only generates itself once, and again when marked dirty.
func (*Chunk) ToZipfile ¶
ToZipfile writes just a chunk's data into a zipfile.
It will write a file like "chunks/{layer}/{coord}.json" if using JSON format or a .bin file for binary format based on the BinaryChunkerEnabled game config constant.
func (*Chunk) UnmarshalBinary ¶
UnmarshalBinary decodes a chunk from binary format.
func (*Chunk) UnmarshalJSON ¶
UnmarshalJSON loads the chunk from JSON and uses the correct accessor to parse the inner details.
DEPRECATED in favor of binary marshalling.
Only supports MapAccessor chunk types, which was the only one supported before this function was deprecated.
type ChunkMap ¶
ChunkMap maps a chunk coordinate to its chunk data.
func (ChunkMap) MarshalJSON ¶
MarshalJSON to convert the chunk map to JSON. This is needed for writing so the JSON encoder knows how to serializes a `map[Point]*Chunk` but the inverse is not necessary to implement.
type Chunker ¶
type Chunker struct { // Layer is optional for the caller, levels use only 0 and // doodads use them for frames. When chunks are exported to // zipfile the Layer keeps them from overlapping. Layer int `json:"-"` // internal use only Size uint8 `json:"size"` // A Zipfile reference for new-style levels and doodads which // keep their chunks in external parts of a zip file. Zipfile *zip.Reader `json:"-"` // Chunks, oh boy. // The v1 drawing format had all the chunks in the JSON file. // New drawings write them to zips. Legacy drawings can be converted // simply by loading and resaving: their Chunks loads from JSON and // is committed to zipfile on save. This makes Chunks also a good // cache even when we have a zipfile to fall back on. Chunks ChunkMap `json:"chunks"` // contains filtered or unexported fields }
Chunker is the data structure that manages the chunks of a level, and provides the API to interact with the pixels using their absolute coordinates while abstracting away the underlying details.
func NewChunker ¶
NewChunker creates a new chunk manager with a given chunk size.
func (*Chunker) Bounds ¶
Bounds returns the boundary points of the lowest and highest chunk which have any data in them.
func (*Chunker) ChunkCoordinate ¶
ChunkCoordinate computes a chunk coordinate from an absolute coordinate.
func (*Chunker) ChunkFromZipfile ¶
ChunkFromZipfile loads a chunk from a zipfile.
func (*Chunker) ClearChunkCache ¶
func (c *Chunker) ClearChunkCache()
ClearChunkCache completely flushes the ChunkMap from memory. BE CAREFUL. If the level is a Zipfile the chunks will reload as needed, but old style levels this will nuke the whole drawing!
func (*Chunker) DeleteRect ¶
DeleteRect deletes a rectangle of pixels between two points. The rect is a relative one with a width and height, and the X,Y values are an absolute world coordinate.
func (*Chunker) FreeCaches ¶
FreeCaches unloads chunks that have not been requested in 2 frames.
Only on chunkers that have zipfiles, old-style levels without zips wouldn't be able to restore their chunks otherwise! Returns -1 if no Zipfile, otherwise number of chunks freed.
func (*Chunker) FreeChunk ¶
FreeChunk unloads a chunk from active memory for zipfile-backed levels.
Not thread safe: it is assumed the caller has the lock on c.Chunks.
func (*Chunker) GCSize ¶
GCSize returns the number of chunks pending free (not accessed in 2+ ticks)
func (*Chunker) Get ¶
Get a pixel at the given coordinate. Returns the Palette entry for that pixel or else returns an error if not found.
func (*Chunker) GetChunk ¶
GetChunk gets a chunk at a certain position. Returns false if not found.
This should be the centralized function to request a Chunk from the Chunker (or IterChunksThemselves). On old-style levels all of the chunks were just in memory as part of the JSON struct, in Zip files we can load/unload them at will from external files.
func (*Chunker) Inflate ¶
Inflate iterates over the pixels in the (loaded) chunks and expands any Sparse Swatches (which have only their palette index, from the file format on disk) to connect references to the swatches in the palette.
func (*Chunker) IterCachedChunks ¶
IterCachedChunks iterates ONLY over the chunks currently cached in memory, e.g. so they can be torn down without loading extra chunks by looping normally.
func (*Chunker) IterChunks ¶
IterChunks returns a channel to iterate over all chunks in the drawing.
func (*Chunker) IterChunksThemselves ¶
IterChunksThemselves iterates all chunks in the drawing rather than coords.
Note: this will mark every chunk as "touched" this frame, so in a zipfile level will load ALL chunks into memory.
func (*Chunker) IterPixels ¶
IterPixels returns a channel to iterate over every pixel in the entire chunker.
func (*Chunker) IterViewport ¶
IterViewport returns a channel to iterate every point that exists within the viewport rect.
func (*Chunker) IterViewportChunks ¶
IterViewportChunks returns a channel to iterate over the Chunk objects that appear within the viewport rect, instead of the pixels in each chunk.
func (*Chunker) MigrateZipfile ¶
MigrateZipfile is called on save to migrate old-style ChunkMap chunks into external zipfile members and free up space in the master Level or Doodad struct.
func (*Chunker) OptimizeChunkerAccessors ¶
func (c *Chunker) OptimizeChunkerAccessors()
OptimizeChunkerAccessors will evaluate all of the chunks of your drawing and possibly migrate them to a different Accessor implementation when saving on disk.
func (*Chunker) Prerender ¶
func (c *Chunker) Prerender()
Prerender visits every chunk and fetches its texture, in order to pre-load the whole drawing for smooth gameplay rather than chunks lazy rendering as they enter the screen.
func (*Chunker) PrerenderN ¶
PrerenderN will pre-render the texture for N number of chunks and then yield back to the caller. Returns the number of chunks that still need textures rendered; zero when the last chunk has been prerendered.
func (*Chunker) RawChunkFromZipfile ¶
RawChunkFromZipfile loads a chunk from a zipfile and returns its raw binary content.
Returns the file extension (".bin" or ".json"), raw bytes, and an error.
func (*Chunker) Redraw ¶
func (c *Chunker) Redraw()
Redraw marks every chunk as dirty and invalidates all their texture caches, forcing the drawing to re-generate from scratch.
func (*Chunker) SetChunk ¶
SetChunk writes the chunk into the cache dict and nothing more.
This function should be the singular writer to the chunk cache.
func (*Chunker) WorldSize ¶
WorldSize returns the bounding coordinates that the Chunker has chunks to manage: the lowest pixels from the lowest chunks to the highest pixels of the highest chunks.
func (*Chunker) WorldSizePositive ¶
WorldSizePositive returns the WorldSize anchored to 0,0 with only positive coordinates.
type File ¶
type File struct {
Data []byte `json:"data,omitempty"`
}
File holds details about a file in the FileSystem.
type FileSystem ¶
type FileSystem struct { Zipfile *zip.Reader `json:"-"` // New Zipfile format accessor // contains filtered or unexported fields }
FileSystem embeds a map of files inside a parent drawing.
Old-style drawings this was a map of filenames to their data in the JSON. New-style drawings this just holds the filenames and the data is read from the zipfile on demand.
func NewFileSystem ¶
func NewFileSystem() *FileSystem
NewFileSystem initializes the FileSystem struct.
func (*FileSystem) Checksum ¶
func (fs *FileSystem) Checksum(filename string) (string, error)
Checksum returns a SHA-256 checksum of a file's data.
func (*FileSystem) Delete ¶
func (fs *FileSystem) Delete(filename string)
Delete a file from the FileSystem. This will store zero bytes in the legacy file map structure to mark it for deletion. On the next save, filemap files with zero bytes skip the ZIP archive.
func (*FileSystem) Exists ¶
func (fs *FileSystem) Exists(filename string) bool
Exists checks if a file exists.
func (*FileSystem) Get ¶
func (fs *FileSystem) Get(filename string) ([]byte, error)
Get a file from the FileSystem.
func (*FileSystem) List ¶
func (fs *FileSystem) List() []string
List files in the FileSystem, including the ZIP file.
In the ZIP file, attachments are under the "assets/" prefix so this function won't mistakenly return chunks or level.json/doodad.json.
func (*FileSystem) ListPrefix ¶
func (fs *FileSystem) ListPrefix(prefix string) []string
ListPrefix returns a list of files starting with the prefix.
func (*FileSystem) MigrateZipfile ¶
func (fs *FileSystem) MigrateZipfile(zf *zip.Writer) error
MigrateZipfile is called on save to write attached files to the ZIP file format.
func (*FileSystem) Set ¶
func (fs *FileSystem) Set(filename string, data []byte)
Set a file into the FileSystem. Note: it will go into the legacy map structure until the next save to disk, at which point queued files are committed to ZIP.
func (*FileSystem) UnmarshalJSON ¶
func (fs *FileSystem) UnmarshalJSON(text []byte) error
UnmarshalJSON reads in a FileSystem from its legacy JSON representation.
type GameRule ¶
type GameRule struct { Difficulty enum.Difficulty `json:"difficulty"` Survival bool `json:"survival,omitempty"` }
GameRule
type JSONChunk ¶
type JSONChunk struct { Type uint64 `json:"type"` Data json.RawMessage `json:"data"` BinData interface{} `json:"-"` }
JSONChunk holds a lightweight (interface-free) copy of the Chunk for unmarshalling JSON files from disk.
type Level ¶
type Level struct { Base Password string `json:"passwd"` UUID string `json:"uuid"` // unique level IDs, especially for the savegame.json GameRule GameRule `json:"rules"` // Chunked pixel data. Chunker *Chunker `json:"chunks"` // The Palette holds the unique "colors" used in this map file, and their // properties (solid, fire, slippery, etc.) Palette *Palette `json:"palette"` // Page boundaries and wallpaper settings. PageType PageType `json:"pageType"` MaxWidth int64 `json:"boundedWidth"` // only if bounded or bordered MaxHeight int64 `json:"boundedHeight"` Wallpaper string `json:"wallpaper"` // The last scrolled position in the editor. ScrollPosition render.Point `json:"scroll"` // Actors keep a list of the doodad instances in this map. Actors ActorMap `json:"actors"` // Publishing: attach any custom doodads the map uses on save. SaveDoodads bool `json:"saveDoodads"` SaveBuiltins bool `json:"saveBuiltins"` // Signature for a level with embedded doodads to still play in free mode. Signature []byte `json:"signature,omitempty"` // Undo history, temporary live data not persisted to the level file. UndoHistory *drawtool.History `json:"-"` // contains filtered or unexported fields }
Level is the container format for Doodle map drawings.
func FromJSON ¶
FromJSON loads a level from "JSON string" (gzip supported).
This is the primary "load level from file on disk" method. It can read levels of all historical file formats the game supported:
- If the data begins with a `{` it is parsed in the legacy (v1) JSON format.
- If the level begins with a Gzip header (hex `1F8B“) it is taken to be a gzip compressed (v2) level JSON file.
- If the file is identified by `net/http#DetectContentType()` to be an application/zip file (v3) it is loaded from zipfile format.
func FromZipfile ¶
FromZipfile reads a level in zipfile format.
func (*Level) DeleteFile ¶
DeleteFile removes an embedded file.
func (*Level) DeleteFiles ¶
DeleteFiles removes all files beginning with the prefix.
func (*Level) GetScreenshotImage ¶
GetScreenshotImage returns a screenshot from the level's data as a Go Image. The filename is like "large.png" or "medium.png" and is appended to "assets/screenshots"
func (*Level) GetScreenshotImageAsUIImage ¶
GetScreenshotImageAsUIImage returns a ui.Image texture of a screenshot.
func (*Level) HasScreenshot ¶
HasScreenshot returns whether screenshots exist for the level.
func (*Level) Inflate ¶
func (l *Level) Inflate()
Inflate the level from its compressed JSON form.
This is called as part of the LoadJSON function when the level is read from disk. In the JSON format, pixels in the level refer to the palette by its index number.
This function calls the following:
- Chunker.Inflate(Palette) to update references to the level's pixels to point to the Swatch entry.
- Actors.Inflate()
- Palette.Inflate() to load private instance values for the palette subsystem.
func (*Level) ListFilesAt ¶
ListFilesAt returns the list of files having a common prefix.
func (*Level) Loop ¶
Loop may be called each loop to allow the level to maintain its memory usage, e.g., for chunks not requested recently from a zipfile level to free those from RAM.
func (*Level) PruneLinks ¶
PruneLinks cleans up any Actor Links that can not be resolved in the level data. For example, if actors were linked in Edit Mode and one actor is deleted leaving a broken link.
Returns the number of broken links pruned.
This is called automatically in WriteFile.
func (*Level) ReloadZipfile ¶
ReloadZipfile re-reads the level's zipfile after a write.
func (*Level) ReplacePalette ¶
ReplacePalette installs a new palette into your level. Your existing level colors, by index, are replaced by the incoming palette. If the new palette is smaller, extraneous indices are left alone.
func (*Level) Teardown ¶
func (m *Level) Teardown()
Teardown the level when the game is done with it. This frees up SDL2 cached texture chunks and reclaims memory in ways the Go garbage collector can not.
func (*Level) ToJSON ¶
ToJSON serializes the level as JSON (gzip supported).
This is the primary "write level to disk" function and can output in a vairety of historical formats controlled by pkg/balance#DrawingFormat:
- balance.FormatJSON (the default): writes the level as an original-style single JSON document that contains all chunk data directly. These levels take a long time to load from disk for any non-trivial level design. (v1)
- balance.FormatGzip: writes as a gzip compressed JSON file (v2)
- balance.FormatZipfile: creates a zip file where most of the level JSON is stored as "index.json" and chunks and attached doodads are separate members of the zipfile.
Notice about gzip: if the pkg/balance.CompressLevels boolean is true, this function will apply gzip compression before returning the byte string. This gzip-compressed level can be read back by any functions that say "gzip supported" in their descriptions.
func (*Level) ToZipfile ¶
ToZipfile serializes the level as a ZIP archive and also migrates data loaded from an older save into the new zip format.
func (*Level) Vacuum ¶
Vacuum runs any maintenance or migration tasks for the level at time of save.
It will prune broken links between actors, or migrate internal data structures to optimize storage on disk of its binary data.
type MapAccessor ¶
type MapAccessor struct {
// contains filtered or unexported fields
}
MapAccessor implements a chunk accessor by using a map of points to their palette indexes. This is the simplest accessor and is best for sparse chunks.
func NewMapAccessor ¶
func NewMapAccessor(chunk *Chunk) *MapAccessor
NewMapAccessor initializes a MapAccessor.
func (*MapAccessor) Delete ¶
func (a *MapAccessor) Delete(p render.Point) error
Delete a pixel from the map.
func (*MapAccessor) Get ¶
func (a *MapAccessor) Get(p render.Point) (*Swatch, error)
Get a pixel from the map.
func (*MapAccessor) Inflate ¶
func (a *MapAccessor) Inflate(pal *Palette) error
Inflate the sparse swatches from their palette indexes.
func (*MapAccessor) Iter ¶
func (a *MapAccessor) Iter() <-chan Pixel
Iter returns a channel to loop over all points in this chunk.
func (*MapAccessor) IterViewport ¶
func (a *MapAccessor) IterViewport(viewport render.Rect) <-chan Pixel
IterViewport returns a channel to loop over pixels in the viewport.
func (*MapAccessor) Len ¶
func (a *MapAccessor) Len() int
Len returns the current size of the map, or number of pixels registered.
func (*MapAccessor) MarshalBinary ¶
func (a *MapAccessor) MarshalBinary() ([]byte, error)
MarshalBinary converts the chunk data to a binary representation, for better compression compared to JSON.
In the binary format each chunk begins with one Varint (the chunk Type) followed by whatever wire format the chunk needs given its type.
This function is related to the CompressMapAccessor config constant: the MapAccessor compression boils down each point to a series if packed varints: the X, Y coord (varint) followed by palette index (Uvarint).
The output of this function is just the compressed MapAccessor stream.
func (*MapAccessor) MarshalJSON ¶
func (a *MapAccessor) MarshalJSON() ([]byte, error)
MarshalJSON to convert the chunk map to JSON.
When serialized, the key is the "X,Y" coordinate and the value is the swatch index of the Palette, rather than redundantly serializing out the Swatch object for every pixel.
DEPRECATED: in the Zipfile format chunks will be saved as binary files instead of with their JSON wrappers, so MarshalJSON will be phased out.
func (*MapAccessor) Set ¶
func (a *MapAccessor) Set(p render.Point, sw *Swatch) error
Set a pixel on the map.
func (*MapAccessor) UnmarshalBinary ¶
func (a *MapAccessor) UnmarshalBinary(compressed []byte) error
UnmarshalBinary will decode a compressed MapAccessor byte stream.
func (*MapAccessor) UnmarshalJSON ¶
func (a *MapAccessor) UnmarshalJSON(b []byte) error
UnmarshalJSON to convert the chunk map back from JSON.
DEPRECATED: in the Zipfile format chunks will be saved as binary files instead of with their JSON wrappers, so MarshalJSON will be phased out.
type Option ¶
type Option struct { Type string `json:"type"` // bool, str, int Name string `json:"name"` Value interface{} `json:"value"` }
Option for runtime, user configurable overrides of Doodad Options.
type PageType ¶
type PageType int
PageType configures the bounds and wallpaper behavior of a Level.
const ( // Unbounded means the map can grow freely in any direction. // - Only the repeat texture is used for the wallpaper. Unbounded PageType = iota // NoNegativeSpace means the map is bounded at the top left edges. // - Can't scroll or visit any pixels in negative X,Y coordinates. // - Wallpaper shows the Corner at 0,0 // - Wallpaper repeats the Top along the Y=0 plane // - Wallpaper repeats the Left along the X=0 plane // - The repeat texture fills the rest of the level. NoNegativeSpace // Bounded is the same as NoNegativeSpace but the level is imposing a // maximum cap on the width and height of the level. // - Can't scroll below X,Y origin at 0,0 // - Can't scroll past the bounded width and height of the level Bounded // Bordered is like Bounded except the corner textures are wrapped // around the other edges of the level too. // - The wallpaper hoz mirrors Left along the X=Width plane // - The wallpaper vert mirrors Top along the Y=Width plane // - The wallpaper 180 rotates the Corner for opposite corners Bordered )
PageType values.
func PageTypeFromString ¶
PageTypeFromString returns a PageType from its string version.
type Palette ¶
type Palette struct { Swatches []*Swatch `json:"swatches"` // Private runtime values ActiveSwatch *Swatch `json:"-"` // name of the actively selected color // contains filtered or unexported fields }
Palette holds an index of colors used in a drawing.
func DefaultPalette ¶
func DefaultPalette() *Palette
DefaultPalette returns a sensible default palette.
func LoadPaletteFromFile ¶
LoadPaletteFromFile reads a list of Swatches from a palette.json file.
func NewBlueprintPalette ¶
func NewBlueprintPalette() *Palette
NewBlueprintPalette returns the blueprint theme's color palette. DEPRECATED in favor of DefaultPalettes.
func (*Palette) FlushCaches ¶
func (p *Palette) FlushCaches()
FlushCaches if you have modified the swatches, especially if you have changed the name of an existing color. This invalidates the "by name" cache and rebuilds it from scratch.
type Pixel ¶
type Pixel struct { X int `json:"x"` Y int `json:"y"` PaletteIndex int `json:"p"` // Private runtime values. Swatch *Swatch `json:"-"` // pointer to its swatch, for when rendered. }
Pixel associates a coordinate with a palette index.
func (Pixel) MarshalJSON ¶
MarshalJSON serializes a Pixel compactly as a simple list.
func (*Pixel) UnmarshalJSON ¶
UnmarshalJSON loads a Pixel from JSON again.
type RLEAccessor ¶
type RLEAccessor struct {
// contains filtered or unexported fields
}
RLEAccessor implements a chunk accessor which stores its on-disk format using Run Length Encoding (RLE), but in memory behaves equivalently to the MapAccessor.
func NewRLEAccessor ¶
func NewRLEAccessor(chunk *Chunk) *RLEAccessor
NewRLEAccessor initializes a RLEAccessor.
func (*RLEAccessor) Delete ¶
func (a *RLEAccessor) Delete(p render.Point) error
Delete a pixel from the map.
func (*RLEAccessor) FromMapAccessor ¶
func (a *RLEAccessor) FromMapAccessor(ma *MapAccessor) *RLEAccessor
FromMapAccessor migrates from a MapAccessor to RLE.
func (*RLEAccessor) Get ¶
func (a *RLEAccessor) Get(p render.Point) (*Swatch, error)
Get a pixel from the map.
func (*RLEAccessor) Inflate ¶
func (a *RLEAccessor) Inflate(pal *Palette) error
Inflate the sparse swatches from their palette indexes.
func (*RLEAccessor) Iter ¶
func (a *RLEAccessor) Iter() <-chan Pixel
Iter returns a channel to loop over all points in this chunk.
func (*RLEAccessor) IterViewport ¶
func (a *RLEAccessor) IterViewport(viewport render.Rect) <-chan Pixel
IterViewport returns a channel to loop over pixels in the viewport.
func (*RLEAccessor) Len ¶
func (a *RLEAccessor) Len() int
Len returns the current size of the map, or number of pixels registered.
func (*RLEAccessor) MarshalBinary ¶
func (a *RLEAccessor) MarshalBinary() ([]byte, error)
MarshalBinary converts the chunk data to a binary representation.
This accessor uses Run Length Encoding (RLE) in its binary format. Starting with the top-left pixel of this chunk, the binary format is a stream of bytes formatted as such:
- UVarint for the palette index number (0-255), with 0xFFFF meaning void - UVarint for the length of repetition of that palette index
func (*RLEAccessor) Set ¶
func (a *RLEAccessor) Set(p render.Point, sw *Swatch) error
Set a pixel on the map.
func (*RLEAccessor) UnmarshalBinary ¶
func (a *RLEAccessor) UnmarshalBinary(compressed []byte) error
UnmarshalBinary will decode a compressed RLEAccessor byte stream.
type Swatch ¶
type Swatch struct { Name string `json:"name"` Color render.Color `json:"color"` Pattern string `json:"pattern"` // like "noise.png" // Optional attributes. Solid bool `json:"solid,omitempty"` SemiSolid bool `json:"semisolid,omitempty"` Fire bool `json:"fire,omitempty"` Water bool `json:"water,omitempty"` Slippery bool `json:"slippery,omitempty"` // contains filtered or unexported fields }
Swatch holds details about a single value in the palette.
func NewSparseSwatch ¶
NewSparseSwatch creates a sparse Swatch from a palette index that will need later expansion, when loading drawings from disk.
func (*Swatch) Attributes ¶
Attributes returns a comma-separated list of attributes as a string on this swatch. This is for debugging and the `doodad show` CLI command to summarize the swatch and shouldn't be used for game logic.
Source Files
¶
Directories
¶
Path | Synopsis |
---|---|
Package publishing contains functionality for "publishing" a Level, which involves the writing and reading of custom doodads embedded inside the levels.
|
Package publishing contains functionality for "publishing" a Level, which involves the writing and reading of custom doodads embedded inside the levels. |
Package rle contains support for Run-Length Encoding of level chunks.
|
Package rle contains support for Run-Length Encoding of level chunks. |