127 lines
3.4 KiB
Go
127 lines
3.4 KiB
Go
package system
|
||
|
||
import (
|
||
"fmt"
|
||
|
||
"joylink.club/rtsssimulation/deprecated/simulation"
|
||
|
||
"joylink.club/ecs"
|
||
"joylink.club/ecs/filter"
|
||
sysEvent "joylink.club/rtsssimulation/deprecated/system/event"
|
||
)
|
||
|
||
// EntityIdentity 实体身份定义
|
||
type EntityIdentity struct {
|
||
Id string
|
||
}
|
||
|
||
// EntityIdentityComponent 实体身份组件
|
||
var EntityIdentityComponent = ecs.NewComponentType[EntityIdentity]()
|
||
|
||
func FindEntityById(world ecs.World, id string) *ecs.Entry {
|
||
query := ecs.NewQuery(filter.Contains(EntityIdentityComponent))
|
||
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 == EntityIdentityComponent.Get(e).Id {
|
||
entry = e
|
||
panic(fmt.Sprintf("找到实体[%s],结束查找", id))
|
||
}
|
||
})
|
||
}()
|
||
//
|
||
return entry
|
||
}
|
||
|
||
var modelStorageQuery = ecs.NewQuery(filter.Contains(ModelStorageComponent))
|
||
|
||
// 测试用
|
||
var XXDebug = false
|
||
|
||
// FindModelStorage 获取模型仓库
|
||
func FindModelStorage(world ecs.World) IModelManager {
|
||
if XXDebug {
|
||
e, _ := modelStorageQuery.First(world)
|
||
return ModelStorageComponent.Get(e).ModelManager
|
||
} else {
|
||
return simulation.FindSimulation(world.Id()).Repo()
|
||
}
|
||
}
|
||
|
||
// 捕获panic并恢复执行
|
||
func simpleRecover() {
|
||
recover()
|
||
}
|
||
|
||
/////////////////////////////////////////////////////////
|
||
|
||
// EntityTag 实体标签
|
||
type EntityTag = ecs.IComponentType
|
||
|
||
type EntityTagHandler struct {
|
||
Tag EntityTag
|
||
}
|
||
|
||
func NewTag() EntityTag {
|
||
return ecs.NewComponentType[struct{}]()
|
||
}
|
||
func NewEntityTagHandler() *EntityTagHandler {
|
||
h := &EntityTagHandler{Tag: NewTag()}
|
||
return h
|
||
}
|
||
|
||
var EntityTagHandlerComponent = ecs.NewComponentType[EntityTagHandler]()
|
||
|
||
/////////////////////////////////////////////////////////
|
||
|
||
// ModelStorageRef 模型仓库引用
|
||
// 用于world内使用,查询模型
|
||
type ModelStorageRef struct {
|
||
ModelManager IModelManager
|
||
}
|
||
|
||
// ModelStorageComponent 模型仓库组件
|
||
var ModelStorageComponent = ecs.NewComponentType[ModelStorageRef]()
|
||
|
||
/////////////////////////////////////////////////////////
|
||
|
||
// DriveRelay 驱动继电器
|
||
// circuitModelId-如道岔模型id
|
||
// relayGroup-继电器组合类型
|
||
// relayName-继电器功能名称
|
||
// xh-true:吸合,false:落下
|
||
func DriveRelay(w ecs.World, circuitModelId string, relayGroup string, relayName string, xh bool) bool {
|
||
if swModel := FindModelStorage(w).FindById(circuitModelId); swModel != nil {
|
||
if roler, isCr := swModel.(IRelayCRole); isCr {
|
||
if relayModel := roler.FindRelayModelByCRole(relayGroup, relayName); relayModel != nil {
|
||
if deviceModel, isDm := relayModel.(IDeviceModel); isDm {
|
||
relayId := deviceModel.Id()
|
||
sysEvent.RelayNeedChangeEventBus.Publish(w, &sysEvent.RelayNeedChangeEvent{Id: relayId, Xh: xh})
|
||
return true
|
||
}
|
||
}
|
||
}
|
||
}
|
||
//
|
||
return false
|
||
}
|
||
|
||
func createRelayNeedChangeEvent(w ecs.World, circuitModelId string, relayGroup string, relayName string, xh bool) (*sysEvent.RelayNeedChangeEvent, bool) {
|
||
if swModel := FindModelStorage(w).FindById(circuitModelId); swModel != nil {
|
||
if roler, isCr := swModel.(IRelayCRole); isCr {
|
||
if relayModel := roler.FindRelayModelByCRole(relayGroup, relayName); relayModel != nil {
|
||
if deviceModel, isDm := relayModel.(IDeviceModel); isDm {
|
||
relayId := deviceModel.Id()
|
||
return &sysEvent.RelayNeedChangeEvent{Id: relayId, Xh: xh}, true
|
||
}
|
||
}
|
||
}
|
||
}
|
||
//
|
||
return nil, false
|
||
}
|