ecs事件多world测试

This commit is contained in:
xzb 2023-08-31 18:13:02 +08:00
parent 0d2b516362
commit 5ca27fb4a1
2 changed files with 32 additions and 20 deletions

View File

@ -27,7 +27,7 @@ type (
func init() { func init() {
//开启事件处理调试 //开启事件处理调试
events.Debug = true events.Debug = false
} }
// 创建事件类型的实例 // 创建事件类型的实例

View File

@ -13,44 +13,56 @@ type FireSwitchDcEvent struct {
var fireSwitchDcEventType = ecs.NewEventType[FireSwitchDcEvent]() var fireSwitchDcEventType = ecs.NewEventType[FireSwitchDcEvent]()
type SwitchSystem struct {
}
func (me *SwitchSystem) WhenFireSwitchDcEvent(w ecs.World, event FireSwitchDcEvent) {
fmt.Println("====>>>world-", w.GoroutineId(), " SwitchSystem 处理事件 ", event)
}
func (me *SwitchSystem) Update(w ecs.World) {
fmt.Println("====>>>world-", w.GoroutineId(), " SwitchSystem update ")
}
var switchSystem = &SwitchSystem{}
var (
wd1 = ecs.NewWorld(1000)
wd2 = ecs.NewWorld(1000)
)
// ////////////////////////////////////////
func main() { func main() {
go world1() go world1()
go world2() go world2()
time.Sleep(30 * time.Second) time.Sleep(4 * time.Second)
fireSwitchDcEventType.Publish(wd1, &FireSwitchDcEvent{Dc: true})
fireSwitchDcEventType.Publish(wd2, &FireSwitchDcEvent{Dc: false})
time.Sleep(40 * time.Second)
} }
func world1() { func world1() {
world := ecs.NewWorld(1000) world := wd1
world.StartUp() world.StartUp()
// //
world.AddSystem(switchSystem)
fireSwitchDcEventType.Subscribe(world, func(w ecs.World, event FireSwitchDcEvent) { //
fmt.Println("==w1>>1 触发道岔定操事件 : dc = ", event.Dc) fireSwitchDcEventType.Subscribe(world, switchSystem.WhenFireSwitchDcEvent)
})
fireSwitchDcEventType.Subscribe(world, func(w ecs.World, event FireSwitchDcEvent) {
fmt.Println("==w1>>2 触发道岔定操事件 : dc = ", event.Dc)
})
// //
time.Sleep(3 * time.Second) time.Sleep(3 * time.Second)
// //
fireSwitchDcEventType.Publish(world, &FireSwitchDcEvent{Dc: true}) fireSwitchDcEventType.Publish(world, &FireSwitchDcEvent{Dc: true})
fireSwitchDcEventType.Publish(world, &FireSwitchDcEvent{Dc: false}) fireSwitchDcEventType.Publish(world, &FireSwitchDcEvent{Dc: false})
time.Sleep(30 * time.Second) time.Sleep(10 * time.Second)
} }
func world2() { func world2() {
world := ecs.NewWorld(1000) world := wd2
world.StartUp() world.StartUp()
// //
world.AddSystem(switchSystem)
fireSwitchDcEventType.Subscribe(world, func(w ecs.World, event FireSwitchDcEvent) { fireSwitchDcEventType.Subscribe(world, switchSystem.WhenFireSwitchDcEvent)
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) time.Sleep(3 * time.Second)
// //
fireSwitchDcEventType.Publish(world, &FireSwitchDcEvent{Dc: true}) fireSwitchDcEventType.Publish(world, &FireSwitchDcEvent{Dc: true})
fireSwitchDcEventType.Publish(world, &FireSwitchDcEvent{Dc: false}) fireSwitchDcEventType.Publish(world, &FireSwitchDcEvent{Dc: false})
time.Sleep(30 * time.Second) time.Sleep(10 * time.Second)
} }