ecs

package module
v1.6.4 Latest Latest
Warning

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

Go to latest
Published: Jun 4, 2020 License: MIT Imports: 6 Imported by: 1

README

Entity Component System

A fast ECS, game engine agnostic, without external dependencies.

GoDoc

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

Constants

This section is empty.

Variables

View Source
var DefaultContextBuilder = func(c0 context.Context, dt float64, sys *System, w *World) Context {
	return ctxt{
		c:      c0,
		dt:     dt,
		system: sys,
		world:  w,
	}
}
View Source
var NoValidate = func(data interface{}) bool { return true }
View Source
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 Cs added in v1.5.0

func Cs(comps ...*Component) []*Component

Cs is a shorthand to create a component slice

func (*Component) Data added in v1.1.1

func (c *Component) Data(e Entity) interface{}

Data returns the component data of an entity. Returns nil if the entity doesn't have this component.

func (*Component) String

func (c *Component) String() string

String returns the string representation of this component

func (*Component) Validate added in v1.0.1

func (c *Component) Validate(data interface{}) bool

Validate if data belongs to the component.

type ComponentDestructor

type ComponentDestructor func(w *World, entity Entity, data interface{})

ComponentDestructor is an optional destructor function that a component might have.

type Context added in v1.2.0

type Context interface {
	context.Context
	DT() float64
	System() *System
	World() *World
}

type ContextBuilderFn added in v1.2.3

type ContextBuilderFn func(c0 context.Context, dt float64, sys *System, w *World) Context

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

type EntityEvent func(e Entity, w *World)

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

type QueryMatch struct {
	Entity     Entity
	Components map[*Component]interface{}
}

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

func (sys *System) AddTag(tag string) *System

AddTag adds a tag to the system. It is used to filter execution with World.RunWithTag and World.RunWithoutTag

func (*System) ContainsTag

func (sys *System) ContainsTag(tag string) bool

ContainsTag checks if the system contains the tag. It is used to filter execution with World.RunWithTag and World.RunWithoutTag

func (*System) Get added in v1.1.0

func (sys *System) Get(key string) interface{}

Get a variable from the system dictionary

func (*System) RemoveTag

func (sys *System) RemoveTag(tag string) *System

RemoveTag removes a tag from the system. It is used to filter execution with World.RunWithTag and World.RunWithoutTag

func (*System) Set added in v1.1.0

func (sys *System) Set(key string, val interface{})

Set a variable to the system dictionary

func (*System) View added in v1.2.0

func (sys *System) View() *View

View returns the current view of the system

func (*System) World added in v1.1.3

func (sys *System) World() *World

World returns the world the system belongs to.

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)

func (*View) World added in v1.0.2

func (v *View) World() *World

World return the world the view belongs to.

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

func (w *World) AddComponentToEntity(entity Entity, component *Component, data interface{}) error

AddComponentToEntity adds a component to an entity.

All the existing views are updated if the entity matches the requirements.

func (*World) Component added in v1.1.4

func (w *World) Component(name string) *Component

Component returns a registered component by name

func (*World) ContainsEntity

func (w *World) ContainsEntity(entity Entity) bool

ContainsEntity tests if an entity is present in the world.

func (*World) Get added in v1.0.3

func (w *World) Get(key string) interface{}

Get a global variable

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

func (w *World) NewEntities(n int) []Entity

NewEntities creates a "n" amount of entities in the current world.

func (*World) NewEntity

func (w *World) NewEntity() Entity

NewEntity creates and entity and adds it to the current world.

func (*World) NewMaskView added in v1.5.0

func (w *World) NewMaskView(excludemask []*Component, includemask []*Component) *View

NewMaskView creates a view on the current world based on the combination of components.

func (*World) NewSystem

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

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

func (w *World) NewView(components ...*Component) *View

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

func (w *World) RemoveComponentFromEntity(entity Entity, component *Component) error

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

func (w *World) RemoveEntity(entity Entity) bool

RemoveEntity removes an entity from the world (and all components)

func (*World) Run

func (w *World) Run(delta float64) (taken time.Duration)

Run will iterate through all systems loop function (sorted by priority).

func (*World) RunWithTag

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

RunWithTag will iterate through all systems that contain the given tag (sorted by priority).

func (*World) RunWithoutTag

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

RunWithoutTag will iterate through all systems that don't contain the given tag (sorted by priority).

func (*World) Set added in v1.0.3

func (w *World) Set(key string, val interface{})

Set a global variable

func (*World) System added in v1.4.0

func (w *World) System(name string) *System

System returns a registered system by name

Jump to

Keyboard shortcuts

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