bento

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Apr 22, 2022 License: MIT Imports: 13 Imported by: 0

README

bento

Bento is a high-level game engine based on ebiten that simplifies creating 2D games.

Bento is meant to be used together with ebiten; you can always fallback to ebiten for finer-grained control over graphics.

You may want to read the API documentation and general reference first.

Disclaimer

Bento is experimental, and it is under developement. Its API is subject to breaking changes.

Documentation

Index

Constants

View Source
const (
	// Right moves an image to the right of a point.
	Right Align = 1 << iota
	// HCenter moves an image to the horizontal center of a point.
	HCenter
	// Left moves an image to the left of a point.
	Left
	// Top moves an image above a point.
	Top
	// VCenter moves an image to the vertical center of a point.
	VCenter
	// Bottom moves an image below a point.
	Bottom

	// Default is the default alignment of an image (to the right and below).
	Default = BottomRight

	TopLeft   = Top | Left
	TopCenter = Top | HCenter
	TopRight  = Top | Right

	CenterLeft  = VCenter | Left
	Center      = VCenter | HCenter
	CenterRight = VCenter | Right

	BottomLeft   = Bottom | Left
	BottomCenter = Bottom | Center
	BottomRight  = Bottom | Right
)
View Source
const (
	// Deg90 is a 90 degree turn in radians.
	Deg90 = math.Pi / 2

	// Deg180 is a 180 degree turn in radians.
	Deg180 = math.Pi

	// Deg270 is a 270 degree turn in radians.
	Deg270 = 1.5 * math.Pi
)

Variables

This section is empty.

Functions

func Bound

func Bound(point, size image.Point) image.Rectangle

Bound calculates a bound, given its top-left point and its size.

func DPIScale

func DPIScale(res int) int

DPIScale scales the given resolution by the device's scale factor. This allows high-DPI rendering.

func Keypress

func Keypress(keys []ebiten.Key) bool

Keypress checks if the key is pressed in the current frame.

func NewImageBound

func NewImageBound(bound image.Rectangle) *ebiten.Image

NewImageBound creates an image from a bound.

func Pad

func Pad(bound image.Rectangle, pad image.Point) image.Rectangle

Pad adds padding to the bound by a fixed amount.

func Radian

func Radian(degree float64) float64

Radian converts an angle in degrees to radians.

func SecondToTick

func SecondToTick(seconds float64) int

SecondToTick converts seconds to ticks.

func TickToSecond

func TickToSecond(ticks int) float64

TickToSecond converts ticks to seconds.

func Unpad

func Unpad(bound image.Rectangle, pad image.Point) image.Rectangle

Unpad removes padding from a bound by a fixed amount.

Types

type Align

type Align int

Align specifies the alignment to render an image at a point (x, y). Align must have at most one horizontal (AlignRight, AlignHCenter, AlignLeft) and vertical (AlignTop, AlignVCenter, AlignBottom) flag.

func (Align) Align

func (a Align) Align(point, size image.Point) image.Point

Align adjusts the point so that it will be the top-left point of an image given its size. The adjusted point can then be passed to ebiten.Image.DrawImage so the image will be in the correct position.

func (Align) Has

func (a Align) Has(flag Align) bool

Has checks if the alignment flag is set.

func (Align) Point

func (a Align) Point(bounds image.Rectangle) image.Point

Point calculates a point in the bounds of an image.

type Animation

type Animation interface {
	Component

	// Draw renders the animation to the image.
	Draw(img *ebiten.Image)
	// Done checks if the animation has finished.
	Done() bool
}

Animation is a effect rendered on a scene/sprite.

type Clock

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

Clock acts as a basic scheduler for operations. One tick is equivalent to a single frame, where 1 second is 60 frames.

func NewClock

func NewClock(n float64) *Clock

NewClock creates a new clock that triggers every n seconds.

func NewClockOnce

func NewClockOnce(n float64) *Clock

NewClockOnce creates a new clock that triggers once after n seconds.

func (*Clock) Done

func (c *Clock) Done() bool

Done checks if the clock's timer has triggered on the current tick. If the timer was set with ScheduleOnce, this will only return true one time.

func (*Clock) Limit

func (c *Clock) Limit() int

Limit returns the clock's timer in ticks.

func (*Clock) Schedule

func (c *Clock) Schedule(n float64)

Schedule sets the clock's timer to trigger every n seconds. This will panic if n is too small to calculate ticks for.

func (*Clock) ScheduleOnce

func (c *Clock) ScheduleOnce(n float64)

ScheduleOnce sets the clock's timer to trigger once, after n seconds. This will panic if n is too small to calculate ticks for.

func (*Clock) Tick

func (c *Clock) Tick()

Tick increments the clock's tick count. This must be called once every tick in ebiten's game loop.

func (*Clock) Ticks

func (c *Clock) Ticks() int

Ticks returns the current ticks of the clock.

type Component added in v0.2.0

type Component interface {
	Update() error
}

Component is a state that updates every tick. Update must be called before using the component's state, i.e

