GitHub快速开始

API 参考取自源码中的 Go 文档注释,以英文书写。

package tileshape

import "github.com/nsgo-dev/game/pkg/tileshape"

概述

Package tileshape derives collision and lighting geometry from a tilemap: a View that physics.MoveAndSlide, physics.CharacterBody, the platformer controller and physics.World take as their physics.TileGrid, the physics.Slope segments of slanted and partial tiles, the merged outline of solid cells and tile shapes, static boxes, and lights2d occluders.

A cell collides as a whole when its tiles carry TileSolid or TileOneWay, from SetFlags, a "solid" or "oneWay" tile property, or a SetCellFlags override, or when one of its tiles has a collision shape that fills the cell. A tile whose collision shapes (tilemap.Tileset.Shapes, such as the ones Tiled's collision editor draws) do not fill the cell collides through those shapes instead: the cell reports neither TileSolid nor TileOneWay to the grid, and the shapes' upward-facing and vertical edges become slopes, so a character walks 45 degree, 2:1 and half-height tiles with no hand-placed segments. A SetCellFlags override takes precedence over every tile in the cell, and shapes follow the flip bits of the tile that placed them.

Geometry is cached in chunks of 16 by 16 cells and built on first use. A View follows the map's change log (Tilemap.TileRevision and Tilemap.TileChangesSince), so after SetTile, FillRect, SetCellFlags or ClearCellFlags its next call rebuilds only the chunks around the cells that changed, and after SetFlags every cell. After writing a layer's Data directly call View.Invalidate for the cells written, and after editing a tileset's Shapes call View.InvalidateAll.

Only bounded orthogonal maps are supported: a View of an unbounded map (Tilemap.Infinite, or a sparse chunked layer) has no cells, and layer offsets are ignored, as they are by the tilemap's own collision queries. A polyline shape is an open edge a character stands on, never part of the outline or the occluders, and never makes its cell solid.

索引

类型

type Options struct {
	// Layers are indices in Tilemap.Layers of the tile layers that collide. When Layers and LayerNames are both empty, every tile layer collides; a View notices a layer added to or removed from Tilemap.Layers after New on its next call and rebuilds every cell.
	Layers []int
	// LayerNames adds the tile layers with these names, looked up when New is called.
	LayerNames []string
	// ShapedSolid makes a cell whose tiles collide only through shapes that do not fill it report TileSolid (or TileOneWay, for a one-way tile) from TileFlagsAt, for code that knows only whole cells, such as physics.MoveAndSlide and physics.World's queries. By default such a cell reports neither, and collides through Slopes.
	ShapedSolid bool
}

Options choose which layers of a map a View reads and how it reports shaped cells.

type Segment struct {
	A, B core.Point
}

Segment is one straight edge of a View's outline, from A to B, with the solid geometry on its right as seen walking from A to B on screen (Y down), so its outward normal is (B.Y-A.Y, A.X-B.X) normalized.

func (s Segment) Normal() core.Point

Normal returns the unit normal of s pointing out of the solid geometry, or (0, 0) for a segment with no length.

type View struct {
	// contains filtered or unexported fields
}

View is the collision and lighting geometry of a tilemap's chosen layers, in world coordinates that honour the map's Origin. It satisfies physics.TileGrid and physics.TileGridOrigin, so it is passed directly to physics.MoveAndSlide, physics.CharacterBody.Move, platformer.Controller.Step and physics.World.Tiles.

A View is not safe for concurrent use, and the slices it returns are shared with its cache: they stay valid, and must not be modified, until the map changes.

func New(m *tilemap.Tilemap, opts Options) *View

New returns a View of m's layers chosen by opts. A View of a nil map, or of an unbounded map (Tilemap.Infinite set, or a sparse chunked layer), has no cells.

A View follows the map through Tilemap.TileChangesSince: on its next call after SetTile, FillRect, SetCellFlags, ClearCellFlags or SetFlags it rebuilds only the chunks around the cells they changed, or every cell when the change log cannot say which (a SetFlags call, a large FillRect, more edits than the log holds).

func (v *View) AppendSlopesIn(dst []physics.Slope, r core.Bounds) []physics.Slope

AppendSlopesIn appends to dst the slopes of the chunks that r, grown by one cell, overlaps, and returns the extended slice: a superset of the slopes in r that is cheaper to pass to physics.CharacterBody.Move on a large map than Slopes.

func (v *View) AppendStaticShapesIn(dst []physics.Shape, r core.Bounds) []physics.Shape

AppendStaticShapesIn appends to dst the static physics shapes of the chunks that r, grown by one cell and oversized-tile reach, overlaps, merged across those chunks.

func (v *View) Boxes() []physics.Box

Boxes returns the solid whole cells and axis-aligned rectangular solid tile shapes (such as half tiles), merged across neighbours into maximal rectangles, as static physics boxes; pass each to physics.NewStaticRigidBox for a physics.RigidWorld. For non-rectangular partial shapes such as slopes, rounded corners, and ellipses, use StaticShapes, Outline, or Slopes.

func (v *View) GridOrigin() (x, y float32)

GridOrigin returns the map's Origin, the world position of cell (0, 0), implementing physics.TileGridOrigin so physics places v's cells where the map draws them. Every segment, slope and box v returns is in the same world coordinates.

func (v *View) GridSize() (cols, rows int)

GridSize returns the map's size in cells.

func (v *View) Invalidate(col, row, w, h int)

Invalidate rebuilds the cells of the rectangle [col, col+w) x [row, row+h), and the geometry around them, on next use. Call it after writing a layer's Data directly; the map's own setters need no call. After editing a tileset's Shapes call InvalidateAll instead, since Invalidate keeps the geometry v has derived from each tile's shapes.

func (v *View) InvalidateAll()

InvalidateAll rebuilds every cell on next use.

Call it after changing a tileset's Shapes, which every cell showing the tile may depend on.

func (v *View) Map() *tilemap.Tilemap

Map returns the tilemap v reads.

func (v *View) Occluders() []lights2d.Occluder

Occluders returns Outline as lights2d occluders, one per 16 by 16 chunk with solid geometry, which cast the same shadows as a rectangle occluder per solid cell and a polygon occluder per solid shape. Assign the result to lights2d.Scene.Occluders again after the map changes: a change rebuilds only the chunks around it, and the others' segments are reused.

func (v *View) Outline() []Segment

Outline returns the boundary of the solid geometry, whole cells and solid tile shapes together, as segments with the solid side on their right: edges shared by neighbouring solid cells or shapes are removed, and collinear edges are merged within each 16 by 16 chunk. One-way cells and shapes are not part of it.

func (v *View) Slopes() []physics.Slope

Slopes returns the segments physics.CharacterBody walks for every tile shape of the map: the upward-facing and vertical edges of solid shapes, the upward-facing edges of one-way shapes, and every polyline, with edges shared by neighbouring tiles removed and collinear edges merged within each 16 by 16 chunk.

func (v *View) StaticShapes() []physics.Shape

StaticShapes returns static physics.Shape colliders for every solid whole cell and solid partial tile shape, merged across neighbours where possible: maximal physics.Box shapes for solid cells and axis-aligned rectangular partial tiles, merged convex physics.Polygon shapes for adjacent convex partial tiles (such as multi-tile slopes and bevels), and closed physics.Chain shapes for concave or high-vertex partial shapes (such as ellipses). Pass each shape to physics.NewStaticRigidBody or &physics.Body{Shape: s} to collide in RigidWorld or World.

func (v *View) TileFlagsAt(col, row int) physics.TileFlags

TileFlagsAt returns the collision flags of the cell at (col, row) across v's layers, or 0 out of bounds. A cell that collides only through partial shapes reports neither TileSolid nor TileOneWay unless Options.ShapedSolid is set; every other flag bit of its tiles is kept.

func (v *View) TileSize() (width, height float32)

TileSize returns the map's tile width and height.