rts-sim-module/fi/signal.go
2023-10-19 13:43:31 +08:00

46 lines
1.4 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 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
}