-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
215 lines (190 loc) · 5.42 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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package main
import (
"log"
"math"
"math/rand"
"os"
"time"
"github.com/gdamore/tcell/v2"
)
var (
screenWidth, screenHeight int
grid [][]int
styleGrid [][]int
lastMouseX, lastMouseY int
mouseMoved bool
hasFloor bool = true
)
func HSVtoRGB(hue int) (int32, int32, int32) {
saturation := 1.0
value := 1.0
c := value * saturation
x := c * (1 - math.Abs(math.Mod(float64(hue)/60.0, 2)-1))
m := value - c
var r, g, b float64
switch {
case hue >= 0 && hue < 60:
r, g, b = c, x, 0
case hue >= 60 && hue < 120:
r, g, b = x, c, 0
case hue >= 120 && hue < 180:
r, g, b = 0, c, x
case hue >= 180 && hue < 240:
r, g, b = 0, x, c
case hue >= 240 && hue < 300:
r, g, b = x, 0, c
case hue >= 300 && hue < 360:
r, g, b = c, 0, x
}
r = (r + m) * 255
g = (g + m) * 255
b = (b + m) * 255
return int32(r), int32(g), int32(b)
}
func BottomRowClear() bool {
for x:=range grid[len(grid)-1]{
if grid[len(grid)-1][x] != 0{
return false
}
}
return true
}
func resetGrid(s tcell.Screen){
grid = make([][]int, screenHeight)
styleGrid = make([][]int, screenHeight)
for i := 0; i < screenHeight; i++ {
grid[i] = make([]int, screenWidth)
styleGrid[i] = make([]int, screenWidth)
for x := 0; x < screenWidth; x++{
s.SetContent(x, i, ' ', nil, tcell.StyleDefault)
}
}
}
func render(s tcell.Screen) {
if !hasFloor{
isclear:=true
for x := range grid[len(grid)-1]{
if grid[len(grid)-1][x] != 0{
isclear=false;
}
grid[len(grid)-1][x]=0
s.SetContent(x, screenHeight-1, ' ', nil, tcell.StyleDefault)
}
if isclear{
hasFloor=true
resetGrid(s)
return
}
}
for y := screenHeight - 2; y >= 0; y-- {
for x := 0; x < screenWidth; x++ {
blockstyle := tcell.StyleDefault.Background(tcell.NewRGBColor(HSVtoRGB(grid[y][x])))
if grid[y][x] > 0 {
if grid[y+1][x] == 0 {
grid[y+1][x] = grid[y][x]
grid[y][x] = 0
s.SetContent(x, y+1, ' ', nil, blockstyle)
styleGrid[y+1][x] = 1
} else if x > 0 && grid[y+1][x-1] == 0 {
grid[y+1][x-1] = grid[y][x]
grid[y][x] = 0
s.SetContent(x-1, y+1, ' ', nil, blockstyle)
styleGrid[y+1][x-1] = 1
} else if x < screenWidth-1 && grid[y+1][x+1] == 0 {
grid[y+1][x+1] = grid[y][x]
grid[y][x] = 0
s.SetContent(x+1, y+1, ' ', nil, blockstyle)
styleGrid[y+1][x+1] = 1
}
} else {
style := tcell.StyleDefault
if y != 0 && grid[y-1][x] != 0 {
style = tcell.StyleDefault.Background(tcell.NewRGBColor(HSVtoRGB(grid[y-1][x])))
grid[y][x] = grid[y-1][x]
grid[y-1][x] = 0
styleGrid[y][x] = 1
} else {
styleGrid[y][x] = 0
}
s.SetContent(x, y, ' ', nil, style)
}
if grid[y][x] != 0 && styleGrid[y][x] == 0 {
s.SetContent(x, y, ' ', nil, tcell.StyleDefault.Background(tcell.NewRGBColor(HSVtoRGB(grid[y][x]))))
}
}
}
}
func main() {
s, err := tcell.NewScreen()
if err != nil {
log.Fatalf("TermISand couldn't initialise the screen :(")
}
defer s.Fini()
if err := s.Init(); err != nil {
log.Fatalf("TermISand couldn't initialise the screen :(")
}
s.EnableFocus()
s.EnableMouse()
screenWidth, screenHeight = s.Size()
resetGrid(s)
s.Clear()
colorNum := 0
eventCh := make(chan tcell.Event, 1)
go func() {
for {
ev := s.PollEvent()
if ev != nil {
eventCh <- ev
}
}
}()
ticker := time.NewTicker(20 * time.Millisecond)
defer ticker.Stop()
for {
select {
case ev := <-eventCh:
switch ev := ev.(type) {
case *tcell.EventKey:
switch ev.Key() {
case tcell.KeyCtrlQ, tcell.KeyCtrlC:
s.Fini()
os.Exit(0)
case tcell.KeyCtrlR:
hasFloor = false
}
case *tcell.EventResize:
screenWidth, screenHeight = s.Size()
resetGrid(s)
s.Sync()
case *tcell.EventMouse:
lastMouseX, lastMouseY = ev.Position()
mouseMoved = true
}
case <-ticker.C:
// Add sand at the last known mouse position if the mouse has moved
if hasFloor && mouseMoved && lastMouseY < screenHeight && lastMouseX < screenWidth && grid[lastMouseY][lastMouseX] == 0 {
grid[lastMouseY][lastMouseX] = colorNum
rand1 := rand.Intn(4)
rand2 := rand.Intn(4)
if (rand1 == 0 || rand2 == 0) && lastMouseY-1 >= 0 && grid[lastMouseY-1][lastMouseX] == 0 {
grid[lastMouseY-1][lastMouseX] = colorNum
}
if (rand1 == 1 || rand2 == 1) && lastMouseY+1 < screenHeight && grid[lastMouseY+1][lastMouseX] == 0 {
grid[lastMouseY+1][lastMouseX] = colorNum
}
if (rand1 == 2 || rand2 == 2) && lastMouseX-1 >= 0 && grid[lastMouseY][lastMouseX-1] == 0 {
grid[lastMouseY][lastMouseX-1] = colorNum
}
if (rand1 == 3 || rand2 == 3) && lastMouseX+1 < screenWidth && grid[lastMouseY][lastMouseX+1] == 0 {
grid[lastMouseY][lastMouseX+1] = colorNum
}
colorNum++
if colorNum == 360 {
colorNum = 1
}
}
render(s)
s.Show()
}
}
}