83 lines
2.8 KiB
Go
83 lines
2.8 KiB
Go
package system
|
||
|
||
import (
|
||
"fmt"
|
||
|
||
"github.com/yohamta/donburi/filter"
|
||
"joylink.club/ecs"
|
||
"joylink.club/rtsssimulation/components"
|
||
"joylink.club/rtsssimulation/state"
|
||
)
|
||
|
||
// 百分比设备系统
|
||
type PercentageSystem struct {
|
||
query *ecs.Query
|
||
}
|
||
|
||
func NewPercentageSystem() *PercentageSystem {
|
||
return &PercentageSystem{query: ecs.NewQuery(filter.Contains(components.PercentageDeviceComponent, components.PercentageDeviceOperatingComponent))}
|
||
}
|
||
|
||
// world 执行
|
||
func (me *PercentageSystem) Update(world ecs.World) {
|
||
me.query.Each(world, func(e *ecs.Entry) {
|
||
operation := components.PercentageDeviceOperatingComponent.Get(e)
|
||
if operation.RemainingDistance <= 0 {
|
||
operation.RemainingDistance = 0
|
||
e.RemoveComponent(components.PercentageDeviceOperatingComponent)
|
||
} else {
|
||
operation.RemainingDistance -= int64(world.Tick())
|
||
if operation.RemainingDistance < 0 {
|
||
operation.RemainingDistance = 0
|
||
}
|
||
}
|
||
//
|
||
rateDevice := components.PercentageDeviceComponent.Get(e)
|
||
if operation.ToH {
|
||
rateDevice.Rate = int32(float64(100) * (float64(operation.InitDistance+operation.SumDistance-operation.RemainingDistance) / float64(operation.LhDistance)))
|
||
} else {
|
||
rateDevice.Rate = 100 - int32(float64(100)*(float64(operation.InitDistance+operation.SumDistance-operation.RemainingDistance)/float64(operation.LhDistance)))
|
||
}
|
||
})
|
||
}
|
||
|
||
// 触发操作百分比设备
|
||
// deviceEntry 百分比组件所在的实体
|
||
// lhDistance 百分比设备从0-100的距离,单位ms
|
||
// toRate 本次操作最终目标百分比
|
||
func FirePercentageDeviceOperation(w ecs.World, deviceEntry *ecs.Entry, lhDistance int64, toRate int32) error {
|
||
if !deviceEntry.HasComponent(components.PercentageDeviceComponent) {
|
||
entityId := components.DeviceIdentityComponent.Get(deviceEntry).Id
|
||
return fmt.Errorf("实体[%s]中不存在百分比组件", entityId)
|
||
}
|
||
curRate := components.PercentageDeviceComponent.Get(deviceEntry).Rate
|
||
if curRate == toRate {
|
||
return nil
|
||
}
|
||
//
|
||
if !deviceEntry.HasComponent(components.PercentageDeviceOperatingComponent) {
|
||
deviceEntry.AddComponent(components.PercentageDeviceOperatingComponent)
|
||
}
|
||
operation := components.PercentageDeviceOperatingComponent.Get(deviceEntry)
|
||
//
|
||
toHigh := toRate > curRate
|
||
var initDistance, sumDistance int64
|
||
if toHigh {
|
||
initDistance = int64(float64(lhDistance) * (float64(curRate) / float64(100)))
|
||
sumDistance = int64(float64(lhDistance) * (float64(toRate-curRate) / float64(100)))
|
||
} else {
|
||
initDistance = int64(float64(lhDistance) * (float64(100-curRate) / float64(100)))
|
||
sumDistance = int64(float64(lhDistance) * (float64(curRate-toRate) / float64(100)))
|
||
}
|
||
//
|
||
*operation = state.PercentageDeviceOperating{
|
||
ToH: toHigh,
|
||
LhDistance: lhDistance,
|
||
InitDistance: initDistance,
|
||
SumDistance: sumDistance,
|
||
RemainingDistance: sumDistance,
|
||
}
|
||
//
|
||
return nil
|
||
}
|