Created
July 24, 2017 12:41
-
-
Save peterhellberg/b6fa45daf6eaa3c5576ffdea701953d7 to your computer and use it in GitHub Desktop.
pico-go particle effect https://github.com/telecoda/pico-go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package code | |
import ( | |
"math" | |
"math/rand" | |
"time" | |
"github.com/telecoda/pico-go/console" | |
) | |
var colors = []console.Color{ | |
console.PICO8_BLUE, | |
console.PICO8_GREEN, | |
console.PICO8_INDIGO, | |
console.PICO8_ORANGE, | |
console.PICO8_PEACH, | |
console.PICO8_PINK, | |
} | |
type cartridge struct { | |
*console.BaseCartridge | |
particles []*particle | |
last time.Time | |
} | |
func NewCart() console.Cartridge { | |
return &cartridge{ | |
BaseCartridge: console.NewBaseCart(), | |
} | |
} | |
func (c *cartridge) Init() { | |
c.particles = []*particle{} | |
c.last = time.Now() | |
explode := func(x, y int) { | |
n := 4 + rand.Intn(15) | |
for i := 0; i < 1+rand.Intn(3); i++ { | |
color := colors[rand.Intn(len(colors))] | |
for i := 0; i < n; i++ { | |
angle := 90.0 + rand.Float64()*180.0*flip() | |
speed := (1.1 * float64(n)) + (float64(i) * rand.Float64()) | |
life := 0.5 + (1.3 * rand.Float64()) | |
c.particles = append(c.particles, | |
newParticle(float64(x), float64(y), angle, speed, life, color), | |
) | |
} | |
} | |
} | |
go func() { | |
for { | |
time.Sleep(300 * time.Millisecond) | |
explode( | |
rand.Intn(c.GetConfig().ConsoleWidth), | |
rand.Intn(c.GetConfig().ConsoleHeight), | |
) | |
} | |
}() | |
} | |
func (c *cartridge) Update() { | |
dt := time.Since(c.last).Seconds() | |
c.last = time.Now() | |
aliveParticles := []*particle{} | |
for _, p := range c.particles { | |
p.update(dt) | |
if p.life > 0 { | |
aliveParticles = append(aliveParticles, p) | |
} | |
} | |
c.particles = aliveParticles | |
} | |
func (c *cartridge) Render() { | |
c.ClsWithColor(console.PICO8_BLACK) | |
for _, p := range c.particles { | |
c.PSetWithColor(int(p.position.X), int(p.position.Y), p.color) | |
} | |
} | |
type vec struct { | |
X, Y float64 | |
} | |
type particle struct { | |
angle float64 | |
speed float64 | |
position vec | |
velocity vec | |
color console.Color | |
life float64 | |
} | |
func newParticle(x, y, angle, speed, life float64, c console.Color) *particle { | |
angleInRadians := angle * math.Pi / 180 | |
return &particle{ | |
angle: angle, | |
speed: speed, | |
position: vec{x, y}, | |
velocity: vec{ | |
X: speed * math.Cos(angleInRadians), | |
Y: -speed * math.Sin(angleInRadians), | |
}, | |
life: life, | |
color: c, | |
} | |
} | |
func (p *particle) update(dt float64) { | |
p.life -= dt | |
if p.life > 0 { | |
p.position.X += p.velocity.X * dt | |
p.position.Y += p.velocity.Y * dt | |
if p.life < 0.2 { | |
p.color = console.PICO8_DARK_GRAY | |
} | |
} | |
} | |
func flip() float64 { | |
if rand.Float64() > 0.5 { | |
return 1.0 | |
} | |
return -1.0 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The current version of pico-go (telecoda/pico-go@7aeb8f3) needs this patch: