mirror of
https://github.com/maaslalani/confetty.git
synced 2024-11-22 07:36:40 +00:00
simple physics refactor
This commit is contained in:
parent
1d4070136b
commit
15dbe41db2
@ -2,11 +2,12 @@ package confetty
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"math"
|
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/maaslalani/confetty/pkg/physics"
|
||||||
|
|
||||||
"github.com/charmbracelet/bubbles/viewport"
|
"github.com/charmbracelet/bubbles/viewport"
|
||||||
tea "github.com/charmbracelet/bubbletea"
|
tea "github.com/charmbracelet/bubbletea"
|
||||||
"github.com/charmbracelet/lipgloss"
|
"github.com/charmbracelet/lipgloss"
|
||||||
@ -46,26 +47,9 @@ type model struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type Particle struct {
|
type Particle struct {
|
||||||
char string
|
char string
|
||||||
acc Acceleration
|
physics *physics.Physics
|
||||||
vel Velocity
|
color lipgloss.Color
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var characters = []string{"▄", "▀", "█"} // "▓", "▒", "░"}
|
var characters = []string{"▄", "▀", "█"} // "▓", "▒", "░"}
|
||||||
@ -84,9 +68,12 @@ func InitialModel() model {
|
|||||||
|
|
||||||
p := &Particle{
|
p := &Particle{
|
||||||
char: lipgloss.NewStyle().Foreground(colors[rand.Intn(len(colors))]).Render(characters[rand.Intn(len(characters))]),
|
char: lipgloss.NewStyle().Foreground(colors[rand.Intn(len(colors))]).Render(characters[rand.Intn(len(characters))]),
|
||||||
pos: Position{x: x, y: y},
|
physics: physics.New(
|
||||||
vel: Velocity{x: (rand.Float64() - 0.5) * 100, y: (rand.Float64() - 0.5) * 100},
|
physics.Vector{X: x, Y: y},
|
||||||
acc: Acceleration{x: 0, y: 9.8},
|
physics.Vector{X: (rand.Float64() - 0.5) * 100, Y: (rand.Float64() - 0.5) * 100},
|
||||||
|
physics.Vector(physics.Gravity),
|
||||||
|
fps,
|
||||||
|
),
|
||||||
}
|
}
|
||||||
particles = append(particles, p)
|
particles = append(particles, p)
|
||||||
}
|
}
|
||||||
@ -104,8 +91,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
|||||||
return m, tea.Quit
|
return m, tea.Quit
|
||||||
case frameMsg:
|
case frameMsg:
|
||||||
for _, p := range m.particles {
|
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.physics.Update()
|
||||||
p.pos.x, p.vel.x = p.pos.x+p.vel.x/fps, p.vel.x+p.acc.x/fps
|
|
||||||
}
|
}
|
||||||
return m, animate()
|
return m, animate()
|
||||||
case tea.WindowSizeMsg:
|
case tea.WindowSizeMsg:
|
||||||
@ -127,8 +113,8 @@ func (m model) View() string {
|
|||||||
grid[i] = make([]string, m.viewport.Width)
|
grid[i] = make([]string, m.viewport.Width)
|
||||||
}
|
}
|
||||||
for _, p := range m.particles {
|
for _, p := range m.particles {
|
||||||
y := int(math.Round(p.pos.y))
|
y := p.physics.PosY()
|
||||||
x := int(math.Round(p.pos.x))
|
x := p.physics.PosX()
|
||||||
if y < 0 || x < 0 || x >= m.viewport.Width-1 || y >= m.viewport.Height-1 {
|
if y < 0 || x < 0 || x >= m.viewport.Width-1 || y >= m.viewport.Height-1 {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
46
pkg/physics/physics.go
Normal file
46
pkg/physics/physics.go
Normal file
@ -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))
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user