rts-sim-module/system/system.go
2023-08-29 15:00:56 +08:00

66 lines
1.7 KiB
Go

package system
import (
"fmt"
"github.com/yohamta/donburi/filter"
"joylink.club/ecs"
"joylink.club/rtsssimulation/components"
"joylink.club/rtsssimulation/umi"
)
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 simpleRecover()
q.Each(world, func(e *ecs.Entry) {
if id == components.DeviceIdentityComponent.Get(e).Id {
entry = e
panic(fmt.Sprintf("找到实体[%s],结束查找", id))
}
})
}()
//
return entry
}
// 捕获panic并恢复执行
func simpleRecover() {
recover()
}
// /////////////////////////////////////////////////////////////////////////
// 模型仓库查询
var worldModelStorageQuery *ecs.Query = ecs.NewQuery(filter.Contains(components.ModelStorageRefComponent))
func WorldModelStorage(world ecs.World) umi.IModelManager {
e, ok := worldModelStorageQuery.First(world)
if ok {
return components.ModelStorageRefComponent.Get(e).ModelManager
} else {
return nil
}
}
// //////////////////////////////////////////////////////////////////////////
// 继电器系统
type relaySystem struct {
// key-设备id
relayQuery map[string]*ecs.Query
}
// 获取设备的相关继电器的查询
func (me *relaySystem) getDeviceRelayQuery(deviceEntry *ecs.Entry) *ecs.Query {
id := components.DeviceIdentityComponent.Get(deviceEntry).Id
query, ok := me.relayQuery[id]
if !ok {
query = ecs.NewQuery(filter.Contains(components.RelayTagHandlerComponent.Get(deviceEntry).Tag))
me.relayQuery[id] = query
}
return query
}