confetty/fireworks/fireworks.go

111 lines
2.4 KiB
Go
Raw Normal View History

2021-08-07 21:45:59 +00:00
package fireworks
import (
"math"
"math/rand"
"time"
"github.com/charmbracelet/harmonica"
2021-08-07 21:45:59 +00:00
"github.com/maaslalani/confetty/array"
2021-08-09 01:08:28 +00:00
"github.com/maaslalani/confetty/simulation"
2021-08-07 21:45:59 +00:00
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"golang.org/x/term"
)
const (
framesPerSecond = 60.0
2021-08-08 00:09:20 +00:00
numParticles = 50
2021-08-07 21:45:59 +00:00
)
var (
colors = []string{"#fdff6a", "#ff718d"}
2021-08-08 00:09:20 +00:00
characters = []string{"+", "*", "•"}
2021-08-07 21:45:59 +00:00
)
type frameMsg time.Time
2021-08-09 01:06:10 +00:00
type fireworkMsg time.Time
2021-08-07 21:45:59 +00:00
func animate() tea.Cmd {
return tea.Tick(time.Second/framesPerSecond, func(t time.Time) tea.Msg {
return frameMsg(t)
})
}
type model struct {
2021-08-09 01:08:28 +00:00
system *simulation.System
2021-08-07 21:45:59 +00:00
}
2021-08-14 21:08:40 +00:00
func Spawn(width, height int) []simulation.Particle {
2021-08-07 21:45:59 +00:00
color := lipgloss.Color(array.Sample(colors))
v := float64(rand.Intn(10) + 20.0)
2021-08-09 01:08:28 +00:00
particles := []simulation.Particle{}
2021-08-07 21:45:59 +00:00
x := rand.Float64() * float64(width)
y := rand.Float64() * float64(height)
for i := 0; i < numParticles; i++ {
2021-08-09 01:08:28 +00:00
p := simulation.Particle{
Physics: harmonica.NewProjectile(
harmonica.FPS(framesPerSecond),
harmonica.Point{X: x, Y: y},
harmonica.Vector{X: math.Cos(float64(i)) * v, Y: math.Sin(float64(i)) * v / 2},
harmonica.Vector(harmonica.TerminalGravity),
2021-08-07 21:45:59 +00:00
),
2021-08-09 01:06:10 +00:00
Char: lipgloss.NewStyle().Foreground(color).Render(array.Sample(characters)),
2021-08-07 21:45:59 +00:00
}
particles = append(particles, p)
}
return particles
}
func InitialModel() model {
2021-08-09 01:06:10 +00:00
width, height, err := term.GetSize(0)
if err != nil {
panic(err)
}
2021-08-09 01:08:28 +00:00
return model{system: &simulation.System{
2021-08-14 21:08:40 +00:00
Particles: Spawn(width, height),
2021-08-09 01:08:28 +00:00
Frame: simulation.Frame{
2021-08-09 01:06:10 +00:00
Width: width,
Height: height,
},
}}
2021-08-07 21:45:59 +00:00
}
// Init initializes the confetti after a small delay
func (m model) Init() tea.Cmd {
2021-08-09 01:06:10 +00:00
return animate()
2021-08-07 21:45:59 +00:00
}
// Update updates the model every frame, it handles the animation loop and
// updates the particle physics every frame
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
2021-08-09 02:51:40 +00:00
switch msg.String() {
case "ctrl+c", "q":
return m, tea.Quit
}
2021-08-14 21:08:40 +00:00
m.system.Particles = Spawn(m.system.Frame.Width, m.system.Frame.Height)
2021-08-09 02:51:40 +00:00
return m, nil
2021-08-07 21:45:59 +00:00
case frameMsg:
2021-08-09 01:06:10 +00:00
m.system.Update()
2021-08-07 21:45:59 +00:00
return m, animate()
case tea.WindowSizeMsg:
2021-08-09 01:06:10 +00:00
m.system.Frame.Width = msg.Width
m.system.Frame.Height = msg.Height
2021-08-07 21:45:59 +00:00
return m, nil
default:
return m, nil
}
}
// View displays all the particles on the screen
func (m model) View() string {
2021-08-09 01:06:10 +00:00
return m.system.Render()
2021-08-07 21:45:59 +00:00
}