ecs

package
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Feb 10, 2023 License: MIT Imports: 3 Imported by: 72

Documentation

Overview

Package ecs contains all of Arche's API.

Index

Constants

View Source
const MaskTotalBits = 64

MaskTotalBits is the size of Mask in bits.

It is the maximum number of component types that may exist in any World.

Variables

This section is empty.

Functions

func Add added in v0.3.0

func Add[A any](w *World, entity Entity) *A

Add adds a component type to an entity.

See also World.Add.

func Add2 added in v0.3.0

func Add2[A any, B any](w *World, entity Entity) (*A, *B)

Add2 adds two component types to an entity.

See also World.Add.

func Add3 added in v0.3.0

func Add3[A any, B any, C any](w *World, entity Entity) (*A, *B, *C)

Add3 adds three component types to an entity.

See also World.Add.

func Add4 added in v0.3.0

func Add4[A any, B any, C any, D any](w *World, entity Entity) (*A, *B, *C, *D)

Add4 adds four component types to an entity.

See also World.Add.

func Add5 added in v0.3.0

func Add5[A any, B any, C any, D any, E any](w *World, entity Entity) (*A, *B, *C, *D, *E)

Add5 adds five component types to an entity.

See also World.Add.

func Assign added in v0.3.0

func Assign[A any](w *World, entity Entity, a *A) *A

Assign adds a components to an entity.

See also World.Assign and World.AssignN.

func Assign2 added in v0.3.0

func Assign2[A any, B any](w *World, entity Entity, a *A, b *B) (*A, *B)

Assign2 adds two components to an entity.

See also World.Assign and World.AssignN.

func Assign3 added in v0.3.0

func Assign3[A any, B any, C any](w *World, entity Entity, a *A, b *B, c *C) (*A, *B, *C)

Assign3 adds three components to an entity.

See also World.Assign and World.AssignN.

func Assign4 added in v0.3.0

func Assign4[A any, B any, C any, D any](w *World, entity Entity, a *A, b *B, c *C, d *D) (*A, *B, *C, *D)

Assign4 adds four components to an entity.

See also World.Assign and World.AssignN.

func Assign5 added in v0.3.0

func Assign5[A any, B any, C any, D any, E any](w *World, entity Entity, a *A, b *B, c *C, d *D, e *E) (*A, *B, *C, *D, *E)

Assign5 adds four components to an entity.

See also World.Assign and World.AssignN.

func Remove added in v0.3.0

func Remove[A any](w *World, entity Entity)

Remove removes a component from an entity.

See also World.Remove.

func Remove2 added in v0.3.0

func Remove2[A any, B any](w *World, entity Entity)

Remove2 removes two components from an entity.

See also World.Remove.

func Remove3 added in v0.3.0

func Remove3[A any, B any, C any](w *World, entity Entity)

Remove3 removes three components from an entity.

See also World.Remove.

func Remove4 added in v0.3.0

func Remove4[A any, B any, C any, D any](w *World, entity Entity)

Remove4 removes four components from an entity.

See also World.Remove.

func Remove5 added in v0.3.0

func Remove5[A any, B any, C any, D any, E any](w *World, entity Entity)

Remove5 removes five components from an entity.

See also World.Remove.

Types

type Component added in v0.2.0

type Component struct {
	ID
	Component interface{}
}

Component is a Component ID/Component pointer pair

type Config added in v0.1.3

type Config struct {
	CapacityIncrement int
}

Config provides configuration for an ECS World.

func NewConfig added in v0.1.3

func NewConfig() Config

NewConfig creates a new default World config.

func (Config) Build added in v0.1.3

func (c Config) Build() World

Build creates a World from this Config.

func (Config) WithCapacityIncrement added in v0.1.3

func (c Config) WithCapacityIncrement(inc int) Config

WithCapacityIncrement return a new Config with CapacityIncrement set. Use with method chaining.

type Entity

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

Entity identifier. Holds an entity ID and it's generation for recycling.

Entities should only be created via the World, using World.NewEntity.

func (Entity) IsZero added in v0.1.1

func (e Entity) IsZero() bool

IsZero returns whether this entity is the reserved zero entity.

type ID

type ID uint8

ID is the component identifier type

func ComponentID

func ComponentID[T any](w *World) ID

