-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtree.go
151 lines (114 loc) · 2.83 KB
/
tree.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
package main
import (
"math/rand"
"time"
"github.com/inancgumus/screen"
)
type tree struct {
lightDelay int
colour string
lightsColour string
particle rune
snow bool
snowColour string
snowParticle rune
snowSpeed int
}
type part struct {
x int
y int
particle rune
colour string
}
func setoutTree(p *[]part, particle rune, colour string, height int, term window) {
j := 1
for y := height / 2; y <= height; y++ {
for k := 0; k < j; k++ {
x := int((term.columns/2)-(j/2)) + k
*p = append(*p, part{x, y, particle, colour})
}
j += 2
}
}
func repaintTree(t []part, lc string) []part {
repaintedTree := []part{}
repaintedTree = append(repaintedTree, t...)
for i, p := range t {
finalParticle := p.particle
finalColour := p.colour
if chance := rand.Intn(100); chance < 10 {
finalParticle = rune('o')
if finalColour = lc; len(finalColour) == 0 || finalColour == "rainbow" {
finalColour = getRandomColour()
}
}
repaintedTree[i] = part{p.x, p.y, finalParticle, finalColour}
}
return repaintedTree
}
func setoutTrunk(p *[]part, treeHeight int, height int, term window) {
trunkWidth := 9
for y := treeHeight; y <= treeHeight+height; y++ {
for k := 0; k < trunkWidth; k++ {
x := int((term.columns/2)-(trunkWidth/2)) + k
*p = append(*p, part{x, y, 'w', "yellow"})
}
}
}
func (t *tree) show() {
screen.Clear()
term := getTerminalAttr()
treeParts := []part{}
trunkParts := []part{}
particle := getFlake(t.particle)
trunkHeight := 3
treeHeight := term.rows - trunkHeight
setoutTree(&treeParts, particle, t.colour, treeHeight, term)
setoutTrunk(&trunkParts, treeHeight, trunkHeight, term)
currentRows := make(map[int]int)
snowflakes := make(map[int]*row)
newTreeParts := []part{}
newTreeParts = append(newTreeParts, treeParts...)
start := time.Now()
for {
if t.snow {
targetColumn := rand.Intn(term.columns)
if targetColumn%2 == 0 {
continue
}
if _, onScreen := snowflakes[targetColumn]; onScreen {
moveFlake(snowflakes,
currentRows,
targetColumn,
false,
t.snowColour,
term,
t.snowParticle)
} else {
flake := getFlake(t.snowParticle)
snowflakes[targetColumn] = &row{0, flake}
printSnowflakeCol(snowflakes, targetColumn, t.snowColour)
}
for existingFlake := range snowflakes {
moveFlake(snowflakes,
currentRows,
existingFlake,
false,
t.snowColour,
term,
t.snowParticle)
}
}
end := time.Now()
diff := end.Sub(start).Seconds()
if diff > float64(t.lightDelay) {
newTreeParts = repaintTree(treeParts, t.lightsColour)
start = time.Now()
}
wholeTree := append(trunkParts, newTreeParts...)
for _, p := range wholeTree {
printCharacter(p.x, p.y, p.particle, p.colour)
}
time.Sleep(time.Second / time.Duration(t.snowSpeed))
}
}