This commit is contained in:
Maas Lalani 2021-08-08 22:49:30 -04:00
parent 316b181e43
commit 45abcd2122
No known key found for this signature in database
GPG Key ID: F53774FA051C052A
1 changed files with 36 additions and 97 deletions

View File

@ -1,15 +1,13 @@
package confetti package confetti
import ( import (
"fmt"
"math/rand" "math/rand"
"strings"
"time" "time"
"github.com/maaslalani/confetty/array" "github.com/maaslalani/confetty/array"
"github.com/maaslalani/confetty/physics" "github.com/maaslalani/confetty/physics"
"github.com/maaslalani/confetty/simulation"
"github.com/charmbracelet/bubbles/viewport"
tea "github.com/charmbracelet/bubbletea" tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss" "github.com/charmbracelet/lipgloss"
"golang.org/x/term" "golang.org/x/term"
@ -23,7 +21,6 @@ const (
var ( var (
colors = []string{"#a864fd", "#29cdff", "#78ff44", "#ff718d", "#fdff6a"} colors = []string{"#a864fd", "#29cdff", "#78ff44", "#ff718d", "#fdff6a"}
characters = []string{"█", "▓", "▒", "░", "▄", "▀"} characters = []string{"█", "▓", "▒", "░", "▄", "▀"}
// characters = []string{"▄", "▀"}
) )
type frameMsg time.Time type frameMsg time.Time
@ -34,57 +31,52 @@ func animate() tea.Cmd {
}) })
} }
func wait(d time.Duration) tea.Cmd {
return func() tea.Msg {
time.Sleep(d)
return nil
}
}
// Confetti model // Confetti model
type model struct { type model struct {
particles []*Particle system *simulation.System
viewport viewport.Model
} }
type Particle struct { func spawn(width, height int) []simulation.Particle {
char string particles := []simulation.Particle{}
physics *physics.Physics
}
func InitialModel() model {
particles := []*Particle{}
width, _, err := term.GetSize(0)
if err != nil {
panic(err)
}
for i := 0; i < numParticles; i++ { for i := 0; i < numParticles; i++ {
x := float64(width / 2) x := float64(width / 2)
y := float64(-1) y := float64(0)
p := &Particle{ p := simulation.Particle{
physics: physics.New( Physics: physics.New(
physics.Point{X: x + (float64(width/4) * (rand.Float64() - 0.5)), Y: y}, physics.Point{X: x + (float64(width/4) * (rand.Float64() - 0.5)), Y: y},
physics.Vector{X: (rand.Float64() - 0.5) * 100, Y: rand.Float64() * 50}, physics.Vector{X: (rand.Float64() - 0.5) * 100, Y: rand.Float64() * 50},
physics.Vector(physics.Gravity), physics.Vector(physics.Gravity),
framesPerSecond, framesPerSecond,
), ),
char: lipgloss.NewStyle(). Char: lipgloss.NewStyle().
Foreground(lipgloss.Color(array.Sample(colors))). Foreground(lipgloss.Color(array.Sample(colors))).
Render(array.Sample(characters)), Render(array.Sample(characters)),
} }
particles = append(particles, p) particles = append(particles, p)
} }
return particles
}
return model{particles: particles} func InitialModel() model {
width, height, err := term.GetSize(0)
if err != nil {
panic(err)
}
return model{system: &simulation.System{
Particles: spawn(width, height),
Frame: simulation.Frame{
Width: width,
Height: height,
},
}}
} }
// Init initializes the confetti after a small delay // Init initializes the confetti after a small delay
func (m model) Init() tea.Cmd { func (m model) Init() tea.Cmd {
return tea.Sequentially(wait(time.Second/2), animate()) return animate()
} }
// Update updates the model every frame, it handles the animation loop and // Update updates the model every frame, it handles the animation loop and
@ -92,36 +84,19 @@ func (m model) Init() tea.Cmd {
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) { switch msg := msg.(type) {
case tea.KeyMsg: case tea.KeyMsg:
return m, tea.Quit switch msg.String() {
case "ctrl+c", "q":
// frame animation return m, tea.Quit
case frameMsg:
particlesVisible := numParticles
for _, p := range m.particles {
p.physics.Update()
y := p.physics.PosY()
x := p.physics.PosX()
// Particle is out of view
if y >= m.viewport.Height-1 || x < 0 || x >= m.viewport.Width-1 {
particlesVisible -= 1
}
} }
m.system.Particles = spawn(m.system.Frame.Width, m.system.Frame.Height)
if particlesVisible <= 0 { return m, nil
for _, p := range m.particles { case frameMsg:
p.physics.Reset() m.system.Update()
} return m, animate()
} case tea.WindowSizeMsg:
m.system.Frame.Width = msg.Width
return m, animate() m.system.Frame.Height = msg.Height
case tea.WindowSizeMsg:
m.viewport.Width = msg.Width
m.viewport.Height = msg.Height
return m, nil return m, nil
default: default:
return m, nil return m, nil
} }
@ -129,41 +104,5 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
// View displays all the particles on the screen // View displays all the particles on the screen
func (m model) View() string { func (m model) View() string {
height := m.viewport.Height return m.system.Render()
width := m.viewport.Width
if height <= 0 || width <= 0 {
return ""
}
var out strings.Builder
grid := make([][]string, m.viewport.Height)
for i := range grid {
grid[i] = make([]string, m.viewport.Width)
}
for _, p := range m.particles {
y := p.physics.PosY()
x := p.physics.PosX()
if y < 0 || x < 0 || y >= height-1 || x >= width-1 {
continue
}
grid[y][x] = p.char
}
// Print out grid
for i := range grid {
for _, col := range grid[i] {
if col == "" {
fmt.Fprint(&out, " ")
} else {
fmt.Fprint(&out, col)
}
}
fmt.Fprint(&out, "\n")
}
return out.String()
} }