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