engine

package
v0.0.0-...-0be2bd3 Latest Latest
Warning

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

Go to latest
Published: Jun 30, 2022 License: Apache-2.0 Imports: 21 Imported by: 0

Documentation

Index

Constants

View Source
const Skip = skip("skip")

Skip is an "error" value that can be returned from visitor callbacks. It tells recursive methods of Game to skip processing the current item and its descendants, but will otherwise continue processing.

Variables

View Source
var (
	// TypeOf(pointer to interface).Elem() is "idiomatic" -
	// see https://pkg.go.dev/reflect#example-TypeOf
	BoundingBoxerType  = reflect.TypeOf((*BoundingBoxer)(nil)).Elem()
	BoundingRecterType = reflect.TypeOf((*BoundingRecter)(nil)).Elem()
	ColliderType       = reflect.TypeOf((*Collider)(nil)).Elem()
	DisablerType       = reflect.TypeOf((*Disabler)(nil)).Elem()
	DrawBoxerType      = reflect.TypeOf((*DrawBoxer)(nil)).Elem()
	DrawerType         = reflect.TypeOf((*Drawer)(nil)).Elem()
	DrawManagerType    = reflect.TypeOf((*DrawManager)(nil)).Elem()
	DrawOrdererType    = reflect.TypeOf((*DrawOrderer)(nil)).Elem()
	HiderType          = reflect.TypeOf((*Hider)(nil)).Elem()
	IdentifierType     = reflect.TypeOf((*Identifier)(nil)).Elem()
	LoaderType         = reflect.TypeOf((*Loader)(nil)).Elem()
	PrepperType        = reflect.TypeOf((*Prepper)(nil)).Elem()
	RegistrarType      = reflect.TypeOf((*Registrar)(nil)).Elem()
	SaverType          = reflect.TypeOf((*Saver)(nil)).Elem()
	ScannerType        = reflect.TypeOf((*Scanner)(nil)).Elem()
	TransformerType    = reflect.TypeOf((*Transformer)(nil)).Elem()
	UpdaterType        = reflect.TypeOf((*Updater)(nil)).Elem()

	// Behaviours lists all the behaviours that can be queried with Game.Query.
	Behaviours = []reflect.Type{
		BoundingBoxerType,
		BoundingRecterType,
		ColliderType,
		DisablerType,
		DrawBoxerType,
		DrawerType,
		DrawManagerType,
		DrawOrdererType,
		HiderType,
		IdentifierType,
		LoaderType,
		PrepperType,
		RegistrarType,
		SaverType,
		ScannerType,
		TransformerType,
		UpdaterType,
	}
)

Reflection types used for queries... Is there a better way?

Functions

func LoadGobz

func LoadGobz(dst any, assets fs.FS, path string) error

LoadGobz gunzips and gob-decodes a component from a file from a FS.

func SaveGobz

func SaveGobz(src any, name string) error

SaveGobz takes an object, gob-encodes it, gzips it, and writes to disk. This requires running on something with a disk to write to (not JS)

Types

type Actor

type Actor struct {
	CollisionDomain string    // id of component to look for colliders inside of
	Pos             geom.Int3 // in voxels; multiply by game.VoxelScale for regular Euclidean space
	Bounds          geom.Box  // in voxels; relative to Pos
	// contains filtered or unexported fields
}

Actor handles basic movement.

func (*Actor) BoundingBox

func (a *Actor) BoundingBox() geom.Box

BoundingBox returns the box Bounds.Add(Pos).

func (*Actor) CollidesAt

func (a *Actor) CollidesAt(p geom.Int3) bool

CollidesAt runs a collision test of the actor, supposing the actor is at a given position (not necessarily a.Pos).

func (*Actor) MoveX

func (a *Actor) MoveX(x float64, onCollide func())

MoveX moves the actor x units in world space. It takes Game.VoxelScale into account (so MoveX(x) moves the actor x/VoxelScale.X voxel units). onCollide is called if a collision occurs, and the actor wil be in the colliding position during the call.

func (*Actor) MoveY

func (a *Actor) MoveY(y float64, onCollide func())

MoveY is like MoveX but in the Y dimension. See MoveX for more information.

func (*Actor) MoveZ

func (a *Actor) MoveZ(z float64, onCollide func())

MoveZ is like MoveX but in the Y dimension. See MoveX for more information.

func (*Actor) Prepare

func (a *Actor) Prepare(g *Game) error

Prepare stores a reference to the game.

func (*Actor) String

func (a *Actor) String() string

type Anim

