94 lines
2.3 KiB
Go
94 lines
2.3 KiB
Go
package ecs
|
|
|
|
import (
|
|
"fmt"
|
|
"reflect"
|
|
"sync"
|
|
|
|
"github.com/yohamta/donburi"
|
|
"github.com/yohamta/donburi/features/events"
|
|
)
|
|
|
|
// 注意事件相关操作的最终执行者均为world协程
|
|
|
|
// 事件相关定义
|
|
type (
|
|
// 事件类型定义
|
|
EventType[T any] struct {
|
|
et *events.EventType[T]
|
|
subscriberMap map[string]events.Subscriber[T]
|
|
subscriberMapLock sync.Mutex //
|
|
}
|
|
// 事件订阅者定义
|
|
Subscriber[T any] func(w World, event T)
|
|
)
|
|
|
|
// 开启底层事件处理调试
|
|
func EventsDebugEnable() {
|
|
events.Debug = true
|
|
}
|
|
|
|
// 创建事件类型的实例
|
|
func NewEventType[T any]() *EventType[T] {
|
|
return &EventType[T]{
|
|
et: events.NewEventType[T](),
|
|
subscriberMap: make(map[string]events.Subscriber[T]),
|
|
subscriberMapLock: sync.Mutex{},
|
|
}
|
|
}
|
|
|
|
// 迭代处理所有事件
|
|
func processAllEvents(w World) {
|
|
events.ProcessAllEvents(w)
|
|
}
|
|
|
|
// 发布该类型的事件
|
|
func (me *EventType[T]) Publish(wd World, event *T) {
|
|
wd.Execute(func() {
|
|
me.et.Publish(wd, *event)
|
|
})
|
|
}
|
|
|
|
// 内部发布事件(同步发布,并直接触发)
|
|
func (me *EventType[T]) internalPublish(wd World, event *T) {
|
|
me.et.Publish(wd, *event)
|
|
processAllEvents(wd)
|
|
}
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// 订阅该类型的事件
|
|
func (me *EventType[T]) Subscribe(wd World, subscriber Subscriber[T]) {
|
|
me.subscriberMapLock.Lock()
|
|
defer me.subscriberMapLock.Unlock()
|
|
subscriberKey := subscriberKey[T](wd, subscriber)
|
|
if _, exist := me.subscriberMap[subscriberKey]; !exist {
|
|
fn := func(w donburi.World, event T) {
|
|
subscriber(wd, event)
|
|
}
|
|
me.subscriberMap[subscriberKey] = fn
|
|
wd.Execute(func() {
|
|
me.et.Subscribe(wd, fn)
|
|
})
|
|
}
|
|
}
|
|
|
|
// 取消订阅该类型的事件
|
|
func (me *EventType[T]) Unsubscribe(wd World, subscriber Subscriber[T]) {
|
|
me.subscriberMapLock.Lock()
|
|
defer me.subscriberMapLock.Unlock()
|
|
subscriberKey := subscriberKey[T](wd, subscriber)
|
|
if sub, ok := me.subscriberMap[subscriberKey]; ok {
|
|
wd.Execute(func() {
|
|
me.et.Unsubscribe(wd, sub)
|
|
delete(me.subscriberMap, subscriberKey)
|
|
})
|
|
}
|
|
}
|
|
|
|
func subscriberKey[T any](wd World, subscriber Subscriber[T]) string {
|
|
wdSubscriberPointer := reflect.ValueOf(subscriber).Pointer()
|
|
subscriberKey := fmt.Sprintf("%d-%d", wd.Id(), wdSubscriberPointer)
|
|
return subscriberKey
|
|
}
|