tau

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Apr 21, 2020 License: BSD-2-Clause, MIT Imports: 13 Imported by: 0

README

tau

WIP

Powered by ebiten

Documentation

Index

Constants

View Source
const (
	SNRepeatSprite string = "tau.RepeatSpriteSystem"
	CNRepeatSprite string = "tau.RepeatSpriteComponent"
)
View Source
const (
	SNSprite = "tau.SpriteSystem"
	CNSprite = "tau.SpriteComponent"
)
View Source
const (
	SNSpriteAnimation     = "tau.SpriteAnimationSystem"
	SNSpriteAnimationLink = "tau.SpriteAnimationLinkSystem"
	CNSpriteAnimation     = "tau.SpriteAnimationComponent"
)
View Source
const (
	SNTransform       = "tau.TransformSystem"
	SNTransformSprite = "tau.TransformSpriteSystem"
	CNTransform       = "tau.TransformComponent"
)
View Source
const (
	// DefaultImageOptions key passed to the default world (&ebiten.DrawImageOptions{})
	DefaultImageOptions string = "default_image_options"
)
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 NewWorld

func NewWorld(e *Engine) *ecs.World

NewWorld creates a new world

func RegisterComponentSystem

func RegisterComponentSystem(cs ComponentSystem)

func RepeatSpriteSystemExec

func RepeatSpriteSystemExec(ctx Context)

RepeatSpriteSystemExec is the function executed every frame

func SetupSystem

func SetupSystem(w *ecs.World, cs ComponentSystem)

func SpriteAnimationLinkSystemExec

func SpriteAnimationLinkSystemExec(ctx Context)

SpriteAnimationLinkSystemExec is what glues the animation and sprite together

func SpriteAnimationSystemExec

func SpriteAnimationSystemExec(ctx Context)

SpriteAnimationSystemExec is the main function of the SpriteSystem

func SpriteSystemExec

func SpriteSystemExec(ctx Context)

SpriteSystemExec is the main function of the SpriteSystem

func TransformSpriteSystemExec

func TransformSpriteSystemExec(ctx Context)

TransformSpriteSystemExec is the main function of the TransformSpriteSystem

func TransformSystemExec

func TransformSystemExec(ctx Context)

TransformSystemExec is the main function of the TransformSystem

func UpsertComponent

func UpsertComponent(w ecs.Worlder, comp ecs.NewComponentInput) *ecs.Component

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      *ecs.World
	Components []*ecs.Component
}

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

func NewArchetype

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

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

func RepeatSpriteArchetype

func RepeatSpriteArchetype(w *ecs.World) *Archetype

RepeatSpriteArchetype order: SpriteComponent(w) RepeatSpriteComponent(w)

func (*Archetype) NewEntity

func (a *Archetype) NewEntity(compdata ...interface{}) ecs.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 BaseComponentSystem

type BaseComponentSystem struct {
}

func (*BaseComponentSystem) SystemExec

func (cs *BaseComponentSystem) SystemExec() SystemExecFn

func (*BaseComponentSystem) SystemInit

func (cs *BaseComponentSystem) SystemInit() SystemInitFn

func (*BaseComponentSystem) SystemPriority

func (cs *BaseComponentSystem) SystemPriority() int

func (*BaseComponentSystem) SystemTags

func (cs *BaseComponentSystem) SystemTags() []string

type BasicCS

type BasicCS struct {
	SysName       string
	SysPriority   int
	SysInit       SystemInitFn
	SysExec       SystemExecFn
	SysTags       []string
	GetComponents func(w ecs.Worlder) []*ecs.Component
}

func (*BasicCS) BasicCS

func (cs *BasicCS) BasicCS() int

func (*BasicCS) Components

func (cs *BasicCS) Components(w ecs.Worlder) []*ecs.Component

func (*BasicCS) SystemExec

func (cs *BasicCS) SystemExec() SystemExecFn

func (*BasicCS) SystemInit

func (cs *BasicCS) SystemInit() SystemInitFn

func (*BasicCS) SystemName

func (cs *BasicCS) SystemName() string

func (*BasicCS) SystemPriority

func (cs *BasicCS) SystemPriority() int

func (*BasicCS) SystemTags

func (cs *BasicCS) SystemTags() []string

type ComponentSystem

type ComponentSystem interface {
	SystemName() string
	SystemPriority() int
	SystemInit() SystemInitFn
	SystemExec() SystemExecFn
	SystemTags() []string
	Components(w ecs.Worlder) []*ecs.Component
}

type Context

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

Context is the context passed to every system update function.

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 Engine

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

Engine is what controls the ECS of tau.

func NewEngine

func NewEngine(v *NewEngineInput) *Engine

NewEngine returns a new Engine

func (*Engine) AddWorld

