Documentation
¶
Index ¶
- func CellsEqual(c1 *tb.Cell, c2 *tb.Cell) bool
- func LoadLevelFromMap(jsonMap string, parsers map[string]EntityParser, l Level) error
- func RgbTo256Color(r, g, b int) tb.Attribute
- type BaseLevel
- type Canvas
- type Drawable
- type DynamicPhysical
- type Entity
- func (e *Entity) ApplyCanvas(c *Canvas)
- func (e *Entity) Draw(s *Screen)
- func (e *Entity) Fill(c *tb.Cell)
- func (e *Entity) Position() (int, int)
- func (e *Entity) SetCanvas(c *Canvas)
- func (e *Entity) SetCell(x, y int, c *tb.Cell)
- func (e *Entity) SetPosition(x, y int)
- func (e *Entity) Size() (int, int)
- func (e *Entity) Tick(ev tb.Event)
- type EntityParser
- type FpsText
- type Game
- type Level
- type Physical
- type Rectangle
- func (r *Rectangle) Color() tb.Attribute
- func (r *Rectangle) Draw(s *Screen)
- func (r *Rectangle) Position() (int, int)
- func (r *Rectangle) SetColor(color tb.Attribute)
- func (r *Rectangle) SetPosition(x, y int)
- func (r *Rectangle) SetSize(w, h int)
- func (r *Rectangle) Size() (int, int)
- func (r *Rectangle) Tick(ev tb.Event)
- type Screen
- func (s *Screen) AddEntity(d Drawable)
- func (s *Screen) Draw()
- func (s *Screen) EnablePixelMode()
- func (s *Screen) Level() Level
- func (s *Screen) RemoveEntity(d Drawable)
- func (s *Screen) RenderCell(x, y int, c *tb.Cell)
- func (s *Screen) SetFps(f float64)
- func (s *Screen) SetLevel(l Level)
- func (s *Screen) Size() (int, int)
- func (s *Screen) Tick(ev tb.Event)
- func (s *Screen) TimeDelta() float64
- type Text
- func (t *Text) Color() (tb.Attribute, tb.Attribute)
- func (t *Text) Draw(s *Screen)
- func (t *Text) Position() (int, int)
- func (t *Text) SetColor(fg, bg tb.Attribute)
- func (t *Text) SetPosition(x, y int)
- func (t *Text) SetText(text string)
- func (t *Text) Size() (int, int)
- func (t *Text) Text() string
- func (t *Text) Tick(ev tb.Event)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func LoadLevelFromMap ¶
func LoadLevelFromMap(jsonMap string, parsers map[string]EntityParser, l Level) error
LoadLevelFromMap can be used to populate a Level with entities, given a JSON string to read from (jsonMap).
The map 'parsers' is a map of entity names to EntityParser functions. This can be used to define parsers for objects that are not Termloop builtins.
The JSON string should take the format of an array of objects, like so: [ {"type": "Rectangle", "data": {"x: 12 ...}}, ... ] For Rectangles and Text, all attributes must be defined in the JSON. For an Entity, fg and bg can be left as empty strings if you do not wish to color the Entity with an image, but the keys must still be present or the parsing will break.
There is an example of how to use this method at _examples/levelmap.go.
LoadLevelFromMap returns an error, or nil if all is well.
func RgbTo256Color ¶
RgbTo256Color takes RGB values and returns the closest color for a 256-color terminal, as an Attr type.
Types ¶
type BaseLevel ¶
type BaseLevel struct { Entities []Drawable // contains filtered or unexported fields }
BaseLevel type represents a Level with a background defined as a Cell, which is tiled. The background is drawn first, then all Entities.
func NewBaseLevel ¶
NewBaseLevel creates a new BaseLevel with background bg. Returns a pointer to the new BaseLevel.
func (*BaseLevel) DrawBackground ¶
DrawBackground draws the background Cell bg to each Cell of the Screen s.
func (*BaseLevel) RemoveEntity ¶
RemoveEntity removes Drawable d from the level's entities.
type Canvas ¶
A Canvas is a 2D array of Cells, used for drawing. The structure of a Canvas is an array of columns. This is so it can be addressed canvas[x][y].
func BackgroundCanvasFromFile ¶
BackgroundCanvasFromFile takes a path to an image file, and creates a canvas of background-only Cells representing the image. This can be applied to an Entity with ApplyCanvas.
func CanvasFromString ¶
CanvasFromString returns a new Canvas, built from the characters in the string str. Newline characters in the string are interpreted as a new Canvas row.
func ForegroundCanvasFromFile ¶
ForegroundCanvasFromFile takes a path to an image file, and creates a canvas of foreground-only Cells representing the image. This can be applied to an Entity with ApplyCanvas.
type Drawable ¶
type Drawable interface { Tick(tb.Event) // Method for processing events, e.g. input Draw(*Screen) // Method for drawing to the screen }
Drawable represents something that can be drawn, and placed in a Level.
type DynamicPhysical ¶
type DynamicPhysical interface { Position() (int, int) // Return position, x and y Size() (int, int) // Return width and height Collide(Physical) // Handle collisions with another Physical }
DynamicPhysical represents something that can process its own collisions. Implementing this is an optional addition to Drawable.
type Entity ¶
type Entity struct {
// contains filtered or unexported fields
}
Entity provides a general Drawable to be rendered.
func NewEntity ¶
NewEntity creates a new Entity, with position (x, y) and size (width, height). Returns a pointer to the new Entity.
func NewEntityFromCanvas ¶
NewEntityFromCanvas returns a pointer to a new Entity, with position (x, y) and Canvas c. Width and height are calculated using the Canvas.
func (*Entity) ApplyCanvas ¶
ApplyCanvas takes a pointer to a Canvas, c, and applies this canvas over the top of the Entity's canvas. Any new values in c will overwrite those in the entity.
func (*Entity) Draw ¶
Draw draws the entity to its current position on the screen. This is usually called every frame.
func (*Entity) SetCanvas ¶
SetCanvas takes a pointer to a Canvas and replaces the Entity's canvas with the pointer's. It also updates the Entity's dimensions.
func (*Entity) SetCell ¶
SetCell updates the attribute of the Cell at x, y to match those of c. The coordinates are relative to the entity itself, not the Screen.
func (*Entity) SetPosition ¶
SetPosition sets the x and y coordinates of the Entity.
type EntityParser ¶
An EntityParser is a function which composes an object from data that has been parsed from a JSON file. Returns a Drawable
type FpsText ¶
type FpsText struct { *Text // contains filtered or unexported fields }
FpsText provides a Text which updates with the current 'framerate' at specified intervals, to be used for testing performance. Please note that the framerate displayed is a measure of Termloop's processing speed - visible framerate is largely dependent on your terminal!
func NewFpsText ¶
NewFpsText creates a new FpsText at position (x, y) and with background and foreground colors fg and bg respectively. It will refresh every 'update' seconds. Returns a pointer to the new FpsText.
type Game ¶
type Game struct {
// contains filtered or unexported fields
}
Represents a top-level Termloop application.
func NewGame ¶
func NewGame() *Game
NewGame creates a new Game, along with a Screen and input handler. Returns a pointer to the new Game.
func (*Game) Log ¶
Log takes a log string and additional parameters, which can be substituted into the string using standard fmt.Printf rules. The formatted string is added to Game g's logs. If debug mode is on, the log will be printed to the terminal when Termloop exits.
func (*Game) SetDebugOn ¶
SetDebugOn sets debug mode's on status to be debugOn.
func (*Game) SetEndKey ¶
SetEndKey sets the Key used to end the game. Default is KeyCtrlC. If you don't want an end key, set it to KeyEsc, as this key isn't supported and will do nothing. (We recommend always having an end key for development/testing.)
type Level ¶
type Level interface { DrawBackground(*Screen) AddEntity(Drawable) RemoveEntity(Drawable) Draw(*Screen) Tick(tb.Event) }
Level interface represents a Drawable with a separate background that is drawn first. It can also contain Drawables of its own.
type Physical ¶
type Physical interface { Position() (int, int) // Return position, x and y Size() (int, int) // Return width and height }
Physical represents something that can collide with another Physical, but cannot process its own collisions. Optional addition to Drawable.
type Rectangle ¶
type Rectangle struct {
// contains filtered or unexported fields
}
A type representing a 2D rectangle, with position, size and color.
func NewRectangle ¶
NewRectangle creates a new Rectangle at position (x, y), with size (width, height) and color color. Returns a pointer to the new Rectangle.
func (*Rectangle) SetPosition ¶
SetPosition sets the coordinates of the Rectangle to be x and y.
type Screen ¶
type Screen struct { Entities []Drawable // contains filtered or unexported fields }
A Screen represents the current state of the display. To draw on the screen, create Drawables and set their positions. Then, add them to the Screen's Level, or to the Screen directly (e.g. a HUD).
func NewScreen ¶
func NewScreen() *Screen
NewScreen creates a new Screen, with no entities or level. Returns a pointer to the new Screen.
func (*Screen) Draw ¶
func (s *Screen) Draw()
Draw is called every frame by the Game to render the current state of the screen.
func (*Screen) EnablePixelMode ¶
func (s *Screen) EnablePixelMode()
EnablePixelMode sets the screen to 'pixel mode' - giving double the canvas height while sacrificing character drawing ability.
func (*Screen) RemoveEntity ¶
RemoveEntity removes Drawable d from the screen's entities.
func (*Screen) RenderCell ¶
RenderCell updates the Cell at a given position on the Screen with the attributes in Cell c.
func (*Screen) SetFps ¶
Set the screen framerate. By default, termloop will draw the the screen as fast as possible, which may use a lot of system resources.
type Text ¶
type Text struct {
// contains filtered or unexported fields
}
Text represents a string that can be drawn to the screen.
func NewText ¶
NewText creates a new Text, at position (x, y). It sets the Text's background and foreground colors to fg and bg respectively, and sets the Text's text to be text. Returns a pointer to the new Text.
func (*Text) SetColor ¶
SetColor sets the (foreground, background) colors of the Text to fg, bg respectively.
func (*Text) SetPosition ¶
SetPosition sets the coordinates of the Text to be (x, y).