-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
179 lines (148 loc) · 3.29 KB
/
main.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// Command ncscr - Starry Night screensaver from Norton Commander from 1980s.
// Serves no real purpose other than nostalgic feelings
package main
import (
"log"
"math/rand"
"time"
"github.com/gdamore/tcell"
)
const (
starsPercent = 0.018 // % of screen taken by the stars
explodeSpeed = 150 * time.Millisecond // explosions speed
// max number of simultaneous explosions
explosions = 2
)
type skies struct {
stars []Star
width, height int
}
// maxStars returns the maximum number of stars for the given screen size
func maxStars(width, height int) int {
return int(float64(width*height) * float64(starsPercent))
}
func NewSkies(width, height, numStars int) *skies {
if numStars == 0 {
numStars = maxStars(width, height)
}
stars := make([]Star, numStars)
return &skies{
stars: stars,
width: width,
height: height,
}
}
func (sky *skies) Play(quit <-chan struct{}, s tcell.Screen) {
targets := make(chan int, explosions)
defer close(targets)
// demolition goroutines
for i := 0; i <= explosions; i++ {
go func() {
for {
select {
case <-quit:
return
case target := <-targets:
newX, newY := sky.getRandXY()
sky.stars[target].Explode(s)
sky.stars[target].ResetAt(s, newX, newY)
}
}
}()
}
timer := time.NewTicker(explodeSpeed)
defer timer.Stop()
for i := 0; i < len(sky.stars); i++ {
select {
case <-quit:
return
case <-timer.C:
x, y := sky.getRandXY()
sky.stars[i] = *NewStar(x, y)
sky.stars[i].Shine(s)
}
}
for {
select {
case <-quit:
return
case <-timer.C:
targets <- rand.Int() % len(sky.stars)
}
}
}
func (sky *skies) getRandXY() (int, int) {
return rand.Int() % sky.width, rand.Int() % sky.height
}
func main() {
tcell.SetEncodingFallback(tcell.EncodingFallbackASCII)
s, err := tcell.NewScreen()
if err != nil {
log.Fatal(err)
}
if err := s.Init(); err != nil {
log.Fatal(err)
}
s.SetStyle(tcell.StyleDefault.
Foreground(tcell.ColorWhite).
Background(tcell.ColorBlack))
s.Clear()
quit := make(chan struct{})
go func() {
for {
ev := s.PollEvent()
switch ev := ev.(type) {
case *tcell.EventKey:
switch ev.Key() {
case tcell.KeyEscape, tcell.KeyEnter, tcell.KeyCtrlC:
close(quit)
return
case tcell.KeyCtrlL, tcell.KeyF10:
s.Sync()
}
case *tcell.EventResize:
s.Sync()
}
}
}()
x, y := s.Size()
skies := NewSkies(x, y, maxStars(x, y))
go skies.Play(quit, s)
<-quit
// loop:
// for {
// select {
// case <-quit:
// break loop
// }
// }
s.Fini()
}
// func makebox(s tcell.Screen) {
// w, h := s.Size()
// if w == 0 || h == 0 {
// return
// }
// glyphs := []rune{'@', '#', '&', '*', '=', '%', 'Z', 'A'}
// lx := rand.Int() % w
// ly := rand.Int() % h
// lw := rand.Int() % (w - lx)
// lh := rand.Int() % (h - ly)
// st := tcell.StyleDefault
// gl := ' '
// if s.Colors() > 256 {
// rgb := tcell.NewHexColor(int32(rand.Int() & 0xffffff))
// st = st.Background(rgb)
// } else if s.Colors() > 1 {
// st = st.Background(tcell.Color(rand.Int() % s.Colors()))
// } else {
// st = st.Reverse(rand.Int()%2 == 0)
// gl = glyphs[rand.Int()%len(glyphs)]
// }
// for row := 0; row < lh; row++ {
// for col := 0; col < lw; col++ {
// s.SetCell(lx+col, ly+row, st, gl)
// }
// }
// s.Show()
// }