From 34aae6c34c6ec14906e355533a5a5a3a6264dcc0 Mon Sep 17 00:00:00 2001 From: Maas Lalani Date: Sat, 7 Aug 2021 16:13:35 -0400 Subject: [PATCH] Repeated explosions --- confetty/confetty.go | 31 ++++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/confetty/confetty.go b/confetty/confetty.go index 6a1bc92..7192c6d 100644 --- a/confetty/confetty.go +++ b/confetty/confetty.go @@ -96,9 +96,25 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { // frame animation 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 + } } + + if particlesVisible <= 0 { + for _, p := range m.particles { + p.physics.Reset() + } + } + return m, animate() case tea.WindowSizeMsg: @@ -113,10 +129,14 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { // View displays all the particles on the screen func (m model) View() string { - if m.viewport.Height <= 0 || m.viewport.Width <= 0 { + height := m.viewport.Height + 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) @@ -126,14 +146,7 @@ func (m model) View() string { y := p.physics.PosY() x := p.physics.PosX() - if y < 0 { - continue - } - - // Particle is out of view - if y >= m.viewport.Height-1 || x < 0 || x >= m.viewport.Width-1 { - // loop motion - p.physics.Reset() + if y < 0 || x < 0 || y >= height-1 || x >= width-1 { continue }