merge、修改example
This commit is contained in:
commit
5744018095
28
events.go
28
events.go
@ -45,31 +45,19 @@ func processAllEvents(w World) {
|
||||
events.ProcessAllEvents(w.(*world).world)
|
||||
}
|
||||
|
||||
// 订阅该类型的事件
|
||||
func (me *EventType[T]) Subscribe(wd World, subscriber Subscriber[T]) {
|
||||
wd.(*world).chanManageEvent <- func() {
|
||||
me.subscribe(wd, subscriber)
|
||||
}
|
||||
}
|
||||
|
||||
// 取消订阅该类型的事件
|
||||
func (me *EventType[T]) Unsubscribe(wd World, subscriber Subscriber[T]) {
|
||||
wd.(*world).chanManageEvent <- func() {
|
||||
me.unsubscribe(wd, subscriber)
|
||||
}
|
||||
}
|
||||
|
||||
// 发布该类型的事件
|
||||
// 在world协程外执行
|
||||
func (me *EventType[T]) Publish(wd World, event *T) {
|
||||
wd.(*world).chanManageEvent <- func() {
|
||||
me.publish(wd, event)
|
||||
me.et.Publish(wd.(*world).world, *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()
|
||||
//
|
||||
@ -85,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()
|
||||
//
|
||||
@ -96,11 +85,6 @@ func (me *EventType[T]) unsubscribe(wd World, subscriber Subscriber[T]) {
|
||||
}
|
||||
}
|
||||
|
||||
// 发布该类型的事件
|
||||
func (me *EventType[T]) publish(wd World, event *T) {
|
||||
me.et.Publish(wd.(*world).world, *event)
|
||||
}
|
||||
|
||||
func subscriberKey[T any](wd World, subscriber Subscriber[T]) string {
|
||||
wdSubscriberPointer := reflect.ValueOf(subscriber).Pointer()
|
||||
subscriberKey := fmt.Sprintf("%d-%d", wd.Id(), wdSubscriberPointer)
|
||||
|
@ -1,45 +1,44 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"joylink.club/ecs"
|
||||
"joylink.club/ecs/examples/rtss/component"
|
||||
system "joylink.club/ecs/examples/rtss/sys"
|
||||
)
|
||||
|
||||
func main() {
|
||||
start := time.Now()
|
||||
for i := 0; i < 10; i++ {
|
||||
w := ecs.NewWorld(1)
|
||||
turnoutSys := system.NewTurnoutSys()
|
||||
w.AddSystem(turnoutSys)
|
||||
w.StartUp()
|
||||
// for i := 0; i < 5; i++ {
|
||||
|
||||
w := ecs.NewWorld(20)
|
||||
turnoutSys := system.NewTurnoutSys()
|
||||
w.AddSystem(turnoutSys)
|
||||
idcomp := component.IdComp
|
||||
entities := w.CreateMany(10, idcomp, system.TurnoutStateComp)
|
||||
for i, entry := range entities {
|
||||
idcomp.SetValue(entry, component.Id(fmt.Sprintf("%d", i)))
|
||||
var db bool
|
||||
var fb bool
|
||||
if i%2 == 0 {
|
||||
db = true
|
||||
} else {
|
||||
fb = true
|
||||
}
|
||||
system.TurnoutStateComp.SetValue(entry, system.TurnoutState{Db: db, Fb: fb})
|
||||
}
|
||||
// tet := ecs.NewEventType[TestEvent](w)
|
||||
// tet.Subscribe()
|
||||
w.StartUp()
|
||||
go func() {
|
||||
for i := 0; i < 10; i++ {
|
||||
system.DingCao(w, "3")
|
||||
time.Sleep(200 * time.Millisecond)
|
||||
}
|
||||
}()
|
||||
}
|
||||
dt := time.Duration(time.Now().Nanosecond() - start.Nanosecond())
|
||||
fmt.Println("执行耗时", dt)
|
||||
// w := ecs.NewWorld(1)
|
||||
// turnoutSys := system.NewTurnoutSys()
|
||||
// w.AddSystem(turnoutSys)
|
||||
// // idcomp := component.IdComp
|
||||
// // entities := w.CreateMany(10, idcomp, system.TurnoutStateComp)
|
||||
// // for i, entry := range entities {
|
||||
// // idcomp.SetValue(entry, component.Id(fmt.Sprintf("%d", i)))
|
||||
// // var db bool
|
||||
// // var fb bool
|
||||
// // if i%2 == 0 {
|
||||
// // db = true
|
||||
// // } else {
|
||||
// // fb = true
|
||||
// // }
|
||||
// // system.TurnoutStateComp.SetValue(entry, system.TurnoutState{Db: db, Fb: fb})
|
||||
// // }
|
||||
// // tet := ecs.NewEventType[TestEvent](w)
|
||||
// // tet.Subscribe()
|
||||
// w.StartUp()
|
||||
// // go func() {
|
||||
// // for i := 0; i < 10; i++ {
|
||||
// // system.DingCao(w, "3")
|
||||
// // time.Sleep(200 * time.Millisecond)
|
||||
// // }
|
||||
// // }()
|
||||
// }
|
||||
time.Sleep(5 * time.Second)
|
||||
}
|
||||
|
@ -1,7 +1,9 @@
|
||||
package system
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"time"
|
||||
|
||||
"github.com/yohamta/donburi/filter"
|
||||
"joylink.club/ecs"
|
||||
@ -16,6 +18,7 @@ var TurnoutStateComp = ecs.NewComponentType[TurnoutState]()
|
||||
|
||||
type TurnoutSys struct {
|
||||
stateQuery *ecs.Query
|
||||
count int
|
||||
}
|
||||
|
||||
func NewTurnoutSys() *TurnoutSys {
|
||||
@ -26,17 +29,18 @@ func NewTurnoutSys() *TurnoutSys {
|
||||
}
|
||||
|
||||
func (sys *TurnoutSys) Update(w ecs.World) {
|
||||
sys.stateQuery.Each(w, func(entry *ecs.Entry) {
|
||||
state := TurnoutStateComp.Get(entry)
|
||||
if state.Dc {
|
||||
if state.Db {
|
||||
state.Dc = false
|
||||
} else {
|
||||
state.Fb = false
|
||||
}
|
||||
}
|
||||
})
|
||||
// log.Println("道岔系统更新")
|
||||
// sys.stateQuery.Each(w, func(entry *ecs.Entry) {
|
||||
// state := TurnoutStateComp.Get(entry)
|
||||
// if state.Dc {
|
||||
// if state.Db {
|
||||
// state.Dc = false
|
||||
// } else {
|
||||
// state.Fb = false
|
||||
// }
|
||||
// }
|
||||
// })
|
||||
sys.count += 1
|
||||
fmt.Println("time: ", time.Now().Format(time.StampMilli), "====>>>world-", w.Id(), " SwitchSystem update , count: ", sys.count)
|
||||
}
|
||||
|
||||
// 道岔定操
|
||||
|
Loading…
Reference in New Issue
Block a user