Documentation ¶
Index ¶
- func AddComp(w *World, e Entity, c Component)
- func DelComp(w *World, e Entity, c Component)
- func DelEntity(w *World, e Entity)
- func GetComp[C any](w *World, e Entity, c Component) (data *C)
- func HasComp(w *World, e Entity, c Component) bool
- func SetComp[C any](w *World, e Entity, c Component, data C)
- func Type(w *World, e Entity, nameComp Component) string
- type CachedQuery
- type Component
- type Entity
- type Filter
- type World
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type CachedQuery ¶
type CachedQuery core.CachedQuery
func (*CachedQuery) Free ¶
func (q *CachedQuery) Free(w *World)
func (*CachedQuery) Run ¶
func (q *CachedQuery) Run(h func([]Entity, []any))
type Component ¶
func NewComponent ¶
type Entity ¶
Example (Basic) ¶
type ( Position struct{ x, y float64 } Walking struct{} ) w := ecs.NewWorld() name := ecs.NewComponent(w) ecs.SetComp(w, name.Entity, name, "Name") position := ecs.NewComponent(w) ecs.SetComp(w, position.Entity, name, "Position") walking := ecs.NewComponent(w) ecs.SetComp(w, walking.Entity, name, "Walking") // Create an entity with name Bob bob := ecs.NewEntity(w) ecs.SetComp(w, bob, name, "Bob") // The set operation finds or creates a component, and sets it. ecs.SetComp(w, bob, position, Position{10, 20}) // The add operation adds a component without setting a value. This is // useful for tags, or when adding a component with its default value. ecs.SetComp(w, bob, walking, Walking{}) // Get the value for the Position component pos := ecs.GetComp[Position](w, bob, position) fmt.Printf("{%f, %f}\n", pos.x, pos.y) // Overwrite the value of the Position component ecs.SetComp(w, bob, position, Position{20, 30}) // Create another named entity alice := ecs.NewEntity(w) ecs.SetComp(w, alice, name, "Alice") ecs.SetComp(w, alice, position, Position{10, 20}) ecs.SetComp(w, alice, walking, Walking{}) // Print all the Components the entity has. This will output: // Position, Walking, (Identifier,Name) fmt.Printf("[%s]\n", ecs.Type(w, alice, name)) // Iterate all entities with Position ecs.QueryAll(position).Run(w, func(entities []ecs.Entity, data []any) { p := *data[0].(*[]Position) for i, e := range entities { entityName := ecs.GetComp[string](w, e, name) fmt.Printf("%s: {%f, %f}\n", *entityName, p[i].x, p[i].y) } }) // DelComp tag ecs.DelComp(w, alice, walking)
Output: {10.000000, 20.000000} [Name, Position, Walking] Bob: {20.000000, 30.000000} Alice: {10.000000, 20.000000}
type Filter ¶
func QueryAll ¶
Example ¶
w := ecs.NewWorld() // Create 10 entities. var entities [10]ecs.Entity for i := range entities { entities[i] = ecs.NewEntity(w) } // Create 2 Components. c1 := ecs.NewComponent(w) c2 := ecs.NewComponent(w) // Add Components to entities. for i, e := range entities[:5] { ecs.SetComp(w, e, c1, i) } for i, e := range entities[3:7] { ecs.SetComp(w, e, c2, i+3) } // Current layout: // // entity:[0 1 2 3 4 5 6 7 8 9] // c1: [0 1 2 3 4 ] // c2: [ 3 4 5 6 ] // c1&c2: [ 3 4 ] // CachedQuery all entities which have both c1 and c2. ecs.QueryAll(c1, c2).Run(w, func(entities []ecs.Entity, data []any) { // The type of the data's element is `Table[T]`, // which can be converted to `[]T` only after type assertion. fmt.Println(*data[0].(*[]int)) })
Output: [3 4]
func QueryAny ¶
Example ¶
w := ecs.NewWorld() // Create 10 entities. var entities [10]ecs.Entity for i := range entities { entities[i] = ecs.NewEntity(w) } // Create 2 Components. c1 := ecs.NewComponent(w) c2 := ecs.NewComponent(w) // Add Components to entities. for i, e := range entities[:5] { ecs.SetComp(w, e, c1, i) } for i, e := range entities[3:7] { ecs.SetComp(w, e, c2, i+3) } // Current layout: // // entity:[0 1 2 3 4 5 6 7 8 9] // c1: [0 1 2 3 4 ] // c2: [ 3 4 5 6 ] // c1&c2: [ 3 4 ] // CachedQuery all entities which have both c1 and c2. var result []int ecs.QueryAny(c1, c2).Run(w, func(entities []ecs.Entity, data []any) { // The type of the data's element is `Table[T]`, // which can be converted to `[]T` only after type assertion. result = append(result, *data[0].(*[]int)...) }) sort.Ints(result) fmt.Println(result)
Output: [0 1 2 3 4 5 6]
func (Filter) Cache ¶
func (f Filter) Cache(w *World) *CachedQuery
Click to show internal directories.
Click to hide internal directories.