Documentation
¶
Index ¶
- Constants
- Variables
- func Clamp(x, min, max float64) float64
- func DefaultComp(st DefaultStarter)
- func DefaultSys(st DefaultStarter)
- func SpriteAnimationLinkSystemExec(ctx Context, screen *ebiten.Image)
- func SpriteAnimationSystemExec(ctx Context, screen *ebiten.Image)
- func SpriteSystemExec(ctx Context, screen *ebiten.Image)
- func TransformSpriteSystemExec(ctx Context, screen *ebiten.Image)
- func TransformSystemExec(ctx Context, screen *ebiten.Image)
- type AnimClipMode
- type Archetype
- type Component
- type Context
- type DefaultStarter
- type Dict
- type Dicter
- type Engine
- type EngineOptions
- type Entity
- type Matrix
- func (m Matrix) Chained(next Matrix) Matrix
- func (m Matrix) Moved(delta Vec) Matrix
- func (m Matrix) Project(u Vec) Vec
- func (m Matrix) Rotated(around Vec, angle float64) Matrix
- func (m Matrix) Scaled(around Vec, scale float64) Matrix
- func (m Matrix) ScaledXY(around Vec, scale Vec) Matrix
- func (m Matrix) String() string
- func (m Matrix) Unproject(u Vec) Vec
- type NewComponentInput
- type NewEngineInput
- type Rect
- func (r Rect) Area() float64
- func (r Rect) Center() Vec
- func (r Rect) Contains(u Vec) bool
- func (r Rect) H() float64
- func (r Rect) Intersect(s Rect) Rect
- func (r Rect) Moved(delta Vec) Rect
- func (r Rect) Norm() Rect
- func (r Rect) Resized(anchor, size Vec) Rect
- func (r Rect) ResizedMin(size Vec) Rect
- func (r Rect) Size() Vec
- func (r Rect) String() string
- func (r Rect) Union(s Rect) Rect
- func (r Rect) W() float64
- type Sprite
- type SpriteAnimation
- type SpriteAnimationClip
- type System
- type SystemFn
- type SystemMiddleware
- type Transform
- type Vec
- func (u Vec) Add(v Vec) Vec
- func (u Vec) Angle() float64
- func (u Vec) Cross(v Vec) float64
- func (u Vec) Dot(v Vec) float64
- func (u Vec) Len() float64
- func (u Vec) Map(f func(float64) float64) Vec
- func (u Vec) Normal() Vec
- func (u Vec) Project(v Vec) Vec
- func (u Vec) Rotated(angle float64) Vec
- func (u Vec) Scaled(c float64) Vec
- func (u Vec) ScaledXY(v Vec) Vec
- func (u Vec) String() string
- func (u Vec) Sub(v Vec) Vec
- func (u Vec) To(v Vec) Vec
- func (u Vec) Unit() Vec
- func (u Vec) XY() (x, y float64)
- type View
- type World
- func (w *World) NewComponent(input NewComponentInput) (*Component, error)
- func (w *World) NewSystem(name string, priority int, fn SystemFn, comps ...*Component) *System
- func (w *World) Run(screen *ebiten.Image, delta float64) (taken time.Duration)
- func (w *World) RunWithTag(tag string, screen *ebiten.Image, delta float64) (taken time.Duration)
- func (w *World) RunWithoutTag(tag string, screen *ebiten.Image, delta float64) (taken time.Duration)
- type WorldComponents
- type WorldDicter
- type WorldTag
- type Worlder
Constants ¶
const ( // SpritePriority - default -10 SpritePriority int = -10 // DefaultImageOptions key passed to the default world (&ebiten.DrawImageOptions{}) DefaultImageOptions string = "default_image_options" )
const ( // SpriteAnimationPriority - default -6 SpriteAnimationPriority int = -6 // SpriteAnimationLinkPriority - default -5 SpriteAnimationLinkPriority int = -5 )
const ( // TransformPriority - default 0 TransformPriority int = 0 // TransformSpritePriority - default -6 (execs after positioning the transforms) TransformSpritePriority int = -6 )
const TagDelta = "delta"
TagDelta is the key set of the time taken between frames (in seconds)
Variables ¶
var IM = Matrix{1, 0, 0, 1, 0, 0}
IM stands for identity matrix. Does nothing, no transformation.
var ZV = Vec{0, 0}
ZV is a zero vector.
Functions ¶
func Clamp ¶
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 SpriteAnimationLinkSystemExec ¶
SpriteAnimationLinkSystemExec is what glues the animation and sprite together
func SpriteAnimationSystemExec ¶
SpriteAnimationSystemExec is the main function of the SpriteSystem
func SpriteSystemExec ¶
SpriteSystemExec is the main function of the SpriteSystem
func TransformSpriteSystemExec ¶
TransformSpriteSystemExec is the main function of the TransformSpriteSystem
func TransformSystemExec ¶
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 ¶
Archetype is a recipe to create entities with a preset of components.
func NewArchetype ¶
NewArchetype returns a new archetype. This func unsures that no duplicated components are added.
type Component ¶
Component -> ecs.Component
func SpriteAnimationComponent ¶
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 ¶
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 ¶
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 DefaultStarter ¶
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.
type Engine ¶
type Engine struct {
// contains filtered or unexported fields
}
Engine is what controls the ECS of troupe.
func (*Engine) AddWorld ¶
AddWorld adds a world to the engine. The priority is used to sort world execution, from hight to low.
func (*Engine) RemoveWorld ¶
RemoveWorld removes a *World
type EngineOptions ¶
EngineOptions is used to setup Ebiten @ Engine.boot
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 ¶
Chained adds another Matrix to this one. All tranformations by the next Matrix will be applied after the transformations of this Matrix.
func (Matrix) Project ¶
Project applies all transformations added to the Matrix to a vector u and returns the result.
Time complexity is O(1).
func (Matrix) Rotated ¶
Rotated rotates everything around a given point by the given angle in radians.
func (Matrix) ScaledXY ¶
ScaledXY scales everything around a given point by the scale factor in each axis respectively.
func (Matrix) 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 ¶
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 ¶
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 ¶
R returns a new Rect with given the Min and Max coordinates.
Note that the returned rectangle is not automatically normalized.
func (Rect) Contains ¶
Contains checks whether a vector u is contained within this Rect (including it's borders).
func (Rect) Intersect ¶
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) Norm ¶
Norm returns the Rect in normal form, such that Max is component-wise greater or equal than Min.
func (Rect) Resized ¶
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 ¶
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) 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)
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 ¶
GetPrecomputedImage returns the last precomputed image
func (*Sprite) GetPrecomputedImageDim ¶
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 ¶
System -> ecs.System
func SpriteAnimationLinkSystem ¶
SpriteAnimationLinkSystem creates the sprite system
func SpriteAnimationSystem ¶
SpriteAnimationSystem creates the sprite system
func TransformSpriteSystem ¶
TransformSpriteSystem creates the transform sprite system
func TransformSystem ¶
TransformSystem creates the transform system
type SystemFn ¶
SystemFn is the loop function of a system
func SysWrapFn ¶
func SysWrapFn(fn SystemFn, mid ...SystemMiddleware) SystemFn
SysWrapFn wraps middlewares into SystemFn
type SystemMiddleware ¶
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 ¶
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 (Vec) Angle ¶
Angle returns the angle between the vector u and the x-axis. The result is in range [-Pi, Pi].
func (Vec) Map ¶
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 ¶
Normal returns a vector normal to u. Equivalent to u.Rotated(math.Pi / 2), but faster.
func (Vec) Project ¶
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) 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)
type World ¶
func (*World) NewComponent ¶
func (w *World) NewComponent(input NewComponentInput) (*Component, error)
NewComponent creates a new component
func (*World) RunWithTag ¶
RunWithTag runs all systems with 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)