Documentation ¶
Index ¶
- Constants
- Variables
- type BaseDynamicEntity
- func (b *BaseDynamicEntity) GetComponent(t string) (interface{}, error)
- func (b *BaseDynamicEntity) GetComponents() []interface{}
- func (b *BaseDynamicEntity) HasComponent(t interface{}) error
- func (b *BaseDynamicEntity) RemoveComponent(c interface{}) error
- func (b *BaseDynamicEntity) SetComponent(c interface{}) error
- type BaseEntity
- type DynamicEntity
- type ECS
- func (ecs *ECS) Access(ent Entity) *EntityWrap
- func (ecs *ECS) AddEntity(ent Entity) (EntityID, error)
- func (ecs *ECS) Get(id EntityID) (*EntityWrap, error)
- func (ecs *ECS) Iterate(types ...interface{}) EntityIterator
- func (ecs *ECS) IterateID(ids ...EntityID) EntityIterator
- func (ecs *ECS) IterateSpecific(t interface{}) EntityIterator
- func (ecs *ECS) Marshal(writer io.Writer) error
- func (ecs *ECS) MustGet(id EntityID) *EntityWrap
- func (ecs *ECS) RegisterComponent(c interface{})
- func (ecs *ECS) RegisterEntity(ent Entity)
- func (ecs *ECS) RemoveEntity(ent Entity) error
- func (ecs *ECS) SetRoutineCount(n int)
- func (ecs *ECS) Unmarshal(reader io.Reader) error
- type Entity
- type EntityID
- type EntityIterator
- type EntityWrap
Constants ¶
const (
EntityNone = EntityID(0)
)
Variables ¶
Functions ¶
This section is empty.
Types ¶
type BaseDynamicEntity ¶
type BaseDynamicEntity struct { BaseEntity sync.Mutex // contains filtered or unexported fields }
BaseDynamicEntity is the base implementation of the DynamicEntity interface and should be embedded into your own structs to make it a dynamic entity.
func (*BaseDynamicEntity) GetComponent ¶
func (b *BaseDynamicEntity) GetComponent(t string) (interface{}, error)
GetComponent tries to fetch a component by name.
func (*BaseDynamicEntity) GetComponents ¶
func (b *BaseDynamicEntity) GetComponents() []interface{}
GetComponents returns a slice with all the component instances as interface{}.
func (*BaseDynamicEntity) HasComponent ¶
func (b *BaseDynamicEntity) HasComponent(t interface{}) error
HasComponent checks if the entity has a certain component. If t is a string it will check if a component is present by name. If t is a struct or a pointer to a struct the name of the type will be used to check if the component is present.
func (*BaseDynamicEntity) RemoveComponent ¶
func (b *BaseDynamicEntity) RemoveComponent(c interface{}) error
RemoveComponent removes a component of the type c.
func (*BaseDynamicEntity) SetComponent ¶
func (b *BaseDynamicEntity) SetComponent(c interface{}) error
SetComponents sets or adds a component with the data of c.
type BaseEntity ¶
type BaseEntity struct {
// contains filtered or unexported fields
}
BaseEntity is the base implementation of the Entity interface and should be embedded into your own structs to make it a entity.
func (*BaseEntity) SetID ¶
func (b *BaseEntity) SetID(id EntityID)
SetID sets the id of the entity. This should not be used by a user as it is managed by the ECS.
type DynamicEntity ¶
type DynamicEntity interface { Entity SetComponent(interface{}) error RemoveComponent(interface{}) error GetComponent(string) (interface{}, error) HasComponent(interface{}) error GetComponents() []interface{} }
DynamicEntity is a special entity with the option to dynamically add and remove components.
type ECS ¶
func (*ECS) Access ¶
func (ecs *ECS) Access(ent Entity) *EntityWrap
Access creates a EntityWrap for a given Entity so that the data of the Entity can be accessed in a convenient way.
func (*ECS) AddEntity ¶
AddEntity adds a Entity to the ECS storage and returns the assigned EntityID.
func (*ECS) Iterate ¶
func (ecs *ECS) Iterate(types ...interface{}) EntityIterator
Iterate searches for entities that contain all the given types and returns a iterator that can be range'd over.
For example you want to get fetch all entities containing a Pos{} and Velocity{} component:
for _, ew := range ecs.Iterate(Pos{}, Velocity{}) { // Work with the EntityWrap }
func (*ECS) IterateID ¶
func (ecs *ECS) IterateID(ids ...EntityID) EntityIterator
IterateID returns a iterator that can be range'd over for the given Entity ids.
func (*ECS) IterateSpecific ¶
func (ecs *ECS) IterateSpecific(t interface{}) EntityIterator
IterateSpecific searches for entities of a named type and returns a iterator that can be range'd over.
For example you want to get fetch all entities that are of the Player Entity type:
for _, ew := range ecs.IterateSpecific(Player{}) { // Work with the EntityWrap }
func (*ECS) MustGet ¶
func (ecs *ECS) MustGet(id EntityID) *EntityWrap
MustGet fetches a Entity by id but won't return a error if not found.
func (*ECS) RegisterComponent ¶
func (ecs *ECS) RegisterComponent(c interface{})
RegisterComponent caches information about components this is needed if you want to serialize dynamic entities as the reflection information needs to be available before the unmarshal.
func (*ECS) RegisterEntity ¶
RegisterEntity caches information about a entity.
func (*ECS) RemoveEntity ¶
RemoveEntity removes a Entity from the ECS storage.
func (*ECS) SetRoutineCount ¶
SetRoutineCount sets the number of go routines that are allowed to spawn to parallelize searches over the entities.
func (*ECS) Unmarshal ¶
Unmarshal reads a JSON encoded ECS snapshot and loads all the entities from it. The inner storage will be overwritten so all entities that have been added before will be deleted.
Important: If you want to serialize dynamic entities you need to register all possible components with RegisterComponent() before!
type EntityIterator ¶
type EntityIterator []*EntityWrap
func (EntityIterator) Count ¶
func (it EntityIterator) Count() int
Count returns the number of found entities.
type EntityWrap ¶
type EntityWrap struct {
// contains filtered or unexported fields
}
EntityWrap is a wrapper for Entity that provides functions to get a view into the Entity components.
func (*EntityWrap) GetEntity ¶
func (ew *EntityWrap) GetEntity() Entity
GetEntity returns the wrapped Entity.
func (*EntityWrap) Valid ¶
func (ew *EntityWrap) Valid() bool
Valid checks if the wrapped Entity is valid (and present).
func (*EntityWrap) View ¶
func (ew *EntityWrap) View(fn interface{}) error
View calls fn with pointer to requested components. If you change any data it will directly modify the Entity data. The pointer that the fn functions is called with are pointing straight to the components.
For example you want to get a view on the Pos{} and Velocity{} struct:
ew.View(func(p *Pos, v *Velocity) { p.X += v.X p.Y += v.Y })
func (*EntityWrap) ViewSpecific ¶
func (ew *EntityWrap) ViewSpecific(fn interface{}) error
ViewSpecific calls fn with pointer to the specific requested struct. Its like fetching a named Entity. Changes to the struct data directly applies to the Entity.
For example you want to get a view on the Player{} Entity struct:
ew.ViewSpecific(func(p *Player) { fmt.Println(p.Name) })