rts-sim-module/system/percentage_system.go
2023-08-25 13:26:23 +08:00

60 lines
1.5 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) {
percentage := components.PercentageDeviceStateComponent.Get(e)
movable := components.MovableDeviceStateComponent.Get(e)
if percentage.Rate != percentage.Target || movable.Speed > 0 {
movable := components.MovableDeviceStateComponent.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))
}