diff --git a/confetti/confetti.go b/confetti/confetti.go index d0fa9a3..9b7749d 100644 --- a/confetti/confetti.go +++ b/confetti/confetti.go @@ -57,13 +57,10 @@ func Spawn(width, height int) []*simulation.Particle { return particles } -func InitialModelWithSize(width, height int) model { +func InitialModel() model { return model{system: &simulation.System{ - Particles: Spawn(width, height), - Frame: simulation.Frame{ - Width: width, - Height: height, - }, + Particles: []*simulation.Particle{}, + Frame: simulation.Frame{}, }} } @@ -88,6 +85,10 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { m.system.Update() return m, animate() case tea.WindowSizeMsg: + if m.system.Frame.Width == 0 && m.system.Frame.Height == 0 { + // For the first frameMsg spawn a system of particles + m.system.Particles = Spawn(msg.Width, msg.Height) + } m.system.Frame.Width = msg.Width m.system.Frame.Height = msg.Height return m, nil diff --git a/confetti/confetti_unix.go b/confetti/confetti_unix.go deleted file mode 100644 index 91e6cfd..0000000 --- a/confetti/confetti_unix.go +++ /dev/null @@ -1,16 +0,0 @@ -//go:build !windows -// +build !windows - -package confetti - -import ( - "golang.org/x/term" -) - -func InitialModel() model { - width, height, err := term.GetSize(0) - if err != nil { - panic(err) - } - return InitialModelWithSize(width, height) -} diff --git a/confetti/confetti_windows.go b/confetti/confetti_windows.go deleted file mode 100644 index 6bc640a..0000000 --- a/confetti/confetti_windows.go +++ /dev/null @@ -1,22 +0,0 @@ -//go:build windows -// +build windows - -package confetti - -import ( - "syscall" - - "golang.org/x/term" -) - -func InitialModel() model { - h, err := syscall.GetStdHandle(syscall.STD_OUTPUT_HANDLE) - if err != nil { - panic(err) - } - width, height, err := term.GetSize(int(h)) - if err != nil { - panic(err) - } - return InitialModelWithSize(width, height) -} diff --git a/fireworks/fireworks.go b/fireworks/fireworks.go index b9d5354..275e223 100644 --- a/fireworks/fireworks.go +++ b/fireworks/fireworks.go @@ -79,13 +79,10 @@ func SpawnExplosion(x, y float64, width, height int) []*simulation.Particle { return particles } -func InitialModelWithSize(width, height int) model { +func InitialModel() model { return model{system: &simulation.System{ - Particles: []*simulation.Particle{SpawnShoot(width, height)}, - Frame: simulation.Frame{ - Width: width, - Height: height, - }, + Particles: []*simulation.Particle{}, + Frame: simulation.Frame{}, }} } @@ -110,6 +107,10 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { m.system.Update() return m, animate() case tea.WindowSizeMsg: + if m.system.Frame.Width == 0 && m.system.Frame.Height == 0 { + // For the first frameMsg spawn a system of particles + m.system.Particles = append(m.system.Particles, SpawnShoot(msg.Width, msg.Height)) + } m.system.Frame.Width = msg.Width m.system.Frame.Height = msg.Height return m, nil diff --git a/fireworks/fireworks_unix.go b/fireworks/fireworks_unix.go deleted file mode 100644 index 0448347..0000000 --- a/fireworks/fireworks_unix.go +++ /dev/null @@ -1,16 +0,0 @@ -//go:build !windows -// +build !windows - -package fireworks - -import ( - "golang.org/x/term" -) - -func InitialModel() model { - width, height, err := term.GetSize(0) - if err != nil { - panic(err) - } - return InitialModelWithSize(width, height) -} diff --git a/fireworks/fireworks_windows.go b/fireworks/fireworks_windows.go deleted file mode 100644 index c8298ad..0000000 --- a/fireworks/fireworks_windows.go +++ /dev/null @@ -1,22 +0,0 @@ -//go:build windows -// +build windows - -package fireworks - -import ( - "syscall" - - "golang.org/x/term" -) - -func InitialModel() model { - h, err := syscall.GetStdHandle(syscall.STD_OUTPUT_HANDLE) - if err != nil { - panic(err) - } - width, height, err := term.GetSize(int(h)) - if err != nil { - panic(err) - } - return InitialModelWithSize(width, height) -}