world 关闭方法添加对未启动过world的处理

This commit is contained in:
walker 2023-12-28 16:53:30 +08:00
parent e83feb89dd
commit 2757e2c4e0
2 changed files with 16 additions and 11 deletions

View File

@ -20,18 +20,18 @@ func main() {
slog.SetDefault(slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{Level: slog.LevelDebug}))) slog.SetDefault(slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{Level: slog.LevelDebug})))
w := ecs.NewWorld(15) w := ecs.NewWorld(15)
// ecs.EventsDebugEnable() // ecs.EventsDebugEnable()
ecs.WorldStateChangeEvent.Subscribe(w, func(_ ecs.World, e ecs.WorldStateChange) { // ecs.WorldStateChangeEvent.Subscribe(w, func(_ ecs.World, e ecs.WorldStateChange) {
slog.Info("世界状态变更", "statechange", e) // slog.Info("世界状态变更", "statechange", e)
if e.NewState == ecs.WorldClosed { // if e.NewState == ecs.WorldClosed {
panic("状态变更监听处理异常") // panic("状态变更监听处理异常")
} // }
}) // })
slog.Info("世界启动") // slog.Info("世界启动")
w.AddSystem(&WorldTimeSys{}) // w.AddSystem(&WorldTimeSys{})
w.StartUp() // w.StartUp()
time.Sleep(2 * time.Second) time.Sleep(1 * time.Second)
w.Close() w.Close()
time.Sleep(4 * time.Second) // time.Sleep(3 * time.Second)
} }

View File

@ -203,6 +203,11 @@ func (w *world) Execute(fn HandleFunc) error {
// 关闭世界 // 关闭世界
func (w *world) Close() { func (w *world) Close() {
if w.state == WorldInit {
slog.Debug("关闭世界", "id", w.Id())
w.updateState(WorldClosed)
return
}
w.cancel() w.cancel()
<-w.done <-w.done
} }