troupe

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Dec 20, 2019 License: BSD-2-Clause, MIT Imports: 13 Imported by: 0

README

Troupe

WIP

Powered by ebiten

Documentation

Index

Constants

View Source
const (
	// SpritePriority - default -10
	SpritePriority int = -10
	// DefaultImageOptions key passed to the default world (&ebiten.DrawImageOptions{})
	DefaultImageOptions string = "default_image_options"
)
View Source
const (
	// SpriteAnimationPriority - default -6
	SpriteAnimationPriority int = -6
	// SpriteAnimationLinkPriority - default -5
	SpriteAnimationLinkPriority int = -5
)
View Source
const (
	// TransformPriority - default 0
	TransformPriority int = 0
	// TransformSpritePriority - default -6 (execs after positioning the transforms)
	TransformSpritePriority int = -6
)
View Source
const TagDelta = "delta"

TagDelta is the key set of the time taken between frames (in seconds)

Variables

View Source
var IM = Matrix{1, 0, 0, 1, 0, 0}

IM stands for identity matrix. Does nothing, no transformation.

View Source
var ZV = Vec{0, 0}

ZV is a zero vector.

Functions

func Clamp

func Clamp(x, min, max float64) float64

Clamp returns x clamped to the interval [min, max].

If x is less than min, min is returned. If x is more than max, max is returned. Otherwise, x is returned.

func DefaultComp

func DefaultComp(st DefaultStarter)

DefaultComp will add a component starter function

func DefaultSys

func DefaultSys(st DefaultStarter)

DefaultSys will add a system starter function

func SpriteAnimationLinkSystemExec

func SpriteAnimationLinkSystemExec(ctx Context, screen *ebiten.Image)

SpriteAnimationLinkSystemExec is what glues the animation and sprite together

func SpriteAnimationSystemExec

func SpriteAnimationSystemExec(ctx Context, screen *ebiten.Image)

SpriteAnimationSystemExec is the main function of the SpriteSystem

func SpriteSystemExec

func SpriteSystemExec(ctx Context, screen *ebiten.Image)

SpriteSystemExec is the main function of the SpriteSystem

func TransformSpriteSystemExec

func TransformSpriteSystemExec(ctx Context, screen *ebiten.Image)

TransformSpriteSystemExec is the main function of the TransformSpriteSystem

func TransformSystemExec

func TransformSystemExec(ctx Context, screen *ebiten.Image)

TransformSystemExec is the main function of the TransformSystem

Types

type AnimClipMode

type AnimClipMode byte

AnimClipMode determines how time is treated outside of the keyframed range of an animation clip.

const (
	// AnimOnce - When time reaches the end of the animation clip, the clip will automatically
	// stop playing and time will be reset to beginning of the clip.
	AnimOnce AnimClipMode = 0
	// AnimLoop - When time reaches the end of the animation clip, time will continue at the beginning.
	AnimLoop AnimClipMode = 1
	// AnimPingPong - When time reaches the end of the animation clip, time will ping pong back between beginning
	// and end.
	AnimPingPong AnimClipMode = 2
	// AnimClampForever - Plays back the animation. When it reaches the end, it will keep playing the last frame
	// and never stop playing.
	//
	// When playing backwards it will reach the first frame and will keep playing that. This is useful for code
	// that uses the Playing bool to avoid activating said trigger.
	AnimClampForever AnimClipMode = 3
)

type Archetype

type Archetype struct {
	World      *World
	Components []*Component
}

Archetype is a recipe to create entities with a preset of components.

func NewArchetype

func NewArchetype(world *World, comps ...*Component) *Archetype

NewArchetype returns a new archetype. This func unsures that no duplicated components are added.

func (*Archetype) NewEntity

func (a *Archetype) NewEntity(compdata ...interface{}) Entity

NewEntity adds a new entity with the component data to the world

The best way to create an archetype entity is to ensure that the component data follows the same order that the components were created in NewArchetype

type Component

type Component = ecs.Component

Component -> ecs.Component

func SpriteAnimationComponent

func SpriteAnimationComponent(w Worlder) *Component

