Source File
observer.go
Belonging Package
github.com/mlange-42/ark/ecs
package ecs// Observer for ECS events.//// Observers react to structural changes, such as entity creation, removal, and component addition/removal.// Use the methods Observe, With, Without, and Do to configure the observer before registering it.//// See [EventType] for available events.// See also [Observer1], [Observer2], etc.type Observer struct {observerDatacomps []Compwith []Compwithout []Compevent EventTypeexclusive bool}// observerData contains the observer data that is required by the observerManager.type observerData struct {compsMask bitMaskwithMask bitMaskwithoutMask bitMaskcallback func(Entity)id observerIDhasComps boolhasWithout boolhasWith bool}// Observe creates a new ECS event observer for the specified event type.//// Observers react to structural changes, such as entity creation, removal, and component addition/removal.// Use the methods For, With, Without, Exclusive, and Do to configure the observer before registering it.//// See also [Observe1], [Observe2], etc.func ( EventType) *Observer {return &Observer{event: ,observerData: observerData{id: maxObserverID,},}}// New creates a new [Observer]. It is safe to call on `nil` instance.// It is a helper method, provided for consistency with generic observers like [Observer1].func (*Observer) ( EventType) *Observer {return Observe()}// For adds components that the observer observes.// The component events, the observer triggers if these components are added to or removed from an entity.//// If not specified, the observer triggers on any component addition or removal.// If multiple components are provided, all must be added/removed at the same time to trigger the observer.//// For entity events (OnCreateEntity, OnRemoveEntity), is has the same effect as With.//// Method calls can be chained, which has the same effect as calling with multiple arguments.func ( *Observer) ( ...Comp) *Observer {if .id != maxObserverID {panic("can't modify a registered observer")}if len() == 0 {return}.comps = append(.comps, ...)return}// With adds components that entities must have to trigger the observer.// If multiple components are provided, the entity must have all of them.//// Method calls can be chained, which has the same effect as calling with multiple arguments.func ( *Observer) ( ...Comp) *Observer {if .id != maxObserverID {panic("can't modify a registered observer")}if len() == 0 {return}.with = append(.with, ...)return}// Without adds components that entities must not have to trigger the observer.// If multiple components are provided, the entity must not have any of them.//// Method calls can be chained, which has the same effect as calling with multiple arguments.func ( *Observer) ( ...Comp) *Observer {if .id != maxObserverID {panic("can't modify a registered observer")}if len() == 0 {return}.without = append(.without, ...)return}// Exclusive makes the observer exclusive in the sense that the components given by [Observer.With]// are matched exactly, and no other components are allowed.//// Overwrites components set via [Observer.Without].func ( *Observer) () *Observer {if .id != maxObserverID {panic("can't modify a registered observer")}.exclusive = truereturn}// Do sets the observer's callback. Must be called exactly once before registration.func ( *Observer) ( func(Entity)) *Observer {if .callback != nil {panic("observer already has a callback")}.callback =return}// Register this observer. This is mandatory for the observer to take effect.func ( *Observer) ( *World) *Observer {.registerObserver()return}// Unregister this observer.func ( *Observer) ( *World) *Observer {.unregisterObserver()return}func ( *observerData) ( *bitMask) bool {if .hasWith && !.Contains(&.withMask) {return false}if .hasWithout && .ContainsAny(&.withoutMask) {return false}return true}func ( *observerData) (, *bitMask) bool {if .hasComps && !.Contains(&.compsMask) {return false}if .hasWith && !.Contains(&.withMask) {return false}if .hasWithout && .ContainsAny(&.withoutMask) {return false}return true}
![]() |
The pages are generated with Golds v0.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. |