rts-sim-module/fi/turnout.go

113 lines
2.9 KiB
Go
Raw Normal View History

package fi
import (
"fmt"
"joylink.club/ecs"
"joylink.club/rtsssimulation/component"
"joylink.club/rtsssimulation/component/component_proto"
"joylink.club/rtsssimulation/entity"
)
// 道岔功能接口
// 设置道岔故障
func SetTurnoutFault(w ecs.World, id string, fault component_proto.Turnout_Fault) error {
result := <-ecs.Request[ecs.EmptyType](w, func() ecs.Result[ecs.EmptyType] {
wd := entity.GetWorldData(w)
entry, ok := wd.EntityMap[id]
if ok {
ct := component.GetTurnoutFaultType(fault)
if !entry.HasComponent(ct) {
entry.AddComponent(ct)
}
} else {
return ecs.NewErrResult(fmt.Errorf("未找到id=%s的道岔", id))
}
return ecs.NewOkEmptyResult()
})
return result.Err
}
// 取消道岔故障
func CancleTurnoutFault(w ecs.World, id string, fault component_proto.Turnout_Fault) error {
result := <-ecs.Request[ecs.EmptyType](w, func() ecs.Result[ecs.EmptyType] {
wd := entity.GetWorldData(w)
entry, ok := wd.EntityMap[id]
if ok {
ct := component.GetTurnoutFaultType(fault)
if entry.HasComponent(ct) {
entry.RemoveComponent(ct)
}
} else {
return ecs.NewErrResult(fmt.Errorf("未找到id=%s的道岔", id))
}
return ecs.NewOkEmptyResult()
})
return result.Err
}
2023-10-08 09:59:59 +08:00
// 驱动道岔转辙机
// id - 道岔uid
// dc - 定操/反操
// on - 设置/取消
func driveTurnoutZzj(w ecs.World, id string, dc bool, on bool) error {
result := <-ecs.Request[ecs.EmptyType](w, func() ecs.Result[ecs.EmptyType] {
wd := entity.GetWorldData(w)
entry, ok := wd.EntityMap[id]
if ok {
if entry.HasComponent(component.Zdj9TwoDriveType) { // 有电路,驱动继电器
drive := component.Zdj9TwoDriveType.Get(entry)
if dc {
drive.YCJ = on
drive.DCJ = on
if on {
drive.FCJ = !on
}
} else {
drive.YCJ = on
drive.FCJ = on
if on {
drive.DCJ = !on
}
}
} else { // 无电路,直接驱动转辙机
tz := component.TurnoutZzjType.Get(entry)
for _, zzj := range tz.ZzjList {
state := component.ZzjStateType.Get(zzj)
state.Td = on
if dc {
state.Dw = true
} else {
state.Dw = false
}
}
}
} else {
return ecs.NewErrResult(fmt.Errorf("未找到id=%s的道岔", id))
}
return ecs.NewOkEmptyResult()
})
return result.Err
}
2023-10-08 09:59:59 +08:00
// 设置道岔转辙机定操
func DriveTurnoutDCOn(w ecs.World, id string) error {
return driveTurnoutZzj(w, id, true /* dc */, true /* on */)
}
2023-10-08 09:59:59 +08:00
// 取消道岔转辙机定操
func DriveTurnoutDCOff(w ecs.World, id string) error {
return driveTurnoutZzj(w, id, true /* dc */, false /* on */)
}
2023-10-08 09:59:59 +08:00
// 设置道岔转辙机反操
func DriveTurnoutFCOn(w ecs.World, id string) error {
return driveTurnoutZzj(w, id, false /* dc */, true /* on */)
}
2023-10-08 09:59:59 +08:00
// 取消道岔转辙机反操
func DriveTurnoutFCOff(w ecs.World, id string) error {
return driveTurnoutZzj(w, id, false /* dc */, false /* on */)
}