-
Notifications
You must be signed in to change notification settings - Fork 0
/
weatherEffect.go
78 lines (66 loc) · 1.67 KB
/
weatherEffect.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
package main
import (
"math/rand"
"time"
)
type WeatherEffect struct {
Point *Point
Sprite, EndSprite Spriteer
speed float64
MaxW, MaxH int
time.Duration
done bool
}
func (receiver *WeatherEffect) GetXY() Point {
return Point{receiver.Point.X, receiver.Point.Y}
}
func (receiver *WeatherEffect) GetSprite() Spriteer {
if receiver.done {
return receiver.EndSprite
}
return receiver.Sprite
}
func (receiver *WeatherEffect) GetZIndex() int {
return 1100
}
func (receiver *WeatherEffect) Update(timeLeft time.Duration) error {
if receiver.done {
if receiver.Duration <= 0 {
receiver.Point.Y = float64(rand.Intn(receiver.MaxH))
receiver.Point.X = float64(rand.Intn(receiver.MaxW))
receiver.Duration = time.Duration(rand.Intn(3) * int(time.Second))
receiver.done = false
}
receiver.Duration -= timeLeft
return nil
} else if receiver.Duration <= 0 {
receiver.done = true
receiver.Duration = time.Second / 3
return nil
}
tf := float64(timeLeft) / float64(time.Second)
if receiver.Point.X <= 0 {
receiver.Point.X = float64(receiver.MaxW)
} else {
receiver.Point.X = receiver.Point.X - float64(receiver.speed)*tf
}
if receiver.Point.Y >= float64(receiver.MaxH) {
receiver.Point.Y = 0
} else {
receiver.Point.Y = receiver.Point.Y + float64(receiver.speed)*tf
}
receiver.Duration -= timeLeft
return nil
}
func NewWeatherEffect(Point *Point, MaxW, MaxH int) (*WeatherEffect, error) {
return &WeatherEffect{
Point: Point,
Sprite: ErrorSprite,
EndSprite: nil,
speed: 15,
MaxW: MaxW,
MaxH: MaxH,
Duration: time.Duration(rand.Intn(3) * int(time.Second)),
done: false,
}, nil
}