rts-sim-module/sys/iscs_sys/iscs_bas_valve.go
2023-12-29 17:00:26 +08:00

71 lines
2.2 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 iscs_sys
import (
"joylink.club/ecs"
"joylink.club/ecs/filter"
"joylink.club/rtsssimulation/component"
"joylink.club/rtsssimulation/consts"
"joylink.club/rtsssimulation/entity"
"math"
)
// ValveSystem 阀门
type ValveSystem struct {
query *ecs.Query
}
func NewValveSystem() *ValveSystem {
return &ValveSystem{
query: ecs.NewQuery(filter.Contains(component.UidType, component.ValveType, component.ValveControllerType, component.TwoPositionTransformType)),
}
}
func (s *ValveSystem) Update(w ecs.World) {
wd := entity.GetWorldData(w)
s.query.Each(w, func(entry *ecs.Entry) {
valveId := component.UidType.Get(entry).Id
valve := component.ValveType.Get(entry)
valveController := component.ValveControllerType.Get(entry)
position := component.TwoPositionTransformType.Get(entry)
//
valve.OpenRate = uint8((float64(position.Pos-consts.TwoPosMin) / float64(consts.TwoPosMax-consts.TwoPosMin)) * float64(100))
valve.Closed = valve.OpenRate <= 0
valve.Opened = valve.OpenRate >= 100
valve.Moving = valve.OpenRate != valveController.TargetOpenRate
//fmt.Printf("==>>阀门[%s],OpenRate = %d%% , 全开 = %t , 全关 = %t , Moving = %t ,Pos = %d\n", valveId, valve.OpenRate, valve.Opened, valve.Closed, valve.Moving, position.Pos)
//
valveModel, ok := wd.Repo.FindById(valveId).(valveModeler)
if !ok {
valveModel = vm
}
//计算速度
speed := int32((float64(consts.TwoPosMax-consts.TwoPosMin) / float64(valveModel.MaxMoveTime())) * float64(w.Tick()))
targetPos := int32((float64(valveController.TargetOpenRate) / float64(100)) * float64(consts.TwoPosMax-consts.TwoPosMin))
targetLen := int32(math.Abs(float64(targetPos - position.Pos)))
//修正尾速
if speed > targetLen {
speed = targetLen
}
if valveController.TargetOpenRate < valve.OpenRate {
speed = -speed
} else if valveController.TargetOpenRate == valve.OpenRate {
speed = 0
}
position.Speed = speed
})
}
var vm = &valveModelDefault{maxMoveTime: 2500}
type valveModeler interface {
//MaxMoveTime 阀门从全关到全开或从全开到全关耗时单位ms
MaxMoveTime() uint16
}
type valveModelDefault struct {
maxMoveTime uint16
}
func (v *valveModelDefault) MaxMoveTime() uint16 {
return v.maxMoveTime
}