ComponentID returns the ID for a component type. Registers the type if it is not already registered.

type Map added in v0.3.0

type Map[T any] struct {
	// contains filtered or unexported fields
}

Map provides a type-safe way to access a component type by entity ID.

Create one with NewMap.

func NewMap added in v0.3.0

func NewMap[T any](w *World) Map[T]

NewMap creates a new Map for a component type.

Map provides a type-safe way to access a component type by entity ID.

See also World.Get, World.Has and World.Set.

func (*Map[T]) Get added in v0.3.0

func (g *Map[T]) Get(entity Entity) *T

Get gets the component for the given entity.

See also World.Get.

func (*Map[T]) Has added in v0.3.0

func (g *Map[T]) Has(entity Entity) bool

Has returns whether the entity has the component.

See also World.Has.

func (*Map[T]) Set added in v0.3.0

func (g *Map[T]) Set(entity Entity, comp *T) *T

Set overwrites the component for the given entity.

Panics if the entity does not have a component of that type.

See also World.Set.

type Q1 added in v0.3.0

type Q1[A any] struct {
	Query
	// contains filtered or unexported fields
}

Q1 is a generic query for one component.

Create one with Query1

func Query1 added in v0.3.0

func Query1[A any](w *World) Q1[A]

Query1 creates a generic query for one component.

See also World.Query.

func (*Q1[A]) Get1 added in v0.3.0

func (q *Q1[A]) Get1() *A

Get1 returns the first queried component for the current query position

type Q2 added in v0.3.0

type Q2[A any, B any] struct {
	Query
	// contains filtered or unexported fields
}

Q2 is a generic query for two components.

Create one with Query2

func Query2 added in v0.3.0

func Query2[A any, B any](w *World) Q2[A, B]

Query2 creates a generic query for two components.

See also World.Query.

func (*Q2[A, B]) Get1 added in v0.3.0

func (q *Q2[A, B]) Get1() *A

Get1 returns the first queried component for the current query position

func (*Q2[A, B]) Get2 added in v0.3.0

func (q *Q2[A, B]) Get2() *B

Get2 returns the second queried component for the current query position

func (*Q2[A, B]) GetAll added in v0.3.0

func (q *Q2[A, B]) GetAll() (*A, *B)

GetAll returns all queried components for the current query position

type Q3 added in v0.3.0

type Q3[A any, B any, C any] struct {
	Query
	// contains filtered or unexported fields
}

Q3 is a generic query for three components.

Create one with Query3

func Query3 added in v0.3.0

func Query3[A any, B any, C any](w *World) Q3[A, B, C]

Query3 creates a generic query for three components.

See also World.Query.

func (*Q3[A, B, C]) Get1 added in v0.3.0

func (q *Q3[A, B, C]) Get1() *A

Get1 returns the first queried component for the current query position

func (*Q3[A, B, C]) Get2 added in v0.3.0

func (q *Q3[A, B, C]) Get2() *B

Get2 returns the second queried component for the current query position

func (*Q3[A, B, C]) Get3 added in v0.3.0

func (q *Q3[A, B, C]) Get3() *C

Get3 returns the third queried component for the current query position

func (*Q3[A, B, C]) GetAll added in v0.3.0

func (q *Q3[A, B, C]) GetAll() (*A, *B, *C)

GetAll returns all queried components for the current query position

type Q4 added in v0.3.0

type Q4[A any, B any, C any, D any] struct {
	Query
	// contains filtered or unexported fields
}

Q4 is a generic query for four components.

Create one with Query4

func Query4 added in v0.3.0

func Query4[A any, B any, C any, D any](w *World) Q4[A, B, C, D]

Query4 creates a generic query for four components.

See also World.Query.

func (*Q4[A, B, C, D]) Get1 added in v0.3.0

func (q *Q4[A, B, C, D]) Get1() *A

Get1 returns the first queried component for the current query position

func (*Q4[A, B, C, D]) Get2 added in v0.3.0

func (q *Q4[A, B, C, D]) Get2() *B

Get2 returns the second queried component for the current query position

func (*Q4[A, B, C, D]) Get3 added in v0.3.0

func (q *Q4[A, B, C, D]) Get3() *C

Get3 returns the third queried component for the current query position

func (*Q4[A, B, C, D]) Get4 added in v0.3.0