type Anim struct {
	Def   *AnimDef
	Index int // current step index
	Ticks int // ticks spent at this step
}

Anim is the current state of an animation being played (think of it as an instance of an AnimDef). nil *Anim can be used, but always returns 0 for the current frame.

func (*Anim) Cell

func (a *Anim) Cell() int

Cell returns the cell index for the current step.

func (*Anim) Reset

func (a *Anim) Reset()

Reset resets both Index and Ticks to 0.

func (*Anim) Update

func (a *Anim) Update() error

Update increments the tick count and advances the frame if necessary.

type AnimDef

type AnimDef struct {
	Steps   []AnimStep
	OneShot bool
}

AnimDef defines an animation, as a sequence of steps and other information.

func (*AnimDef) NewAnim

func (d *AnimDef) NewAnim() *Anim

NewAnim spawns a new anim using this def, or nil if d is nil.

type AnimStep

type AnimStep struct {
	Cell     int // show this cell
	Duration int // for this long, in ticks
}

AnimStep describes a step in an animation.

type AnimatedTile

type AnimatedTile struct {
	AnimKey string
	// contains filtered or unexported fields
}

AnimatedTile uses an Anim to choose a tile index.

func (*AnimatedTile) Cell

func (a *AnimatedTile) Cell() int

Cell returns the value of Cell provided by the animation.

func (*AnimatedTile) Scan

func (a *AnimatedTile) Scan(visit VisitFunc) error

Scan visits a.anim.

type Billboard

type Billboard struct {
	ID
	Hides
	Pos geom.Int3
	Src ImageRef
	// contains filtered or unexported fields
}

Billboard draws an image at a position.

func (*Billboard) BoundingBox

func (b *Billboard) BoundingBox() geom.Box

BoundingBox returns a 0-depth box incorporating the image size.

func (*Billboard) Draw

func (b *Billboard) Draw(screen *ebiten.Image, opts *ebiten.DrawImageOptions)

Draw draws the image.

func (*Billboard) Prepare

func (b *Billboard) Prepare(g *Game) error

Prepare saves the reference to Game.

func (*Billboard) Scan

func (b *Billboard) Scan(visit VisitFunc) error

Scan visits &b.Src.

func (*Billboard) String

func (b *Billboard) String() string

String returns "Billboard@(b.Pos)".

func (*Billboard) Transform

func (b *Billboard) Transform() (opts ebiten.DrawImageOptions)

Transform returns a translation by the projected position.

type BoundingBoxer

type BoundingBoxer interface {
	BoundingBox() geom.Box
}

BoundingBoxer components have a bounding box.

type BoundingRecter

type BoundingRecter interface {
	BoundingRect() image.Rectangle
}

BoundingRecter components have a bounding rectangle.

type Bounds

type Bounds image.Rectangle

Bounds implements Bounder directly (as an image.Rectangle value).

func (Bounds) BoundingRect

func (b Bounds) BoundingRect() image.Rectangle

BoundingRect returns b as an image.Rectangle.

type Camera

type Camera struct {
	ID
	Child any
	Disables
	Hides

	// Camera controls
	// These directly manipulate the camera. If you want to restrict the camera
	// view area to the child's bounding rectangle, use PointAt.
	Centre   image.Point // voxel coordinates
	Rotation float64     // radians
	Zoom     float64     // unitless
	// contains filtered or unexported fields
}

Camera models a camera that is viewing something.

func (*Camera) PointAt

func (c *Camera) PointAt(centre geom.Int3, zoom float64)

PointAt points the camera at a particular centre point and zoom, but adjusts for the bounds of the child component (if available).

func (*Camera) Prepare

func (c *Camera) Prepare(game *Game) error

Prepare grabs a copy of game (needed for screen dimensions)

func (*Camera) Scan

func (c *Camera) Scan(visit VisitFunc) error

Scan visits c.Child.

func (*Camera) String

func (c *Camera) String() string

func (*Camera) Transform

func (c *Camera) Transform() (opts ebiten.DrawImageOptions)

Transform returns the camera transform.

type Collider

type Collider interface {
	CollidesWith(geom.Box) bool
}

Collider components have tangible form.

type Container

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

Container is a component that contains many other components, in order. It can be used as both a component in its own right, or as an ordered set. A nil *Container contains no items and modifications will panic (like a map).

func MakeContainer

func MakeContainer(items ...any) *Container

MakeContainer puts the items into a new Container.

func (*Container) Add

func (c *Container) Add(component any)

Add adds an item to the end of the container, if not already present. Adding nil, or a component already present in the container, does nothing. Add is _not_ safe to call on a nil *Container.

