Initial commit.

This commit is contained in:
2024-12-08 12:24:33 -05:00
commit d9d09e5169
17 changed files with 671 additions and 0 deletions

87
elements/slider.go Normal file
View File

@@ -0,0 +1,87 @@
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
}