func (q *Q4[A, B, C, D]) Get4() *D

Get4 returns the fourth queried component for the current query position

func (*Q4[A, B, C, D]) GetAll added in v0.3.0

func (q *Q4[A, B, C, D]) GetAll() (*A, *B, *C, *D)

GetAll returns all queried components for the current query position

type Q5 added in v0.3.0

type Q5[A any, B any, C any, D any, E any] struct {
	Query
	// contains filtered or unexported fields
}

Q5 is a generic query for five components.

Create one with Query5

func Query5 added in v0.3.0

func Query5[A any, B any, C any, D any, E any](w *World) Q5[A, B, C, D, E]

Query5 creates a generic query for five components.

See also World.Query.

func (*Q5[A, B, C, D, E]) Get1 added in v0.3.0

func (q *Q5[A, B, C, D, E]) Get1() *A

Get1 returns the first queried component for the current query position

func (*Q5[A, B, C, D, E]) Get2 added in v0.3.0

func (q *Q5[A, B, C, D, E]) Get2() *B

Get2 returns the second queried component for the current query position

func (*Q5[A, B, C, D, E]) Get3 added in v0.3.0

func (q *Q5[A, B, C, D, E]) Get3() *C

Get3 returns the third queried component for the current query position

func (*Q5[A, B, C, D, E]) Get4 added in v0.3.0

func (q *Q5[A, B, C, D, E]) Get4() *D

Get4 returns the fourth queried component for the current query position

func (*Q5[A, B, C, D, E]) Get5 added in v0.3.0

func (q *Q5[A, B, C, D, E]) Get5() *E

Get5 returns the fifth queried component for the current query position

func (*Q5[A, B, C, D, E]) GetAll added in v0.3.0

func (q *Q5[A, B, C, D, E]) GetAll() (*A, *B, *C, *D, *E)

GetAll returns all queried components for the current query position

type Q6 added in v0.3.0

type Q6[A any, B any, C any, D any, E any, F any] struct {
	Query
	// contains filtered or unexported fields
}

Q6 is a generic query for six components.

Create one with Query6

func Query6 added in v0.3.0

func Query6[A any, B any, C any, D any, E any, F any](w *World) Q6[A, B, C, D, E, F]

Query6 creates a generic query for six components.

See also World.Query.

func (*Q6[A, B, C, D, E, F]) Get1 added in v0.3.0

func (q *Q6[A, B, C, D, E, F]) Get1() *A

Get1 returns the first queried component for the current query position

func (*Q6[A, B, C, D, E, F]) Get2 added in v0.3.0

func (q *Q6[A, B, C, D, E, F]) Get2() *B

Get2 returns the second queried component for the current query position

func (*Q6[A, B, C, D, E, F]) Get3 added in v0.3.0

func (q *Q6[A, B, C, D, E, F]) Get3() *C

Get3 returns the third queried component for the current query position

func (*Q6[A, B, C, D, E, F]) Get4 added in v0.3.0

func (q *Q6[A, B, C, D, E, F]) Get4() *D

Get4 returns the fourth queried component for the current query position

func (*Q6[A, B, C, D, E, F]) Get5 added in v0.3.0

func (q *Q6[A, B, C, D, E, F]) Get5() *E

Get5 returns the fifth queried component for the current query position

func (*Q6[A, B, C, D, E, F]) Get6 added in v0.3.0

func (q *Q6[A, B, C, D, E, F]) Get6() *F

Get6 returns the sixth queried component for the current query position

func (*Q6[A, B, C, D, E, F]) GetAll added in v0.3.0

func (q *Q6[A, B, C, D, E, F]) GetAll() (*A, *B, *C, *D, *E, *F)

GetAll returns all queried components for the current query position

type Q7 added in v0.3.0

type Q7[A any, B any, C any, D any, E any, F any, G any] struct {
	Query
	// contains filtered or unexported fields
}

Q7 is a generic query for seven components.

Create one with Query7

func Query7 added in v0.3.0

func Query7[A any, B any, C any, D any, E any, F any, G any](w *World) Q7[A, B, C, D, E, F, G]

Query7 creates a generic query for seven components.

See also World.Query.

func (*Q7[A, B, C, D, E, F, G]) Get1 added in v0.3.0

func (q *Q7[A, B, C, D, E, F, G]) Get1() *A

