rts-sim-module/sys/iscs_sys/iscs_bas_valve.go

71 lines
2.2 KiB
Go
Raw Normal View History

2023-12-25 17:37:41 +08:00
package iscs_sys
2023-12-26 17:52:28 +08:00
import (
2023-12-29 17:48:57 +08:00
"math"
2023-12-26 17:52:28 +08:00
"joylink.club/ecs"
"joylink.club/ecs/filter"
"joylink.club/rtsssimulation/component"
2023-12-27 11:28:34 +08:00
"joylink.club/rtsssimulation/consts"
"joylink.club/rtsssimulation/entity"
2023-12-26 17:52:28 +08:00
)
// ValveSystem 阀门
type ValveSystem struct {
query *ecs.Query
}
func NewValveSystem() *ValveSystem {
return &ValveSystem{
2023-12-29 17:48:57 +08:00
query: ecs.NewQuery(filter.Contains(component.UidType, component.ValveType, component.ValveControllerType, component.FixedPositionTransformType)),
2023-12-26 17:52:28 +08:00
}
}
func (s *ValveSystem) Update(w ecs.World) {
2023-12-27 11:28:34 +08:00
wd := entity.GetWorldData(w)
2023-12-26 17:52:28 +08:00
s.query.Each(w, func(entry *ecs.Entry) {
2023-12-27 11:28:34 +08:00
valveId := component.UidType.Get(entry).Id
2023-12-26 17:52:28 +08:00
valve := component.ValveType.Get(entry)
2023-12-27 11:28:34 +08:00
valveController := component.ValveControllerType.Get(entry)
2023-12-29 17:48:57 +08:00
position := component.FixedPositionTransformType.Get(entry)
2023-12-27 11:28:34 +08:00
//
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
2023-12-27 13:35:33 +08:00
//fmt.Printf("==>>阀门[%s],OpenRate = %d%% , 全开 = %t , 全关 = %t , Moving = %t ,Pos = %d\n", valveId, valve.OpenRate, valve.Opened, valve.Closed, valve.Moving, position.Pos)
2023-12-27 11:28:34 +08:00
//
valveModel, ok := wd.Repo.FindById(valveId).(valveModeler)
if !ok {
valveModel = vm
}
2023-12-27 13:35:33 +08:00
//计算速度
2023-12-27 11:28:34 +08:00
speed := int32((float64(consts.TwoPosMax-consts.TwoPosMin) / float64(valveModel.MaxMoveTime())) * float64(w.Tick()))
2023-12-27 13:35:33 +08:00
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
}
2023-12-27 11:28:34 +08:00
if valveController.TargetOpenRate < valve.OpenRate {
speed = -speed
} else if valveController.TargetOpenRate == valve.OpenRate {
speed = 0
}
2023-12-27 13:35:33 +08:00
position.Speed = speed
2023-12-26 17:52:28 +08:00
})
}
2023-12-27 11:28:34 +08:00
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
}