package ecsimport ()// relationType is the runtime type of RelationMarkervar relationType = reflect.TypeFor[RelationMarker]()// RelationMarker is a marker for entity relation components.// It must be embedded as first field of a component that represent an entity relationship// (see the example).//// Entity relations allow for fast queries using entity relationships.// E.g. to iterate over all entities that are the child of a certain parent entity.typeRelationMarkerstruct{}// relationID is a pair of relation component type and relation target.type relationID struct { target Entity component ID}// Relation is the common type for specifying relationship targets.// It can be created with [Rel], [RelIdx] and [RelID].//// - [Rel] uses a generic type parameter to identify the component.// It is safe, but has some run-time overhead for component [ID] lookup on first usage.// - [RelIdx] uses an index to identify the component. It is fast but more error-prone.// - [RelID] uses a component ID. It is for use with the [Unsafe] API.typeRelationstruct { componentType reflect.Type// Component type of the relation target Entity// Target entity of the relation component ID// Component ID of the relation index uint8// Component index of the relation in a mapper or query}// Rel creates a new [Relation] for a component type.//// It can be used as a safer but slower alternative to [RelIdx].// Requires a component ID lookup when used the first time.// On reuse, it is as fast as [RelIdx].func [ any]( Entity) Relation {returnRelation{target: ,componentType: reflect.TypeFor[](),index: 255, }}// RelIdx creates a new [Relation] for a component index.// The index refers to the position of the component in the generics// of e.g. a [Map2] or [Filter2].// For filters, components specified by [Filter2.With] are also covered by the index.//// It can be used as faster but less safe alternative to [Rel].//// Note that the index should not be confused with a component [ID] as obtained by [ComponentID]!// For component IDs, use [RelID].func ( int, Entity) Relation {returnRelation{index: uint8(),target: , }}// RelID creates a new [Relation] for a component ID.//// It is used in Ark's unsafe, ID-based API.func ( ID, Entity) Relation {returnRelation{target: ,component: ,index: 255, }}// relationIDForUnsafe converts the Relation to a relationID.//// Relation must use ID or component type.// Panics if used with an index Relation.//// Modifies the Relation to use an ID.func ( *Relation) ( *World) relationID {if .index < 255 {panic("relations created with RelIdx can't be used in the unsafe API, use RelID or Rel instead") }if .componentType != nil { .component = TypeID(, .componentType) .componentType = nil }returnrelationID{target: .target,component: .component, }}// id returns the component ID of this Relation.func ( *Relation) ( []ID, *World) ID {if .index < 255 {return [.index] }if .componentType != nil { .component = TypeID(, .componentType) .componentType = nil }return .component}// targetEntity returns the target [Entity] of this Relation.func ( *Relation) () Entity {return .target}// Helper for converting relationSlicetype relationSlice []Relation// ToRelations converts a slice of Relation items to relationIDs.func ( relationSlice) ( *World, *bitMask, []ID, []relationID, bool) []relationID {iflen() == 0 {return }return .toRelationsSlowPath(, , , , )}// toRelationsSlowPath is the slow path of ToRelations for more than zero relations.func ( relationSlice) ( *World, *bitMask, []ID, []relationID, bool) []relationID {if { := make([]relationID, 0, len()+len()) = append(, ...) = }// Fast special case for a single relation (20% speedup)iflen() == 1 { := [0] := .id(, ) .storage.checkRelationTarget(.targetEntity()) .storage.checkRelationComponent()if !.Get(.id) {panic(fmt.Sprintf("requested relation component with ID %d was not specified in the filter or map", .id)) }returnappend(, relationID{target: .target, component: }) }// Slower with loop for more than one relationfor , := range { := .id(, ) .storage.checkRelationTarget(.targetEntity()) .storage.checkRelationComponent()if !.Get(.id) {panic(fmt.Sprintf("requested relation component with ID %d was not specified in the filter or map", .id)) } = append(, relationID{target: .target, component: }) }return}// ToRelationIDsForUnsafe converts a slice of Relation items from the unsafe API to relationIDs.func ( relationSlice) ( *World, []relationID) []relationID {for , := range { = append(, .relationIDForUnsafe()) }return}// toRelation converts an entity and a component ID to relationIDs.func toRelation( *World, Entity, ID, []relationID) []relationID { .storage.checkRelationTarget() .storage.checkRelationComponent() = [:0] = append(, relationID{target: , component: })return}// Helper for converting relationstype relationEntities []Entity// ToRelation converts a slice of entities to relationIDs.func ( relationEntities) ( *World, ID, []relationID) []relationID { = [:0]iflen() == 0 {return }for , := range { .storage.checkRelationTarget() .storage.checkRelationComponent() = append(, relationID{target: , component: }) }return}
The pages are generated with Goldsv0.8.7. (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds.