mirror of
https://github.com/maaslalani/confetty.git
synced 2024-11-14 11:46:43 +00:00
simple physics refactor
This commit is contained in:
parent
1d4070136b
commit
15dbe41db2
@ -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
|
||||
}
|
||||
|
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