func (e *Engine) AddWorld(w *ecs.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() *ecs.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 *ecs.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 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 Middleware

type Middleware func(next SystemExecFn) SystemExecFn

Middleware is a system middleware

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 RepeatSprite

type RepeatSprite struct {
	RepeatX      int
	RepeatY      int
	DrawDisabled bool
}

RepeatSprite is the component data of a RepeatSpriteComponent

type RepeatSpriteComponentSystem

type RepeatSpriteComponentSystem struct {
	BaseComponentSystem
}

func (*RepeatSpriteComponentSystem) Components

func (cs *RepeatSpriteComponentSystem) Components(w ecs.Worlder) []*ecs.Component

func (*RepeatSpriteComponentSystem) SystemExec

func (cs *RepeatSpriteComponentSystem) SystemExec() SystemExecFn

func (*RepeatSpriteComponentSystem) SystemName

func (cs *RepeatSpriteComponentSystem) SystemName() string

func (*RepeatSpriteComponentSystem) SystemPriority

func (cs *RepeatSpriteComponentSystem) SystemPriority() int

func (*RepeatSpriteComponentSystem) SystemTags

func (cs *RepeatSpriteComponentSystem) SystemTags() []string

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 SpriteAnimationComponentSystem

type SpriteAnimationComponentSystem struct {
	BaseComponentSystem
}

func (*SpriteAnimationComponentSystem) Components

func (*SpriteAnimationComponentSystem) SystemExec

func (*SpriteAnimationComponentSystem) SystemName

func (cs *SpriteAnimationComponentSystem) SystemName() string

func (*SpriteAnimationComponentSystem) SystemPriority

func (cs *SpriteAnimationComponentSystem) SystemPriority() int

type SpriteAnimationLinkComponentSystem

type SpriteAnimationLinkComponentSystem struct {
	BaseComponentSystem
}

func (*SpriteAnimationLinkComponentSystem) Components

func (*SpriteAnimationLinkComponentSystem) SystemExec

func (*SpriteAnimationLinkComponentSystem) SystemName

func (cs *SpriteAnimationLinkComponentSystem) SystemName() string

func (*SpriteAnimationLinkComponentSystem) SystemPriority

func (cs *SpriteAnimationLinkComponentSystem) SystemPriority() int

func (*SpriteAnimationLinkComponentSystem) SystemTags

func (cs *SpriteAnimationLinkComponentSystem) SystemTags() []string

type SpriteComponentSystem

type SpriteComponentSystem struct {
	BaseComponentSystem
}

func (*SpriteComponentSystem) Components

func (cs *SpriteComponentSystem) Components(w ecs.Worlder) []*ecs.Component

func (*SpriteComponentSystem) SystemExec

func (cs *SpriteComponentSystem) SystemExec() SystemExecFn

func (*SpriteComponentSystem) SystemInit

func (cs *SpriteComponentSystem) SystemInit() SystemInitFn

func (*SpriteComponentSystem) SystemName

func (cs *SpriteComponentSystem) SystemName() string

func (*SpriteComponentSystem) SystemPriority

func (cs *SpriteComponentSystem) SystemPriority() int

func (*SpriteComponentSystem) SystemTags

func (cs *SpriteComponentSystem) SystemTags() []string

type SystemExecFn

type SystemExecFn func(ctx Context)

func SystemWrap

func SystemWrap(fn SystemExecFn, mid ...Middleware) SystemExecFn

SystemWrap wraps middlewares into a SystemExecFn

type SystemInitFn

type SystemInitFn func(w *ecs.World, sys *ecs.System)

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 TransformComponentSystem

type TransformComponentSystem struct {
	BaseComponentSystem
}

func (*TransformComponentSystem) Components

func (cs *TransformComponentSystem) Components(w ecs.Worlder) []*ecs.Component

func (*TransformComponentSystem) SystemExec

func (cs *TransformComponentSystem) SystemExec() SystemExecFn

func (*TransformComponentSystem) SystemInit

func (cs *TransformComponentSystem) SystemInit() SystemInitFn

func (*TransformComponentSystem) SystemName

func (cs *TransformComponentSystem) SystemName() string

func (*TransformComponentSystem) SystemPriority

func (cs *TransformComponentSystem) SystemPriority() int

type TransformSpriteComponentSystem

type TransformSpriteComponentSystem struct {
	BaseComponentSystem
}

func (*TransformSpriteComponentSystem) Components

func (*TransformSpriteComponentSystem) SystemExec

func (*TransformSpriteComponentSystem) SystemName

func (cs *TransformSpriteComponentSystem) SystemName() string

func (*TransformSpriteComponentSystem) SystemPriority

func (cs *TransformSpriteComponentSystem) SystemPriority() int

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 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"
)

Directories

Path Synopsis
cmd
tcli command
examples
animation command
hello command
sprites command
fs
os
internal
sets
Package sets contains implementations of sets.
Package sets contains implementations of sets.
smid
Package smid provides common middlewares for systems.
Package smid provides common middlewares for systems.

Jump to

Keyboard shortcuts

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