rts-sim-module/fi/relay.go
2023-11-06 13:41:57 +08:00

69 lines
1.7 KiB
Go

package fi
import (
"fmt"
"joylink.club/ecs"
"joylink.club/rtsssimulation/component"
"joylink.club/rtsssimulation/entity"
)
// 继电器功能接口
// 控制有极继电器励磁
// id - 继电器uid
// td - 是否通电励磁
// xq - 是否到吸起位置(有极继电器)
func driveYjRelay(w ecs.World, entry *ecs.Entry, td bool, xq bool) {
rd := component.RelayDriveType.Get(entry)
rd.Td = td
rd.Xq = xq
}
// 控制无极继电器励磁
// id - 继电器uid
// td - 是否通电励磁
// xq - 是否到吸起位置(有极继电器)
func driveWjRelay(w ecs.World, entry *ecs.Entry, td bool) {
rd := component.RelayDriveType.Get(entry)
rd.Td = td
}
// 驱动继电器到吸起位置
func DriveRelayUp(w ecs.World, id string) 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.YjRelayTag) {
driveYjRelay(w, entry, true, true)
} else {
driveWjRelay(w, entry, true)
}
} else {
return ecs.NewErrResult(fmt.Errorf("未找到id=%s的继电器", id))
}
return ecs.NewOkEmptyResult()
})
return result.Err
}
// 驱动继电器到落下位置
func DriveRelayDown(w ecs.World, id string) 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.YjRelayTag) {
driveYjRelay(w, entry, true, false)
} else {
driveWjRelay(w, entry, false)
}
} else {
return ecs.NewErrResult(fmt.Errorf("未找到id=%s的继电器", id))
}
return ecs.NewOkEmptyResult()
})
return result.Err
}