Shooting firework effect

This commit is contained in:
MrMelon 2021-10-31 18:57:51 +00:00 committed by Maas Lalani
parent 1373a345bc
commit ec2c5d6128
6 changed files with 113 additions and 39 deletions

View File

@ -4,7 +4,6 @@ import (
"math/rand" "math/rand"
"time" "time"
"github.com/charmbracelet/harmonica"
"github.com/maaslalani/confetty/array" "github.com/maaslalani/confetty/array"
"github.com/maaslalani/confetty/simulation" "github.com/maaslalani/confetty/simulation"
@ -42,11 +41,11 @@ func Spawn(width, height int) []simulation.Particle {
y := float64(0) y := float64(0)
p := simulation.Particle{ p := simulation.Particle{
Physics: harmonica.NewProjectile( Physics: simulation.NewProjectile(
harmonica.FPS(framesPerSecond), simulation.FPS(framesPerSecond),
harmonica.Point{X: x + (float64(width/4) * (rand.Float64() - 0.5)), Y: y, Z: 0}, simulation.Point{X: x + (float64(width/4) * (rand.Float64() - 0.5)), Y: y, Z: 0},
harmonica.Vector{X: (rand.Float64() - 0.5) * 100, Y: rand.Float64() * 50, Z: 0}, simulation.Vector{X: (rand.Float64() - 0.5) * 100, Y: rand.Float64() * 50, Z: 0},
harmonica.Vector(harmonica.TerminalGravity), simulation.Vector(simulation.TerminalGravity),
), ),
Char: lipgloss.NewStyle(). Char: lipgloss.NewStyle().
Foreground(lipgloss.Color(array.Sample(colors))). Foreground(lipgloss.Color(array.Sample(colors))).

View File

@ -5,7 +5,6 @@ import (
"math/rand" "math/rand"
"time" "time"
"github.com/charmbracelet/harmonica"
"github.com/maaslalani/confetty/array" "github.com/maaslalani/confetty/array"
"github.com/maaslalani/confetty/simulation" "github.com/maaslalani/confetty/simulation"
@ -21,6 +20,8 @@ const (
var ( var (
colors = []string{"#a864fd", "#29cdff", "#78ff44", "#ff718d", "#fdff6a"} colors = []string{"#a864fd", "#29cdff", "#78ff44", "#ff718d", "#fdff6a"}
characters = []string{"+", "*", "•"} characters = []string{"+", "*", "•"}
head_char = "▄"
tail_char = "│"
) )
type frameMsg time.Time type frameMsg time.Time
@ -35,24 +36,43 @@ type model struct {
system *simulation.System system *simulation.System
} }
func Spawn(width, height int) []simulation.Particle { func SpawnShoot(width, height int) simulation.Particle {
color := lipgloss.Color(array.Sample(colors))
v := float64(rand.Intn(15) + 15.0)
x := rand.Float64() * float64(width)
p := simulation.Particle{
Physics: simulation.NewProjectile(
simulation.FPS(framesPerSecond),
simulation.Point{X: x, Y: float64(height)},
simulation.Vector{X: 0, Y: -v},
simulation.Vector(simulation.TerminalGravity),
),
Char: lipgloss.NewStyle().Foreground(color).Render(head_char),
TailChar: lipgloss.NewStyle().Foreground(color).Render(tail_char),
Shooting: true,
ExplosionCall: SpawnExplosion,
}
return p
}
func SpawnExplosion(x, y float64, width, height int) []simulation.Particle {
color := lipgloss.Color(array.Sample(colors)) color := lipgloss.Color(array.Sample(colors))
v := float64(rand.Intn(10) + 20.0) v := float64(rand.Intn(10) + 20.0)
particles := []simulation.Particle{} particles := []simulation.Particle{}
x := rand.Float64() * float64(width)
y := rand.Float64() * float64(height)
for i := 0; i < numParticles; i++ { for i := 0; i < numParticles; i++ {
p := simulation.Particle{ p := simulation.Particle{
Physics: harmonica.NewProjectile( Physics: simulation.NewProjectile(
harmonica.FPS(framesPerSecond), simulation.FPS(framesPerSecond),
harmonica.Point{X: x, Y: y}, simulation.Point{X: x, Y: y},
harmonica.Vector{X: math.Cos(float64(i)) * v, Y: math.Sin(float64(i)) * v / 2}, simulation.Vector{X: math.Cos(float64(i)) * v, Y: math.Sin(float64(i)) * v / 2},
harmonica.Vector(harmonica.TerminalGravity), simulation.Vector(simulation.TerminalGravity),
), ),
Char: lipgloss.NewStyle().Foreground(color).Render(array.Sample(characters)), Char: lipgloss.NewStyle().Foreground(color).Render(array.Sample(characters)),
Shooting: false,
} }
particles = append(particles, p) particles = append(particles, p)
} }
@ -61,7 +81,7 @@ func Spawn(width, height int) []simulation.Particle {
func InitialModelWithSize(width, height int) model { func InitialModelWithSize(width, height int) model {
return model{system: &simulation.System{ return model{system: &simulation.System{
Particles: Spawn(width, height), Particles: []simulation.Particle{SpawnShoot(width, height)},
Frame: simulation.Frame{ Frame: simulation.Frame{
Width: width, Width: width,
Height: height, Height: height,
@ -83,7 +103,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
case "ctrl+c", "q": case "ctrl+c", "q":
return m, tea.Quit return m, tea.Quit
} }
m.system.Particles = append(m.system.Particles, Spawn(m.system.Frame.Width, m.system.Frame.Height)...) m.system.Particles = append(m.system.Particles, SpawnShoot(m.system.Frame.Width, m.system.Frame.Height))
return m, nil return m, nil
case frameMsg: case frameMsg:

4
go.mod
View File

@ -4,10 +4,8 @@ go 1.16
require ( require (
github.com/charmbracelet/bubbletea v0.19.0 github.com/charmbracelet/bubbletea v0.19.0
github.com/charmbracelet/harmonica v0.1.1-0.20211027165653-ff7be1c0cd31
github.com/charmbracelet/lipgloss v0.4.0 github.com/charmbracelet/lipgloss v0.4.0
github.com/containerd/console v1.0.3 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect github.com/mattn/go-isatty v0.0.14 // indirect
golang.org/x/sys v0.0.0-20211025201205-69cdffdb9359 // indirect golang.org/x/sys v0.0.0-20211031064116-611d5d643895 // indirect
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 golang.org/x/term v0.0.0-20210927222741-03fcf44c2211
) )

10
go.sum
View File

@ -1,12 +1,9 @@
github.com/charmbracelet/bubbletea v0.19.0 h1:1gz4rbxl3qZik/oP8QW2vUtul2gO8RDDzmoLGERpTQc= github.com/charmbracelet/bubbletea v0.19.0 h1:1gz4rbxl3qZik/oP8QW2vUtul2gO8RDDzmoLGERpTQc=
github.com/charmbracelet/bubbletea v0.19.0/go.mod h1:VuXF2pToRxDUHcBUcPmCRUHRvFATM4Ckb/ql1rBl3KA= github.com/charmbracelet/bubbletea v0.19.0/go.mod h1:VuXF2pToRxDUHcBUcPmCRUHRvFATM4Ckb/ql1rBl3KA=
github.com/charmbracelet/harmonica v0.1.1-0.20211027165653-ff7be1c0cd31 h1:SDIjOIg+gsaRt3vmfQ3hIP6KT4PCC2/ARbTU5hiy/Gg=
github.com/charmbracelet/harmonica v0.1.1-0.20211027165653-ff7be1c0cd31/go.mod h1:KSri/1RMQOZLbw7AHqgcBycp8pgJnQMYYT8QZRqZ1Ao=
github.com/charmbracelet/lipgloss v0.4.0 h1:768h64EFkGUr8V5yAKV7/Ta0NiVceiPaV+PphaW1K9g= github.com/charmbracelet/lipgloss v0.4.0 h1:768h64EFkGUr8V5yAKV7/Ta0NiVceiPaV+PphaW1K9g=
github.com/charmbracelet/lipgloss v0.4.0/go.mod h1:vmdkHvce7UzX6xkyf4cca8WlwdQ5RQr8fzta+xl7BOM= github.com/charmbracelet/lipgloss v0.4.0/go.mod h1:vmdkHvce7UzX6xkyf4cca8WlwdQ5RQr8fzta+xl7BOM=
github.com/containerd/console v1.0.2 h1:Pi6D+aZXM+oUw1czuKgH5IJ+y0jhYcwBJfx5/Ghn9dE=
github.com/containerd/console v1.0.2/go.mod h1:ytZPjGgY2oeTkAONYafi2kSj0aYggsf8acV1PGKCbzQ= github.com/containerd/console v1.0.2/go.mod h1:ytZPjGgY2oeTkAONYafi2kSj0aYggsf8acV1PGKCbzQ=
github.com/containerd/console v1.0.3 h1:lIr7SlA5PxZyMV30bDW0MGbiOPXwc63yRuCP0ARubLw=
github.com/containerd/console v1.0.3/go.mod h1:7LqA/THxQ86k76b8c/EMSiaJ3h1eZkMkXar0TQ1gf3U=
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY= github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0= github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
github.com/mattn/go-isatty v0.0.13/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= github.com/mattn/go-isatty v0.0.13/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
@ -23,6 +20,7 @@ github.com/muesli/reflow v0.3.0 h1:IFsN6K9NfGtjeggFP+68I4chLZV2yIKsXJFNZ+eWh6s=
github.com/muesli/reflow v0.3.0/go.mod h1:pbwTDkVPibjO2kyvBQRBxTWEEGDGq0FlB1BIKtnHY/8= github.com/muesli/reflow v0.3.0/go.mod h1:pbwTDkVPibjO2kyvBQRBxTWEEGDGq0FlB1BIKtnHY/8=
github.com/muesli/termenv v0.9.0 h1:wnbOaGz+LUR3jNT0zOzinPnyDaCZUQRZj9GxK8eRVl8= github.com/muesli/termenv v0.9.0 h1:wnbOaGz+LUR3jNT0zOzinPnyDaCZUQRZj9GxK8eRVl8=
github.com/muesli/termenv v0.9.0/go.mod h1:R/LzAKf+suGs4IsO95y7+7DpFHO0KABgnZqtlyx2mBw= github.com/muesli/termenv v0.9.0/go.mod h1:R/LzAKf+suGs4IsO95y7+7DpFHO0KABgnZqtlyx2mBw=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY= github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
@ -33,8 +31,8 @@ golang.org/x/sys v0.0.0-20210119212857-b64e53b001e4/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211025201205-69cdffdb9359 h1:2B5p2L5IfGiD7+b9BOoRMC6DgObAVZV+Fsp050NqXik= golang.org/x/sys v0.0.0-20211031064116-611d5d643895 h1:iaNpwpnrgL5jzWS0vCNnfa8HqzxveCFpFx3uC/X4Tps=
golang.org/x/sys v0.0.0-20211025201205-69cdffdb9359/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20211031064116-611d5d643895/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20210422114643-f5beecf764ed/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20210422114643-f5beecf764ed/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 h1:JGgROgKl9N8DuW20oFS5gxc+lE67/N3FcwmBPMe7ArY= golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 h1:JGgROgKl9N8DuW20oFS5gxc+lE67/N3FcwmBPMe7ArY=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=

33
simulation/projectile.go Normal file
View File

@ -0,0 +1,33 @@
package simulation
type Projectile struct {
Position Point
Velocity Vector
Acceleration Vector
DeltaTime float64
}
type Point struct {
X, Y, Z float64
}
type Vector struct {
X, Y, Z float64
}
var Gravity = Vector{0, -9.81, 0}
var TerminalGravity = Vector{0, 9.81, 0}
func NewProjectile(del float64, pos Point, vel, acc Vector) *Projectile {
return &Projectile{pos, vel, acc, del}
}
func (p *Projectile) Update() {
p.Position.X += (p.Velocity.X * p.DeltaTime)
p.Position.Y += (p.Velocity.Y * p.DeltaTime)
p.Position.Z += (p.Velocity.Z * p.DeltaTime)
p.Velocity.X += (p.Acceleration.X * p.DeltaTime)
p.Velocity.Y += (p.Acceleration.Y * p.DeltaTime)
p.Velocity.Z += (p.Acceleration.Z * p.DeltaTime)
}

View File

@ -3,8 +3,7 @@ package simulation
import ( import (
"fmt" "fmt"
"strings" "strings"
"time"
"github.com/charmbracelet/harmonica"
) )
type System struct { type System struct {
@ -14,8 +13,11 @@ type System struct {
type Particle struct { type Particle struct {
Char string Char string
Physics *harmonica.Projectile TailChar string
Physics *Projectile
Hidden bool Hidden bool
Shooting bool
ExplosionCall func(x, y float64, width, height int) []Particle
} }
type Frame struct { type Frame struct {
@ -23,6 +25,10 @@ type Frame struct {
Height int Height int
} }
func FPS(n int) float64 {
return (time.Second / time.Duration(n)).Seconds()
}
func RemoveParticleFromArray(s []Particle, i int) []Particle { func RemoveParticleFromArray(s []Particle, i int) []Particle {
s[i] = s[len(s)-1] s[i] = s[len(s)-1]
return s[:len(s)-1] return s[:len(s)-1]
@ -30,8 +36,19 @@ func RemoveParticleFromArray(s []Particle, i int) []Particle {
func (s *System) Update() { func (s *System) Update() {
for i := len(s.Particles) - 1; i >= 0; i-- { for i := len(s.Particles) - 1; i >= 0; i-- {
p := s.Particles[i].Physics.Position() p := s.Particles[i]
if p.X > float64(s.Frame.Width) || p.X < 0 || p.Y > float64(s.Frame.Height) { pos := p.Physics.Position
// if the shooting particle is slow enough then hide it and call the explosion function
if !p.Hidden && p.Shooting && p.Physics.Velocity.Y > -3 {
p.Hidden = true
if p.ExplosionCall != nil {
s.Particles = append(s.Particles, p.ExplosionCall(p.Physics.Position.X, p.Physics.Position.Y, s.Frame.Width, s.Frame.Height)...)
}
}
// remove particles that are hidden or out of the side/bottom of the frame
if p.Hidden || pos.X > float64(s.Frame.Width) || pos.X < 0 || pos.Y > float64(s.Frame.Height) {
s.Particles = RemoveParticleFromArray(s.Particles, i) s.Particles = RemoveParticleFromArray(s.Particles, i)
} else { } else {
s.Particles[i].Physics.Update() s.Particles[i].Physics.Update()
@ -40,9 +57,9 @@ func (s *System) Update() {
} }
func (s *System) Visible(p Particle) bool { func (s *System) Visible(p Particle) bool {
y := int(p.Physics.Position().Y) y := int(p.Physics.Position.Y)
x := int(p.Physics.Position().X) x := int(p.Physics.Position.X)
return y >= 0 && y < s.Frame.Height-1 && x >= 0 && x < s.Frame.Width-1 return !p.Hidden && y >= 0 && y < s.Frame.Height-1 && x >= 0 && x < s.Frame.Width-1
} }
func (s *System) Render() string { func (s *System) Render() string {
@ -53,7 +70,16 @@ func (s *System) Render() string {
} }
for _, p := range s.Particles { for _, p := range s.Particles {
if s.Visible(p) { if s.Visible(p) {
plane[int(p.Physics.Position().Y)][int(p.Physics.Position().X)] = p.Char plane[int(p.Physics.Position.Y)][int(p.Physics.Position.X)] = p.Char
if p.Shooting {
l := -int(p.Physics.Velocity.Y)
for i := 1; i < l; i++ {
y := int(p.Physics.Position.Y) + i
if y > 0 && y < s.Frame.Height-1 {
plane[y][int(p.Physics.Position.X)] = p.TailChar
}
}
}
} }
} }
for i := range plane { for i := range plane {