rts-sim-module/deprecated/system/system.go

127 lines
3.4 KiB
Go
Raw Normal View History

2023-08-24 10:34:37 +08:00
package system
import (
"fmt"
2023-09-20 16:57:53 +08:00
"joylink.club/rtsssimulation/deprecated/simulation"
2023-08-24 10:34:37 +08:00
"joylink.club/ecs"
"joylink.club/ecs/filter"
sysEvent "joylink.club/rtsssimulation/deprecated/system/event"
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-25 15:39:03 +08:00
// 测试用
2023-09-28 11:32:02 +08:00
var XXDebug = false
2023-09-25 15:39:03 +08:00
2023-09-15 13:48:48 +08:00
// FindModelStorage 获取模型仓库
2023-09-22 10:53:51 +08:00
func FindModelStorage(world ecs.World) IModelManager {
2023-09-28 11:32:02 +08:00
if XXDebug {
2023-09-25 15:39:03 +08:00
e, _ := modelStorageQuery.First(world)
return ModelStorageComponent.Get(e).ModelManager
} else {
2023-09-26 10:25:00 +08:00
return simulation.FindSimulation(world.Id()).Repo()
2023-09-25 15:39:03 +08:00
}
2023-09-12 18:01:51 +08:00
}
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
// EntityTag 实体标签
type EntityTag = ecs.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-20 18:07:47 +08:00
func NewTag() EntityTag {
return ecs.NewComponentType[struct{}]()
}
func NewEntityTagHandler() *EntityTagHandler {
h := &EntityTagHandler{Tag: NewTag()}
return h
}
var EntityTagHandlerComponent = ecs.NewComponentType[EntityTagHandler]()
2023-09-12 17:07:17 +08:00
/////////////////////////////////////////////////////////
// ModelStorageRef 模型仓库引用
2023-09-12 17:07:17 +08:00
// 用于world内使用查询模型
type ModelStorageRef struct {
2023-09-22 10:53:51 +08:00
ModelManager IModelManager
2023-08-29 15:00:56 +08:00
}
2023-09-12 18:01:51 +08:00
// ModelStorageComponent 模型仓库组件
2023-09-12 18:01:51 +08:00
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 {
2023-09-22 10:53:51 +08:00
if roler, isCr := swModel.(IRelayCRole); isCr {
if relayModel := roler.FindRelayModelByCRole(relayGroup, relayName); relayModel != nil {
2023-09-22 10:53:51 +08:00
if deviceModel, isDm := relayModel.(IDeviceModel); isDm {
2023-09-21 16:23:03 +08:00
relayId := deviceModel.Id()
2023-09-20 16:57:53 +08:00
sysEvent.RelayNeedChangeEventBus.Publish(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 {
2023-09-22 10:53:51 +08:00
if roler, isCr := swModel.(IRelayCRole); isCr {
2023-09-18 16:11:16 +08:00
if relayModel := roler.FindRelayModelByCRole(relayGroup, relayName); relayModel != nil {
2023-09-22 10:53:51 +08:00
if deviceModel, isDm := relayModel.(IDeviceModel); isDm {
2023-09-21 16:23:03 +08:00
relayId := deviceModel.Id()
2023-09-18 16:11:16 +08:00
return &sysEvent.RelayNeedChangeEvent{Id: relayId, Xh: xh}, true
}
}
}
}
//
return nil, false
}