2023-08-04 11:02:08 +08:00
|
|
|
package ecs
|
|
|
|
|
2023-08-31 16:19:41 +08:00
|
|
|
import (
|
2023-08-31 17:49:25 +08:00
|
|
|
"fmt"
|
2023-08-31 16:19:41 +08:00
|
|
|
"reflect"
|
2023-08-31 17:49:25 +08:00
|
|
|
"sync"
|
2023-08-31 16:19:41 +08:00
|
|
|
|
|
|
|
"github.com/yohamta/donburi"
|
|
|
|
"github.com/yohamta/donburi/features/events"
|
|
|
|
)
|
|
|
|
|
|
|
|
// 注意事件相关操作的最终执行者均为world协程
|
|
|
|
|
|
|
|
// 事件相关定义
|
2023-08-04 11:02:08 +08:00
|
|
|
type (
|
2023-08-31 16:19:41 +08:00
|
|
|
// 事件类型定义
|
2023-08-04 11:02:08 +08:00
|
|
|
EventType[T any] struct {
|
2023-08-31 17:49:25 +08:00
|
|
|
et *events.EventType[T]
|
|
|
|
subscriberMap map[string]events.Subscriber[T]
|
|
|
|
subscriberMapLock sync.Mutex //
|
2023-08-04 11:02:08 +08:00
|
|
|
}
|
2023-08-31 16:19:41 +08:00
|
|
|
// 事件订阅者定义
|
|
|
|
Subscriber[T any] func(w World, event T)
|
|
|
|
// 事件管理回调定义
|
|
|
|
ManageEventFunc func()
|
2023-08-04 11:02:08 +08:00
|
|
|
)
|
2023-08-31 16:19:41 +08:00
|
|
|
|
|
|
|
func init() {
|
|
|
|
//开启事件处理调试
|
2023-08-31 18:13:02 +08:00
|
|
|
events.Debug = false
|
2023-08-31 16:19:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// 创建事件类型的实例
|
2023-08-31 17:21:46 +08:00
|
|
|
func NewEventType[T any]() *EventType[T] {
|
2023-08-31 16:19:41 +08:00
|
|
|
return &EventType[T]{
|
2023-08-31 17:49:25 +08:00
|
|
|
et: events.NewEventType[T](),
|
|
|
|
subscriberMap: make(map[string]events.Subscriber[T]),
|
|
|
|
subscriberMapLock: sync.Mutex{},
|
2023-08-31 16:19:41 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 迭代处理所有事件
|
|
|
|
// 在world协程中执行
|
|
|
|
func processAllEvents(w World) {
|
|
|
|
events.ProcessAllEvents(w.(*world).world)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 订阅该类型的事件
|
|
|
|
func (me *EventType[T]) Subscribe(wd World, subscriber Subscriber[T]) {
|
|
|
|
if wd.GoroutineId() == currentGoId() {
|
|
|
|
me.subscribe(wd, subscriber)
|
|
|
|
} else {
|
2023-08-31 17:21:46 +08:00
|
|
|
wd.(*world).chanManageEvent <- func() {
|
2023-08-31 16:19:41 +08:00
|
|
|
me.subscribe(wd, subscriber)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 取消订阅该类型的事件
|
|
|
|
func (me *EventType[T]) Unsubscribe(wd World, subscriber Subscriber[T]) {
|
|
|
|
if wd.GoroutineId() == currentGoId() {
|
|
|
|
me.unsubscribe(wd, subscriber)
|
|
|
|
} else {
|
2023-08-31 17:21:46 +08:00
|
|
|
wd.(*world).chanManageEvent <- func() {
|
2023-08-31 16:19:41 +08:00
|
|
|
me.unsubscribe(wd, subscriber)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 发布该类型的事件
|
|
|
|
func (me *EventType[T]) Publish(wd World, event *T) {
|
|
|
|
if wd.GoroutineId() == currentGoId() {
|
|
|
|
me.publish(wd, event)
|
|
|
|
} else {
|
2023-08-31 17:21:46 +08:00
|
|
|
wd.(*world).chanManageEvent <- func() {
|
2023-08-31 16:19:41 +08:00
|
|
|
me.publish(wd, event)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
// 订阅该类型的事件
|
|
|
|
func (me *EventType[T]) subscribe(wd World, subscriber Subscriber[T]) {
|
2023-08-31 17:49:25 +08:00
|
|
|
me.subscriberMapLock.Lock()
|
|
|
|
defer me.subscriberMapLock.Unlock()
|
|
|
|
//
|
|
|
|
subscriberKey := subscriberKey[T](wd, subscriber)
|
|
|
|
me.subscriberMap[subscriberKey] = func(w donburi.World, event T) {
|
2023-08-31 16:19:41 +08:00
|
|
|
subscriber(wd, event)
|
|
|
|
}
|
2023-08-31 17:49:25 +08:00
|
|
|
me.et.Subscribe(wd.(*world).world, me.subscriberMap[subscriberKey])
|
2023-08-31 16:19:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// 取消订阅该类型的事件
|
|
|
|
func (me *EventType[T]) unsubscribe(wd World, subscriber Subscriber[T]) {
|
2023-08-31 17:49:25 +08:00
|
|
|
me.subscriberMapLock.Lock()
|
|
|
|
defer me.subscriberMapLock.Unlock()
|
|
|
|
//
|
|
|
|
subscriberKey := subscriberKey[T](wd, subscriber)
|
|
|
|
if sub, ok := me.subscriberMap[subscriberKey]; ok {
|
2023-08-31 16:19:41 +08:00
|
|
|
me.et.Unsubscribe(wd.(*world).world, sub)
|
2023-08-31 17:49:25 +08:00
|
|
|
delete(me.subscriberMap, subscriberKey)
|
2023-08-31 16:19:41 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 发布该类型的事件
|
|
|
|
func (me *EventType[T]) publish(wd World, event *T) {
|
|
|
|
me.et.Publish(wd.(*world).world, *event)
|
|
|
|
}
|
2023-08-31 17:49:25 +08:00
|
|
|
|
|
|
|
func subscriberKey[T any](wd World, subscriber Subscriber[T]) string {
|
|
|
|
wdSubscriberPointer := reflect.ValueOf(subscriber).Pointer()
|
|
|
|
subscriberKey := fmt.Sprintf("%d-%d", wd.GoroutineId(), wdSubscriberPointer)
|
|
|
|
return subscriberKey
|
|
|
|
}
|