array sample refactor

This commit is contained in:
Maas Lalani 2021-08-06 22:08:18 -04:00
parent 15dbe41db2
commit ebb2591f66
No known key found for this signature in database
GPG Key ID: F53774FA051C052A
2 changed files with 19 additions and 13 deletions

9
pkg/array/array.go Normal file
View File

@ -0,0 +1,9 @@
package array
import "math/rand"
// Sample returns a random element from an array
// (can't wait for generics!)
func Sample(s []string) string {
return s[rand.Intn(len(s))]
}

View File

@ -6,6 +6,7 @@ import (
"strings"
"time"
"github.com/maaslalani/confetty/pkg/array"
"github.com/maaslalani/confetty/pkg/physics"
"github.com/charmbracelet/bubbles/viewport"
@ -14,17 +15,12 @@ import (
"golang.org/x/term"
)
const (
fps = 60.0
)
const fps = 60.0
var colors = []lipgloss.Color{
lipgloss.Color("#a864fd"),
lipgloss.Color("#29cdff"),
lipgloss.Color("#78ff44"),
lipgloss.Color("#ff718d"),
lipgloss.Color("#fdff6a"),
}
var (
colors = []string{"#a864fd", "#29cdff", "#78ff44", "#ff718d", "#fdff6a"}
characters = []string{"▄", "▀", "█"} // "▓", "▒", "░"}
)
type frameMsg time.Time
@ -52,8 +48,6 @@ type Particle struct {
color lipgloss.Color
}
var characters = []string{"▄", "▀", "█"} // "▓", "▒", "░"}
func InitialModel() model {
particles := []*Particle{}
@ -67,14 +61,17 @@ func InitialModel() model {
y := float64(height / 2)
p := &Particle{
char: lipgloss.NewStyle().Foreground(colors[rand.Intn(len(colors))]).Render(characters[rand.Intn(len(characters))]),
physics: physics.New(
physics.Vector{X: x, Y: y},
physics.Vector{X: (rand.Float64() - 0.5) * 100, Y: (rand.Float64() - 0.5) * 100},
physics.Vector(physics.Gravity),
fps,
),
char: lipgloss.NewStyle().
Foreground(lipgloss.Color(array.Sample(colors))).
Render(array.Sample(characters)),
}
particles = append(particles, p)
}