func (*Container) Contains

func (c *Container) Contains(component any) bool

Contains reports if an item exists in the container. Contains is safe to call on a nil *Container.

func (*Container) Element

func (c *Container) Element(i int) any

Element returns the item at index i, or nil for a free slot. Element is _not_ safe to call on a nil *Container.

func (*Container) GobDecode

func (c *Container) GobDecode(in []byte) error

GobDecode decodes a byte slice as though it were a slice of items.

func (*Container) GobEncode

func (c *Container) GobEncode() ([]byte, error)

GobEncode encodes c as the slice of items. When called on a nil *Container, GobEncode returns a nil slice.

func (*Container) IndexOf

func (c *Container) IndexOf(component any) (int, bool)

IndexOf reports if an item exists in the container and returns the index if present. IndexOf is safe to call on a nil *Container.

func (*Container) ItemCount

func (c *Container) ItemCount() int

ItemCount returns the number of (non-nil) items in the container. ItemCount is safe to call on a nil *Container.

func (*Container) Len

func (c *Container) Len() int

Len returns the number of items plus the number of nil slots in the container. Len is safe to call on a nil *Container.

func (*Container) Remove

func (c *Container) Remove(component any)

Remove replaces an item with nil. If the number of nil items is greater than half the slice, the slice is compacted (indexes of items will change). Removing an item not in the Container does nothing. Remove is safe to call on a nil *Container.

func (*Container) Scan

func (c *Container) Scan(visit VisitFunc) error

Scan visits every non-nil component in the container. Scan is safe to call on a nil *Container.

func (*Container) String

func (c *Container) String() string

func (*Container) Swap

func (c *Container) Swap(i, j int)

Swap swaps any two items, free slots, or a combination. Swap is _not_ safe to call on a nil *Container.

type DebugToast

type DebugToast struct {
	ID
	Hides
	Pos   image.Point
	Timer int // ticks
	Text  string
}

DebugToast debugprints a string for a while, then disappears.

func (*DebugToast) Draw

func (d *DebugToast) Draw(screen *ebiten.Image, _ *ebiten.DrawImageOptions)

Draw uses DebugPrintAt to draw d.Text at the position d.Pos.

func (*DebugToast) String

func (d *DebugToast) String() string

func (*DebugToast) Toast

func (d *DebugToast) Toast(text string)

Toast sets the text to appear for 2 seconds.

func (*DebugToast) Update

func (d *DebugToast) Update() error

Update hides the toast text once the timer is exhausted.

type Disabler

type Disabler interface {
	Disabled() bool
	Disable()
	Enable()
}

Disabler components can be disabled.

type Disables

type Disables bool

Disables implements Disabler directly (as a bool).

func (*Disables) Disable

func (d *Disables) Disable()

Disable sets d to true.

func (Disables) Disabled

func (d Disables) Disabled() bool

Disabled returns d as a bool.

func (*Disables) Enable

func (d *Disables) Enable()

Enable sets d to false.

type DrawBoxer

type DrawBoxer interface {
	BoundingBoxer
	Drawer
}

DrawBoxer components can both draw and have a bounding box (used for draw ordering).

type DrawDAG

type DrawDAG struct {
	ChunkSize int
	Child     any
	Disables
	Hides
	// contains filtered or unexported fields
}

DrawDAG is a DrawManager that organises DrawBoxer descendants in a directed acyclic graph (DAG), in order to draw them according to ordering constraints. It combines a DAG with a spatial index used when updating vertices to reduce the number of tests between components.

func (DrawDAG) Dot

func (d DrawDAG) Dot() string

Dot returns a dot-syntax-like description of the graph.

func (*DrawDAG) Draw

func (d *DrawDAG) Draw(screen *ebiten.Image, opts *ebiten.DrawImageOptions)

Draw draws everything in the DAG in topological order.

func (DrawDAG) ManagesDrawingSubcomponents

func (DrawDAG) ManagesDrawingSubcomponents()

ManagesDrawingSubcomponents is present so DrawDAG is recognised as a DrawManager.

func (*DrawDAG) Prepare

func (d *DrawDAG) Prepare(game *Game) error

Prepare adds all subcomponents to the DAG.

func (*DrawDAG) Register

func (d *DrawDAG) Register(component, _ any) error

Register recursively registers compponent and all descendants that are DrawBoxers into internal data structures (the DAG, etc) unless they are descendants of a different DrawManager.

func (*DrawDAG) Scan

func (d *DrawDAG) Scan(visit VisitFunc) error

Scan visits d.Child.

func (*DrawDAG) String

