From 2757e2c4e06b03afab94727cee9d948dae6d1754 Mon Sep 17 00:00:00 2001 From: walker Date: Thu, 28 Dec 2023 16:53:30 +0800 Subject: [PATCH] =?UTF-8?q?world=20=E5=85=B3=E9=97=AD=E6=96=B9=E6=B3=95?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=AF=B9=E6=9C=AA=E5=90=AF=E5=8A=A8=E8=BF=87?= =?UTF-8?q?world=E7=9A=84=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- examples/close/main.go | 22 +++++++++++----------- world.go | 5 +++++ 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/examples/close/main.go b/examples/close/main.go index d37c5ba..6fd4c09 100644 --- a/examples/close/main.go +++ b/examples/close/main.go @@ -20,18 +20,18 @@ func main() { slog.SetDefault(slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{Level: slog.LevelDebug}))) w := ecs.NewWorld(15) // ecs.EventsDebugEnable() - ecs.WorldStateChangeEvent.Subscribe(w, func(_ ecs.World, e ecs.WorldStateChange) { - slog.Info("世界状态变更", "statechange", e) - if e.NewState == ecs.WorldClosed { - panic("状态变更监听处理异常") - } - }) - slog.Info("世界启动") - w.AddSystem(&WorldTimeSys{}) - w.StartUp() + // ecs.WorldStateChangeEvent.Subscribe(w, func(_ ecs.World, e ecs.WorldStateChange) { + // slog.Info("世界状态变更", "statechange", e) + // if e.NewState == ecs.WorldClosed { + // panic("状态变更监听处理异常") + // } + // }) + // slog.Info("世界启动") + // w.AddSystem(&WorldTimeSys{}) + // w.StartUp() - time.Sleep(2 * time.Second) + time.Sleep(1 * time.Second) w.Close() - time.Sleep(4 * time.Second) + // time.Sleep(3 * time.Second) } diff --git a/world.go b/world.go index ef4366a..73d6716 100644 --- a/world.go +++ b/world.go @@ -203,6 +203,11 @@ func (w *world) Execute(fn HandleFunc) error { // 关闭世界 func (w *world) Close() { + if w.state == WorldInit { + slog.Debug("关闭世界", "id", w.Id()) + w.updateState(WorldClosed) + return + } w.cancel() <-w.done }