百分比设备

This commit is contained in:
xzb 2023-09-22 09:31:15 +08:00
parent 5c4ea818da
commit 1c5ee80394

View File

@ -12,15 +12,13 @@ type PercentageDeviceState struct {
V int64
//位置,[PD_L,PD_M]
P int64
//百分比,[0,100]
R int8
}
func NewPercentageDeviceStateL() *PercentageDeviceState {
return &PercentageDeviceState{V: 0, P: PD_L, R: 0}
return &PercentageDeviceState{V: 0, P: PD_L}
}
func NewPercentageDeviceStateM() *PercentageDeviceState {
return &PercentageDeviceState{V: 0, P: PD_M, R: 100}
return &PercentageDeviceState{V: 0, P: PD_M}
}
func (me *PercentageDeviceState) isToL() bool {
return me.P <= PD_L
@ -29,6 +27,17 @@ func (me *PercentageDeviceState) isToM() bool {
return me.P >= PD_M
}
// Rate 获取百分比即(P/(PD_M-PD_L))*100
func (me *PercentageDeviceState) Rate() int8 {
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))
}
// 百分比设备位置范围
const (
PD_L int64 = 0
@ -76,6 +85,4 @@ func (me *PercentageMovableSystem) move(w ecs.World, state *PercentageDeviceStat
if state.P > PD_M {
state.P = PD_M
}
//
state.R = int8((float64(state.P) / float64(PD_M)) * float64(100))
}