2023-09-27 18:39:18 +08:00
|
|
|
package entity
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"joylink.club/ecs"
|
2023-10-09 11:17:25 +08:00
|
|
|
"joylink.club/ecs/filter"
|
2023-09-27 18:39:18 +08:00
|
|
|
"joylink.club/rtsssimulation/component"
|
|
|
|
"joylink.club/rtsssimulation/repository"
|
|
|
|
)
|
|
|
|
|
|
|
|
// 初始化世界数据
|
2023-09-28 14:34:00 +08:00
|
|
|
func LoadWorldData(w ecs.World, repo *repository.Repository) {
|
2023-09-28 15:38:18 +08:00
|
|
|
if repo == nil {
|
|
|
|
panic("repo不能为nil")
|
|
|
|
}
|
2023-10-09 14:24:53 +08:00
|
|
|
entry := w.Entry(w.Create(component.WorldDataType))
|
2023-09-27 18:39:18 +08:00
|
|
|
component.WorldDataType.Set(entry, &component.WorldData{
|
|
|
|
Repo: repo,
|
|
|
|
Time: time.Now().UnixMilli(),
|
|
|
|
EntityMap: make(map[string]*ecs.Entry),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-09-28 17:18:23 +08:00
|
|
|
var worldDataQuery = ecs.NewQuery(filter.Contains(component.WorldDataType))
|
2023-09-28 15:38:18 +08:00
|
|
|
|
2023-09-27 18:39:18 +08:00
|
|
|
// 获取世界数据
|
|
|
|
func GetWorldData(w ecs.World) *component.WorldData {
|
2023-09-28 17:18:23 +08:00
|
|
|
entry, ok := worldDataQuery.First(w)
|
2023-09-28 15:38:18 +08:00
|
|
|
if ok {
|
|
|
|
return component.WorldDataType.Get(entry)
|
|
|
|
}
|
2023-10-20 15:05:56 +08:00
|
|
|
panic("不存在世界数据组件")
|
2023-09-27 18:39:18 +08:00
|
|
|
}
|
2023-09-28 17:18:23 +08:00
|
|
|
|
|
|
|
// 根据uid获取对象实体
|
|
|
|
func GetEntityByUid(w ecs.World, uid string) (entry *ecs.Entry, ok bool) {
|
|
|
|
wd := GetWorldData(w)
|
|
|
|
entry, ok = wd.EntityMap[uid]
|
|
|
|
return
|
|
|
|
}
|