ecs

package module
v0.0.0-...-cd6bd66 Latest Latest
Warning

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

Go to latest
Published: Jul 29, 2024 License: BSD-3-Clause Imports: 3 Imported by: 0

README

go-ecs

PkgGoDev

This library provides a generic Entity Component System for developing games.

// Construction.
e := ecs.New()

// Create entities.
entity := e.Add()

// Delete entities.
e.Delete(entity)

// Set component in entity.
ecs.Set(e, MyComponent{value})
ecs.Set2(e, MyComponent{value1}, OtherComponent{value2})

// Get component(s) given entity ID.
c, ok := ecs.Get[MyComponent](e, entityId)

c1, c2, ok := ecs.Get2[MyComponent, OtherComponent](e, entityId)

// Unset (or remove) component from entity.
ecs.Unset[MyComponent](e, entityId)

// Iterate all entities that have a component.
for it := ecs.Iterate[MyComponent](e); ; {
  c, ok := it.Next()
  if !ok {
    break
  }

  // Do something with 'c'.
}

// Iterate all entities that have both components.
for it := ecs.Join[MyComponent, OtherComponent](e); ; {
  c1, c2, ok := it.Next()
  if !ok {
    break
  }

  // Do something with 'c1' and 'c2'.
}

// Iterate all entities that have all 3 components.
for it := ecs.Join3[MyC1, MyC2, MyC3](e) ; ; { ... }

// Get singleton entity with given component.
entityID, c, ok := e.IterateAny[MyComponent](e)

// Get singleton entity with both components.
entityID, c1, c2, ok := e.JoinAny[MyComponent, OtherComponents](e)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Get

func Get[T any](e *ECS, entityId int) (*T, bool)

Returns a component of the given type for an entity given its ID. Returns a pointer to the component and true if said entity exists, otherwise it returns false.

The pointer is valid as long as the ECS is not modified (see ECS type)

func Get2

func Get2[A, B any](e *ECS, entityId int) (*A, *B, bool)

Same as 'Get' for 2 component types. Returns true only if the entity has all types.

The pointers are valid as long as the ECS is not modified (see ECS type)

func Get3

func Get3[A, B, C any](e *ECS, entityId int) (*A, *B, *C, bool)

Same as 'Get' for 3 component types. Returns true only if the entity has all types.

The pointers are valid as long as the ECS is not modified (see ECS type)

func Init

func Init[A any](e *ECS, entityId int, a A)

Initializes an entity and its component. If the entity already exists, it is first removed and then re-added. If the intention is not to initialize the entity, then use 'Set' instead.

func Init2

func Init2[A, B any](e *ECS, entityId int, a A, b B)

func Init3

func Init3[A, B, C any](e *ECS, entityId int, a A, b B, c C)

func Init4

func Init4[A, B, C, D any](e *ECS, entityId int, a A, b B, c C, d D)

func Init5

func Init5[A, B, C, D, E any](ecs *ECS, entityId int, a A, b B, c C, d D, e E)

func Init6

func Init6[A, B, C, D, E, F any](ecs *ECS, entityId int, a A, b B, c C, d D, e E, f F)

func Init7

func Init7[A, B, C, D, E, F, G any](ecs *ECS, entityId int, a A, b B, c C, d D, e E, f F, g G)

func Init8

func Init8[A, B, C, D, E, F, G, H any](ecs *ECS, entityId int, a A, b B, c C, d D, e E, f F, g G, h H)

func Iterate

func Iterate[A any](e *ECS) *sparseset.Iterator[A]

Returns an iterator that iterates all entities that have the given component type.

for iterator := ecs.Iterate[MyComponent](e); ; {
  c, ok := e.Next()
  if !ok {
    break
  }

  // Do something with 'c'.
}

The pointer returned by the iterator is valid as long as the ECS is not modified (see ECS type)

func IterateAny

func IterateAny[T any](e *ECS) (int, *T, bool)

