mirror of
https://github.com/maaslalani/confetty.git
synced 2024-11-21 23:26:40 +00:00
progressive refactor
This commit is contained in:
parent
d4669cfdd0
commit
c5f3d26331
@ -3,16 +3,13 @@
|
||||
package fireworks
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
"math/rand"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/maaslalani/confetty/array"
|
||||
"github.com/maaslalani/confetty/physics"
|
||||
|
||||
"github.com/charmbracelet/bubbles/viewport"
|
||||
tea "github.com/charmbracelet/bubbletea"
|
||||
"github.com/charmbracelet/lipgloss"
|
||||
"golang.org/x/term"
|
||||
@ -26,10 +23,10 @@ const (
|
||||
var (
|
||||
colors = []string{"#fdff6a", "#ff718d"}
|
||||
characters = []string{"+", "*", "•"}
|
||||
// characters = []string{"▄", "▀"}
|
||||
)
|
||||
|
||||
type frameMsg time.Time
|
||||
type fireworkMsg time.Time
|
||||
|
||||
func animate() tea.Cmd {
|
||||
return tea.Tick(time.Second/framesPerSecond, func(t time.Time) tea.Msg {
|
||||
@ -37,63 +34,52 @@ func animate() tea.Cmd {
|
||||
})
|
||||
}
|
||||
|
||||
func wait(d time.Duration) tea.Cmd {
|
||||
return func() tea.Msg {
|
||||
time.Sleep(d)
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// Fireworks model
|
||||
type model struct {
|
||||
particles []*Particle
|
||||
viewport viewport.Model
|
||||
}
|
||||
|
||||
type Particle struct {
|
||||
char string
|
||||
physics *physics.Physics
|
||||
}
|
||||
|
||||
func spawn() []*Particle {
|
||||
width, height, err := term.GetSize(0)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
system *System
|
||||
}
|
||||
|
||||
func spawn(width, height int) []Particle {
|
||||
color := lipgloss.Color(array.Sample(colors))
|
||||
v := float64(rand.Intn(10) + 20.0)
|
||||
|
||||
particles := []*Particle{}
|
||||
particles := []Particle{}
|
||||
|
||||
x := rand.Float64() * float64(width)
|
||||
y := rand.Float64() * float64(height)
|
||||
|
||||
for i := 0; i < numParticles; i++ {
|
||||
p := &Particle{
|
||||
physics: physics.New(
|
||||
p := Particle{
|
||||
Physics: physics.New(
|
||||
physics.Point{X: x, Y: y},
|
||||
physics.Vector{X: math.Cos(float64(i)) * v, Y: math.Sin(float64(i)) * v / 2},
|
||||
physics.Vector(physics.Gravity),
|
||||
framesPerSecond,
|
||||
),
|
||||
char: lipgloss.NewStyle().
|
||||
Foreground(color).
|
||||
Render(array.Sample(characters)),
|
||||
Char: lipgloss.NewStyle().Foreground(color).Render(array.Sample(characters)),
|
||||
}
|
||||
|
||||
particles = append(particles, p)
|
||||
}
|
||||
return particles
|
||||
}
|
||||
|
||||
func InitialModel() model {
|
||||
return model{particles: spawn()}
|
||||
width, height, err := term.GetSize(0)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
return model{system: &System{
|
||||
Particles: spawn(width, height),
|
||||
Frame: Frame{
|
||||
Width: width,
|
||||
Height: height,
|
||||
},
|
||||
}}
|
||||
}
|
||||
|
||||
// Init initializes the confetti after a small delay
|
||||
func (m model) Init() tea.Cmd {
|
||||
return tea.Sequentially(wait(time.Second/2), animate())
|
||||
return animate()
|
||||
}
|
||||
|
||||
// Update updates the model every frame, it handles the animation loop and
|
||||
@ -102,34 +88,13 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||
switch msg := msg.(type) {
|
||||
case tea.KeyMsg:
|
||||
return m, tea.Quit
|
||||
|
||||
// frame animation
|
||||
case frameMsg:
|
||||
particlesVisible := numParticles
|
||||
for _, p := range m.particles {
|
||||
p.physics.Update()
|
||||
|
||||
y := p.physics.PosY()
|
||||
x := p.physics.PosX()
|
||||
|
||||
// Particle is out of view
|
||||
if y < 0 || y >= m.viewport.Height-1 || x < 0 || x >= m.viewport.Width-1 {
|
||||
particlesVisible -= 1
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
if particlesVisible <= 0 {
|
||||
m.particles = spawn()
|
||||
}
|
||||
|
||||
m.system.Update()
|
||||
return m, animate()
|
||||
|
||||
case tea.WindowSizeMsg:
|
||||
m.viewport.Width = msg.Width
|
||||
m.viewport.Height = msg.Height
|
||||
m.system.Frame.Width = msg.Width
|
||||
m.system.Frame.Height = msg.Height
|
||||
return m, nil
|
||||
|
||||
default:
|
||||
return m, nil
|
||||
}
|
||||
@ -137,41 +102,5 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||
|
||||
// View displays all the particles on the screen
|
||||
func (m model) View() string {
|
||||
height := m.viewport.Height
|
||||
width := m.viewport.Width
|
||||
if height <= 0 || width <= 0 {
|
||||
return ""
|
||||
}
|
||||
|
||||
var out strings.Builder
|
||||
|
||||
grid := make([][]string, m.viewport.Height)
|
||||
for i := range grid {
|
||||
grid[i] = make([]string, m.viewport.Width)
|
||||
}
|
||||
|
||||
for _, p := range m.particles {
|
||||
y := p.physics.PosY()
|
||||
x := p.physics.PosX()
|
||||
|
||||
if y < 0 || x < 0 || y >= height-1 || x >= width-1 {
|
||||
continue
|
||||
}
|
||||
|
||||
grid[y][x] = p.char
|
||||
}
|
||||
|
||||
// Print out grid
|
||||
for i := range grid {
|
||||
for _, col := range grid[i] {
|
||||
if col == "" {
|
||||
fmt.Fprint(&out, " ")
|
||||
} else {
|
||||
fmt.Fprint(&out, col)
|
||||
}
|
||||
}
|
||||
fmt.Fprint(&out, "\n")
|
||||
}
|
||||
|
||||
return out.String()
|
||||
return m.system.Render()
|
||||
}
|
||||
|
69
fireworks/system.go
Normal file
69
fireworks/system.go
Normal file
@ -0,0 +1,69 @@
|
||||
package fireworks
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/maaslalani/confetty/physics"
|
||||
)
|
||||
|
||||
type System struct {
|
||||
Frame Frame
|
||||
Particles []Particle
|
||||
}
|
||||
|
||||
type Particle struct {
|
||||
Char string
|
||||
Physics *physics.Physics
|
||||
Hidden bool
|
||||
}
|
||||
|
||||
type Frame struct {
|
||||
Width int
|
||||
Height int
|
||||
}
|
||||
|
||||
func (s *System) Update() {
|
||||
for _, p := range s.Particles {
|
||||
if p.Hidden {
|
||||
continue
|
||||
}
|
||||
|
||||
if !s.Visible(p) {
|
||||
p.Hidden = true
|
||||
continue
|
||||
}
|
||||
|
||||
p.Physics.Update()
|
||||
}
|
||||
}
|
||||
|
||||
func (s *System) Visible(p Particle) bool {
|
||||
y := p.Physics.PosY()
|
||||
x := p.Physics.PosX()
|
||||
return y >= 0 && y < s.Frame.Height-1 && x >= 0 && x < s.Frame.Width-1
|
||||
}
|
||||
|
||||
func (s *System) Render() string {
|
||||
var out strings.Builder
|
||||
plane := make([][]string, s.Frame.Height)
|
||||
for i := range plane {
|
||||
plane[i] = make([]string, s.Frame.Width)
|
||||
}
|
||||
for _, p := range s.Particles {
|
||||
if s.Visible(p) {
|
||||
plane[p.Physics.PosY()][p.Physics.PosX()] = p.Char
|
||||
}
|
||||
}
|
||||
for i := range plane {
|
||||
for _, col := range plane[i] {
|
||||
if col == "" {
|
||||
fmt.Fprint(&out, " ")
|
||||
} else {
|
||||
fmt.Fprint(&out, col)
|
||||
}
|
||||
}
|
||||
fmt.Fprint(&out, "\n")
|
||||
}
|
||||
return out.String()
|
||||
}
|
Loading…
Reference in New Issue
Block a user