SpriteAnimationComponent will get the registered sprite anim component of the world. If a component is not present, it will create a new component using world.NewComponent

func SpriteComponent

func SpriteComponent(w Worlder) *Component

SpriteComponent will get the registered sprite component of the world. If a component is not present, it will create a new component using world.NewComponent

func TransformComponent

func TransformComponent(w Worlder) *Component

TransformComponent will get the registered transform component of the world. If a component is not present, it will create a new component using world.NewComponent

type Context

type Context interface {
	ecs.Context
	Engine() *Engine
	FPS() float64
	Frame() int64
	IsDrawingSkipped() bool
	DefaultDrawImageOptions() *ebiten.DrawImageOptions
}

type DefaultStarter

type DefaultStarter func(e *Engine, w *World)

DefaultStarter is the function used to start a component or a system

type Dict

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

Dict is a concurrency-safe map.

func (*Dict) Exists

func (d *Dict) Exists(key string) bool

Exists checks if the item exists

func (*Dict) Get

func (d *Dict) Get(key string) interface{}

Get an item

func (*Dict) Set

func (d *Dict) Set(key string, value interface{})

Set an item

type Dicter

type Dicter = ecs.Dicter

Dicter -> ecs.Dicter

type Engine

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

Engine is what controls the ECS of troupe.

func NewEngine

func NewEngine(v *NewEngineInput) *Engine

NewEngine returns a new Engine

func (*Engine) AddWorld

func (e *Engine) AddWorld(w *World, priority int)

AddWorld adds a world to the engine. The priority is used to sort world execution, from hight to low.

func (*Engine) Default

func (e *Engine) Default() *World

Default world

func (*Engine) FS

func (e *Engine) FS() fs.Filesystem

FS returns the filesystem

func (*Engine) Get

func (e *Engine) Get(key string) interface{}

Get an item from the global map

func (*Engine) RemoveWorld

func (e *Engine) RemoveWorld(w *World) bool

RemoveWorld removes a *World

func (*Engine) Run

func (e *Engine) Run() error

Run boots up the game engine

func (*Engine) Set

func (e *Engine) Set(key string, value interface{})

Set an item to the global map

type EngineOptions

type EngineOptions struct {
	Width  int
	Height int
	Scale  float64
	Title  string
}

EngineOptions is used to setup Ebiten @ Engine.boot

type Entity

type Entity = ecs.Entity

Entity -> ecs.Entity

type Matrix

type Matrix [6]float64

Matrix is a 2x3 affine matrix that can be used for all kinds of spatial transforms, such as movement, scaling and rotations.

Matrix has a handful of useful methods, each of which adds a transformation to the matrix. For example:

pixel.IM.Moved(pixel.V(100, 200)).Rotated(pixel.ZV, math.Pi/2)

This code creates a Matrix that first moves everything by 100 units horizontally and 200 units vertically and then rotates everything by 90 degrees around the origin.

Layout is: [0] [2] [4] [1] [3] [5]

0   0   1  (implicit row)

func (Matrix) Chained

func (m Matrix) Chained(next Matrix) Matrix

Chained adds another Matrix to this one. All tranformations by the next Matrix will be applied after the transformations of this Matrix.

func (Matrix) Moved

func (m Matrix) Moved(delta Vec) Matrix

Moved moves everything by the delta vector.

func (Matrix) Project

func (m Matrix) Project(u Vec) Vec

Project applies all transformations added to the Matrix to a vector u and returns the result.

Time complexity is O(1).

func (Matrix) Rotated

func (m Matrix) Rotated(around Vec, angle float64) Matrix

Rotated rotates everything around a given point by the given angle in radians.

func (Matrix) Scaled

func (m Matrix) Scaled(around Vec, scale float64) Matrix

Scaled scales everything around a given point by the scale factor.

func (Matrix) ScaledXY

func (m Matrix) ScaledXY(around Vec, scale Vec) Matrix

ScaledXY scales everything around a given point by the scale factor in each axis respectively.

func (Matrix) String

func (m Matrix) String() string

String returns a string representation of the Matrix.

