diff --git a/debug/debug.go b/debug/debug.go index 2b2c7b1..55b86ce 100644 --- a/debug/debug.go +++ b/debug/debug.go @@ -1,10 +1,26 @@ package debug import ( + "bytes" + + "github.com/yohamta/donburi" "github.com/yohamta/donburi/features/debug" - "joylink.club/ecs" ) -func PrintEntityCounts(w ecs.World) { - debug.PrintEntityCounts(w) +type EntityCounts = debug.EntityCounts + +func GetEntityCounts(w donburi.World) []EntityCounts { + return debug.GetEntityCounts(w) +} + +// 打印所有实体数量 +func SPrintEntityCounts(w donburi.World) string { + var out bytes.Buffer + out.WriteString("Entity Counts:\n") + for _, c := range GetEntityCounts(w) { + out.WriteString(c.String()) + out.WriteString("\n") + } + out.WriteString("\n") + return out.String() } diff --git a/go.mod b/go.mod index 8116882..8286f09 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module joylink.club/ecs -go 1.20 +go 1.21 require github.com/yohamta/donburi v1.3.8 diff --git a/world.go b/world.go index 4618c9a..eb5f2d3 100644 --- a/world.go +++ b/world.go @@ -2,6 +2,7 @@ package ecs import ( "fmt" + "log/slog" "math" "time" @@ -173,6 +174,12 @@ func (w *world) executeTodos() { } } func (w *world) run() { + defer func() { + if err := recover(); err != nil { + slog.Error("世界运行异常:", "stacks", err) + w.state = Error + } + }() for { if w.state == Error { // 世界错误,关闭世界 @@ -204,6 +211,6 @@ func (w *world) run() { w.times += w.speed } // dt := time.Since(start) - // fmt.Println("仿真系统执行耗时:", dt.Milliseconds(), "ms") + // slog.Info("仿真系统执行耗时:" + dt.Milliseconds() + "ms") } }