package iscs_sys import ( "fmt" "joylink.club/ecs" "joylink.club/ecs/filter" "joylink.club/rtsssimulation/component" "joylink.club/rtsssimulation/consts" "joylink.club/rtsssimulation/entity" ) // 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", valveId, valve.OpenRate, valve.Opened, valve.Closed) // valveModel, ok := wd.Repo.FindById(valveId).(valveModeler) if !ok { valveModel = vm } speed := int32((float64(consts.TwoPosMax-consts.TwoPosMin) / float64(valveModel.MaxMoveTime())) * float64(w.Tick())) if valveController.TargetOpenRate < valve.OpenRate { speed = -speed } else if valveController.TargetOpenRate == valve.OpenRate { speed = 0 } }) } 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 }