m := pixel.IM
fmt.Println(m) // Matrix(1 0 0 | 0 1 0)

func (Matrix) Unproject

func (m Matrix) Unproject(u Vec) Vec

Unproject does the inverse operation to Project.

It turns out that multiplying a vector by the inverse matrix of m can be nearly-accomplished by subtracting the translate part of the matrix and multplying by the inverse of the top-left 2x2 matrix, and the inverse of a 2x2 matrix is simple enough to just be inlined in the computation.

Time complexity is O(1).

type NewComponentInput

type NewComponentInput = ecs.NewComponentInput

NewComponentInput -> ecs.NewComponentInput

type NewEngineInput

type NewEngineInput struct {
	Width  int
	Height int
	Scale  float64
	Title  string
	FS     fs.Filesystem
}

NewEngineInput is the input data of NewEngine

func (*NewEngineInput) Options

func (i *NewEngineInput) Options() EngineOptions

Options will create a EngineOptions struct to be used in an *Engine

type Rect

type Rect struct {
	Min, Max Vec
}

Rect is a 2D rectangle aligned with the axes of the coordinate system. It is defined by two points, Min and Max.

The invariant should hold, that Max's components are greater or equal than Min's components respectively.

func R

func R(minX, minY, maxX, maxY float64) Rect

R returns a new Rect with given the Min and Max coordinates.

Note that the returned rectangle is not automatically normalized.

func (Rect) Area

func (r Rect) Area() float64

Area returns the area of r. If r is not normalized, area may be negative.

func (Rect) Center

func (r Rect) Center() Vec

Center returns the position of the center of the Rect.

func (Rect) Contains

func (r Rect) Contains(u Vec) bool

