I dont know if pointers help here but now there are pointers

This commit is contained in:
MrMelon 2021-10-31 19:51:36 +00:00 committed by Maas Lalani
parent ec2c5d6128
commit d89ba24246
3 changed files with 14 additions and 13 deletions

View File

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

View File

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

View File

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