优化查询实体

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)
se := sim.FaceSystem.FindEntity("signal1")
fmt.Println("==>>fe : ", se)
time.Sleep(60 * time.Second)
//time.Sleep(60 * time.Second)
//
testSwitchTurn(sim.World, sim.FaceSystem)
//testSignalOpt(world, face)

View File

@ -10,9 +10,6 @@ import (
// 应答器查询
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 {
@ -31,12 +28,7 @@ func (me *BaliseSystem) Update(w ecs.World) {
// 发送消息到应答器
// 当要清空应答器中报文时,则message=nil
func SendMessageToTransponder(world ecs.World, transponderId string, message *cstate.BaliseContent) bool {
var transponderEntry *ecs.Entry = nil
baliseQuery.Each(world, func(e *ecs.Entry) {
if transponderId == components.DeviceIdentityComponent.Get(e).Id {
transponderEntry = e
}
})
transponderEntry := queryEntityById(world, baliseQuery, transponderId)
//
if transponderEntry == nil {
return false

View File

@ -64,19 +64,7 @@ func (me *FaceSystem) Call(request FaceRequest) (any, bool) {
// 根据id获取实体
func (me *FaceSystem) FindEntity(id string) *ecs.Entry {
query := ecs.NewQuery(filter.Contains(components.DeviceIdentityComponent))
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
return findEntityById(me.world, id)
}
// 获取world当前时间
@ -90,3 +78,21 @@ func (me *FaceSystem) WorldTime() *time.Time {
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
psdCellQuery := ecs.NewQuery(filter.Contains(psdTag))
var psdCellEntry *ecs.Entry = nil
psdCellQuery.Each(world, func(e *ecs.Entry) {
if psdCellId == components.DeviceIdentityComponent.Get(e).Id {
psdCellEntry = e
}
})
psdCellEntry := queryEntityById(world, psdCellQuery, psdCellId)
if psdCellEntry == nil {
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) {
var psdEntry *ecs.Entry = nil
psdQuery.Each(world, func(e *ecs.Entry) {
if psdId == components.DeviceIdentityComponent.Get(e).Id {
psdEntry = e
}
})
psdEntry := queryEntityById(world, psdQuery, psdId)
if psdEntry == nil {
return nil, fmt.Errorf("屏蔽门[%s]实体不存在", psdId)
} 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) {
var signalEntry *ecs.Entry
signalQuery.Each(w, func(e *ecs.Entry) {
if signalId == components.DeviceIdentityComponent.Get(e).Id {
signalEntry = e
}
})
var signalEntry *ecs.Entry = queryEntityById(w, signalQuery, signalId)
if signalEntry != nil {
return signalEntry, nil
} else {

View File

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