优化查询实体

This commit is contained in:
xzb 2023-08-22 15:04:46 +08:00
parent 0a288cf3d5
commit 06c28871b4
6 changed files with 25 additions and 47 deletions

View File

@ -29,7 +29,7 @@ func main() {
time.Sleep(2 * time.Second) time.Sleep(2 * time.Second)
se := sim.FaceSystem.FindEntity("signal1") se := sim.FaceSystem.FindEntity("signal1")
fmt.Println("==>>fe : ", se) fmt.Println("==>>fe : ", se)
time.Sleep(60 * time.Second) //time.Sleep(60 * time.Second)
// //
testSwitchTurn(sim.World, sim.FaceSystem) testSwitchTurn(sim.World, sim.FaceSystem)
//testSignalOpt(world, face) //testSignalOpt(world, face)

View File

@ -10,9 +10,6 @@ import (
// 应答器查询 // 应答器查询
var baliseQuery *ecs.Query = ecs.NewQuery(filter.Contains(components.DeviceIdentityComponent, components.BaliseStateComponent)) var baliseQuery *ecs.Query = ecs.NewQuery(filter.Contains(components.DeviceIdentityComponent, components.BaliseStateComponent))
// 列车查询
var trainQuery *ecs.Query = ecs.NewQuery(filter.Contains(components.DeviceIdentityComponent, components.TrainStateComponent))
// 应答器模拟系统 // 应答器模拟系统
// 检测当有列车经过时,把报文发送给该列车 // 检测当有列车经过时,把报文发送给该列车
type BaliseSystem struct { type BaliseSystem struct {
@ -31,12 +28,7 @@ func (me *BaliseSystem) Update(w ecs.World) {
// 发送消息到应答器 // 发送消息到应答器
// 当要清空应答器中报文时,则message=nil // 当要清空应答器中报文时,则message=nil
func SendMessageToTransponder(world ecs.World, transponderId string, message *cstate.BaliseContent) bool { func SendMessageToTransponder(world ecs.World, transponderId string, message *cstate.BaliseContent) bool {
var transponderEntry *ecs.Entry = nil transponderEntry := queryEntityById(world, baliseQuery, transponderId)
baliseQuery.Each(world, func(e *ecs.Entry) {
if transponderId == components.DeviceIdentityComponent.Get(e).Id {
transponderEntry = e
}
})
// //
if transponderEntry == nil { if transponderEntry == nil {
return false return false

View File

@ -64,19 +64,7 @@ func (me *FaceSystem) Call(request FaceRequest) (any, bool) {
// 根据id获取实体 // 根据id获取实体
func (me *FaceSystem) FindEntity(id string) *ecs.Entry { func (me *FaceSystem) FindEntity(id string) *ecs.Entry {
query := ecs.NewQuery(filter.Contains(components.DeviceIdentityComponent)) return findEntityById(me.world, id)
var entry *ecs.Entry = nil
func() {
defer util.Recover()
query.Each(me.world, func(e *ecs.Entry) {
if id == components.DeviceIdentityComponent.Get(e).Id {
entry = e
panic(fmt.Sprintf("找到实体[%s],结束查找", id))
}
})
}()
//
return entry
} }
// 获取world当前时间 // 获取world当前时间
@ -90,3 +78,21 @@ func (me *FaceSystem) WorldTime() *time.Time {
return nil return nil
} }
} }
func findEntityById(world ecs.World, id string) *ecs.Entry {
query := ecs.NewQuery(filter.Contains(components.DeviceIdentityComponent))
return queryEntityById(world, query, id)
}
func queryEntityById(world ecs.World, q *ecs.Query, id string) *ecs.Entry {
var entry *ecs.Entry = nil
func() {
defer util.Recover()
q.Each(world, func(e *ecs.Entry) {
if id == components.DeviceIdentityComponent.Get(e).Id {
entry = e
panic(fmt.Sprintf("找到实体[%s],结束查找", id))
}
})
}()
//
return entry
}

View File

@ -101,12 +101,7 @@ func FirePsdCellOperation(world ecs.World, psdId string, psdCellId string, close
//屏蔽门标签 //屏蔽门标签
psdTag := components.EntityTagHandlerComponent.Get(psdEntry).Tag psdTag := components.EntityTagHandlerComponent.Get(psdEntry).Tag
psdCellQuery := ecs.NewQuery(filter.Contains(psdTag)) psdCellQuery := ecs.NewQuery(filter.Contains(psdTag))
var psdCellEntry *ecs.Entry = nil psdCellEntry := queryEntityById(world, psdCellQuery, psdCellId)
psdCellQuery.Each(world, func(e *ecs.Entry) {
if psdCellId == components.DeviceIdentityComponent.Get(e).Id {
psdCellEntry = e
}
})
if psdCellEntry == nil { if psdCellEntry == nil {
return fmt.Errorf("屏蔽门[%s]的单元门[%s]的实体不存在", psdId, psdCellId) return fmt.Errorf("屏蔽门[%s]的单元门[%s]的实体不存在", psdId, psdCellId)
} }
@ -116,12 +111,7 @@ func FirePsdCellOperation(world ecs.World, psdId string, psdCellId string, close
// 获取屏蔽门实体 // 获取屏蔽门实体
func findPsdEntry(world ecs.World, psdId string) (*ecs.Entry, error) { func findPsdEntry(world ecs.World, psdId string) (*ecs.Entry, error) {
var psdEntry *ecs.Entry = nil psdEntry := queryEntityById(world, psdQuery, psdId)
psdQuery.Each(world, func(e *ecs.Entry) {
if psdId == components.DeviceIdentityComponent.Get(e).Id {
psdEntry = e
}
})
if psdEntry == nil { if psdEntry == nil {
return nil, fmt.Errorf("屏蔽门[%s]实体不存在", psdId) return nil, fmt.Errorf("屏蔽门[%s]实体不存在", psdId)
} else { } else {

View File

@ -39,12 +39,7 @@ func SetSignalDisplay(w ecs.World, signalId string, display cstate.SignalAspect)
// 获取信号机实体 // 获取信号机实体
func findSignalEntry(w ecs.World, signalId string) (*ecs.Entry, error) { func findSignalEntry(w ecs.World, signalId string) (*ecs.Entry, error) {
var signalEntry *ecs.Entry var signalEntry *ecs.Entry = queryEntityById(w, signalQuery, signalId)
signalQuery.Each(w, func(e *ecs.Entry) {
if signalId == components.DeviceIdentityComponent.Get(e).Id {
signalEntry = e
}
})
if signalEntry != nil { if signalEntry != nil {
return signalEntry, nil return signalEntry, nil
} else { } else {

View File

@ -35,12 +35,7 @@ func FireSwitchTurn(w ecs.World, switchId string, terminalRate int32) error {
if terminalRate < 0 || terminalRate > 100 { if terminalRate < 0 || terminalRate > 100 {
return fmt.Errorf("道岔转动终点百分比[%d]不在范围[0,100]内", terminalRate) return fmt.Errorf("道岔转动终点百分比[%d]不在范围[0,100]内", terminalRate)
} }
var switchEntry *ecs.Entry = nil var switchEntry *ecs.Entry = queryEntityById(w, switchQuery, switchId)
switchQuery.Each(w, func(e *ecs.Entry) {
if switchId == components.DeviceIdentityComponent.Get(e).Id {
switchEntry = e
}
})
if switchEntry == nil { if switchEntry == nil {
return fmt.Errorf("道岔[%s]的实体不存在", switchId) return fmt.Errorf("道岔[%s]的实体不存在", switchId)
} }