package singleton import ( "sync" "joylink.club/ecs" ) var EntityUidIndexType = ecs.NewComponentType[EntityUidIndex]() // 对象实体Uid索引 // 只索引具有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 (idx *EntityUidIndex) Has(uid string) bool { idx.mu.RLock() defer idx.mu.RUnlock() _, ok := idx.entityMap[uid] return ok } func (idx *EntityUidIndex) Remove(uid string) { idx.mu.Lock() defer idx.mu.Unlock() delete(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), }) }