ecs事件去除协程id
This commit is contained in:
parent
c3b9d965c6
commit
747a81e44a
42
events.go
42
events.go
@ -45,43 +45,19 @@ func processAllEvents(w World) {
|
|||||||
events.ProcessAllEvents(w.(*world).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) {
|
// 在world协程外执行
|
||||||
if wd.GoroutineId() == currentGoId() {
|
func (me *EventType[T]) PublishOutWorld(wd World, event *T) {
|
||||||
me.publish(wd, event)
|
wd.(*world).chanManageEvent <- func() {
|
||||||
} else {
|
me.Publish(wd, event)
|
||||||
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()
|
me.subscriberMapLock.Lock()
|
||||||
defer me.subscriberMapLock.Unlock()
|
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()
|
me.subscriberMapLock.Lock()
|
||||||
defer me.subscriberMapLock.Unlock()
|
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)
|
me.et.Publish(wd.(*world).world, *event)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
22
world.go
22
world.go
@ -1,11 +1,8 @@
|
|||||||
package ecs
|
package ecs
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"runtime"
|
|
||||||
"strconv"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/yohamta/donburi"
|
"github.com/yohamta/donburi"
|
||||||
@ -49,11 +46,9 @@ type World interface {
|
|||||||
Close()
|
Close()
|
||||||
Tick() int
|
Tick() int
|
||||||
Running() bool
|
Running() bool
|
||||||
GoroutineId() uint64
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type world struct {
|
type world struct {
|
||||||
gId uint64
|
|
||||||
world donburi.World
|
world donburi.World
|
||||||
systems []ISystem
|
systems []ISystem
|
||||||
state WorldState
|
state WorldState
|
||||||
@ -80,9 +75,6 @@ func NewWorld(tick int) World {
|
|||||||
chanManageEvent: make(chan ManageEventFunc, 1024),
|
chanManageEvent: make(chan ManageEventFunc, 1024),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
func (w *world) GoroutineId() uint64 {
|
|
||||||
return w.gId
|
|
||||||
}
|
|
||||||
func (w *world) Running() bool {
|
func (w *world) Running() bool {
|
||||||
return w.state == Running
|
return w.state == Running
|
||||||
}
|
}
|
||||||
@ -196,7 +188,6 @@ func (w *world) processManageEventFuncs() {
|
|||||||
|
|
||||||
}
|
}
|
||||||
func (w *world) run() {
|
func (w *world) run() {
|
||||||
w.gId = currentGoId()
|
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-w.quit: // 退出信号
|
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
|
|
||||||
}
|
|
||||||
|
Loading…
Reference in New Issue
Block a user