rts-sim-module/system/percentage_system.go

98 lines
2.6 KiB
Go
Raw Normal View History

2023-09-21 15:22:22 +08:00
package system
import (
2023-09-22 10:32:57 +08:00
"fmt"
2023-09-21 15:22:22 +08:00
"github.com/yohamta/donburi/filter"
"joylink.club/ecs"
)
// PercentageDeviceState 百分比设备
// 起始位置p=0,终点位置p=100000,总路程100000
type PercentageDeviceState struct {
//速度,矢量
V int64
//位置,[PD_L,PD_M]
P int64
}
func NewPercentageDeviceStateL() *PercentageDeviceState {
2023-09-22 09:31:15 +08:00
return &PercentageDeviceState{V: 0, P: PD_L}
2023-09-21 15:22:22 +08:00
}
func NewPercentageDeviceStateM() *PercentageDeviceState {
2023-09-22 09:31:15 +08:00
return &PercentageDeviceState{V: 0, P: PD_M}
2023-09-21 15:22:22 +08:00
}
2023-09-21 17:04:28 +08:00
func (me *PercentageDeviceState) isToL() bool {
return me.P <= PD_L
}
func (me *PercentageDeviceState) isToM() bool {
return me.P >= PD_M
}
2023-09-21 15:22:22 +08:00
2023-09-22 10:32:57 +08:00
// GetRate 获取百分比即(P/(PD_M-PD_L))*100
func (me *PercentageDeviceState) GetRate() int8 {
2023-09-22 09:31:15 +08:00
if me.P < PD_L {
me.P = PD_L
}
if me.P > PD_M {
me.P = PD_M
}
return int8((float64(me.P) / float64(PD_M)) * float64(100))
}
2023-09-22 10:32:57 +08:00
// SetRate 设置百分比即P=(PD_M-PD_L)*(rate/100)
func (me *PercentageDeviceState) SetRate(rate int8) {
if rate < 0 || rate > 100 {
panic(fmt.Sprintf("rate须在[0,100]中rate=%d", rate))
}
me.P = int64(float64(PD_M-PD_L) * (float64(rate) / float64(100)))
}
2023-09-21 15:22:22 +08:00
// 百分比设备位置范围
const (
PD_L int64 = 0
PD_M int64 = 100000
)
// PercentageDeviceStateComponent 一维移动的物体组件
var PercentageDeviceStateComponent = ecs.NewComponentType[PercentageDeviceState]()
var PercentageDeviceState1Component = ecs.NewComponentType[PercentageDeviceState]()
var PercentageDeviceState2Component = ecs.NewComponentType[PercentageDeviceState]()
type PercentageMovableSystem struct {
query *ecs.Query
}
func NewPercentageMovableSystem() *PercentageMovableSystem {
return &PercentageMovableSystem{
query: ecs.NewQuery(filter.Or(filter.Contains(PercentageDeviceStateComponent), filter.Contains(PercentageDeviceState1Component), filter.Contains(PercentageDeviceState2Component))),
}
}
// Update world 执行
func (me *PercentageMovableSystem) Update(w ecs.World) {
me.query.Each(w, func(e *ecs.Entry) {
2023-09-21 17:04:28 +08:00
var state *PercentageDeviceState
2023-09-21 15:22:22 +08:00
if e.HasComponent(PercentageDeviceStateComponent) {
2023-09-21 17:04:28 +08:00
state = PercentageDeviceStateComponent.Get(e)
2023-09-21 15:22:22 +08:00
}
if e.HasComponent(PercentageDeviceState1Component) {
2023-09-21 17:04:28 +08:00
state = PercentageDeviceState1Component.Get(e)
2023-09-21 15:22:22 +08:00
}
if e.HasComponent(PercentageDeviceState2Component) {
2023-09-21 17:04:28 +08:00
state = PercentageDeviceState2Component.Get(e)
}
if state != nil {
me.move(w, state)
2023-09-21 15:22:22 +08:00
}
})
}
2023-09-21 17:04:28 +08:00
func (me *PercentageMovableSystem) move(w ecs.World, state *PercentageDeviceState) {
state.P += state.V * int64(w.Tick())
if state.P < PD_L {
state.P = PD_L
2023-09-21 15:22:22 +08:00
}
2023-09-21 17:04:28 +08:00
if state.P > PD_M {
state.P = PD_M
2023-09-21 15:22:22 +08:00
}
}