2023-08-16 17:17:20 +08:00
|
|
|
|
package system
|
|
|
|
|
|
2023-08-16 18:13:18 +08:00
|
|
|
|
import (
|
|
|
|
|
"github.com/yohamta/donburi/filter"
|
|
|
|
|
"joylink.club/ecs"
|
|
|
|
|
"joylink.club/rtsssimulation/components"
|
|
|
|
|
"joylink.club/rtsssimulation/state"
|
|
|
|
|
)
|
2023-08-16 17:17:20 +08:00
|
|
|
|
|
2023-08-18 10:06:17 +08:00
|
|
|
|
var psdQuery = ecs.NewQuery(filter.Contains(components.DeviceIdentityComponent, components.PsdStateComponent, components.EntityTagHandlerComponent))
|
2023-08-17 14:25:18 +08:00
|
|
|
|
|
2023-08-16 17:17:20 +08:00
|
|
|
|
type PsdSystem struct {
|
2023-08-17 14:25:18 +08:00
|
|
|
|
psdTagMap map[state.EntityTag][]*ecs.Query
|
2023-08-16 17:17:20 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewPsdSystem() *PsdSystem {
|
2023-08-16 18:13:18 +08:00
|
|
|
|
return &PsdSystem{
|
2023-08-17 14:25:18 +08:00
|
|
|
|
psdTagMap: make(map[state.EntityTag][]*ecs.Query),
|
2023-08-16 18:13:18 +08:00
|
|
|
|
}
|
2023-08-16 17:17:20 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// world 执行
|
|
|
|
|
func (me *PsdSystem) Update(world ecs.World) {
|
2023-08-16 18:13:18 +08:00
|
|
|
|
me.updatePsdCell(world)
|
2023-08-17 14:25:18 +08:00
|
|
|
|
me.updatePsd(world)
|
2023-08-16 18:13:18 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 车站单侧屏蔽门
|
|
|
|
|
func (me *PsdSystem) updatePsd(world ecs.World) {
|
2023-08-17 14:25:18 +08:00
|
|
|
|
psdQuery.Each(world, func(e *ecs.Entry) {
|
2023-08-18 10:06:17 +08:00
|
|
|
|
psdTag := components.EntityTagHandlerComponent.Get(e).Tag
|
2023-08-17 14:25:18 +08:00
|
|
|
|
_, ok := me.psdTagMap[psdTag]
|
2023-08-16 18:13:18 +08:00
|
|
|
|
if !ok {
|
2023-08-17 14:25:18 +08:00
|
|
|
|
me.psdTagMap[psdTag] = make([]*ecs.Query, 2)
|
|
|
|
|
//psd的所有cell查询
|
|
|
|
|
psdCellQuery := ecs.NewQuery(filter.Contains(psdTag))
|
|
|
|
|
me.psdTagMap[psdTag][0] = psdCellQuery
|
|
|
|
|
//psd的所有cell中的操作查询
|
2023-08-18 10:06:17 +08:00
|
|
|
|
psdCellOperateQuery := ecs.NewQuery(filter.Contains(psdTag, components.PsdCellOperatingComponent))
|
2023-08-17 14:25:18 +08:00
|
|
|
|
me.psdTagMap[psdTag][1] = psdCellOperateQuery
|
2023-08-16 18:13:18 +08:00
|
|
|
|
}
|
|
|
|
|
var allClosed bool = true
|
|
|
|
|
var allOpened bool = true
|
2023-08-17 14:25:18 +08:00
|
|
|
|
me.psdTagMap[psdTag][0].Each(world, func(e *ecs.Entry) {
|
2023-08-18 10:06:17 +08:00
|
|
|
|
cellState := components.PsdCellStateComponent.Get(e)
|
2023-08-16 18:13:18 +08:00
|
|
|
|
if allClosed {
|
|
|
|
|
if cellState.CloseRate < 100 {
|
|
|
|
|
allClosed = false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if allOpened {
|
|
|
|
|
if cellState.CloseRate > 0 {
|
|
|
|
|
allOpened = false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
//
|
2023-08-18 10:06:17 +08:00
|
|
|
|
psdState := components.PsdStateComponent.Get(e)
|
2023-08-16 18:13:18 +08:00
|
|
|
|
psdState.AllClosed = allClosed
|
|
|
|
|
psdState.AllOpened = allOpened
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-17 14:25:18 +08:00
|
|
|
|
const (
|
2023-08-17 17:39:57 +08:00
|
|
|
|
//把时间映射到距离,1000ms<=>1000mm
|
|
|
|
|
//注意距离不是门的真实宽度,仅用于门开关相关计算
|
2023-08-17 14:25:18 +08:00
|
|
|
|
//门移动最大路程3000mm
|
|
|
|
|
pSD_CELL_LEN = 3000
|
|
|
|
|
//cell门移动速度,单位mm/ms
|
|
|
|
|
pSD_CELL_MOVE_SPEED = 1
|
|
|
|
|
)
|
|
|
|
|
|
2023-08-16 18:13:18 +08:00
|
|
|
|
// 单个门
|
|
|
|
|
func (me *PsdSystem) updatePsdCell(world ecs.World) {
|
2023-08-17 14:25:18 +08:00
|
|
|
|
for _, psdTagQueries := range me.psdTagMap {
|
|
|
|
|
//遍历某个车站单侧的所有单个门的操作
|
|
|
|
|
psdTagQueries[1].Each(world, func(e *ecs.Entry) {
|
2023-08-18 10:06:17 +08:00
|
|
|
|
if psdCellOpt := components.PsdCellOperatingComponent.Get(e); psdCellOpt.Start {
|
|
|
|
|
psdCellState := components.PsdCellStateComponent.Get(e)
|
2023-08-17 14:25:18 +08:00
|
|
|
|
if psdCellOpt.RemainingDistance <= 0 {
|
|
|
|
|
psdCellOpt.Start = false
|
|
|
|
|
psdCellOpt.RemainingDistance = 0
|
2023-08-18 10:06:17 +08:00
|
|
|
|
e.RemoveComponent(components.PsdCellOperatingComponent)
|
2023-08-17 14:25:18 +08:00
|
|
|
|
} else {
|
|
|
|
|
psdCellOpt.RemainingDistance -= int64(pSD_CELL_MOVE_SPEED * world.Tick())
|
|
|
|
|
}
|
|
|
|
|
//
|
|
|
|
|
if psdCellOpt.CloseOperate {
|
|
|
|
|
psdCellState.CloseRate = int32((float32(psdCellOpt.InitDistance+psdCellOpt.SumDistance-psdCellOpt.RemainingDistance) / float32(pSD_CELL_LEN)) * 100)
|
|
|
|
|
} else {
|
|
|
|
|
psdCellState.CloseRate = 100 - int32((float32(psdCellOpt.InitDistance+psdCellOpt.SumDistance-psdCellOpt.RemainingDistance)/float32(pSD_CELL_LEN))*100)
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-08-16 18:13:18 +08:00
|
|
|
|
})
|
|
|
|
|
}
|
2023-08-16 17:17:20 +08:00
|
|
|
|
}
|
2023-08-17 14:25:18 +08:00
|
|
|
|
|
|
|
|
|
// 车站单侧屏蔽门互锁解除操作
|
|
|
|
|
func FirePsdInterlockRelease(world ecs.World, psdId string, lockRelease bool) bool {
|
|
|
|
|
var psdEntry *ecs.Entry = nil
|
|
|
|
|
psdQuery.Each(world, func(e *ecs.Entry) {
|
2023-08-18 10:06:17 +08:00
|
|
|
|
if psdId == components.DeviceIdentityComponent.Get(e).Id {
|
2023-08-17 14:25:18 +08:00
|
|
|
|
psdEntry = e
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
//
|
|
|
|
|
if psdEntry == nil {
|
|
|
|
|
return false
|
|
|
|
|
}
|
2023-08-18 10:06:17 +08:00
|
|
|
|
components.PsdStateComponent.Get(psdEntry).InterlockReleased = lockRelease
|
2023-08-17 14:25:18 +08:00
|
|
|
|
//
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 车站单侧屏蔽门操作
|
|
|
|
|
// closeOpt true-关门操作,false-开门操作
|
|
|
|
|
func FirePsdMove(world ecs.World, psdId string, closeOpt bool) bool {
|
|
|
|
|
var psdEntry *ecs.Entry = nil
|
|
|
|
|
psdQuery.Each(world, func(e *ecs.Entry) {
|
2023-08-18 10:06:17 +08:00
|
|
|
|
if psdId == components.DeviceIdentityComponent.Get(e).Id {
|
2023-08-17 14:25:18 +08:00
|
|
|
|
psdEntry = e
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
//
|
|
|
|
|
if psdEntry == nil {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
//
|
|
|
|
|
var closeRate int
|
|
|
|
|
if closeOpt {
|
|
|
|
|
closeRate = 100
|
|
|
|
|
} else {
|
|
|
|
|
closeRate = 0
|
|
|
|
|
}
|
|
|
|
|
//
|
2023-08-18 10:06:17 +08:00
|
|
|
|
psdTag := components.EntityTagHandlerComponent.Get(psdEntry).Tag
|
2023-08-17 14:25:18 +08:00
|
|
|
|
psdCellQuery := ecs.NewQuery(filter.Contains(psdTag))
|
|
|
|
|
psdCellQuery.Each(world, func(e *ecs.Entry) {
|
2023-08-18 10:06:17 +08:00
|
|
|
|
psdCellId := components.DeviceIdentityComponent.Get(e).Id
|
2023-08-17 14:25:18 +08:00
|
|
|
|
firePsdCellMove0(world, psdEntry, psdCellId, closeRate)
|
|
|
|
|
})
|
|
|
|
|
//
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 单个门操作
|
|
|
|
|
// psdId 单侧屏蔽门id
|
|
|
|
|
// psdCellId 屏蔽门cell的id
|
|
|
|
|
// closeRate 门最终关闭的百分比,0-完全开门,100-完全关门
|
|
|
|
|
func FirePsdCellMove(world ecs.World, psdId string, psdCellId string, closeRate int) bool {
|
|
|
|
|
var psdEntry *ecs.Entry = nil
|
|
|
|
|
psdQuery.Each(world, func(e *ecs.Entry) {
|
2023-08-18 10:06:17 +08:00
|
|
|
|
if psdId == components.DeviceIdentityComponent.Get(e).Id {
|
2023-08-17 14:25:18 +08:00
|
|
|
|
psdEntry = e
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
return firePsdCellMove0(world, psdEntry, psdCellId, closeRate)
|
|
|
|
|
}
|
|
|
|
|
func firePsdCellMove0(world ecs.World, psdEntry *ecs.Entry, psdCellId string, closeRate int) bool {
|
|
|
|
|
//
|
|
|
|
|
if psdEntry == nil {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
//
|
2023-08-18 10:06:17 +08:00
|
|
|
|
psdTag := components.EntityTagHandlerComponent.Get(psdEntry).Tag
|
2023-08-17 14:25:18 +08:00
|
|
|
|
psdCellQuery := ecs.NewQuery(filter.Contains(psdTag))
|
|
|
|
|
var psdCellEntry *ecs.Entry = nil
|
|
|
|
|
psdCellQuery.Each(world, func(e *ecs.Entry) {
|
2023-08-18 10:06:17 +08:00
|
|
|
|
if psdCellId == components.DeviceIdentityComponent.Get(e).Id {
|
2023-08-17 14:25:18 +08:00
|
|
|
|
psdCellEntry = e
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
if psdCellEntry == nil {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
//
|
2023-08-18 10:06:17 +08:00
|
|
|
|
psdCellState := components.PsdCellStateComponent.Get(psdCellEntry)
|
2023-08-17 14:25:18 +08:00
|
|
|
|
if closeRate == int(psdCellState.CloseRate) {
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
// 判断是关门还是开门操作
|
|
|
|
|
close := closeRate > int(psdCellState.CloseRate)
|
|
|
|
|
//
|
2023-08-18 10:06:17 +08:00
|
|
|
|
if !psdCellEntry.HasComponent(components.PsdCellOperatingComponent) {
|
|
|
|
|
psdCellEntry.AddComponent(components.PsdCellOperatingComponent)
|
2023-08-17 14:25:18 +08:00
|
|
|
|
}
|
|
|
|
|
//
|
|
|
|
|
var initDistance int64
|
|
|
|
|
var sumDistance int64
|
|
|
|
|
if close {
|
|
|
|
|
initDistance = int64(float32(pSD_CELL_LEN) * (float32(psdCellState.CloseRate) / float32(100)))
|
|
|
|
|
sumDistance = int64(float32(pSD_CELL_LEN) * (float32(closeRate-int(psdCellState.CloseRate)) / float32(100)))
|
|
|
|
|
} else {
|
|
|
|
|
initDistance = int64(float32(pSD_CELL_LEN) * (float32(100-psdCellState.CloseRate) / float32(100)))
|
|
|
|
|
sumDistance = int64(float32(pSD_CELL_LEN) * (float32(int(psdCellState.CloseRate)-closeRate) / float32(100)))
|
|
|
|
|
}
|
|
|
|
|
//
|
2023-08-18 10:06:17 +08:00
|
|
|
|
*(components.PsdCellOperatingComponent.Get(psdCellEntry)) = state.PsdCellOperating{
|
2023-08-17 14:25:18 +08:00
|
|
|
|
Start: true,
|
|
|
|
|
CloseOperate: close,
|
|
|
|
|
InitDistance: initDistance,
|
|
|
|
|
SumDistance: sumDistance,
|
|
|
|
|
RemainingDistance: sumDistance,
|
|
|
|
|
}
|
|
|
|
|
//
|
|
|
|
|
return true
|
|
|
|
|
}
|