-
Notifications
You must be signed in to change notification settings - Fork 0
/
magnet.go
79 lines (63 loc) · 1.57 KB
/
magnet.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package main
import (
"math"
"github.com/hajimehoshi/ebiten/v2"
)
type Magnet struct {
Image *ebiten.Image
Options *ebiten.DrawImageOptions
flyDuration int
flyDistance int
}
func NewMagnet() *Magnet {
m := &Magnet{
Image: LoadImage("assets/magnet.png"),
flyDistance: 0,
flyDuration: 0,
}
m.toStartPosition()
return m
}
func (m *Magnet) Draw(screen *ebiten.Image) {
diff := float64(m.flyDistance / 5)
if diff >= 4 {
diff = 4
}
ty := m.calculateY() + diff/5
tx := 2 + diff
if m.flyDuration >= m.flyDistance*130 {
m.Options.GeoM.Translate(-float64(tx), +ty*float64((m.flyDuration/10)))
} else if m.flyDuration >= m.flyDistance*100 && m.flyDuration <= m.flyDistance*130 {
m.Options.GeoM.Translate(-float64(tx), 0)
} else {
m.Options.GeoM.Translate(-float64(tx), -ty+float64(m.flyDuration/4))
}
screen.DrawImage(m.Image, m.Options)
}
func (m *Magnet) Update(p *Player) {
m.flyDistance = p.ThrowDistance()
if p.IsThrowReleased() {
m.flyDuration++
}
}
func (m *Magnet) Found() bool {
return m.Options.GeoM.Element(1, 2) >= OceanFloor
}
func (m *Magnet) calculateY() float64 {
x := m.Options.GeoM.Element(0, 2)
diameter := m.flyDistance
radius := float64(diameter / 2)
h := x - radius
return radius + math.Sqrt(math.Pow(radius, 2)-math.Pow(x-h, 2))
}
func (m *Magnet) toStartPosition() {
op := &ebiten.DrawImageOptions{}
op.GeoM.Translate(float64(windowWidth)/2, float64(windowHeight)/2)
op.GeoM.Translate(float64(windowWidth)/5, 0)
m.Options = op
}
func (m *Magnet) reset() {
m.flyDuration = 0
m.flyDistance = 0
m.toStartPosition()
}