rts-sim-module/sys/circuit_sys/ckm.go

74 lines
2.1 KiB
Go

package circuit_sys
import (
"joylink.club/ecs"
"joylink.club/ecs/filter"
"joylink.club/rtsssimulation/component"
"joylink.club/rtsssimulation/consts"
"joylink.club/rtsssimulation/entity"
"log/slog"
)
type CkmSys struct {
query *ecs.Query
}
func NewCkmSys() *CkmSys {
return &CkmSys{
query: ecs.NewQuery(filter.Contains(entity.PsdBaseComponentTypeArr...)),
}
}
func (p *CkmSys) Update(world ecs.World) {
worldData := entity.GetWorldData(world)
p.query.Each(world, func(entry *ecs.Entry) {
posCom := component.FixedPositionTransformType.Get(entry)
state := component.CkmStateType.Get(entry)
if entry.HasComponent(component.CkmCircuitType) {
circuit := component.CkmCircuitType.Get(entry)
//门开/关继电器及状态
if posCom.Pos == consts.TwoPosMin {
component.RelayDriveType.Get(circuit.MGJ).Td = true
component.RelayDriveType.Get(circuit.MKJ).Td = false
state.Close = true
} else {
component.RelayDriveType.Get(circuit.MGJ).Td = false
component.RelayDriveType.Get(circuit.MKJ).Td = true
state.Close = false
}
//门故障继电器及状态
component.RelayDriveType.Get(circuit.MGZJ).Td = entry.HasComponent(component.CkmMgzTag)
state.Mgz = entry.HasComponent(component.CkmMgzTag)
//开/关门继电器驱动状态
kmBit, err := worldData.QueryQdBit(component.UidType.Get(circuit.KMJ).Id)
if err == nil {
component.RelayDriveType.Get(circuit.KMJ).Td = kmBit
} else {
slog.Error(err.Error())
}
gmBit, err := worldData.QueryQdBit(component.UidType.Get(circuit.GMJ).Id)
if err == nil {
component.RelayDriveType.Get(circuit.GMJ).Td = gmBit
} else {
slog.Error(err.Error())
}
state.Km = component.BitStateType.Get(circuit.KMJ).Val
state.Gm = component.BitStateType.Get(circuit.GMJ).Val
} else {
if posCom.Pos == consts.TwoPosMin {
state.Close = true
} else {
state.Close = false
}
state.Mgz = entry.HasComponent(component.CkmMgzTag)
}
//驱动
if state.Gm {
posCom.Speed = -component.CalculateTwoPositionAvgSpeed(3*1000, world.Tick())
} else if state.Km {
posCom.Speed = component.CalculateTwoPositionAvgSpeed(3*1000, world.Tick())
}
})
}