unlimited confetti

This commit is contained in:
Maas Lalani 2021-08-07 14:43:40 -04:00
parent eb18e9536b
commit 9c99591a2b
No known key found for this signature in database
GPG Key ID: F53774FA051C052A
2 changed files with 34 additions and 11 deletions

View File

@ -22,7 +22,8 @@ const (
var (
colors = []string{"#a864fd", "#29cdff", "#78ff44", "#ff718d", "#fdff6a"}
characters = []string{"▄", "▀"} // "█", "▓", "▒", "░"}
characters = []string{"█", "▓", "▒", "░", "▄", "▀"}
// characters = []string{"▄", "▀"}
)
type frameMsg time.Time
@ -125,7 +126,14 @@ func (m model) View() string {
y := p.physics.PosY()
x := p.physics.PosX()
if y < 0 || y >= m.viewport.Height-1 || x < 0 || x >= m.viewport.Width-1 {
if y < 0 {
continue
}
// Particle is out of view
if y >= m.viewport.Height-1 || x < 0 || x >= m.viewport.Width-1 {
// loop motion
p.physics.Reset()
continue
}

View File

@ -1,6 +1,8 @@
package physics
import "math"
import (
"math"
)
type Position Vector
type Velocity Vector
@ -11,11 +13,16 @@ var Gravity = Acceleration{
Y: 9.81,
}
type Physics struct {
type Motion struct {
pos Position
vel Velocity
acc Acceleration
fps float64
}
type Physics struct {
current Motion
initial Motion
fps float64
}
type Vector struct {
@ -24,23 +31,31 @@ type Vector struct {
}
func New(pos, vel, acc Vector, fps float64) *Physics {
return &Physics{
motion := Motion{
pos: Position(pos),
vel: Velocity(vel),
acc: Acceleration(acc),
fps: fps,
}
return &Physics{
initial: motion,
current: motion,
fps: fps,
}
}
func (p *Physics) Reset() {
p.current = p.initial
}
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
p.current.pos.Y, p.current.vel.Y = p.current.pos.Y+p.current.vel.Y/p.fps, p.current.vel.Y+p.current.acc.Y/p.fps
p.current.pos.X, p.current.vel.X = p.current.pos.X+p.current.vel.X/p.fps, p.current.vel.X+p.current.acc.X/p.fps
}
func (p Physics) PosX() int {
return int(math.Round(p.pos.X))
return int(math.Round(p.current.pos.X))
}
func (p Physics) PosY() int {
return int(math.Round(p.pos.Y))
return int(math.Round(p.current.pos.Y))
}