Documentation ¶
Overview ¶
Package ecs - Entity Component System
https://en.wikipedia.org/wiki/Entity%E2%80%93component%E2%80%93system
This ECS implementation supports a max of 256 component types per World.
Index ¶
- Variables
- type Component
- type ComponentDestructor
- type Context
- type ContextBuilderFn
- type Entity
- type EntityEvent
- type NewComponentInput
- type QueryMatch
- type System
- func (sys *System) AddTag(tag string) *System
- func (sys *System) ContainsTag(tag string) bool
- func (sys *System) Get(key string) interface{}
- func (sys *System) RemoveTag(tag string) *System
- func (sys *System) Set(key string, val interface{})
- func (sys *System) View() *View
- func (sys *System) World() *World
- type SystemExec
- type SystemExecWrapper
- type SystemMiddleware
- type ValidateComponentData
- type View
- type World
- func (w *World) AddComponentToEntity(entity Entity, component *Component, data interface{}) error
- func (w *World) Component(name string) *Component
- func (w *World) ContainsEntity(entity Entity) bool
- func (w *World) Get(key string) interface{}
- func (w *World) NewComponent(input NewComponentInput) (*Component, error)
- func (w *World) NewEntities(n int) []Entity
- func (w *World) NewEntity() Entity
- func (w *World) NewMaskView(excludemask []*Component, includemask []*Component) *View
- func (w *World) NewSystem(name string, priority int, fn SystemExec, comps ...*Component) *System
- func (w *World) NewSystemX(name string, priority int, fn SystemExec, ...) *System
- func (w *World) NewView(components ...*Component) *View
- func (w *World) Query(components ...*Component) []QueryMatch
- func (w *World) QueryMask(excludemask []*Component, includemask []*Component) []QueryMatch
- func (w *World) RemoveComponentFromEntity(entity Entity, component *Component) error
- func (w *World) RemoveEntity(entity Entity) bool
- func (w *World) Run(delta float64) (taken time.Duration)
- func (w *World) RunWithTag(tag string, delta float64) (taken time.Duration)
- func (w *World) RunWithoutTag(tag string, delta float64) (taken time.Duration)
- func (w *World) Set(key string, val interface{})
- func (w *World) System(name string) *System
Constants ¶
This section is empty.
Variables ¶
var DefaultContextBuilder = func(c0 context.Context, dt float64, sys *System, w *World) Context {
return ctxt{
c: c0,
dt: dt,
system: sys,
world: w,
}
}
var NoValidate = func(data interface{}) bool { return true }
var VoidDestructor = func(w *World, entity Entity, data interface{}) {}
Functions ¶
This section is empty.
Types ¶
type Component ¶
type Component struct {
// contains filtered or unexported fields
}
Component is a container of raw data.
func (*Component) Data ¶ added in v1.1.1
Data returns the component data of an entity. Returns nil if the entity doesn't have this component.
type ComponentDestructor ¶
ComponentDestructor is an optional destructor function that a component might have.
type ContextBuilderFn ¶ added in v1.2.3
type Entity ¶
type Entity uint64
Entity is a unique identifier. It is used to group components in a World.
type EntityEvent ¶ added in v1.6.0
EntityEvent is used for event handlers of entities, views and systems
type NewComponentInput ¶
type NewComponentInput struct { // Name of the component. Used for debugging purposes. Name string // ValidateDataFn is the optional component data validator function. ValidateDataFn ValidateComponentData // DestructorFn is the optional destructor function of a component. DestructorFn ComponentDestructor }
NewComponentInput is the input args of the World.NewComponent function.
type QueryMatch ¶
QueryMatch represents an entity and a subset of components that belongs to this entity.
type System ¶
type System struct {
// contains filtered or unexported fields
}
System is the brain of an ECS. The system performs global actions on every Entity that possesses a Component (or a combination of components) of the same aspect as that System.
func (*System) AddTag ¶
AddTag adds a tag to the system. It is used to filter execution with World.RunWithTag and World.RunWithoutTag
func (*System) ContainsTag ¶
ContainsTag checks if the system contains the tag. It is used to filter execution with World.RunWithTag and World.RunWithoutTag
func (*System) RemoveTag ¶
RemoveTag removes a tag from the system. It is used to filter execution with World.RunWithTag and World.RunWithoutTag
type SystemExec ¶
type SystemExec func(ctx Context)
SystemExec is the loop function of a system.
dt (delta time): the time taken between the last World.Run
view: the combination of entity + component(s) data
func SysWrapFn ¶ added in v1.3.0
func SysWrapFn(fn SystemExec, mid ...SystemMiddleware) SystemExec
type SystemExecWrapper ¶ added in v1.4.2
type SystemExecWrapper func(*World, SystemExec) SystemExec
SystemExecWrapper is used to alter every system exec upon registering a system
var DefaultSystemExecWrapper SystemExecWrapper = func(w *World, x SystemExec) SystemExec {
return x
}
DefaultSystemExecWrapper is called when the system is added to a world. It is used if the World's SystemExecWrapper is unset.
type SystemMiddleware ¶ added in v1.3.0
type SystemMiddleware func(next SystemExec) SystemExec
SystemMiddleware // TODO: godoc
type ValidateComponentData ¶
type ValidateComponentData func(data interface{}) bool
ValidateComponentData is a function that receives data and determines if the received data is the expected struct that the component should have.
type View ¶
type View struct {
// contains filtered or unexported fields
}
View is the result set of a stored query on the data (entity + components).
func (*View) Matches ¶
func (v *View) Matches() []QueryMatch
Matches returns all the matches of a view.
func (*View) SetOnEntityAdded ¶ added in v1.6.0
func (v *View) SetOnEntityAdded(fn EntityEvent)
func (*View) SetOnEntityRemoved ¶ added in v1.6.0
func (v *View) SetOnEntityRemoved(fn EntityEvent)
type World ¶
type World struct { SystemExecWrapper SystemExecWrapper // contains filtered or unexported fields }
World is the "world" that entities, components and systems live and interact.
func NewWorld ¶
func NewWorld() *World
NewWorld creates a world and initializes the internal storage (necessary).
func NewWorldWithCtx ¶ added in v1.2.3
func NewWorldWithCtx(b ContextBuilderFn) *World
NewWorldWithCtx creates a world and sets the context buolder
func (*World) AddComponentToEntity ¶
AddComponentToEntity adds a component to an entity.
All the existing views are updated if the entity matches the requirements.
func (*World) ContainsEntity ¶
ContainsEntity tests if an entity is present in the world.
func (*World) NewComponent ¶
func (w *World) NewComponent(input NewComponentInput) (*Component, error)
NewComponent takes the input and returns a pointer of a component, used to add component data to an entity.
This function is necessary because the data of a component doesn't have any metadata to identify which component it belongs to. That's because the data of a component is as simple as possible, without any embedded logic, just data.
The max amount of component types, per system, is 256.
func (*World) NewEntities ¶
NewEntities creates a "n" amount of entities in the current world.
func (*World) NewMaskView ¶ added in v1.5.0
NewMaskView creates a view on the current world based on the combination of components.
func (*World) NewSystem ¶
NewSystem constructs a new system.
The priority is used to sort the execution between systems.
fn is the loop function of a system.
comps is the combination of components that a system will iterate on.
func (*World) NewSystemX ¶ added in v1.5.0
func (w *World) NewSystemX(name string, priority int, fn SystemExec, includecomps, excludecomps []*Component) *System
NewSystemX constructs a new system.
The priority is used to sort the execution between systems.
fn is the loop function of a system.
includecomps is the combination of components that a system will iterate on. excludecomps is a inverse mask of components that a system will use to exclude entities from the list.
func (*World) NewView ¶
NewView creates a view on the current world based on the combination of components.
func (*World) Query ¶
func (w *World) Query(components ...*Component) []QueryMatch
Query will return all entities (and components) that contain the combination of components.
func (*World) QueryMask ¶ added in v1.5.0
func (w *World) QueryMask(excludemask []*Component, includemask []*Component) []QueryMatch
QueryMask will return all entities (and components) that contain the combination of components, excluding excludemask.
func (*World) RemoveComponentFromEntity ¶
RemoveComponentFromEntity removes a component from an entity.
All the existing views are updated if the entity matches the requirements.
func (*World) RemoveEntity ¶ added in v1.6.4
RemoveEntity removes an entity from the world (and all components)
func (*World) RunWithTag ¶
RunWithTag will iterate through all systems that contain the given tag (sorted by priority).
func (*World) RunWithoutTag ¶
RunWithoutTag will iterate through all systems that don't contain the given tag (sorted by priority).