36 lines
733 B
Go
36 lines
733 B
Go
package singleton
|
|
|
|
import (
|
|
"sync"
|
|
|
|
"joylink.club/ecs"
|
|
)
|
|
|
|
var EntityUidIndexType = ecs.NewComponentType[EntityUidIndex]()
|
|
|
|
// 对象实体Uid索引
|
|
// 只索引具有uid且不会销毁的实体
|
|
type EntityUidIndex struct {
|
|
mu sync.RWMutex
|
|
entityMap map[string]ecs.Entity
|
|
}
|
|
|
|
func (idx *EntityUidIndex) Add(uid string, entity ecs.Entity) {
|
|
idx.mu.Lock()
|
|
defer idx.mu.Unlock()
|
|
idx.entityMap[uid] = entity
|
|
}
|
|
|
|
func (idx *EntityUidIndex) Get(uid string) ecs.Entity {
|
|
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.Entity, 512),
|
|
})
|
|
}
|