Get1 returns the first queried component for the current query position

func (*Q7[A, B, C, D, E, F, G]) Get2 added in v0.3.0

func (q *Q7[A, B, C, D, E, F, G]) Get2() *B

Get2 returns the second queried component for the current query position

func (*Q7[A, B, C, D, E, F, G]) Get3 added in v0.3.0

func (q *Q7[A, B, C, D, E, F, G]) Get3() *C

Get3 returns the third queried component for the current query position

func (*Q7[A, B, C, D, E, F, G]) Get4 added in v0.3.0

func (q *Q7[A, B, C, D, E, F, G]) Get4() *D

Get4 returns the fourth queried component for the current query position

func (*Q7[A, B, C, D, E, F, G]) Get5 added in v0.3.0

func (q *Q7[A, B, C, D, E, F, G]) Get5() *E

Get5 returns the fifth queried component for the current query position

func (*Q7[A, B, C, D, E, F, G]) Get6 added in v0.3.0

func (q *Q7[A, B, C, D, E, F, G]) Get6() *F

Get6 returns the sixth queried component for the current query position

func (*Q7[A, B, C, D, E, F, G]) Get7 added in v0.3.0

func (q *Q7[A, B, C, D, E, F, G]) Get7() *G

Get7 returns the seventh queried component for the current query position

func (*Q7[A, B, C, D, E, F, G]) GetAll added in v0.3.0

func (q *Q7[A, B, C, D, E, F, G]) GetAll() (*A, *B, *C, *D, *E, *F, *G)

GetAll returns all queried components for the current query position

type Q8 added in v0.3.0

type Q8[A any, B any, C any, D any, E any, F any, G any, H any] struct {
	Query
	// contains filtered or unexported fields
}

Q8 is a generic query for seven components.

Create one with Query8

func Query8 added in v0.3.0

func Query8[A any, B any, C any, D any, E any, F any, G any, H any](w *World) Q8[A, B, C, D, E, F, G, H]

Query8 creates a generic query for seven components.

See also World.Query.

func (*Q8[A, B, C, D, E, F, G, H]) Get1 added in v0.3.0

func (q *Q8[A, B, C, D, E, F, G, H]) Get1() *A

Get1 returns the first queried component for the current query position

func (*Q8[A, B, C, D, E, F, G, H]) Get2 added in v0.3.0

func (q *Q8[A, B, C, D, E, F, G, H]) Get2() *B

Get2 returns the second queried component for the current query position

func (*Q8[A, B, C, D, E, F, G, H]) Get3 added in v0.3.0

func (q *Q8[A, B, C, D, E, F, G, H]) Get3() *C

Get3 returns the third queried component for the current query position

func (*Q8[A, B, C, D, E, F, G, H]) Get4 added in v0.3.0

func (q *Q8[A, B, C, D, E, F, G, H]) Get4() *D

Get4 returns the fourth queried component for the current query position

func (*Q8[A, B, C, D, E, F, G, H]) Get5 added in v0.3.0

func (q *Q8[A, B, C, D, E, F, G, H]) Get5() *E

Get5 returns the fifth queried component for the current query position

func (*Q8[A, B, C, D, E, F, G, H]) Get6 added in v0.3.0

func (q *Q8[A, B, C, D, E, F, G, H]) Get6() *F

Get6 returns the sixth queried component for the current query position

func (*Q8[A, B, C, D, E, F, G, H]) Get7 added in v0.3.0

func (q *Q8[A, B, C, D, E, F, G, H]) Get7() *G

Get7 returns the seventh queried component for the current query position

func (*Q8[A, B, C, D, E, F, G, H]) Get8 added in v0.3.0

func (q *Q8[A, B, C, D, E, F, G, H]) Get8() *H

Get8 returns the eighth queried component for the current query position

func (*Q8[A, B, C, D, E, F, G, H]) GetAll added in v0.3.0

func (q *Q8[A, B, C, D, E, F, G, H]) GetAll() (*A, *B, *C, *D, *E, *F, *G, *H)

GetAll returns all queried components for the current query position

type Query

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

Query is an iterator to iterate entities.

Create queries through the World using World.Query. See also the generic variants Query1, Query2, Query3, ...

Example:

query := world.Query(posID, rotID)
for query.Next() {
    pos := (*position)(query.Get(posID))
    pos.X += 1.0
}

