rts-sim-module/component/singleton/entity_uid_index.go

36 lines
733 B
Go
Raw Normal View History

package singleton
import (
"sync"
"joylink.club/ecs"
)
var EntityUidIndexType = ecs.NewComponentType[EntityUidIndex]()
// 对象实体Uid索引
2023-12-29 17:48:57 +08:00
// 只索引具有uid且不会销毁的实体
type EntityUidIndex struct {
mu sync.RWMutex
entityMap map[string]*ecs.Entry
}
func (idx *EntityUidIndex) Add(uid string, entity *ecs.Entry) {
idx.mu.Lock()
defer idx.mu.Unlock()
idx.entityMap[uid] = entity
}
func (idx *EntityUidIndex) Get(uid string) *ecs.Entry {
idx.mu.RLock()
defer idx.mu.RUnlock()
return idx.entityMap[uid]
}
func loadUidEntityIndex(w ecs.World) {
entry := w.Entry(w.Create(EntityUidIndexType))
EntityUidIndexType.Set(entry, &EntityUidIndex{
entityMap: make(map[string]*ecs.Entry, 512),
})
}