type myComponent struct {
	sub Component
}
func (mc *myComponent) Update() error {
	if err := mc.sub.Update(); err != nil {
		return err
	}

	// use mc.sub here
}

or else accessing the component's state may result in undefined behaviour/panics.

type Debug

type Debug struct {
	Font *Font
}

Debug is a struct of options for debug mode. A font is required to render certain elements.

type Delta

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

Delta is a delta that changes over time.

func NewDelta

func NewDelta(
	dalgo DeltaAlgorithm,
	delta image.Point,
	period float64,
) *Delta

NewDelta creates an delta with the total delta, and the period over which to increase the current delta.

func (*Delta) Delta

func (d *Delta) Delta() (x, y float64)

Delta returns the current delta. This will panic if delta has not been updated yet.

func (*Delta) DeltaPt added in v0.2.0

func (d *Delta) DeltaPt() image.Point

func (*Delta) Done

func (d *Delta) Done() bool

Done checks if the current delta is equal to the total delta.

func (*Delta) Update

func (d *Delta) Update()

Update updates the delta.

type DeltaAlgorithm

type DeltaAlgorithm int

DeltaAlgorithm specifies the algorithm to use when generating deltas.

const (
	// Linear specifies a delta is constant.
	Linear DeltaAlgorithm = iota
	// Exponential specifies a delta in exponential (e^x) space.
	Exponential
)

type Entity

type Entity struct {
	Sprite
	*Transition

	Op *ebiten.DrawImageOptions
}

Entity is a sprite with rendering state. While a sprite renders to an image, an entity handles drawing the rendered image to the screen. Op is the options used to draw the entity to the screen.

func NewEntities

func NewEntities(sprites ...Sprite) []*Entity

NewEntities constructs a slice of entities from several sprites.

func NewEntity

func NewEntity(sprite Sprite) *Entity

NewEntity constructs an entity from a sprite. Entities are hidden by default.

func (*Entity) Draw

func (e *Entity) Draw(screen *ebiten.Image)

Draw draws the sprite's render onto the screen.

func (*Entity) Update

func (e *Entity) Update() error

Update updates the sprite's state.

type Font

type Font struct {
	Face font.Face
}

Font is a wrapper around a fontface, used to render text to images.

func (*Font) Draw

func (f *Font) Draw(
	str string,
	clr color.Color,
	img *ebiten.Image,
	point image.Point,
)

Draw renders the text on an image at the point as-is (i.e without any alignment). NOTE: point is the bottom-left point of the text.

func (*Font) Load

func (f *Font) Load(src []byte, opts *opentype.FaceOptions) error

Load loads an OpenType fontface from a source.

func (*Font) Write

func (f *Font) Write(
	str string,
	clr color.Color,
	img *ebiten.Image,
	point image.Point,
	align Align,
) image.Rectangle

Write renders the text on an image at the point with alignment and returns its bounds. point is the top-left point of the text.

func (*Font) WriteCenter

func (f *Font) WriteCenter(str string, clr color.Color, img *ebiten.Image) image.Rectangle

WriteCenter renders the text in the center of an image.

type InitError added in v0.2.0

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

InitError indicates that the component has not been updated yet with .Update.

func (*InitError) Error added in v0.2.0

func (ie *InitError) Error() string

type RenderState

type RenderState int

RenderState represents the rendering state of an entity on the stage.

The render lifecycle has several states: Hidden (default): The entity is not rendering to the screen. Entering: An enter transition is being rendered on the entity. Visible: The entity is rendering normally. Exiting: An exit transition is being rendered on the entity.

After an entity has exited the stage, the render state will change back to Hidden.

const (
	Hidden RenderState = iota
	Entering
	Visible
	Exiting
)

func (RenderState) String

func (i RenderState) String() string

type Scene

type Scene interface {
	// Update updates the state of the scene, if any.
	Update(stage *Stage) error
	// Draw renders the scene on screen.
	Draw(screen *ebiten.Image)
	// Enter returns the enter transition of the scene, if any.
	Enter() Animation
	// Exit returns the enter transition of the scene, if any.
	Exit() Animation
	// Entities returns a slice of entities to render on the scene.
	Entities() []*Entity
}

Scene is a special kind of entity that draws directly to a screen instead of rendering to a image.

type Scroll

type Scroll struct {
	Font *Font
	// contains filtered or unexported fields
}

Scroll allows several pieces of text to be scrolled across an image.

func NewScroll

func NewScroll(font *Font, tx string) *Scroll

NewScroll creates a new scroll.

func (*Scroll) Done

func (s *Scroll) Done() bool

Done checks if the scrolling has finished.

func (*Scroll) Draw

func (s *Scroll) Draw(
	clr color.Color,
	point image.Point,
	img *ebiten.Image,
)

Draw renders the scroll on a new image. point is the bottom-left point of the scroll.

func (*Scroll) SetText

func (s *Scroll) SetText(tx string)

SetText changes the text currently scrolling.