Contains checks whether a vector u is contained within this Rect (including it's borders).

func (Rect) H

func (r Rect) H() float64

H returns the height of the Rect.

func (Rect) Intersect

func (r Rect) Intersect(s Rect) Rect

Intersect returns the maximal Rect which is covered by both r and s. Rects r and s must be normalized.

If r and s don't overlap, this function returns R(0, 0, 0, 0).

func (Rect) Moved

func (r Rect) Moved(delta Vec) Rect

Moved returns the Rect moved (both Min and Max) by the given vector delta.

func (Rect) Norm

func (r Rect) Norm() Rect

Norm returns the Rect in normal form, such that Max is component-wise greater or equal than Min.

func (Rect) Resized

func (r Rect) Resized(anchor, size Vec) Rect

Resized returns the Rect resized to the given size while keeping the position of the given anchor.

r.Resized(r.Min, size)      // resizes while keeping the position of the lower-left corner
r.Resized(r.Max, size)      // same with the top-right corner
r.Resized(r.Center(), size) // resizes around the center

This function does not make sense for resizing a rectangle of zero area and will panic. Use ResizedMin in the case of zero area.

func (Rect) ResizedMin

func (r Rect) ResizedMin(size Vec) Rect

ResizedMin returns the Rect resized to the given size while keeping the position of the Rect's Min.

Sizes of zero area are safe here.

func (Rect) Size

func (r Rect) Size() Vec

Size returns the vector of width and height of the Rect.

func (Rect) String

func (r Rect) String() string

String returns the string representation of the Rect.

r := pixel.R(100, 50, 200, 300)
r.String()     // returns "Rect(100, 50, 200, 300)"
fmt.Println(r) // Rect(100, 50, 200, 300)

func (Rect) Union

func (r Rect) Union(s Rect) Rect

Union returns the minimal Rect which covers both r and s. Rects r and s must be normalized.

func (Rect) W

func (r Rect) W() float64

W returns the width of the Rect.

type Sprite

type Sprite struct {
	X       float64
	Y       float64
	Angle   float64
	ScaleX  float64
	ScaleY  float64
	OriginX float64
	OriginY float64

	Bounds image.Rectangle // Bounds for drawing subimage

	Options *ebiten.DrawImageOptions
	Image   *ebiten.Image

	DrawDisabled bool // if true, the SpriteSystem will not draw this
	// contains filtered or unexported fields
}

Sprite is the data of a sprite component.

func (*Sprite) GetPrecomputedImage

func (s *Sprite) GetPrecomputedImage() *ebiten.Image

GetPrecomputedImage returns the last precomputed image

func (*Sprite) GetPrecomputedImageDim

func (s *Sprite) GetPrecomputedImageDim() (width, height float64)

GetPrecomputedImageDim returns the last precomputed image dimmensions

type SpriteAnimation

type SpriteAnimation struct {
	Enabled     bool
	Playing     bool
	ActiveClip  int
	ActiveFrame int
	Clips       []SpriteAnimationClip
	T           float64
	// Default fps for clips with no fps specified
	Fps float64
	// contains filtered or unexported fields
}

SpriteAnimation holds the data of a sprite animation (and clips)

func (*SpriteAnimation) PlayClip

func (a *SpriteAnimation) PlayClip(name string)

PlayClip sets the animation to play a clip by name

type SpriteAnimationClip

type SpriteAnimationClip struct {
	// The name of an animation is not allowed to be changed during runtime
	// but since this is part of a component (and components shouldn't have logic),
	// it is a public member.
	Name     string
	Frames   []image.Rectangle
	Fps      float64
	ClipMode AnimClipMode
}

SpriteAnimationClip is an animation clip, like a character walk cycle.

type System

type System = ecs.System

System -> ecs.System

func SpriteAnimationLinkSystem

func SpriteAnimationLinkSystem(w *World) *System

SpriteAnimationLinkSystem creates the sprite system

func SpriteAnimationSystem

func SpriteAnimationSystem(w *World) *System

SpriteAnimationSystem creates the sprite system

func SpriteSystem

func SpriteSystem(w *World) *System

SpriteSystem creates the sprite system

func TransformSpriteSystem

func TransformSpriteSystem(w *World) *System

TransformSpriteSystem creates the transform sprite system

func TransformSystem

func TransformSystem(w *World) *System

TransformSystem creates the transform system

type SystemFn

type SystemFn func(ctx Context, screen *ebiten.Image)

SystemFn is the loop function of a system

func SysWrapFn

func SysWrapFn(fn SystemFn, mid ...SystemMiddleware) SystemFn

SysWrapFn wraps middlewares into SystemFn

type SystemMiddleware

type SystemMiddleware func(next SystemFn) SystemFn

SystemMiddleware is a system middleware

type Transform

type Transform struct {
	Parent *Transform
	X      float64
	Y      float64
	Angle  float64
	ScaleX float64
	ScaleY float64

	// calculated transform matrix
	M Matrix
	// contains filtered or unexported fields
}

Transform is a hierarchy based matrix

func NewTransform

func NewTransform() *Transform

NewTransform returns a new transform with ScaleX = 1 and ScaleY = 1

type Vec

type Vec struct {
	X, Y float64
}

Vec is a 2D vector type with X and Y coordinates.

Create vectors with the V constructor:

u := pixel.V(1, 2)
v := pixel.V(8, -3)

Use various methods to manipulate them:

  w := u.Add(v)
  fmt.Println(w)        // Vec(9, -1)
  fmt.Println(u.Sub(v)) // Vec(-7, 5)
  u = pixel.V(2, 3)
  v = pixel.V(8, 1)
  if u.X < 0 {
	     fmt.Println("this won't happen")
  }
  x := u.Unit().Dot(v.Unit())

func Lerp

func Lerp(a, b Vec, t float64) Vec

Lerp returns a linear interpolation between vectors a and b.

This function basically returns a point along the line between a and b and t chooses which one. If t is 0, then a will be returned, if t is 1, b will be returned. Anything between 0 and 1 will return the appropriate point between a and b and so on.

func Unit

func Unit(angle float64) Vec

Unit returns a vector of length 1 facing the given angle.

func V

func V(x, y float64) Vec

V returns a new 2D vector with the given coordinates.

func (Vec) Add

func (u Vec) Add(v Vec) Vec

Add returns the sum of vectors u and v.

func (Vec) Angle

func (u Vec) Angle() float64

Angle returns the angle between the vector u and the x-axis. The result is in range [-Pi, Pi].

func (Vec) Cross

func (u Vec) Cross(v Vec) float64

Cross return the cross product of vectors u and v.

func (Vec) Dot

func (u Vec) Dot(v Vec) float64

Dot returns the dot product of vectors u and v.

func (Vec) Len

func (u Vec) Len() float64

Len returns the length of the vector u.

func (Vec) Map

func (u Vec) Map(f func(float64) float64) Vec

Map applies the function f to both x and y components of the vector u and returns the modified vector.

u := pixel.V(10.5, -1.5)
v := u.Map(math.Floor)   // v is Vec(10, -2), both components of u floored

func (Vec) Normal

func (u Vec) Normal() Vec

Normal returns a vector normal to u. Equivalent to u.Rotated(math.Pi / 2), but faster.

func (Vec) Project

func (u Vec) Project(v Vec) Vec

Project returns a projection (or component) of vector u in the direction of vector v.

Behaviour is undefined if v is a zero vector.

func (Vec) Rotated

func (u Vec) Rotated(angle float64) Vec

Rotated returns the vector u rotated by the given angle in radians.

func (Vec) Scaled

func (u Vec) Scaled(c float64) Vec

Scaled returns the vector u multiplied by c.

func (Vec) ScaledXY

func (u Vec) ScaledXY(v Vec) Vec

ScaledXY returns the vector u multiplied by the vector v component-wise.

func (Vec) String

func (u Vec) String() string

String returns the string representation of the vector u.

u := pixel.V(4.5, -1.3)
u.String()     // returns "Vec(4.5, -1.3)"
fmt.Println(u) // Vec(4.5, -1.3)

func (Vec) Sub

func (u Vec) Sub(v Vec) Vec

Sub returns the difference betweeen vectors u and v.

func (Vec) To

func (u Vec) To(v Vec) Vec

To returns the vector from u to v. Equivalent to v.Sub(u).

func (Vec) Unit

func (u Vec) Unit() Vec

Unit returns a vector of length 1 facing the direction of u (has the same angle).

func (Vec) XY

func (u Vec) XY() (x, y float64)

XY returns the components of the vector in two return values.

type View

type View = ecs.View

View -> ecs.View

type World

type World struct {
	*ecs.World
}

func NewWorld

func NewWorld(e *Engine) *World

NewWorld creates a new world

func (*World) NewComponent

func (w *World) NewComponent(input NewComponentInput) (*Component, error)

NewComponent creates a new component

func (*World) NewSystem

func (w *World) NewSystem(name string, priority int, fn SystemFn, comps ...*Component) *System

NewSystem creates a new system

func (*World) Run

func (w *World) Run(screen *ebiten.Image, delta float64) (taken time.Duration)

Run all systems

func (*World) RunWithTag

func (w *World) RunWithTag(tag string, screen *ebiten.Image, delta float64) (taken time.Duration)

RunWithTag runs all systems with the specified tag

func (*World) RunWithoutTag

func (w *World) RunWithoutTag(tag string, screen *ebiten.Image, delta float64) (taken time.Duration)

RunWithoutTag runs all systems without the specified tag

type WorldComponents

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

func (*WorldComponents) Get

func (wc *WorldComponents) Get(w Worlder) *Component

func (*WorldComponents) Set

func (wc *WorldComponents) Set(w Worlder, c *Component)

type WorldDicter

type WorldDicter = ecs.WorldDicter

WorldDicter -> ecs.WorldDicter

type WorldTag

type WorldTag = string

WorldTag is a tag used to filter systems of a world

const (
	// WorldTagDraw -> systems that draw things
	WorldTagDraw WorldTag = "draw"
	// WorldTagUpdate -> systems that update things (!= draw)
	WorldTagUpdate WorldTag = "update"
)

type Worlder

type Worlder = ecs.Worlder

Worlder -> ecs.Worlder

Directories

Path Synopsis
cmd
tcli command
examples
animation command
hello command
sprites command
fs
os

Jump to

Keyboard shortcuts

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