ecs事件处理
This commit is contained in:
parent
0cdbe6fdd0
commit
806b92aa61
10
events.go
10
events.go
|
@ -15,7 +15,6 @@ type (
|
|||
EventType[T any] struct {
|
||||
et *events.EventType[T]
|
||||
subscriberMap map[uintptr]events.Subscriber[T]
|
||||
chanToWorld chan<- ManageEventFunc
|
||||
}
|
||||
// 事件订阅者定义
|
||||
Subscriber[T any] func(w World, event T)
|
||||
|
@ -29,11 +28,10 @@ func init() {
|
|||
}
|
||||
|
||||
// 创建事件类型的实例
|
||||
func NewEventType[T any](w World) *EventType[T] {
|
||||
func NewEventType[T any]() *EventType[T] {
|
||||
return &EventType[T]{
|
||||
et: events.NewEventType[T](),
|
||||
subscriberMap: make(map[uintptr]events.Subscriber[T]),
|
||||
chanToWorld: w.(*world).chanManageEvent,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -48,7 +46,7 @@ func (me *EventType[T]) Subscribe(wd World, subscriber Subscriber[T]) {
|
|||
if wd.GoroutineId() == currentGoId() {
|
||||
me.subscribe(wd, subscriber)
|
||||
} else {
|
||||
me.chanToWorld <- func() {
|
||||
wd.(*world).chanManageEvent <- func() {
|
||||
me.subscribe(wd, subscriber)
|
||||
}
|
||||
}
|
||||
|
@ -59,7 +57,7 @@ func (me *EventType[T]) Unsubscribe(wd World, subscriber Subscriber[T]) {
|
|||
if wd.GoroutineId() == currentGoId() {
|
||||
me.unsubscribe(wd, subscriber)
|
||||
} else {
|
||||
me.chanToWorld <- func() {
|
||||
wd.(*world).chanManageEvent <- func() {
|
||||
me.unsubscribe(wd, subscriber)
|
||||
}
|
||||
}
|
||||
|
@ -70,7 +68,7 @@ func (me *EventType[T]) Publish(wd World, event *T) {
|
|||
if wd.GoroutineId() == currentGoId() {
|
||||
me.publish(wd, event)
|
||||
} else {
|
||||
me.chanToWorld <- func() {
|
||||
wd.(*world).chanManageEvent <- func() {
|
||||
me.publish(wd, event)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -11,16 +11,41 @@ type FireSwitchDcEvent struct {
|
|||
Dc bool
|
||||
}
|
||||
|
||||
var fireSwitchDcEventType = ecs.NewEventType[FireSwitchDcEvent]()
|
||||
|
||||
func main() {
|
||||
go world1()
|
||||
go world2()
|
||||
time.Sleep(30 * time.Second)
|
||||
}
|
||||
func world1() {
|
||||
world := ecs.NewWorld(1000)
|
||||
world.StartUp()
|
||||
//
|
||||
fireSwitchDcEventType := ecs.NewEventType[FireSwitchDcEvent](world)
|
||||
|
||||
fireSwitchDcEventType.Subscribe(world, func(w ecs.World, event FireSwitchDcEvent) {
|
||||
fmt.Println("==>>1 触发道岔定操事件 : dc = ", event.Dc)
|
||||
fmt.Println("==w1>>1 触发道岔定操事件 : dc = ", event.Dc)
|
||||
})
|
||||
fireSwitchDcEventType.Subscribe(world, func(w ecs.World, event FireSwitchDcEvent) {
|
||||
fmt.Println("==>>2 触发道岔定操事件 : dc = ", event.Dc)
|
||||
fmt.Println("==w1>>2 触发道岔定操事件 : dc = ", event.Dc)
|
||||
})
|
||||
//
|
||||
time.Sleep(3 * time.Second)
|
||||
//
|
||||
fireSwitchDcEventType.Publish(world, &FireSwitchDcEvent{Dc: true})
|
||||
fireSwitchDcEventType.Publish(world, &FireSwitchDcEvent{Dc: false})
|
||||
time.Sleep(30 * time.Second)
|
||||
}
|
||||
func world2() {
|
||||
world := ecs.NewWorld(1000)
|
||||
world.StartUp()
|
||||
//
|
||||
|
||||
fireSwitchDcEventType.Subscribe(world, func(w ecs.World, event FireSwitchDcEvent) {
|
||||
fmt.Println("==w2>>1 触发道岔定操事件 : dc = ", event.Dc)
|
||||
})
|
||||
fireSwitchDcEventType.Subscribe(world, func(w ecs.World, event FireSwitchDcEvent) {
|
||||
fmt.Println("==w2>>2 触发道岔定操事件 : dc = ", event.Dc)
|
||||
})
|
||||
//
|
||||
time.Sleep(3 * time.Second)
|
||||
|
|
Loading…
Reference in New Issue