-
Notifications
You must be signed in to change notification settings - Fork 1
/
dropper.go
94 lines (79 loc) · 1.21 KB
/
dropper.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package main
import (
"math/rand"
"time"
)
type Dropper struct {
World World
next *Fruit
counter int
}
func NewDropper(world World) *Dropper {
d := &Dropper{World: world}
d.Drop()
d.next.X = world.Width / 2
return d
}
func (d *Dropper) MoveLeft() {
if d.next == nil {
return
}
d.next.X -= 2
d.wrap()
}
func (d *Dropper) MoveRight() {
if d.next == nil {
return
}
d.next.X += 2
d.wrap()
}
func (d *Dropper) wrap() {
if d.next == nil {
return
}
if d.next.X-d.next.Radius < 0 {
d.next.X = d.next.Radius
}
if d.World.Width-d.next.Radius < d.next.X {
d.next.X = d.World.Width - d.next.Radius
}
}
func (d *Dropper) Next() *Fruit {
if d.counter < 0 {
return nil
}
return d.next
}
func (d *Dropper) Tick() {
if d.counter >= 0 {
return
}
d.counter++
}
func (d *Dropper) Drop() *Fruit {
if d.counter < 0 {
return nil
}
var x float64
var y float64
if d.next != nil {
x = d.next.X
y = d.next.Y
}
r := rand.New(rand.NewSource(time.Now().UnixNano()))
f := r.Float64()
var next *Fruit
if f < 0.5 {
next = NewApple(x, y)
} else if f < 0.75 {
next = NewOrange(x, y)
} else {
next = NewGrape(x, y)
}
ret := d.next
d.next = next
d.wrap()
d.counter = -15
return ret
}