func (*Query) Close added in v0.1.2

func (q *Query) Close()

Close closes the Query and unlocks the world.

Automatically called when iteration finishes. Needs to be called only if breaking out of the query iteration.

func (*Query) Entity

func (q *Query) Entity() Entity

Entity returns the Entity at the iterator's position

func (*Query) Get

func (q *Query) Get(comp ID) unsafe.Pointer

Get returns the pointer to the given component at the iterator's current Entity

func (*Query) Has

func (q *Query) Has(comp ID) bool

Has returns whether the current Entity has the given component

func (*Query) Next

func (q *Query) Next() bool

Next proceeds to the next Entity in the Query.

type World

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

World is the central type holding Entity and component data.

func FromConfig added in v0.1.3

func FromConfig(conf Config) World

FromConfig creates a new World from a Config

func NewWorld

func NewWorld() World

NewWorld creates a new World

func (*World) Add

func (w *World) Add(entity Entity, comps ...ID)

Add adds components to an Entity.

Panics when called on a locked world or for an already removed entity. Do not use during Query iteration!

See also the generic variants Add, Add2, Add3, ...

func (*World) Alive

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

Alive reports whether an entity is still alive.

func (*World) Assign added in v0.2.0

func (w *World) Assign(entity Entity, id ID, comp interface{}) unsafe.Pointer

Assign assigns a component to an Entity, using a given pointer for the content. See also World.AssignN.

The passed component must be a pointer. Returns a pointer to the assigned memory. The passed in pointer is not a valid reference to that memory!

Panics when called on a locked world or for an already removed entity. Do not use during Query iteration!

See also the generic variants Assign, Assign2, Assign3, ...

func (*World) AssignN added in v0.2.0

func (w *World) AssignN(entity Entity, comps ...Component)

AssignN assigns multiple components to an Entity, using pointers for the content. See also World.Assign.

The passed components must be pointers. The passed in pointers are no valid references to the assigned memory!

Panics when called on a locked world or for an already removed entity. Do not use during Query iteration!

See also the generic variants Assign, Assign2, Assign3, ...

func (*World) Exchange added in v0.2.0

func (w *World) Exchange(entity Entity, add []ID, rem []ID)

Exchange adds and removes components in one pass

Panics when called on a locked world or for an already removed entity. Do not use during Query iteration!

func (*World) Get

func (w *World) Get(entity Entity, comp ID) unsafe.Pointer

Get returns a pointer th the given component of an Entity.

Returns `nil` if the entity has no such component. Panics when called for an already removed entity.

See also Map.Get for a generic variant.

func (*World) Has

func (w *World) Has(entity Entity, comp ID) bool

Has returns whether an Entity has a given component.

Panics when called for an already removed entity.

See also Map.Has for a generic variant.

func (*World) IsLocked added in v0.1.2

func (w *World) IsLocked() bool

IsLocked returns whether the world is locked by any queries.

func (*World) NewEntity

func (w *World) NewEntity() Entity

NewEntity returns a new or recycled Entity.

Panics when called on a locked world. Do not use during Query iteration!

func (*World) Query

func (w *World) Query(comps ...ID) Query

Query creates a Query iterator for the given components.

Locks the world to prevent changes to component compositions.

See also the generic variants Query1, Query2, Query3, ...

func (*World) RemEntity

func (w *World) RemEntity(entity Entity)

RemEntity removes and recycles an Entity.

Panics when called on a locked world or for an already removed entity. Do not use during Query iteration!

func (*World) Remove

func (w *World) Remove(entity Entity, comps ...ID)

Remove removes components from an entity.

Panics when called on a locked world or for an already removed entity.

Do not use during Query iteration!

See also the generic variants Remove, Remove2, Remove3, ...

func (*World) Set added in v0.3.0

func (w *World) Set(entity Entity, id ID, comp interface{}) unsafe.Pointer

Set overwrites a component for an Entity, using a given pointer for the content.

The passed component must be a pointer. Returns a pointer to the assigned memory. The passed in pointer is not a valid reference to that memory!

Panics when called on a locked world or for an already removed entity. Do not use during Query iteration!

Panics if the entity does not have a component of that type.

See also Map.Set for a generic variant.

Jump to

Keyboard shortcuts

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