修改获取WorldData单例组件bug

This commit is contained in:
walker 2023-09-28 15:38:18 +08:00
parent fe214fd3bc
commit 4d914eb66e
2 changed files with 21 additions and 2 deletions

View File

@ -3,6 +3,7 @@ package entity
import (
"time"
"github.com/yohamta/donburi/filter"
"joylink.club/ecs"
"joylink.club/rtsssimulation/component"
"joylink.club/rtsssimulation/repository"
@ -10,6 +11,9 @@ import (
// 初始化世界数据
func LoadWorldData(w ecs.World, repo *repository.Repository) {
if repo == nil {
panic("repo不能为nil")
}
entry := w.Create(component.WorldDataType)
component.WorldDataType.Set(entry, &component.WorldData{
Repo: repo,
@ -18,8 +22,13 @@ func LoadWorldData(w ecs.World, repo *repository.Repository) {
})
}
var query = ecs.NewQuery(filter.Contains(component.WorldDataType))
// 获取世界数据
func GetWorldData(w ecs.World) *component.WorldData {
entry := component.WorldDataType.MustFirst(w)
return component.WorldDataType.Get(entry)
entry, ok := query.First(w)
if ok {
return component.WorldDataType.Get(entry)
}
panic("世界数据不存在")
}

10
examples/main.go Normal file
View File

@ -0,0 +1,10 @@
package main
import (
rtss_simulation "joylink.club/rtsssimulation"
"joylink.club/rtsssimulation/repository"
)
func main() {
rtss_simulation.NewSimulation(&repository.Repository{})
}