Returns any entity that has the given component. Returns the entity ID, the pointer to the component and true if said entity exists, otherwise it returns false.

The pointer is valid as long as the ECS is not modified (see ECS type)

func Join

func Join[A, B any](e *ECS) *sparseset.JoinIterator[A, B]

Returns an iterator that iterates all entities that have all component types.

for iterator := ecs.Join[MyComponent, OtherComponent](e); ; {
  c1, c2, ok := e.Next()
  if !ok {
    break
  }

  // Do something with 'c1' and 'c2'.
}

The pointers returned by the iterator are valid as long as the ECS is not modified (see ECS type)

func Join3

func Join3[A, B, C any](e *ECS) *sparseset.Join3Iterator[A, B, C]

Same as 'Join' for 3 component types.

func Join3Any

func Join3Any[A, B, C any](e *ECS) (int, *A, *B, *C, bool)

func Join4

func Join4[A, B, C, D any](e *ECS) *sparseset.Join4Iterator[A, B, C, D]

Same as 'Join' for 4 component types.

func JoinAny

func JoinAny[A, B any](e *ECS) (int, *A, *B, bool)

Returns any entity that has all the given components. Returns the entity ID, the pointers to the components and true if said entitiy exists, otherwise it returns false. The pointers are valid as long as the ECS is not modified.

The pointers are valid as long as the ECS is not modified (see ECS type)

func Set

func Set[A any](e *ECS, entityId int, a A)

Sets a component for an entity given its ID.

func Set2

func Set2[A, B any](ecs *ECS, entityId int, a A, b B)

Same as 'Set' for 2 component types.

func Set3

func Set3[A, B, C any](ecs *ECS, entityId int, a A, b B, c C)

Same as 'Set' for 3 component types.

func Set4

func Set4[A, B, C, D any](ecs *ECS, entityId int, a A, b B, c C, d D)

Same as 'Set' for 4 component types.

func Set5

func Set5[A, B, C, D, E any](ecs *ECS, entityId int, a A, b B, c C, d D, e E)

Same as 'Set' for 5 component types.

func Set6

func Set6[A, B, C, D, E, F any](ecs *ECS, entityId int, a A, b B, c C, d D, e E, f F)

Same as 'Set' for 6 component types.

func Set7

func Set7[A, B, C, D, E, F, G any](ecs *ECS, entityId int, a A, b B, c C, d D, e E, f F, g G)

Same as 'Set' for 7 component types.

func Set8

func Set8[A, B, C, D, E, F, G, H any](ecs *ECS, entityId int, a A, b B, c C, d D, e E, f F, g G, h H)

Same as 'Set' for 8 component types.

func SortStableFunc

func SortStableFunc[T any](e *ECS, compare func(int, *T, int, *T) int)

Sorts the components using a stable sort function according to the given comparator function. The comparator function uses the same semantics are 'cmp.Compare' from the http://pkg.go.dev/cmp package.

func Unset

func Unset[T any](e *ECS, entityId int)

Removes a component from an entity given its ID. If the entity already does not have said component, then it's a no-op.

Types

type ECS

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

ECS is the Entity Component System.

Several functions / methods return pointers to components. These pointers are only valid as long as the ECS is not modified by adding or removing entities. If adding or removing entities, the ECS's internal memory may reallocated, thus invaliding those pointers. For this reason, pointers to components obtained prior to adding or removing entities should not be accessed if the ECS is modified in this way. Also, it's recommended not to store pointers to components inside data types for later use because if they become invalidated it's easy to forget and access them later.

func New

func New() *ECS

Returns a new instance of the ECS with default options.

func (*ECS) Add

func (e *ECS) Add() int

Creates a new entity and returns the entity ID.

func (*ECS) Remove

func (e *ECS) Remove(entityId int)

Removes an entity given its ID and removes all of its components.

Jump to

Keyboard shortcuts

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