From 5ca27fb4a1eecbdd9fe4508f562c4f74baafcc0e Mon Sep 17 00:00:00 2001 From: xzb <223@qq.com> Date: Thu, 31 Aug 2023 18:13:02 +0800 Subject: [PATCH] =?UTF-8?q?ecs=E4=BA=8B=E4=BB=B6=E5=A4=9Aworld=E6=B5=8B?= =?UTF-8?q?=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- events.go | 2 +- examples/rtss-event/main.go | 50 +++++++++++++++++++++++-------------- 2 files changed, 32 insertions(+), 20 deletions(-) diff --git a/events.go b/events.go index 6444b7d..6d4e220 100644 --- a/events.go +++ b/events.go @@ -27,7 +27,7 @@ type ( func init() { //开启事件处理调试 - events.Debug = true + events.Debug = false } // 创建事件类型的实例 diff --git a/examples/rtss-event/main.go b/examples/rtss-event/main.go index bcc6b54..575e014 100644 --- a/examples/rtss-event/main.go +++ b/examples/rtss-event/main.go @@ -13,44 +13,56 @@ type FireSwitchDcEvent struct { 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() { go world1() 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() { - world := ecs.NewWorld(1000) + world := wd1 world.StartUp() // - - fireSwitchDcEventType.Subscribe(world, func(w ecs.World, event FireSwitchDcEvent) { - fmt.Println("==w1>>1 触发道岔定操事件 : dc = ", event.Dc) - }) - fireSwitchDcEventType.Subscribe(world, func(w ecs.World, event FireSwitchDcEvent) { - fmt.Println("==w1>>2 触发道岔定操事件 : dc = ", event.Dc) - }) + world.AddSystem(switchSystem) + // + fireSwitchDcEventType.Subscribe(world, switchSystem.WhenFireSwitchDcEvent) // time.Sleep(3 * time.Second) // fireSwitchDcEventType.Publish(world, &FireSwitchDcEvent{Dc: true}) fireSwitchDcEventType.Publish(world, &FireSwitchDcEvent{Dc: false}) - time.Sleep(30 * time.Second) + time.Sleep(10 * time.Second) } func world2() { - world := ecs.NewWorld(1000) + world := wd2 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) - }) + world.AddSystem(switchSystem) + fireSwitchDcEventType.Subscribe(world, switchSystem.WhenFireSwitchDcEvent) // time.Sleep(3 * time.Second) // fireSwitchDcEventType.Publish(world, &FireSwitchDcEvent{Dc: true}) fireSwitchDcEventType.Publish(world, &FireSwitchDcEvent{Dc: false}) - time.Sleep(30 * time.Second) + time.Sleep(10 * time.Second) }