Skip to content

Commit

Permalink
Brick particles
Browse files Browse the repository at this point in the history
  • Loading branch information
yeoji committed May 11, 2020
1 parent 5d6b405 commit a881139
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 34 deletions.
76 changes: 47 additions & 29 deletions breakout/objects/brick.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"games50-go/internal/particles"
"image"
"image/color"
"time"

"github.com/hajimehoshi/ebiten"
)
Expand All @@ -25,6 +24,14 @@ func (t brickTier) string() string {
return []string{"basic", "extra", "super", "ultra"}[t]
}

var paletteColour = map[colour]color.Color{
Blue: color.RGBA{99, 155, 255, 255},
Green: color.RGBA{106, 190, 47, 255},
Red: color.RGBA{217, 87, 99, 255},
Purple: color.RGBA{215, 123, 186, 255},
Yellow: color.RGBA{251, 242, 54, 255},
}

type Brick struct {
x float64
y float64
Expand All @@ -36,35 +43,15 @@ type Brick struct {
}

func NewBrick(x float64, y float64, tier brickTier, colour colour) Brick {
pEmitter := particles.ParticleEmitter{
ParticleImage: assets.GetSprite("particles", "brick-explode"),
Config: particles.ParticleEmitterConfig{
MaxParticles: 64,
EmitterLife: 600 * time.Millisecond,
Lifetime: particles.Range{Min: 0.5, Max: 1},
Acceleration: particles.Acceleration{MinX: -15, MinY: 0, MaxX: 15, MaxY: 80},
Spawn: particles.Spawn{
SpawnType: particles.SpawnTypeRect,
SpawnRect: particles.SpawnRect{
Height: 40,
Width: 30,
Offset: particles.Position{X: -10, Y: -20},
},
Frequency: 10 * time.Millisecond,
Position: particles.Position{X: x + 16, Y: y + 8},
},
Colours: []color.Color{color.RGBA{106, 190, 47, 110}, color.RGBA{106, 190, 47, 0}},
},
}

return Brick{
x: x,
y: y,
tier: tier,
colour: colour,
InPlay: true,
pEmitter: &pEmitter,
b := Brick{
x: x,
y: y,
tier: tier,
colour: colour,
InPlay: true,
}
b.newParticleEmitter()
return b
}

func (b *Brick) Update() {
Expand All @@ -74,6 +61,12 @@ func (b *Brick) Update() {
func (b *Brick) Hit() {
assets.PlaySound("brick_hit")

paletteR, paletteG, paletteB, _ := paletteColour[b.colour].RGBA()
paletteR, paletteG, paletteB = paletteR/0x101, paletteG/0x101, paletteB/0x101
b.pEmitter.SetColours([]color.Color{
color.RGBA{uint8(paletteR), uint8(paletteG), uint8(paletteB), uint8(55 * (int(b.tier) + 1))},
color.RGBA{uint8(paletteR), uint8(paletteG), uint8(paletteB), 0},
})
b.pEmitter.Emit()

if b.Locked {
Expand Down Expand Up @@ -112,10 +105,35 @@ func (b *Brick) Render(screen *ebiten.Image) {
screen.DrawImage(assets.GetSprite(fmt.Sprintf("bricks-%s", b.colour.string()), b.tier.string()), brickOptions)
}
}
}

func (b *Brick) RenderParticles(screen *ebiten.Image) {
b.pEmitter.Render(screen)
}

func (b *Brick) BoundingBox() image.Rectangle {
return image.Rect(int(b.x), int(b.y), int(b.x)+constants.BrickWidth, int(b.y)+constants.BrickHeight)
}

func (b *Brick) newParticleEmitter() {
paletteR, paletteG, paletteB, _ := paletteColour[b.colour].RGBA()
paletteR, paletteG, paletteB = paletteR/0x101, paletteG/0x101, paletteB/0x101

b.pEmitter = &particles.ParticleEmitter{
ParticleImage: assets.GetSprite("particles", "brick-explode"),
Config: particles.ParticleEmitterConfig{
MaxParticles: 64,
Lifetime: particles.Range{Min: 0.5, Max: 1},
Acceleration: particles.Acceleration{MinX: -70, MinY: 40, MaxX: 70, MaxY: 80},
Spawn: particles.Spawn{
SpawnType: particles.SpawnTypeRect,
SpawnRect: particles.SpawnRect{
Height: 30,
Width: 40,
Offset: particles.Position{X: -20, Y: -15},
},
Position: particles.Position{X: b.x + 16, Y: b.y + 8},
},
},
}
}
2 changes: 1 addition & 1 deletion breakout/objects/level.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func (l *Level) generateLevelBricks() {

lockedBrick := utils.RandomNumInRange(1, 2) == 1
if lockedBrick {
l.Bricks[utils.RandomNumInRange(0, len(l.Bricks))].Locked = true
l.Bricks[utils.RandomNumInRange(0, len(l.Bricks)-1)].Locked = true
}
}

Expand Down
3 changes: 3 additions & 0 deletions breakout/states/play.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,9 @@ func (s *PlayState) Render(screen *ebiten.Image) {
for _, brick := range s.level.Bricks {
brick.Render(screen)
}
for _, brick := range s.level.Bricks {
brick.RenderParticles(screen)
}

renderScore(s.score, screen)
renderHealth(s.health, screen)
Expand Down
4 changes: 4 additions & 0 deletions internal/particles/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,7 @@ type Position struct {
X float64
Y float64
}

func (e *ParticleEmitter) SetColours(colours []color.Color) {
e.Config.Colours = colours
}
3 changes: 0 additions & 3 deletions internal/particles/emitter.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"time"

"github.com/hajimehoshi/ebiten"
"github.com/rs/zerolog/log"
)

type ParticleEmitter struct {
Expand All @@ -26,8 +25,6 @@ func (e *ParticleEmitter) Emit() {
e.stopped = make(chan bool, 1)
e.active = true

log.Printf("Started emitting")

go func() {
for {
select {
Expand Down
9 changes: 8 additions & 1 deletion internal/particles/particle.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type Particle struct {
}

func NewParticle(image *ebiten.Image, lifetime float64, position Position, acceleration Acceleration, colours []color.Color) *Particle {
return &Particle{
p := &Particle{
image: image,
lifetime: lifetime,
remainingLife: lifetime,
Expand All @@ -30,6 +30,13 @@ func NewParticle(image *ebiten.Image, lifetime float64, position Position, accel
accelerationY: utils.RandomFloatInRange(acceleration.MinY, acceleration.MaxY),
colours: colours,
}

// initialize the active colour
if len(p.colours) > 0 {
p.activeColour = p.colours[0]
}

return p
}

func (p *Particle) Update() {
Expand Down

0 comments on commit a881139

Please sign in to comment.