-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscene.go
70 lines (55 loc) · 1.13 KB
/
scene.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
package stars
import (
"context"
"github.com/lucasepe/doodlekit"
)
func Scene(total int) doodlekit.Scene {
return &scene{
total: total,
}
}
type scene struct {
w, h int
stars []star
colors []int
total int
speed float64
}
func (s *scene) Init(ctx context.Context) {
gc := doodlekit.Canvas(ctx)
rng := doodlekit.Rng(ctx)
if s.total <= 0 {
s.total = 100
}
s.w, s.h = gc.Width(), gc.Height()
s.speed = 1.5
s.colors = []int{7, 13, 2}
s.stars = make([]star, s.total)
for i := 0; i < s.total; i++ {
s.stars[i].x = rng.RndI(0, s.w)
s.stars[i].y = rng.RndI(0, s.h)
s.stars[i].vx = rng.Rnd(0.4, 2)
}
}
func (s *scene) Update(ctx context.Context, dt float64) {
rng := doodlekit.Rng(ctx)
for i := 0; i < s.total; i++ {
s.stars[i].x = s.stars[i].x - int(s.speed*s.stars[i].vx)
if s.stars[i].x < 0 {
s.stars[i].x = s.w
s.stars[i].y = rng.RndI(0, s.h)
s.stars[i].vx = rng.Rnd(0.4, 2)
}
}
}
func (s *scene) Draw(ctx context.Context) {
gc := doodlekit.Canvas(ctx)
for _, el := range s.stars {
gc.Color(s.colors[int(el.vx)])
gc.Pix(int(el.x), int(el.y))
}
}
type star struct {
x, y int
vx float64
}