From 78a718424f034813cd1075dfa019307e0e5e3539 Mon Sep 17 00:00:00 2001 From: MrMelon Date: Sun, 31 Oct 2021 17:58:01 +0000 Subject: [PATCH] Allow multiple confetti and fireworks particles to be spawned at a time --- confetti/confetti.go | 11 ++++++++++- fireworks/fireworks.go | 14 +++++++++++--- simulation/simulation.go | 14 +++++--------- 3 files changed, 26 insertions(+), 13 deletions(-) diff --git a/confetti/confetti.go b/confetti/confetti.go index 89fb57c..167912c 100644 --- a/confetti/confetti.go +++ b/confetti/confetti.go @@ -88,10 +88,19 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { case "ctrl+c", "q": return m, tea.Quit } - m.system.Particles = Spawn(m.system.Frame.Width, m.system.Frame.Height) + m.system.Particles = append(m.system.Particles, Spawn(m.system.Frame.Width, m.system.Frame.Height)...) + return m, nil case frameMsg: m.system.Update() + + 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) + } + } + return m, animate() case tea.WindowSizeMsg: m.system.Frame.Width = msg.Width diff --git a/fireworks/fireworks.go b/fireworks/fireworks.go index 4389944..870da4e 100644 --- a/fireworks/fireworks.go +++ b/fireworks/fireworks.go @@ -20,12 +20,11 @@ const ( ) var ( - colors = []string{"#fdff6a", "#ff718d"} + colors = []string{"#a864fd", "#29cdff", "#78ff44", "#ff718d", "#fdff6a"} characters = []string{"+", "*", "•"} ) type frameMsg time.Time -type fireworkMsg time.Time func animate() tea.Cmd { return tea.Tick(time.Second/framesPerSecond, func(t time.Time) tea.Msg { @@ -90,10 +89,19 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { case "ctrl+c", "q": return m, tea.Quit } - m.system.Particles = Spawn(m.system.Frame.Width, m.system.Frame.Height) + m.system.Particles = append(m.system.Particles, Spawn(m.system.Frame.Width, m.system.Frame.Height)...) + return m, nil case frameMsg: m.system.Update() + + 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) + } + } + return m, animate() case tea.WindowSizeMsg: m.system.Frame.Width = msg.Width diff --git a/simulation/simulation.go b/simulation/simulation.go index aced52c..e7a494b 100644 --- a/simulation/simulation.go +++ b/simulation/simulation.go @@ -23,17 +23,13 @@ type Frame struct { Height int } +func RemoveParticleFromArray(s []Particle, i int) []Particle { + s[i] = s[len(s)-1] + return s[:len(s)-1] +} + func (s *System) Update() { for _, p := range s.Particles { - if p.Hidden { - continue - } - - if !s.Visible(p) { - p.Hidden = true - continue - } - p.Physics.Update() } }