From 6bc31f21b18f09328b667e81b0e0a3eef23260a7 Mon Sep 17 00:00:00 2001 From: MrMelon Date: Sun, 31 Oct 2021 18:07:22 +0000 Subject: [PATCH] Include off-screen detection in simulation update loop --- confetti/confetti.go | 8 -------- fireworks/fireworks.go | 8 -------- simulation/simulation.go | 9 +++++++-- 3 files changed, 7 insertions(+), 18 deletions(-) diff --git a/confetti/confetti.go b/confetti/confetti.go index 167912c..b3aee21 100644 --- a/confetti/confetti.go +++ b/confetti/confetti.go @@ -93,14 +93,6 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { 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 870da4e..715731c 100644 --- a/fireworks/fireworks.go +++ b/fireworks/fireworks.go @@ -94,14 +94,6 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { 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 e7a494b..5ec862b 100644 --- a/simulation/simulation.go +++ b/simulation/simulation.go @@ -29,8 +29,13 @@ func RemoveParticleFromArray(s []Particle, i int) []Particle { } func (s *System) Update() { - for _, p := range s.Particles { - p.Physics.Update() + for i := len(s.Particles) - 1; i >= 0; i-- { + p := s.Particles[i].Physics.Position() + if p.X > float64(s.Frame.Width) || p.X < 0 || p.Y > float64(s.Frame.Height) { + s.Particles = RemoveParticleFromArray(s.Particles, i) + } else { + s.Particles[i].Physics.Update() + } } }