2021-08-07 23:57:00 +00:00
|
|
|
package confetti
|
2021-08-07 01:24:56 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"math/rand"
|
|
|
|
"time"
|
|
|
|
|
2021-10-23 05:10:40 +00:00
|
|
|
"github.com/charmbracelet/harmonica"
|
2021-08-07 18:46:09 +00:00
|
|
|
"github.com/maaslalani/confetty/array"
|
2021-08-09 02:49:30 +00:00
|
|
|
"github.com/maaslalani/confetty/simulation"
|
2021-08-07 02:00:08 +00:00
|
|
|
|
2021-08-07 01:24:56 +00:00
|
|
|
tea "github.com/charmbracelet/bubbletea"
|
|
|
|
"github.com/charmbracelet/lipgloss"
|
|
|
|
"golang.org/x/term"
|
|
|
|
)
|
|
|
|
|
2021-08-07 03:43:02 +00:00
|
|
|
const (
|
|
|
|
framesPerSecond = 60.0
|
|
|
|
numParticles = 75
|
|
|
|
)
|
2021-08-07 01:24:56 +00:00
|
|
|
|
2021-08-07 02:08:18 +00:00
|
|
|
var (
|
|
|
|
colors = []string{"#a864fd", "#29cdff", "#78ff44", "#ff718d", "#fdff6a"}
|
2021-08-07 18:43:40 +00:00
|
|
|
characters = []string{"█", "▓", "▒", "░", "▄", "▀"}
|
2021-08-07 02:08:18 +00:00
|
|
|
)
|
2021-08-07 01:24:56 +00:00
|
|
|
|
|
|
|
type frameMsg time.Time
|
|
|
|
|
|
|
|
func animate() tea.Cmd {
|
2021-08-07 03:43:02 +00:00
|
|
|
return tea.Tick(time.Second/framesPerSecond, func(t time.Time) tea.Msg {
|
2021-08-07 01:24:56 +00:00
|
|
|
return frameMsg(t)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-08-07 03:43:02 +00:00
|
|
|
// Confetti model
|
2021-08-07 01:24:56 +00:00
|
|
|
type model struct {
|
2021-08-09 02:49:30 +00:00
|
|
|
system *simulation.System
|
2021-08-07 01:24:56 +00:00
|
|
|
}
|
|
|
|
|
2021-08-14 21:08:40 +00:00
|
|
|
func Spawn(width, height int) []simulation.Particle {
|
2021-08-09 02:49:30 +00:00
|
|
|
particles := []simulation.Particle{}
|
2021-08-07 03:43:02 +00:00
|
|
|
for i := 0; i < numParticles; i++ {
|
2021-08-07 01:24:56 +00:00
|
|
|
x := float64(width / 2)
|
2021-08-09 02:49:30 +00:00
|
|
|
y := float64(0)
|
2021-08-07 01:24:56 +00:00
|
|
|
|
2021-08-09 02:49:30 +00:00
|
|
|
p := simulation.Particle{
|
2021-10-23 05:10:40 +00:00
|
|
|
Physics: harmonica.NewProjectile(
|
|
|
|
harmonica.FPS(framesPerSecond),
|
|
|
|
harmonica.Point{X: x + (float64(width/4) * (rand.Float64() - 0.5)), Y: y, Z: 0},
|
|
|
|
harmonica.Vector{X: (rand.Float64() - 0.5) * 100, Y: rand.Float64() * 50, Z: 0},
|
|
|
|
harmonica.Vector(harmonica.TerminalGravity),
|
2021-08-07 02:00:08 +00:00
|
|
|
),
|
2021-08-09 02:49:30 +00:00
|
|
|
Char: lipgloss.NewStyle().
|
2021-08-07 02:08:18 +00:00
|
|
|
Foreground(lipgloss.Color(array.Sample(colors))).
|
|
|
|
Render(array.Sample(characters)),
|
2021-08-07 01:24:56 +00:00
|
|
|
}
|
2021-08-07 02:08:18 +00:00
|
|
|
|
2021-08-07 01:24:56 +00:00
|
|
|
particles = append(particles, p)
|
|
|
|
}
|
2021-08-09 02:49:30 +00:00
|
|
|
return particles
|
|
|
|
}
|
|
|
|
|
|
|
|
func InitialModel() model {
|
|
|
|
width, height, err := term.GetSize(0)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2021-08-07 01:24:56 +00:00
|
|
|
|
2021-08-09 02:49:30 +00:00
|
|
|
return model{system: &simulation.System{
|
2021-08-14 21:08:40 +00:00
|
|
|
Particles: Spawn(width, height),
|
2021-08-09 02:49:30 +00:00
|
|
|
Frame: simulation.Frame{
|
|
|
|
Width: width,
|
|
|
|
Height: height,
|
|
|
|
},
|
|
|
|
}}
|
2021-08-07 01:24:56 +00:00
|
|
|
}
|
|
|
|
|
2021-08-07 03:43:02 +00:00
|
|
|
// Init initializes the confetti after a small delay
|
2021-08-07 01:24:56 +00:00
|
|
|
func (m model) Init() tea.Cmd {
|
2021-08-09 02:49:30 +00:00
|
|
|
return animate()
|
2021-08-07 01:24:56 +00:00
|
|
|
}
|
|
|
|
|
2021-08-07 03:43:02 +00:00
|
|
|
// Update updates the model every frame, it handles the animation loop and
|
|
|
|
// updates the particle physics every frame
|
2021-08-07 01:24:56 +00:00
|
|
|
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
|
|
|
switch msg := msg.(type) {
|
|
|
|
case tea.KeyMsg:
|
2021-08-09 02:49:30 +00:00
|
|
|
switch msg.String() {
|
|
|
|
case "ctrl+c", "q":
|
|
|
|
return m, tea.Quit
|
2021-08-07 20:13:35 +00:00
|
|
|
}
|
2021-10-31 17:58:01 +00:00
|
|
|
m.system.Particles = append(m.system.Particles, Spawn(m.system.Frame.Width, m.system.Frame.Height)...)
|
|
|
|
|
2021-08-09 02:49:30 +00:00
|
|
|
return m, nil
|
|
|
|
case frameMsg:
|
|
|
|
m.system.Update()
|
2021-10-31 17:58:01 +00:00
|
|
|
|
|
|
|
for i := len(m.system.Particles) - 1; i >= 0; i-- {
|
|
|
|
p := m.system.Particles[i].Physics.Position()
|
|
|
|
if p.X > float64(m.system.Frame.Width) || p.X < 0 || p.Y > float64(m.system.Frame.Height) {
|
|
|
|
m.system.Particles = simulation.RemoveParticleFromArray(m.system.Particles, i)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-07 01:24:56 +00:00
|
|
|
return m, animate()
|
|
|
|
case tea.WindowSizeMsg:
|
2021-08-09 02:49:30 +00:00
|
|
|
m.system.Frame.Width = msg.Width
|
|
|
|
m.system.Frame.Height = msg.Height
|
2021-08-07 01:24:56 +00:00
|
|
|
return m, nil
|
|
|
|
default:
|
|
|
|
return m, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-07 03:43:02 +00:00
|
|
|
// View displays all the particles on the screen
|
2021-08-07 01:24:56 +00:00
|
|
|
func (m model) View() string {
|
2021-08-09 02:49:30 +00:00
|
|
|
return m.system.Render()
|
2021-08-07 01:24:56 +00:00
|
|
|
}
|