func (d *DrawDAG) String() string

func (*DrawDAG) Unregister

func (d *DrawDAG) Unregister(component any)

Unregister unregisters the component and all subcomponents.

func (*DrawDAG) Update

func (d *DrawDAG) Update() error

Update checks for any changes to descendants, and updates its internal data structures accordingly.

type DrawDFS

type DrawDFS struct {
	Child any
	Hides
	// contains filtered or unexported fields
}

DrawDFS is a DrawManager that does not add any structure. Components are drawn in the order in which they are encountered by a depth-first search through the game tree using Query, without any extra sorting based on Z values or consideration for DrawOrderer.

func (*DrawDFS) Draw

func (d *DrawDFS) Draw(screen *ebiten.Image, opts *ebiten.DrawImageOptions)

Draw draws all descendant components (that are not managed by some other DrawManager) in a pre-order traversal.

func (DrawDFS) ManagesDrawingSubcomponents

func (DrawDFS) ManagesDrawingSubcomponents()

ManagesDrawingSubcomponents is present so DrawDFS is recognised as a DrawManager.

func (*DrawDFS) Prepare

func (d *DrawDFS) Prepare(g *Game) error

Prepare saves a reference to g.

func (*DrawDFS) Scan

func (d *DrawDFS) Scan(visit VisitFunc) error

Scan visits d.Child.

func (*DrawDFS) String

func (d *DrawDFS) String() string

type DrawManager

type DrawManager interface {
	ManagesDrawingSubcomponents()
}

