rts-sim-module/sys/device_sys/relay.go
walker 0bba8f0934 删除donburi包引用
修改filter导入为从ecs项目导入
整理包结构,将弃用的包放入deprecated文件中
2023-10-09 11:17:25 +08:00

59 lines
1.8 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.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
}
}
})
}