You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
54 lines
1.1 KiB
54 lines
1.1 KiB
package effects
|
|
|
|
import (
|
|
"fmt"
|
|
"git.aiterp.net/lucifer3/server/device"
|
|
"math/rand"
|
|
"time"
|
|
)
|
|
|
|
type Random struct {
|
|
States []device.State `json:"states,omitempty"`
|
|
Interpolate bool `json:"interpolate,omitempty"`
|
|
AnimationMS int64 `json:"animationMs,omitempty"`
|
|
}
|
|
|
|
func (e Random) State(_, _, _, _ int) device.State {
|
|
if len(e.States) == 0 {
|
|
return device.State{}
|
|
}
|
|
|
|
indexFactor := rand.Float64()
|
|
stateIncrement := 1.0 / float64(len(e.States)-1)
|
|
|
|
for i := range e.States {
|
|
a := float64(i) * stateIncrement
|
|
if indexFactor >= a {
|
|
si := e.States[i]
|
|
sj := e.States[(i+1)%len(e.States)]
|
|
f := (indexFactor - a) / stateIncrement
|
|
|
|
if f < 0 || f > 1 {
|
|
panic(f)
|
|
}
|
|
|
|
if e.Interpolate {
|
|
return si.Interpolate(sj, f)
|
|
} else if f < 0.5 {
|
|
return si
|
|
} else {
|
|
return sj
|
|
}
|
|
}
|
|
}
|
|
|
|
return e.States[len(e.States)-1]
|
|
}
|
|
|
|
func (e Random) Frequency() time.Duration {
|
|
return time.Duration(e.AnimationMS) * time.Millisecond
|
|
}
|
|
|
|
func (e Random) EffectDescription() string {
|
|
return fmt.Sprintf("Random(states:%s, anim:%dms, int:%t)", statesDescription(e.States), e.AnimationMS, e.Interpolate)
|
|
}
|