From 15dbe41db20d6d3daa7e44b7ac9ca73a8bd43106 Mon Sep 17 00:00:00 2001 From: Maas Lalani Date: Fri, 6 Aug 2021 22:00:08 -0400 Subject: [PATCH] simple physics refactor --- README.md | 3 +++ pkg/confetty/confetty.go | 42 ++++++++++++------------------------ pkg/physics/physics.go | 46 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 63 insertions(+), 28 deletions(-) create mode 100644 README.md create mode 100644 pkg/physics/physics.go diff --git a/README.md b/README.md new file mode 100644 index 0000000..4bde715 --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +# ConfeTTY + +Terminal Confetti. diff --git a/pkg/confetty/confetty.go b/pkg/confetty/confetty.go index 748bc0b..c01306d 100644 --- a/pkg/confetty/confetty.go +++ b/pkg/confetty/confetty.go @@ -2,11 +2,12 @@ package confetty import ( "fmt" - "math" "math/rand" "strings" "time" + "github.com/maaslalani/confetty/pkg/physics" + "github.com/charmbracelet/bubbles/viewport" tea "github.com/charmbracelet/bubbletea" "github.com/charmbracelet/lipgloss" @@ -46,26 +47,9 @@ type model struct { } type Particle struct { - char string - acc Acceleration - vel Velocity - pos Position - color lipgloss.Color -} - -type Position struct { - x float64 - y float64 -} - -type Velocity struct { - x float64 - y float64 -} - -type Acceleration struct { - x float64 - y float64 + char string + physics *physics.Physics + color lipgloss.Color } var characters = []string{"▄", "▀", "█"} // "▓", "▒", "░"} @@ -84,9 +68,12 @@ func InitialModel() model { p := &Particle{ char: lipgloss.NewStyle().Foreground(colors[rand.Intn(len(colors))]).Render(characters[rand.Intn(len(characters))]), - pos: Position{x: x, y: y}, - vel: Velocity{x: (rand.Float64() - 0.5) * 100, y: (rand.Float64() - 0.5) * 100}, - acc: Acceleration{x: 0, y: 9.8}, + physics: physics.New( + physics.Vector{X: x, Y: y}, + physics.Vector{X: (rand.Float64() - 0.5) * 100, Y: (rand.Float64() - 0.5) * 100}, + physics.Vector(physics.Gravity), + fps, + ), } particles = append(particles, p) } @@ -104,8 +91,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { return m, tea.Quit case frameMsg: for _, p := range m.particles { - p.pos.y, p.vel.y = p.pos.y+p.vel.y/fps, p.vel.y+p.acc.y/fps - p.pos.x, p.vel.x = p.pos.x+p.vel.x/fps, p.vel.x+p.acc.x/fps + p.physics.Update() } return m, animate() case tea.WindowSizeMsg: @@ -127,8 +113,8 @@ func (m model) View() string { grid[i] = make([]string, m.viewport.Width) } for _, p := range m.particles { - y := int(math.Round(p.pos.y)) - x := int(math.Round(p.pos.x)) + y := p.physics.PosY() + x := p.physics.PosX() if y < 0 || x < 0 || x >= m.viewport.Width-1 || y >= m.viewport.Height-1 { continue } diff --git a/pkg/physics/physics.go b/pkg/physics/physics.go new file mode 100644 index 0000000..956b65a --- /dev/null +++ b/pkg/physics/physics.go @@ -0,0 +1,46 @@ +package physics + +import "math" + +type Position Vector +type Velocity Vector +type Acceleration Vector + +var Gravity = Acceleration{ + X: 0, + Y: 9.81, +} + +type Physics struct { + pos Position + vel Velocity + acc Acceleration + fps float64 +} + +type Vector struct { + X float64 + Y float64 +} + +func New(pos, vel, acc Vector, fps float64) *Physics { + return &Physics{ + pos: Position(pos), + vel: Velocity(vel), + acc: Acceleration(acc), + fps: fps, + } +} + +func (p *Physics) Update() { + p.pos.Y, p.vel.Y = p.pos.Y+p.vel.Y/p.fps, p.vel.Y+p.acc.Y/p.fps + p.pos.X, p.vel.X = p.pos.X+p.vel.X/p.fps, p.vel.X+p.acc.X/p.fps +} + +func (p Physics) PosX() int { + return int(math.Round(p.pos.X)) +} + +func (p Physics) PosY() int { + return int(math.Round(p.pos.Y)) +}