rts-sim-module/fi/signal.go

79 lines
2.6 KiB
Go
Raw Normal View History

2023-10-19 13:43:31 +08:00
package fi
import (
"fmt"
"joylink.club/ecs"
"joylink.club/rtsssimulation/component"
"joylink.club/rtsssimulation/component/component_proto"
"joylink.club/rtsssimulation/entity"
)
// UpdateSignalLightFault 设置信号机灯故障
//
// light : 灯泡
// faultType :故障类型
// set : true-设置故障false-取消故障
func UpdateSignalLightFault(w ecs.World, signalId string, light component_proto.Light_Color, faultType component_proto.Signal_Fault, set bool) error {
r := <-ecs.Request[ecs.EmptyType](w, func() ecs.Result[ecs.EmptyType] {
wd := entity.GetWorldData(w)
signalEntry, ok := wd.EntityMap[signalId]
if ok {
switch faultType {
case component_proto.Signal_DS: //断丝故障
{
lightEntry := component.SignalLightsType.Get(signalEntry).GetLightByColor(light)
if lightEntry == nil {
return ecs.NewErrResult(fmt.Errorf("信号机[%s]没有色灯[%d]", signalId, light))
}
if set {
if !lightEntry.HasComponent(component.LightFaultDsType) {
lightEntry.AddComponent(component.LightFaultDsType)
}
} else {
if lightEntry.HasComponent(component.LightFaultDsType) {
lightEntry.RemoveComponent(component.LightFaultDsType)
}
}
}
default:
return ecs.NewErrResult(fmt.Errorf("信号机[%s]不支持该故障类型[%s]", signalId, faultType))
}
}
return ecs.NewOkEmptyResult()
})
return r.Err
}
2023-10-20 13:51:13 +08:00
// SignalLightDrive 当没有电路时,直接驱动信号机灯位上的灯
// ldLights : 需要亮的灯;当为空时则信号机所有色灯灭灯
func SignalLightDrive(w ecs.World, signalId string, ldLights ...component_proto.Light_Color) error {
r := <-ecs.Request[ecs.EmptyType](w, func() ecs.Result[ecs.EmptyType] {
wd := entity.GetWorldData(w)
signalEntry, ok := wd.EntityMap[signalId]
if !ok {
return ecs.NewErrResult(fmt.Errorf("信号机[%s]不存实体", signalId))
}
//
lights := component.SignalLightsType.Get(signalEntry)
//ldLights色灯亮
if len(ldLights) > 0 { //色灯亮
if !lights.HaveLightsByColor(ldLights...) {
return ecs.NewErrResult(fmt.Errorf("要点亮的这几个色灯不在信号机[%s]的灯位中", signalId))
}
for _, mdLightEntry := range lights.Lights {
component.LightDriveType.Get(mdLightEntry).Td = false
}
for _, ldLight := range ldLights {
ldLightEntry := lights.GetLightByColor(ldLight)
component.LightDriveType.Get(ldLightEntry).Td = true
}
} else { //信号机所有色灯灭灯
for _, mdLightEntry := range lights.Lights {
component.LightDriveType.Get(mdLightEntry).Td = false
}
}
return ecs.NewOkEmptyResult()
})
return r.Err
}