2023-08-24 10:34:37 +08:00
|
|
|
|
package system
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
2023-09-12 17:07:17 +08:00
|
|
|
|
"github.com/yohamta/donburi/component"
|
2023-08-24 10:34:37 +08:00
|
|
|
|
"github.com/yohamta/donburi/filter"
|
|
|
|
|
"joylink.club/ecs"
|
2023-09-15 16:39:11 +08:00
|
|
|
|
sysEvent "joylink.club/rtsssimulation/system/event"
|
2023-08-28 15:38:21 +08:00
|
|
|
|
"joylink.club/rtsssimulation/umi"
|
2023-08-24 10:34:37 +08:00
|
|
|
|
)
|
|
|
|
|
|
2023-09-15 13:48:48 +08:00
|
|
|
|
// EntityIdentity 实体身份定义
|
2023-09-12 17:07:17 +08:00
|
|
|
|
type EntityIdentity struct {
|
|
|
|
|
Id string
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-15 13:48:48 +08:00
|
|
|
|
// EntityIdentityComponent 实体身份组件
|
2023-09-12 17:07:17 +08:00
|
|
|
|
var EntityIdentityComponent = ecs.NewComponentType[EntityIdentity]()
|
|
|
|
|
|
2023-08-24 10:34:37 +08:00
|
|
|
|
func FindEntityById(world ecs.World, id string) *ecs.Entry {
|
2023-09-12 17:07:17 +08:00
|
|
|
|
query := ecs.NewQuery(filter.Contains(EntityIdentityComponent))
|
2023-08-24 10:34:37 +08:00
|
|
|
|
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) {
|
2023-09-12 17:07:17 +08:00
|
|
|
|
if id == EntityIdentityComponent.Get(e).Id {
|
2023-08-24 10:34:37 +08:00
|
|
|
|
entry = e
|
|
|
|
|
panic(fmt.Sprintf("找到实体[%s],结束查找", id))
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}()
|
|
|
|
|
//
|
|
|
|
|
return entry
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-12 18:01:51 +08:00
|
|
|
|
var modelStorageQuery = ecs.NewQuery(filter.Contains(ModelStorageComponent))
|
|
|
|
|
|
2023-09-15 13:48:48 +08:00
|
|
|
|
// FindModelStorage 获取模型仓库
|
2023-09-12 18:01:51 +08:00
|
|
|
|
func FindModelStorage(world ecs.World) umi.IModelManager {
|
|
|
|
|
e, _ := modelStorageQuery.First(world)
|
|
|
|
|
return ModelStorageComponent.Get(e).ModelManager
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-24 10:34:37 +08:00
|
|
|
|
// 捕获panic并恢复执行
|
|
|
|
|
func simpleRecover() {
|
|
|
|
|
recover()
|
|
|
|
|
}
|
2023-08-28 15:38:21 +08:00
|
|
|
|
|
2023-09-12 17:07:17 +08:00
|
|
|
|
/////////////////////////////////////////////////////////
|
2023-08-28 15:38:21 +08:00
|
|
|
|
|
2023-09-15 16:39:11 +08:00
|
|
|
|
// EntityTag 实体标签
|
2023-09-12 17:07:17 +08:00
|
|
|
|
type EntityTag = component.IComponentType
|
2023-08-29 15:00:56 +08:00
|
|
|
|
|
2023-09-12 17:07:17 +08:00
|
|
|
|
type EntityTagHandler struct {
|
|
|
|
|
Tag EntityTag
|
2023-08-29 15:00:56 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-09-12 17:07:17 +08:00
|
|
|
|
/////////////////////////////////////////////////////////
|
|
|
|
|
|
2023-09-15 16:39:11 +08:00
|
|
|
|
// ModelStorageRef 模型仓库引用
|
2023-09-12 17:07:17 +08:00
|
|
|
|
// 用于world内使用,查询模型
|
|
|
|
|
type ModelStorageRef struct {
|
|
|
|
|
ModelManager umi.IModelManager
|
2023-08-29 15:00:56 +08:00
|
|
|
|
}
|
2023-09-12 18:01:51 +08:00
|
|
|
|
|
2023-09-15 16:39:11 +08:00
|
|
|
|
// ModelStorageComponent 模型仓库组件
|
2023-09-12 18:01:51 +08:00
|
|
|
|
var ModelStorageComponent = ecs.NewComponentType[ModelStorageRef]()
|
2023-09-15 16:39:11 +08:00
|
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
// 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.(umi.IRelayCRole); isCr {
|
|
|
|
|
if relayModel := roler.FindRelayModelByCRole(relayGroup, relayName); relayModel != nil {
|
|
|
|
|
if deviceModel, isDm := relayModel.(umi.IDeviceModel); isDm {
|
|
|
|
|
relayId := deviceModel.GetId()
|
|
|
|
|
sysEvent.RelayNeedChangeEventBus.PublishOutWorld(w, &sysEvent.RelayNeedChangeEvent{Id: relayId, Xh: xh})
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
//
|
|
|
|
|
return false
|
|
|
|
|
}
|
2023-09-18 16:11:16 +08:00
|
|
|
|
|
|
|
|
|
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.(umi.IRelayCRole); isCr {
|
|
|
|
|
if relayModel := roler.FindRelayModelByCRole(relayGroup, relayName); relayModel != nil {
|
|
|
|
|
if deviceModel, isDm := relayModel.(umi.IDeviceModel); isDm {
|
|
|
|
|
relayId := deviceModel.GetId()
|
|
|
|
|
return &sysEvent.RelayNeedChangeEvent{Id: relayId, Xh: xh}, true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
//
|
|
|
|
|
return nil, false
|
|
|
|
|
}
|