rts-sim-module/sys/device_sys/zzj.go

74 lines
1.8 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package device_sys
import (
"joylink.club/ecs"
"joylink.club/ecs/filter"
"joylink.club/rtsssimulation/component"
"joylink.club/rtsssimulation/consts"
)
type ZzjSys struct {
query *ecs.Query
}
func NewZzjSys() *ZzjSys {
return &ZzjSys{
query: ecs.NewQuery(filter.Contains(component.ZzjStateType, component.FixedPositionTransformType)),
}
}
const (
// 转辙机从一个位置到另一个位置转动时间单位ms
TurnTime = 3 * 1000
// 自动开闭器解锁点位置百分比
KbqJsPercent = 0.05
)
func (z *ZzjSys) Update(w ecs.World) {
z.query.Each(w, func(entry *ecs.Entry) {
zzj := component.ZzjStateType.Get(entry)
tp := component.FixedPositionTransformType.Get(entry)
if entry.HasComponent(component.TurnoutFaultJcType) {
// 道岔挤岔,设置位置到中间,且不再能转动
tp.Pos = consts.TwoPosMax / 2
tp.Speed = 0
} else { // 正常转辙机带动
if zzj.Td { // 通电
if tp.Speed >= 0 && tp.Pos > consts.TwoPosMin && zzj.Dw { // 转到定位
tp.Speed = -component.CalculateTwoPositionAvgSpeed(TurnTime, w.Tick())
} else if tp.Speed <= 0 && tp.Pos < consts.TwoPosMax && !zzj.Dw { // 转到反位
tp.Speed = component.CalculateTwoPositionAvgSpeed(TurnTime, w.Tick())
}
} else { // 未通电
if tp.Speed != 0 { // 停止
tp.Speed = 0
}
}
}
if tp.Pos == consts.TwoPosMax { // 到反位
if !zzj.JD12 {
zzj.JD12 = true
}
if !zzj.JD34 {
zzj.JD34 = true
}
} else if tp.Pos == consts.TwoPosMin { // 到定位
if zzj.JD12 {
zzj.JD12 = false
}
if zzj.JD34 {
zzj.JD34 = false
}
} else if tp.Percentage() > KbqJsPercent && tp.Percentage() < (1-KbqJsPercent) { //中间位置
if zzj.JD12 {
zzj.JD12 = false
}
if !zzj.JD34 {
zzj.JD34 = true
}
}
})
}