rts-sim-module/sys/device_sys/zzj.go
walker d3a187e00f 添加创建仿真
完善仿真实体加载
2023-09-28 14:34:00 +08:00

67 lines
1.6 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 (
"github.com/yohamta/donburi/filter"
"joylink.club/ecs"
"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.TwoPositionTransformType)),
}
}
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.TwoPositionTransformType.Get(entry)
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
}
}
})
}