confetty/fireworks/fireworks.go

123 lines
3.1 KiB
Go
Raw Normal View History

2021-08-07 14:45:59 -07:00
package fireworks
import (
"math"
"math/rand"
"time"
"github.com/maaslalani/confetty/array"
2021-08-08 18:08:28 -07:00
"github.com/maaslalani/confetty/simulation"
2021-08-07 14:45:59 -07:00
tea "github.com/charmbracelet/bubbletea"
2021-11-04 21:24:58 -07:00
"github.com/charmbracelet/harmonica"
2021-08-07 14:45:59 -07:00
"github.com/charmbracelet/lipgloss"
)
const (
2021-11-04 21:08:48 -07:00
framesPerSecond = 30.0
2021-08-07 17:09:20 -07:00
numParticles = 50
2021-08-07 14:45:59 -07:00
)
var (
colors = []string{"#a864fd", "#29cdff", "#78ff44", "#ff718d", "#fdff6a"}
2021-08-07 17:09:20 -07:00
characters = []string{"+", "*", "•"}
2021-11-04 21:24:58 -07:00
head = "▄"
tail = "│"
2021-08-07 14:45:59 -07:00
)
type frameMsg time.Time
func animate() tea.Cmd {
return tea.Tick(time.Second/framesPerSecond, func(t time.Time) tea.Msg {
return frameMsg(t)
})
}
type model struct {
2021-08-08 18:08:28 -07:00
system *simulation.System
2021-08-07 14:45:59 -07:00
}
func SpawnShoot(width, height int) *simulation.Particle {
2021-10-31 11:57:51 -07:00
color := lipgloss.Color(array.Sample(colors))
v := float64(rand.Intn(15) + 15.0)
x := rand.Float64() * float64(width)
p := simulation.Particle{
2021-11-04 21:24:58 -07:00
Physics: harmonica.NewProjectile(
harmonica.FPS(framesPerSecond),
harmonica.Point{X: x, Y: float64(height)},
harmonica.Vector{X: 0, Y: -v},
harmonica.TerminalGravity,
2021-10-31 11:57:51 -07:00
),
2021-11-04 21:24:58 -07:00
Char: lipgloss.NewStyle().Foreground(color).Render(head),
TailChar: lipgloss.NewStyle().Foreground(color).Render(tail),
2021-12-31 14:23:37 -08:00
Color: color,
2021-10-31 11:57:51 -07:00
Shooting: true,
ExplosionCall: SpawnExplosion,
}
return &p
2021-10-31 11:57:51 -07:00
}
2021-12-31 14:23:37 -08:00
func SpawnExplosion(color lipgloss.Color, x, y float64, width, height int) []*simulation.Particle {
2021-08-07 14:45:59 -07:00
v := float64(rand.Intn(10) + 20.0)
particles := []*simulation.Particle{}
2021-08-07 14:45:59 -07:00
for i := 0; i < numParticles; i++ {
2021-08-08 18:08:28 -07:00
p := simulation.Particle{
2021-11-04 21:24:58 -07:00
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.TerminalGravity,
2021-08-07 14:45:59 -07:00
),
2021-10-31 11:57:51 -07:00
Char: lipgloss.NewStyle().Foreground(color).Render(array.Sample(characters)),
Shooting: false,
2021-08-07 14:45:59 -07:00
}
particles = append(particles, &p)
2021-08-07 14:45:59 -07:00
}
return particles
}
func InitialModel() model {
2021-08-08 18:08:28 -07:00
return model{system: &simulation.System{
Particles: []*simulation.Particle{},
Frame: simulation.Frame{},
2021-08-08 18:06:10 -07:00
}}
2021-08-07 14:45:59 -07:00
}
// Init initializes the confetti after a small delay
func (m model) Init() tea.Cmd {
2021-08-08 18:06:10 -07:00
return animate()
2021-08-07 14:45:59 -07: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-08 19:51:40 -07:00
switch msg.String() {
case "ctrl+c", "q":
return m, tea.Quit
}
2021-10-31 11:57:51 -07:00
m.system.Particles = append(m.system.Particles, SpawnShoot(m.system.Frame.Width, m.system.Frame.Height))
2021-08-08 19:51:40 -07:00
return m, nil
2021-08-07 14:45:59 -07:00
case frameMsg:
2021-08-08 18:06:10 -07:00
m.system.Update()
2021-08-07 14:45:59 -07:00
return m, animate()
case tea.WindowSizeMsg:
if m.system.Frame.Width == 0 && m.system.Frame.Height == 0 {
// For the first frameMsg spawn a system of particles
m.system.Particles = append(m.system.Particles, SpawnShoot(msg.Width, msg.Height))
}
2021-08-08 18:06:10 -07:00
m.system.Frame.Width = msg.Width
m.system.Frame.Height = msg.Height
2021-08-07 14:45:59 -07:00
return m, nil
default:
return m, nil
}
}
// View displays all the particles on the screen
func (m model) View() string {
2021-08-08 18:06:10 -07:00
return m.system.Render()
2021-08-07 14:45:59 -07:00
}