88 lines
1.5 KiB
Go
88 lines
1.5 KiB
Go
package elements
|
|
|
|
import (
|
|
"ducky/gamedata"
|
|
"image/color"
|
|
"math"
|
|
|
|
"github.com/hajimehoshi/ebiten/v2"
|
|
"golang.org/x/exp/rand"
|
|
)
|
|
|
|
const (
|
|
sliderWidth = 640 / 5
|
|
sliderHeight = 480
|
|
easeValue = 16
|
|
)
|
|
|
|
type Slider struct {
|
|
Sprite *ebiten.Image
|
|
position gamedata.Coordinates
|
|
targetposition gamedata.Coordinates
|
|
cycle int
|
|
callback func()
|
|
called bool
|
|
color color.Color
|
|
}
|
|
|
|
func NewSlider() *Slider {
|
|
slider := &Slider{
|
|
Sprite: ebiten.NewImage(sliderWidth, sliderHeight),
|
|
called: false,
|
|
color: color.RGBA{
|
|
R: uint8(rand.Intn(256)) & 0xff,
|
|
G: uint8(rand.Intn(256)) & 0xff,
|
|
B: uint8(rand.Intn(256)) & 0xff,
|
|
A: 0xff,
|
|
},
|
|
}
|
|
return slider
|
|
}
|
|
|
|
func (s *Slider) Update() {
|
|
|
|
dx := s.targetposition.X - s.position.X
|
|
dy := s.targetposition.Y - s.position.Y
|
|
|
|
deltapos := math.Sqrt(dx*dx + dy*dy)
|
|
|
|
if deltapos > 0.5 {
|
|
s.position.X = s.position.X + dx/easeValue
|
|
s.position.Y = s.position.Y + dy/easeValue
|
|
} else {
|
|
if s.callback != nil && !s.called {
|
|
s.callback()
|
|
s.called = true
|
|
}
|
|
}
|
|
|
|
s.cycle++
|
|
}
|
|
|
|
func (s *Slider) Draw() {
|
|
s.Sprite.Clear()
|
|
s.Sprite.Fill(s.color)
|
|
}
|
|
|
|
func (s *Slider) GetPosition() gamedata.Coordinates {
|
|
return s.position
|
|
}
|
|
|
|
func (s *Slider) SetPosition(pos gamedata.Coordinates) {
|
|
s.position = pos
|
|
}
|
|
|
|
func (s *Slider) SetTargetPosition(pos gamedata.Coordinates) {
|
|
s.targetposition = pos
|
|
}
|
|
|
|
func (s *Slider) SetCallback(f func()) {
|
|
if f != nil {
|
|
s.callback = f
|
|
}
|
|
}
|
|
|
|
func (s *Slider) Reset() {
|
|
s.called = false
|
|
}
|