diff --git a/confetti/confetti.go b/confetti/confetti.go index e685ddf..3522665 100644 --- a/confetti/confetti.go +++ b/confetti/confetti.go @@ -34,8 +34,8 @@ type model struct { system *simulation.System } -func Spawn(width, height int) []simulation.Particle { - particles := []simulation.Particle{} +func Spawn(width, height int) []*simulation.Particle { + particles := []*simulation.Particle{} for i := 0; i < numParticles; i++ { x := float64(width / 2) y := float64(0) @@ -52,7 +52,7 @@ func Spawn(width, height int) []simulation.Particle { Render(array.Sample(characters)), } - particles = append(particles, p) + particles = append(particles, &p) } return particles } diff --git a/fireworks/fireworks.go b/fireworks/fireworks.go index 58aa7df..e5112c7 100644 --- a/fireworks/fireworks.go +++ b/fireworks/fireworks.go @@ -36,7 +36,7 @@ type model struct { system *simulation.System } -func SpawnShoot(width, height int) simulation.Particle { +func SpawnShoot(width, height int) *simulation.Particle { color := lipgloss.Color(array.Sample(colors)) v := float64(rand.Intn(15) + 15.0) @@ -54,14 +54,14 @@ func SpawnShoot(width, height int) simulation.Particle { Shooting: true, ExplosionCall: SpawnExplosion, } - return p + return &p } -func SpawnExplosion(x, y float64, width, height int) []simulation.Particle { +func SpawnExplosion(x, y float64, width, height int) []*simulation.Particle { color := lipgloss.Color(array.Sample(colors)) v := float64(rand.Intn(10) + 20.0) - particles := []simulation.Particle{} + particles := []*simulation.Particle{} for i := 0; i < numParticles; i++ { p := simulation.Particle{ @@ -74,14 +74,14 @@ func SpawnExplosion(x, y float64, width, height int) []simulation.Particle { Char: lipgloss.NewStyle().Foreground(color).Render(array.Sample(characters)), Shooting: false, } - particles = append(particles, p) + particles = append(particles, &p) } return particles } func InitialModelWithSize(width, height int) model { return model{system: &simulation.System{ - Particles: []simulation.Particle{SpawnShoot(width, height)}, + Particles: []*simulation.Particle{SpawnShoot(width, height)}, Frame: simulation.Frame{ Width: width, Height: height, diff --git a/simulation/simulation.go b/simulation/simulation.go index fba581c..7bc6a71 100644 --- a/simulation/simulation.go +++ b/simulation/simulation.go @@ -8,7 +8,7 @@ import ( type System struct { Frame Frame - Particles []Particle + Particles []*Particle } type Particle struct { @@ -17,7 +17,7 @@ type Particle struct { Physics *Projectile Hidden bool Shooting bool - ExplosionCall func(x, y float64, width, height int) []Particle + ExplosionCall func(x, y float64, width, height int) []*Particle } type Frame struct { @@ -29,7 +29,8 @@ func FPS(n int) float64 { return (time.Second / time.Duration(n)).Seconds() } -func RemoveParticleFromArray(s []Particle, i int) []Particle { +func RemoveParticleFromArray(s []*Particle, i int) []*Particle { + s[i] = nil s[i] = s[len(s)-1] return s[:len(s)-1] } @@ -56,7 +57,7 @@ func (s *System) Update() { } } -func (s *System) Visible(p Particle) bool { +func (s *System) Visible(p *Particle) bool { y := int(p.Physics.Position.Y) x := int(p.Physics.Position.X) return !p.Hidden && y >= 0 && y < s.Frame.Height-1 && x >= 0 && x < s.Frame.Width-1