Include off-screen detection in simulation update loop

This commit is contained in:
MrMelon 2021-10-31 18:07:22 +00:00 committed by Maas Lalani
parent 78a718424f
commit 6bc31f21b1
3 changed files with 7 additions and 18 deletions

View File

@ -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

View File

@ -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

View File

@ -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()
}
}
}