71 lines
2.2 KiB
Go
71 lines
2.2 KiB
Go
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
|
||
}
|