func (*Scroll) Size

func (s *Scroll) Size() image.Point

Size returns the total size of the scroll.

func (*Scroll) Skip

func (s *Scroll) Skip()

Skip causes the next render to render the whole text instead of waiting for scrolling.

func (*Scroll) Speed

func (s *Scroll) Speed(secs float64)

Speed changes the speed of scrolling text, where secs is the number of seconds to wait between scrolling each character and must be positive.

func (*Scroll) Text

func (s *Scroll) Text() string

Text returns the current text in the scroll.

func (*Scroll) Update

func (s *Scroll) Update()

Update updates the state of the scroll.

type Sprite

type Sprite interface {
	Component

	// Render renders the sprite to an image, given the screen's size.
	Render(entity *Entity, size image.Point) *ebiten.Image
}

Sprite is an image with state.

type Stage

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

Stage is a scene manager which implements the ebiten.Game interface. The current scene must never be nil. If debug is not nil, debug mode is enabled.

func NewStage

func NewStage(initial Scene, debug *Debug) *Stage

NewStage creates a stage with an inital scene. NOTE: The initial scene's enter animation is rendered!

func (*Stage) Change

func (s *Stage) Change(newScene Scene)

Change changes the scene to render in the next frame.

func (*Stage) Draw

func (s *Stage) Draw(screen *ebiten.Image)

Draw renders the current scene to the screen.

func (*Stage) Layout

func (s *Stage) Layout(outsideWidth, outsideHeight int) (screenWidth, screenHeight int)

Layout returns the screen's size.

func (*Stage) Update

func (s *Stage) Update() error

Update updates the current scene's state.

type Stream

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

Stream is a stream of text over a channel.

func (*Stream) Read

func (s *Stream) Read() *string

Read returns the next text from the stream. If the stream is closed, nil is returned.

func (*Stream) Source

func (s *Stream) Source(fn func(chan<- string))

Source runs the function asynchronusly on the stream.

type Tileset

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

Tileset is a image with tiles that can be tiled into a single sprite.

func NewTileset

func NewTileset(img *ebiten.Image, tsize int, cache bool) *Tileset

NewTileset creates a new tileset with a image and tile size. If cache is true, calls to the Tile method will lazily cache tile images for faster loading.

func (*Tileset) Render

func (t *Tileset) Render(tilemap [][]int) *ebiten.Image

Render renders the tileset to an image, given a tilemap. The tilemap must have at least one row, and all rows must have equal length.

func (*Tileset) Size

func (t *Tileset) Size() image.Point

Size returns the tileset size as (columns, rows).

func (*Tileset) Tile

func (t *Tileset) Tile(index int) *ebiten.Image

Tile returns the tile at index as a subimage of the tileset, where index is the tile column multiplied by the tile row. This panics if the index is out of bounds.

func (*Tileset) Tilesize

func (t *Tileset) Tilesize() int

Tilesize returns the size of a tile in the tileset.

type Transition

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

func NewTransition

func NewTransition() *Transition

NewTransition creates a new transition.

func (*Transition) Draw

func (t *Transition) Draw(image *ebiten.Image)

Draw draws the transition onto the image.

func (*Transition) Hide

func (t *Transition) Hide(exit Animation)

Hide sets the render state to Hidden after rendering an exit transition. If exit is nil, the state is immediately changed.

func (*Transition) RenderState

func (t *Transition) RenderState() RenderState

RenderState returns the rendering state of the transition.

func (*Transition) Show

func (t *Transition) Show(enter Animation)

Show sets the render state to Visible after rendering an enter animation. If enter is nil, the state is immediately changed.

func (*Transition) Update

func (t *Transition) Update() error

Update updates the transition's state.

type Vec

type Vec struct {
	Path vector.Path
}

Vec is a wrapper around ebiten's vector path for drawing operations.

func (*Vec) Arc

func (vec *Vec) Arc(center image.Point, radius int, from, to float32)

Arc draws a circular arc with a center and radius. from and to are angles in radians.

func (*Vec) Circle

func (vec *Vec) Circle(center image.Point, radius int)

Circle draws a circle with a center and radius.

func (*Vec) Draw

func (vec *Vec) Draw(
	clr color.Color,
	img *ebiten.Image,
	o *ebiten.DrawTrianglesOptions,
)

Draw renders this vector path with color to an image.

func (*Vec) DrawShader

func (vec *Vec) DrawShader(
	clr color.Color,
	img *ebiten.Image,
	shader *ebiten.Shader,
	o *ebiten.DrawTrianglesShaderOptions,
)

DrawShader renders this vector path with a shader to an image.

func (*Vec) Line

func (vec *Vec) Line(to image.Point)

Line draws a line from the current position to another position.

func (*Vec) Move

func (vec *Vec) Move(to image.Point)

Move moves the vec's currnet position to a new position. This doesn't draw anything.

func (*Vec) Rect

func (vec *Vec) Rect(bounds image.Rectangle)

Rect draws a rectangle with bounds.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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