DrawManager is a component responsible for calling Draw on all Drawer components beneath it, except those beneath another DrawManager (it might call Draw on the DrawManager, but that's it).

type DrawOrderer

type DrawOrderer interface {
	DrawAfter(Drawer) bool
	DrawBefore(Drawer) bool
}

DrawOrderer components have more specific ideas about draw ordering than merely "my Z is bigger than yours".

type Drawer

type Drawer interface {
	Draw(*ebiten.Image, *ebiten.DrawImageOptions)
}

Drawer components can draw themselves. Draw is called often. Draw is not requierd to call Draw on subcomponents, if they are known to the engine (as part of a DrawManager).

type DummyLoad

type DummyLoad struct {
	time.Duration
}

DummyLoad is a loader that just takes up time and doesn't actually load anything.

func (DummyLoad) Load

func (d DummyLoad) Load(fs.FS) error

Load sleeps for d.Duration, then returns nil.

type Fill

type Fill struct {
	ID
	Colour color.Color
	Hides
}

Fill fills the screen with a colour.

func (*Fill) Draw

func (f *Fill) Draw(screen *ebiten.Image, opts *ebiten.DrawImageOptions)

Draw fills the screen with the colour.

func (*Fill) String

func (f *Fill) String() string

type Game

type Game struct {
	Disables
	Hides
	Projection geom.Projector
	Root       Drawer
	ScreenSize image.Point
	VoxelScale geom.Float3
	// contains filtered or unexported fields
}

Game implements the ebiten methods using a collection of components. One component must be the designated root component.

func (*Game) Children

func (g *Game) Children(c any) *Container

Children returns the direct subcomponents of the given component, or nil if there are none. This only returns sensible values for registered components.

func (*Game) Component

func (g *Game) Component(id string) Identifier

Component returns the component with a given ID, or nil if there is none. This only returns sensible values for registered components (e.g. after LoadAndPrepare).

func (*Game) Draw

func (g *Game) Draw(screen *ebiten.Image)

Draw draws everything.

func (*Game) Ident

func (g *Game) Ident() string

Ident returns "__GAME__".

func (*Game) Layout

func (g *Game) Layout(outsideWidth, outsideHeight int) (w, h int)

Layout returns the configured screen width/height.

func (*Game) Load

func (g *Game) Load(component any, assets fs.FS) error

Load loads a component and all subcomponents recursively. Note that this method does not implement Loader itself.

func (*Game) LoadAndPrepare

func (g *Game) LoadAndPrepare(assets fs.FS) error

LoadAndPrepare first calls Load on all Loaders. Once loading is complete, it builds the component databases and then calls Prepare on every Preparer. LoadAndPrepare must be called before any calls to Component or Query.

func (*Game) Parent

func (g *Game) Parent(c any) any

Parent returns the parent of a given component, or nil if there is none. This only returns sensible values for registered components.

func (*Game) Path

func (g *Game) Path(component any) []any

Path returns a slice with the path of components to reach component from g (including g and component).

func (*Game) PathRegister

func (g *Game) PathRegister(component, parent any) error

PathRegister calls Register on every Registrar in the path between g and parent (top-to-bottom, i.e. game first, component last).

func (*Game) PathUnregister

func (g *Game) PathUnregister(component any)

PathUnregister calls Unregister on every Registrar in the path between g and parent (bottom-to-top, i.e. component first, game last).

func (*Game) Prepare

func (g *Game) Prepare(component any) error

Prepare prepares a component and all subcomponents recursively. Note that this method does not implement Prepper itself.

func (*Game) Query

func (g *Game) Query(ancestor any, behaviour reflect.Type, visitPre, visitPost VisitFunc) error

Query recursively searches for components having both a given ancestor and implementing a given behaviour (see Behaviors in interface.go). visitPre is called before descendants are visited, while visitPost is called after descendants are visited. nil visitPre/visitPost are ignored.

It is up to the visitPre and visitPost callbacks to handle components that do not themselves implement the behaviour - more specifically, every ancestor (up to the given one) of each component with the behaviour will be visited. Visiting components in the tree that *don't* implement the behaviour is important when behaviours of the parent need to influence the behaviours of the children (e.g. a component can be a Hider and hiding all descendants, but not necessarily be a Drawer itself).

Query only visits components that are registered.

Note that every component is an ancestor of itself.

Query returns the first error returned from either visitor callback, except Skip when it is returned from a recursive call. Returning Skip from visitPre will cause visitPost and the descendants of the component to be skipped (see the implementation of Update for an example of how to use this).

func (*Game) REPL

func (g *Game) REPL(src io.Reader, dst io.Writer, assets fs.FS) error

REPL runs a read-evaluate-print-loop. Commands are taken from src and output is written to dst. assets is needed for commands like reload.

func (*Game) Register

func (g *Game) Register(component, parent any) error

Register registers a component into the component database (as the child of a given parent). Passing a nil component or parent is an error. Registering multiple components with the same ID is also an error. Registering a component will recursively register all children found via Scan.

func (*Game) ReversePath

func (g *Game) ReversePath(component any) []any

ReversePath returns the same slice as Path, but reversed. (ReversePath is faster than Path).

func (*Game) Scan

func (g *Game) Scan(visit VisitFunc) error

Scan visits g.Root.

func (*Game) String

func (g *Game) String() string

func (*Game) Unregister

func (g *Game) Unregister(component any)

Unregister removes the component from the component database. Passing a nil component has no effect. Unregistering a component will recursively unregister child components found via Scan.

func (*Game) Update

func (g *Game) Update() error

Update updates everything. Subcomponents are updated before parent components. Disabled components, and components with a disabled ancestor, are not updated.

type Hider

type Hider interface {
	Hidden() bool
	Hide()
	Show()
}

Hider components can be hidden.

type Hides

type Hides bool

Hides implements Hider directly (as a bool).

func (Hides) Hidden

func (h Hides) Hidden() bool

Hidden returns h as a bool.

func (*Hides) Hide

func (h *Hides) Hide()

Hide sets h to true.

func (*Hides) Show

func (h *Hides) Show()

Show sets h to false.

type ID

type ID string

ID implements Identifier directly (as a string value).

func (ID) Ident

func (id ID) Ident() string

Ident returns id as a string.

type Identifier

type Identifier interface {
	Ident() string
}

Identifier components have a sense of self. This makes it easier for components to find and interact with one another. Returning the empty string is treated as having no identifier.

type ImageRef

type ImageRef struct {
	Path string
	// contains filtered or unexported fields
}

ImageRef loads images from the AssetFS into *ebiten.Image form. It is your responsibility to import _ "image/..." for whatever format the files are in, and to load it (either return it as a subcomponent from Scan so that Game will Load it, or call Load yourself).

func (*ImageRef) Image

func (r *ImageRef) Image() *ebiten.Image

Image returns the image, or nil if not loaded. Multiple distinct ImageRefs can use the same path efficiently.

func (*ImageRef) Load

func (r *ImageRef) Load(assets fs.FS) error

Load loads the image. Load is required before Image returns. Loading the same path multiple times uses a cache to return the same image.

func (*ImageRef) String

func (r *ImageRef) String() string

type Loader

type Loader interface {
	Load(fs.FS) error
}

Loader components get the chance to load themselves. This happens before preparation.

type LoadingSwitch

type LoadingSwitch struct {
	During, After interface {
		Disabler
		Hider
	}
	// contains filtered or unexported fields
}

LoadingSwitch switches between two subcomponents. While After is being loaded asynchronously, During is shown. Once loading is complete, During is hidden and After is shown.

func (*LoadingSwitch) Load

func (s *LoadingSwitch) Load(assets fs.FS) error

Load stores a copy of assets to pass to s.After.Load later.

func (*LoadingSwitch) Prepare

func (s *LoadingSwitch) Prepare(game *Game) error

Prepare loads, registers, and prepares.After in a separate goroutine. Once ready, LoadingSwitch hides s.During and shows s.After.

func (*LoadingSwitch) Scan

func (s *LoadingSwitch) Scan(visit VisitFunc) error

Scan only scans s.During. Only s.During is loaded automatically - s.After is loaded asynchronously from Prepare below.

type Parallax

type Parallax struct {
	CameraID string
	Factor   float64 // how much to translate in response to the camera
	Child    any
	// contains filtered or unexported fields
}

Parallax is a container that translates based on the position of a camera, intended to produce a "parallax" like effect.

func (*Parallax) Prepare

func (p *Parallax) Prepare(game *Game) error

Prepare obtains a reference to the camera.

func (*Parallax) Scan

func (p *Parallax) Scan(visit VisitFunc) error

Scan visits p.Child.

func (*Parallax) String

func (p *Parallax) String() string

func (*Parallax) Transform

func (p *Parallax) Transform() (opts ebiten.DrawImageOptions)

Transform returns a GeoM translation of Factor * camera.Centre.

type PerfDisplay

type PerfDisplay struct {
	Hides
}

PerfDisplay debugprints CurrentTPS and CurrentFPS in the top left.

func (PerfDisplay) Draw

func (p PerfDisplay) Draw(screen *ebiten.Image, _ *ebiten.DrawImageOptions)

Draw uses DebugPrint to print the TPS and FPS in the top-left.

func (PerfDisplay) String

func (PerfDisplay) String() string

type Prepper

type Prepper interface {
	Prepare(game *Game) error
}

Prepper components can be prepared. It is called after the component database has been populated but before the game is run. The component can store the reference to game, if needed, and also query the component database.

type Prism

type Prism struct {
	Cell int
	// contains filtered or unexported fields
}

Prism represents a single prism in a PrismMap.

func (*Prism) BoundingBox

func (p *Prism) BoundingBox() geom.Box

BoundingBox returns a bounding box for the prism.

func (*Prism) Draw

func (p *Prism) Draw(screen *ebiten.Image, opts *ebiten.DrawImageOptions)

Draw draws the prism.

func (*Prism) DrawAfter

func (p *Prism) DrawAfter(x Drawer) bool

DrawAfter reports if the prism should be drawn after x.

func (*Prism) DrawBefore

func (p *Prism) DrawBefore(x Drawer) bool

DrawBefore reports if the prism should be drawn before x.

func (*Prism) String

func (p *Prism) String() string

func (*Prism) Transform

func (p *Prism) Transform() (opts ebiten.DrawImageOptions)

Transform returns a translation by the projected position.

type PrismMap

type PrismMap struct {
	ID
	Disables
	Hides
	Ersatz     bool
	Map        map[geom.Int3]*Prism // pos -> prism
	DrawOffset image.Point          // offset applies to whole map
	PosToWorld geom.IntMatrix3x4    // p.pos -> world voxelspace
	PrismSize  geom.Int3            // in world voxelspace units
	PrismTop   []image.Point        // polygon vertices anticlockwise, Y means Z
	Sheet      Sheet
	// contains filtered or unexported fields
}

PrismMap is a generalised 3D tilemap/wallmap/voxelmap etc.

func (*PrismMap) CollidesWith

func (m *PrismMap) CollidesWith(b geom.Box) bool

CollidesWith checks if the box collides with any prism.

func (*PrismMap) Prepare

func (m *PrismMap) Prepare(g *Game) error

Prepare computes an inverse of PosToWorld and prepares all the prisms.

func (*PrismMap) Scan

func (m *PrismMap) Scan(visit VisitFunc) error

Scan visits &m.Sheet and all Prisms.

func (*PrismMap) String

func (m *PrismMap) String() string

func (*PrismMap) Transform

func (m *PrismMap) Transform() (opts ebiten.DrawImageOptions)

Transform retrurns a translation by the draw offset.

type Registrar

type Registrar interface {
	Register(component, parent any) error
	Unregister(component any)
}

Registrar components can register and unregister other components (usually into internal data structures). Registrars are expected to automatically register/unregister subcomponents of components (usually recursively).

type Saver

type Saver interface {
	Save() error
}

Saver components can be saved to disk.

type Scanner

type Scanner interface {
	Scan(visit VisitFunc) error
}

Scanner components can be scanned. It is called when the game tree is walked (such as when the game component database is constructed) and various times afterward (such as during Update). Scan should visit each immediate subcomponent, aborting early if an error is returned from the visitor.

type Scene

type Scene struct {
	ID
	Bounds // world coordinates
	Child  any
	Disables
	Hides
}

Scene is a component for adding an identity, bounds, and other properties.

func (*Scene) Scan

func (s *Scene) Scan(visit VisitFunc) error

Scan visits s.Child.

func (*Scene) String

func (s *Scene) String() string

type SceneRef

type SceneRef struct {
	Path string

	*Scene // not gob encoded
}

SceneRef loads a gzipped, gob-encoded Scene from the asset FS. After Load, Scene is usable. This is mostly useful for scenes that refer to other scenes, e.g.

   sc := &Scene{
	    Components: []any{
			&SceneRef{Path: "assets/foo.gob.gz"}  // inflated at Load time
		},
   }

func (*SceneRef) GobDecode

func (r *SceneRef) GobDecode(b []byte) error

GobDecode saves the byte slice as Path.

func (*SceneRef) GobEncode

func (r *SceneRef) GobEncode() ([]byte, error)

GobEncode returns Path as a byte slice.

func (*SceneRef) Load

func (r *SceneRef) Load(assets fs.FS) error

Load loads the scene from the file.

func (*SceneRef) Save

func (r *SceneRef) Save() error

Save saves the scene to a file in the current directory.

func (*SceneRef) String

func (r *SceneRef) String() string

type Sheet

type Sheet struct {
	AnimDefs map[string]*AnimDef
	CellSize image.Point
	Src      ImageRef
	// contains filtered or unexported fields
}

Sheet handles images that consist of a grid of equally sized regions (cells) and can produce subimages for the cell at an index. This is useful for various applications such as sprite animation and tile maps. Additionally each sheet carries a collection of animations that use the sheet.

func (*Sheet) NewAnim

func (s *Sheet) NewAnim(key string) *Anim

NewAnim returns a new Anim for the given key, or nil if not found in AnimDefs.

func (*Sheet) NewAnims

func (s *Sheet) NewAnims() map[string]*Anim

NewAnims returns a new Anim for every AnimDef in the AnimDefs map.

func (*Sheet) Prepare

func (s *Sheet) Prepare(*Game) error

Prepare computes the width of the image (in cells).

func (*Sheet) Scan

func (s *Sheet) Scan(visit VisitFunc) error

Scan visits &s.Src.

func (*Sheet) String

func (s *Sheet) String() string

func (*Sheet) SubImage

func (s *Sheet) SubImage(i int) *ebiten.Image

SubImage returns an *ebiten.Image corresponding to the given cell index.

type SolidRect

type SolidRect struct {
	ID
	geom.Box
}

SolidRect is a minimal implementation of a Collider defined by a single Box.

func (SolidRect) CollidesWith

func (s SolidRect) CollidesWith(r geom.Box) bool

CollidesWith reports if r overlaps with s.Box.

type Sprite

type Sprite struct {
	Actor      Actor
	DrawOffset image.Point
	Hides
	Sheet Sheet
	// contains filtered or unexported fields
}

Sprite combines an Actor with the ability to Draw from a single spritesheet.

func (*Sprite) Anim

func (s *Sprite) Anim() *Anim

Anim returns the current Anim.

func (*Sprite) BoundingBox

func (s *Sprite) BoundingBox() geom.Box

BoundingBox forwards the call to Actor.

func (*Sprite) Draw

func (s *Sprite) Draw(screen *ebiten.Image, opts *ebiten.DrawImageOptions)

Draw draws the current cell to the screen.

func (*Sprite) Scan

func (s *Sprite) Scan(visit VisitFunc) error

Scan visits &s.Actor and &s.Sheet.

func (*Sprite) SetAnim

func (s *Sprite) SetAnim(a *Anim)

SetAnim sets the Anim to use for the sprite. If it is not the same as the one currently set, it resets the new anim.

func (*Sprite) String

func (s *Sprite) String() string

func (*Sprite) Transform

func (s *Sprite) Transform() (opts ebiten.DrawImageOptions)

Transform returns a translation by the DrawOffset and Actor.Pos projected

func (*Sprite) Update

func (s *Sprite) Update() error

Update updates the Sprite's anim. anim can change a bit so we don't tell Game about it, but that means it must be updated manually.

type StaticTile

type StaticTile int

StaticTile returns a fixed tile index.

func (StaticTile) Cell

func (s StaticTile) Cell() int

Cell returns s as an int.

type Tile

type Tile interface {
	Cell() int
}

Tile is the interface needed by Tilemap.

type Tilemap

type Tilemap struct {
	ID
	Disables
	Hides
	Map    map[image.Point]Tile // tilespace coordinate -> tile
	Ersatz bool                 // disables collisions ("fake wall")
	Offset image.Point          // world coordinates
	Sheet  Sheet
}

Tilemap renders a grid of rectangular tiles at equal Z position.

func (*Tilemap) CollidesWith

func (t *Tilemap) CollidesWith(b geom.Box) bool

CollidesWith implements Collider.

func (*Tilemap) Draw

func (t *Tilemap) Draw(screen *ebiten.Image, opts *ebiten.DrawImageOptions)

Draw draws the tilemap.

func (*Tilemap) Load

func (t *Tilemap) Load(fs.FS) error

Load instantiates animations for all AnimatedTiles.

func (*Tilemap) Scan

func (t *Tilemap) Scan(visit VisitFunc) error

Scan visits &t.Sheet and all tiles.

func (*Tilemap) SetTileAt

func (t *Tilemap) SetTileAt(wc image.Point, tile Tile)

SetTileAt sets the tile at the given world coordinate.

func (*Tilemap) TileAt

func (t *Tilemap) TileAt(wc image.Point) Tile

TileAt returns the tile present at the given world coordinate.

func (*Tilemap) TileBounds

func (t *Tilemap) TileBounds(wc image.Point) image.Rectangle

TileBounds returns a rectangle describing the tile boundary for the tile at the given world coordinate.

func (*Tilemap) Transform

func (t *Tilemap) Transform() (opts ebiten.DrawImageOptions)

Transform returns a translation by t.Offset.

type Transformer

type Transformer interface {
	Transform() ebiten.DrawImageOptions
}

Transformer components can provide draw options to apply to themselves and any child components. The opts passed to Draw of a component c will be the cumulative opts of all parents of c plus the value returned from c.Transform.

type Updater

type Updater interface {
	Update() error
}

Updater components can update themselves. Update is called repeatedly. Each component must call Update on any internal components not known to the engine (i.e. not passed to Game.Register or returned from Scan).

type VisitFunc

type VisitFunc func(any) error

VisitFunc callbacks are either provided or called by various Game functions. For example, Query takes two VisitFuncs that are called for each result, and Scan is given a VisitFunc that should be called with each component. For recursive operations, return Skip for components that should be skipped.

func (VisitFunc) Many

func (v VisitFunc) Many(x ...any) error

Many calls a VisitFunc for multiple args, and returns on first non-nil error.

type Wall

type Wall struct {
	ID
	Ersatz     bool        // disables collisions ("fake wall")
	Offset     image.Point //  offset the whole wall
	Sheet      Sheet
	UnitOffset image.Point // drawing offset
	UnitSize   image.Point // tile size
	Units      map[image.Point]*WallUnit
}

Wall is a more flexible kind of tilemap. WallUnits can be added at the same level as other components and are responsible for their own drawing, so that Game can do draw ordering, e.g. hide the player character behind a wall. But Wall is still responsible for collisions.

func (*Wall) CollidesWith

func (w *Wall) CollidesWith(b geom.Box) bool

CollidesWith implements a tilerange collosion check, similar to Tilemap.

func (*Wall) Prepare

func (w *Wall) Prepare(*Game) error

Prepare makes sure all WallUnits know about Wall and where they are, for drawing.

func (*Wall) Scan

func (w *Wall) Scan(visit VisitFunc) error

Scan visits &w.Sheet and all WallUnits.

func (*Wall) Transform

func (w *Wall) Transform() (opts ebiten.DrawImageOptions)

Transform returns a GeoM translation by Offset.

type WallUnit

type WallUnit struct {
	Disables
	Hides
	Tile Tile // chooses which cell in wall.Sheet to draw
	// contains filtered or unexported fields
}

WallUnit is a unit in a wall. Unlike a tile in a tilemap, WallUnit is responsible for drawing itself.

func (*WallUnit) Draw

func (u *WallUnit) Draw(screen *ebiten.Image, opts *ebiten.DrawImageOptions)

Draw draws this wall unit.

func (*WallUnit) Scan

func (u *WallUnit) Scan(visit VisitFunc) error

Scan visits u.Tile.

func (*WallUnit) Transform

func (u *WallUnit) Transform() (opts ebiten.DrawImageOptions)

Transform returns a translation by the position and offset.

Jump to

Keyboard shortcuts

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