rts-sim-module/system/percentage_system.go
2023-08-25 17:36:12 +08:00

64 lines
1.6 KiB
Go

package system
import (
"github.com/yohamta/donburi/filter"
"joylink.club/ecs"
"joylink.club/rtsssimulation/components"
)
type PercentageDeviceSystem struct {
query *ecs.Query
}
func NewPercentageDeviceSystem() *PercentageDeviceSystem {
return &PercentageDeviceSystem{query: ecs.NewQuery(filter.Contains(components.PercentageDeviceStateComponent, components.MovableDeviceStateComponent))}
}
const (
PercentageRateValueMax int64 = 100000
PercentageRateValueMin int64 = 0
)
const (
PercentageRateMax int8 = 100
PercentageRateMin int8 = 0
)
// world 执行
func (me *PercentageDeviceSystem) Update(world ecs.World) {
me.query.Each(world, func(e *ecs.Entry) {
movable := components.MovableDeviceStateComponent.Get(e)
if movable.Speed > 0 {
percentage := components.PercentageDeviceStateComponent.Get(e)
if movable.ToH {
if movable.Position >= percentage.Target {
movable.Speed = 0
movable.Position = percentage.Target
}
} else {
if movable.Position <= percentage.Target {
movable.Speed = 0
movable.Position = percentage.Target
}
}
//
percentage.Rate = movable.Position
}
})
}
// 映射百分比到大数值
func GetRateValue(rate int8) int64 {
return int64(float64(float32(rate)/float32(100)) * float64(PercentageRateValueMax))
}
// 大数值映射到百分比
func GetRate(rateValue int64) int8 {
return int8((float64(rateValue) / float64(PercentageRateValueMax)) * float64(100))
}
// 比率值变动速率计算
func CalculateRateSpeed(tick int32, lhDistance int64) int32 {
return int32(float64(tick) * (float64(PercentageRateValueMax) / float64(lhDistance)))
}