From 747a81e44a4318a0390422156de44b7c3afddb06 Mon Sep 17 00:00:00 2001 From: xzb <223@qq.com> Date: Fri, 15 Sep 2023 11:17:31 +0800 Subject: [PATCH] =?UTF-8?q?ecs=E4=BA=8B=E4=BB=B6=E5=8E=BB=E9=99=A4?= =?UTF-8?q?=E5=8D=8F=E7=A8=8Bid?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- events.go | 42 ++++++++++-------------------------------- world.go | 22 ---------------------- 2 files changed, 10 insertions(+), 54 deletions(-) diff --git a/events.go b/events.go index a7c16f9..8dcd63d 100644 --- a/events.go +++ b/events.go @@ -45,43 +45,19 @@ 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 { - wd.(*world).chanManageEvent <- func() { - me.subscribe(wd, subscriber) - } - } -} - -// 取消订阅该类型的事件 -func (me *EventType[T]) Unsubscribe(wd World, subscriber Subscriber[T]) { - if wd.GoroutineId() == currentGoId() { - me.unsubscribe(wd, subscriber) - } else { - wd.(*world).chanManageEvent <- func() { - me.unsubscribe(wd, subscriber) - } - } -} - // 发布该类型的事件 -func (me *EventType[T]) Publish(wd World, event *T) { - if wd.GoroutineId() == currentGoId() { - me.publish(wd, event) - } else { - wd.(*world).chanManageEvent <- func() { - me.publish(wd, event) - } +// 在world协程外执行 +func (me *EventType[T]) PublishOutWorld(wd World, event *T) { + wd.(*world).chanManageEvent <- func() { + me.Publish(wd, event) } } /////////////////////////////////////////////////////////////////////////////////// // 订阅该类型的事件 -func (me *EventType[T]) subscribe(wd World, subscriber Subscriber[T]) { +// 在world启动前执行 +func (me *EventType[T]) Subscribe(wd World, subscriber Subscriber[T]) { me.subscriberMapLock.Lock() defer me.subscriberMapLock.Unlock() // @@ -97,7 +73,8 @@ func (me *EventType[T]) subscribe(wd World, subscriber Subscriber[T]) { } // 取消订阅该类型的事件 -func (me *EventType[T]) unsubscribe(wd World, subscriber Subscriber[T]) { +// 在world启动前执行 +func (me *EventType[T]) Unsubscribe(wd World, subscriber Subscriber[T]) { me.subscriberMapLock.Lock() defer me.subscriberMapLock.Unlock() // @@ -109,7 +86,8 @@ func (me *EventType[T]) unsubscribe(wd World, subscriber Subscriber[T]) { } // 发布该类型的事件 -func (me *EventType[T]) publish(wd World, event *T) { +// 在world协程中执行 +func (me *EventType[T]) Publish(wd World, event *T) { me.et.Publish(wd.(*world).world, *event) } diff --git a/world.go b/world.go index 51ba15a..c1ca23a 100644 --- a/world.go +++ b/world.go @@ -1,11 +1,8 @@ package ecs import ( - "bytes" "fmt" "log" - "runtime" - "strconv" "time" "github.com/yohamta/donburi" @@ -49,11 +46,9 @@ type World interface { Close() Tick() int Running() bool - GoroutineId() uint64 } type world struct { - gId uint64 world donburi.World systems []ISystem state WorldState @@ -80,9 +75,6 @@ func NewWorld(tick int) World { chanManageEvent: make(chan ManageEventFunc, 1024), } } -func (w *world) GoroutineId() uint64 { - return w.gId -} func (w *world) Running() bool { return w.state == Running } @@ -196,7 +188,6 @@ func (w *world) processManageEventFuncs() { } func (w *world) run() { - w.gId = currentGoId() for { select { case <-w.quit: // 退出信号 @@ -238,16 +229,3 @@ func (w *world) run() { } } } - -// 获取当前协程id -func currentGoId() (gid uint64) { - b := make([]byte, 16) - b = b[:runtime.Stack(b, false)] - b = bytes.TrimPrefix(b, []byte("goroutine ")) - b = b[:bytes.IndexByte(b, ' ')] - n, err := strconv.ParseUint(string(b), 10, 64) - if err != nil { - panic(err) - } - return n -}