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