Initial commit.
This commit is contained in:
54
elements/duck.go
Normal file
54
elements/duck.go
Normal file
@@ -0,0 +1,54 @@
|
||||
package elements
|
||||
|
||||
import (
|
||||
"ducky/gamedata"
|
||||
"image"
|
||||
|
||||
"github.com/hajimehoshi/ebiten/v2"
|
||||
"golang.org/x/exp/rand"
|
||||
)
|
||||
|
||||
const (
|
||||
DuckyWidth = 36
|
||||
DuckyHeight = DuckyWidth
|
||||
)
|
||||
|
||||
type Ducky struct {
|
||||
Sprite *ebiten.Image
|
||||
asset *ebiten.Image
|
||||
cycle int
|
||||
position gamedata.Coordinates
|
||||
}
|
||||
|
||||
func NewDucky(asset *ebiten.Image) *Ducky {
|
||||
d := &Ducky{
|
||||
Sprite: ebiten.NewImage(DuckyWidth, DuckyHeight),
|
||||
asset: asset,
|
||||
cycle: rand.Intn(11),
|
||||
}
|
||||
return d
|
||||
}
|
||||
|
||||
func (d *Ducky) Update() {
|
||||
d.cycle++
|
||||
}
|
||||
|
||||
func (d *Ducky) Draw() {
|
||||
d.Sprite.Clear()
|
||||
|
||||
idx := d.cycle / 4 % 10
|
||||
x0 := idx * DuckyWidth
|
||||
y0 := 0
|
||||
x1 := x0 + DuckyWidth
|
||||
y1 := DuckyHeight
|
||||
|
||||
d.Sprite.DrawImage(d.asset.SubImage(image.Rect(x0, y0, x1, y1)).(*ebiten.Image), nil)
|
||||
}
|
||||
|
||||
func (d *Ducky) SetPosition(pos gamedata.Coordinates) {
|
||||
d.position = pos
|
||||
}
|
||||
|
||||
func (d *Ducky) GetPosition() gamedata.Coordinates {
|
||||
return d.position
|
||||
}
|
||||
87
elements/slider.go
Normal file
87
elements/slider.go
Normal 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
|
||||
}
|
||||
33
elements/spotlight.go
Normal file
33
elements/spotlight.go
Normal file
@@ -0,0 +1,33 @@
|
||||
package elements
|
||||
|
||||
import "github.com/hajimehoshi/ebiten/v2"
|
||||
|
||||
type Spotlight struct {
|
||||
Sprite *ebiten.Image
|
||||
asset *ebiten.Image
|
||||
cycle int
|
||||
}
|
||||
|
||||
func NewSpotlight(asset *ebiten.Image) *Spotlight {
|
||||
sl := &Spotlight{
|
||||
Sprite: ebiten.NewImage(100, 100),
|
||||
asset: asset,
|
||||
}
|
||||
|
||||
return sl
|
||||
}
|
||||
|
||||
func (s *Spotlight) Update() {
|
||||
s.cycle++
|
||||
}
|
||||
|
||||
func (s *Spotlight) Draw() {
|
||||
s.Sprite.Clear()
|
||||
|
||||
op := &ebiten.DrawImageOptions{}
|
||||
op.GeoM.Translate(-16, -16)
|
||||
op.GeoM.Scale(3, 3)
|
||||
op.GeoM.Translate(50, 50)
|
||||
s.Sprite.DrawImage(s.asset, op)
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user