rts-sim-module/sys/device_sys/relay.go

63 lines
1.9 KiB
Go

package device_sys
import (
"unsafe"
"joylink.club/ecs"
"joylink.club/ecs/filter"
"joylink.club/rtsssimulation/component"
"joylink.club/rtsssimulation/consts"
)
type RelaySys struct {
query *ecs.Query
}
func NewRelaySys() *RelaySys {
return &RelaySys{
query: ecs.NewQuery(filter.Contains(component.RelayTag, component.RelayDriveType, component.BitStateType)),
}
}
func (rs *RelaySys) Update(w ecs.World) {
rs.query.Each(w, func(entry *ecs.Entry) {
// 查询实体是哪种继电器类型,根据继电器类型处理继电器吸起落下逻辑(暂时先简化直接处理)
rd := component.RelayDriveType.Get(entry)
state := component.BitStateType.Get(entry)
if entry.HasComponent(component.RelayFaultForceType) {
state.Val = component.RelayFaultForceType.Get(entry).Q
return
}
if entry.HasComponent(component.WjRelayTag) { // 无极继电器
if rd.Td && !state.Val { // 通电吸起
state.Val = true
} else if state.Val && !rd.Td { // 断电落下
if entry.HasComponent(component.HfRelayTag) { // 缓放继电器
if entry.HasComponent(component.CounterDownType) { //
cd := component.CounterDownType.Get(entry)
if cd.Val <= 0 { //
state.Val = false
entry.RemoveComponent(component.CounterDownType) // 移除倒计时组件
}
} else {
entry.AddComponent(component.CounterDownType, unsafe.Pointer(&component.CounterDown{
Val: consts.RelayHfTime,
Step: w.Tick(),
}))
}
} else { // 落下
state.Val = false
}
} else if rd.Td && entry.HasComponent(component.CounterDownType) {
entry.RemoveComponent(component.CounterDownType) // 移除倒计时组件
}
} else if entry.HasComponent(component.YjRelayTag) { // 有极继电器
if rd.Td && rd.Xq && !state.Val { // 吸起
state.Val = true
} else if rd.Td && !rd.Xq && state.Val { // 落下
state.Val = false
}
}
})
}