ecs

package
v0.0.0-...-2630cb0 Latest Latest
Warning

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

Go to latest
Published: May 10, 2020 License: MIT Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BehaviorSystem

func BehaviorSystem(updater interface{}) func(e *ECS)

BehaviorSystem is a shorthand for simple systems that only keep track of one type of entity, and only perform operations in Update.

updater: func(e *ECS, ev EventContainer, delta float64, entityID uint64, componentSet interface{})

func RemoveEntity

func RemoveEntity(eid uint64, entityMap interface{})

RemoveEntity Removes an entity from the given map of entity IDs (uint64) if it exists. Essentially undoes the work of UnpackEntity.

func UnpackEntity

func UnpackEntity(event EntityAddedEvent, entityMap interface{}) interface{}

UnpackEntity takes an event and adds it to the given map if its components match the fields in the map's value struct. entityMap should be a pointer to a map[uint64]eCollection, where eCollection is some struct type (generally with a name beginning with e for entity) which represents some subset of an entity's components that are used by the system. This also returns a pointer to the the added structure, or nil if the entity didn't meet requirements.

Types

type ECS

type ECS struct {
	EventReceivers  []chan EventContainer
	CurrentEvents   chan interface{}
	NextFrameEvents chan interface{}
	EIDCounter      uint64
	Running         bool
	LastFrame       time.Time
}

ECS Represents an entity component system.

func NewECS

func NewECS() ECS

NewECS initializes and returns a new ECS instance

func (*ECS) AddEntity

func (e *ECS) AddEntity(components ...interface{}) uint64

AddEntity adds an entity with the given components. These should be pointers to structs. Returns the ID of the new entity.

func (*ECS) PublishNextFrame

func (e *ECS) PublishNextFrame(event interface{})

PublishNextFrame publishes an event to be handled on the next frame.

func (*ECS) PublishThisFrame

func (e *ECS) PublishThisFrame(event interface{})

PublishThisFrame publishes an event to be handled later on the current frame. Note that this is unreliable if used during update end.

func (*ECS) RemoveEntity

func (e *ECS) RemoveEntity(id uint64)

RemoveEntity removes the given entity from the ECS on the next frame.

func (*ECS) Run

func (e *ECS) Run()

Run starts the ECS loop. This is a blocking operation.

func (*ECS) Stop

func (e *ECS) Stop()

Stop stops the ECS loop.

func (*ECS) Subscribe

func (e *ECS) Subscribe() chan EventContainer

Subscribe subscribes to events. It returns a channel which can be used to receive events. The passed event container's `Done` method should be called after each event is fully processed.

type EntityAddedEvent

type EntityAddedEvent struct {
	ID         uint64
	Components map[reflect.Type]interface{}
}

EntityAddedEvent is triggered when an entity is added. Stores the entity ID and a map of all components.

type EntityRemovedEvent

type EntityRemovedEvent struct {
	ID uint64
}

EntityRemovedEvent is triggered when an entity is removed.

type EventContainer

type EventContainer struct {
	Wg    *sync.WaitGroup  // This wait group is used to wait for all systems to handle the event.
	Event interface{}      // The event struct.
	Next  chan interface{} // Channel used to submit new events to occur immediately after the current one.
}

EventContainer stores an event and related utilities.

func (EventContainer) Done

func (e EventContainer) Done()

Done should be called by all systems after handling a given event.

type SetupEvent

type SetupEvent struct{}

SetupEvent is triggered once the ECS is ready to run - it is safe to add required entities here.

type UpdateBeginEvent

type UpdateBeginEvent struct {
	Delta float64
}

UpdateBeginEvent is triggered when an update begins

type UpdateEndEvent

type UpdateEndEvent struct {
	Delta float64
}

UpdateEndEvent is triggered after UpdateBeginEvent. This is used to ensure certain tasks are completed before moving on to the next frame.

Jump to

Keyboard shortcuts

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