Breakout (LUA LÖve 2D) (en)

PowerUp = Class{}

function PowerUp:init(skin)

    — simple positional and dimensional variables

    self.width = 16

    self.height = 16

    — P200504 : Velocity of the PowerUp (going down, a bit slower than ball, in average)

    self.dy = 40

    self.y = 0

    — this will effectively be the color of our ball, and we will index

    — our table of Quads relating to the global block texture using this

    self.skin = skin

end

— PBU200505 : (Inspired by ball.collides)

function PowerUp:spawn(params)

    if params.levelhaskey

    and (not params.isunlocked) then — PBU200506 I don’t want no more key if brick is already unlocked

        if params.powerup.hadmultiball then

            self.skin = 10

        else

            self.skin = math.random(10) — PBU200505 The PowerUps can be the Key

        end  

    else

        if params.powerup.hadmultiball then

            — PBU200506 just change paddle size power up when done

            self.skin = math.random(2) + 4 — Will be powerups 5 and 6 that are the paddle size

        else

            self.skin = math.random(9) — PBU200505 Everything else than “key” (not 10)

        end    

    end    

    self.x = params.currentbrick.x + 8 — PBU200504 : Center of the brick

    self.y = params.currentbrick.y + 16 — PBU200504 : Below the brick

    self.dy = 40  — P200504 : Velocity of the PowerUp (going down, a bit slower than ball, in average)

    — PBU200506 : play sound “Power Ups Spawn”

    gSounds[‘powerupspawn’]:play()

    return true

end

— PBU200505 : PowerUps activation when touched by the paddle

function PowerUp:activate(params)

    — PBU200505 red paddle

    if params.powerup.skin == 1 then

        params.paddle.skin = 3

        — PBU200506 : play sound “paddle color change”

        gSounds[‘paddlecolorchange’]:play()

    end

    — PBU200505 green paddle

    if params.powerup.skin == 2 then

        params.paddle.skin = 2

        — PBU200506 : play sound “paddle color change”

        gSounds[‘paddlecolorchange’]:play()

    end

    — PBU200505 paddle size and width ++

    if params.powerup.skin == 5 and params.paddle.size < 4 then

        params.paddle.size = params.paddle.size + 1

        params.paddle.width = params.paddle.width + 32

                — PBU200506 : play sound “paddle bigger”

                gSounds[‘paddlegetbigger’]:play()

    end

    — PBU200505 paddle size and width —

    if params.powerup.skin == 6 and params.paddle.size > 1 then

        params.paddle.size = params.paddle.size – 1

        params.paddle.width = params.paddle.width – 32

        — PBU200506 : play sound “paddle smaller”

        gSounds[‘paddlegetsmaller’]:play()

    end

    — PBU200505 3 balls — With Powerups skins 3, 4, 7, 8, 9

    — maybe have to do some refactoringt and put the collision code in ball class ???

    if params.powerup.skin == 3

    or params.powerup.skin == 4

    or params.powerup.skin ==  7

    or params.powerup.skin ==  8

    or params.powerup.skin ==  9  then

        params.powerup.hadmultiball = true — PBU200506 Will not go Multiball again

        params.ball[2].inplay = true

        params.ball[3].inplay = true

        params.ball[2].x = params.ball[1].x

        params.ball[3].x = params.ball[1].x

        params.ball[2].y = params.ball[1].y

        params.ball[3].y = params.ball[1].y

        — give ball random starting velocity

        params.ball[2].dx = math.random(-200, 200)

        params.ball[2].dy = math.random(-50, -60)

        params.ball[3].dx = math.random(-200, 200)

        params.ball[3].dy = math.random(-50, -60)

        — PBU200506 : play sound “3 balls”

        gSounds[‘multiball’]:play()

    end

    — PBU200505 Case Of the “key” powerup

    if params.powerup.skin == 10 then

        params.isunlocked = true

        — PBU200506 : play sound “Brick is unlocked”

        gSounds[‘brickisunlocked’]:play()

    end

— Here : todo : manage the other cases

    — PBU200505 : put haspowerup of playstate to false

    self.dy = 0  — P200504 : stopping the powerups, just for checkign behaviour

    params.haspowerup = false

    return true

end

–[[

    PBU200505 : Same “collides” method as for Ball (should refactor code to hav only 1 instance of this ??)

    Expects an argument with a bounding box, be that a paddle or a brick,

    and returns true if the bounding boxes of this and the argument overlap.

]]

function PowerUp:collides(target)

    — first, check to see if the left edge of either is farther to the right

    — than the right edge of the other

    if self.x > target.x + target.width or target.x > self.x + self.width then

        return false

    end

    — then check to see if the bottom edge of either is higher than the top

    — edge of the other

    if self.y > target.y + target.height or target.y > self.y + self.height then

        return false

    end

    — if the above aren’t true, they’re overlapping

    return true

end

— PBU200505  : Places the ball just below the Brick

function PowerUp:reset(params)

    self.x = params.brick.x + 8 — PBU200504 : Center of the brick (8 + 16 + 8)

    self.y = params.brick.y + 16 — PBU200504 : Below the brick

    self.dy = 40  — P200504 : Velocity of the PowerUp (going down, a bit slower than ball, in average)

end

function PowerUp:update(params)

    self.y = self.y + self.dy * params.currentdt — PBU200504 PowerUp falling down

    — PBU200504 : What if PowerUp goes below the floor (not touched by player)

    if self.y > VIRTUAL_HEIGHT then

        — PBU200505 : put haspowerup of playstate to false

        params.haspowerup = false

        self.dy = 0  — P200504 : stopping the powerups, just for checkign behaviour

    end

end

function PowerUp:render(params)

    — gTexture is our global texture for all blocks

    — gPowerUpFrames is a table of quads mapping to each individual PowerUp skin in the texture

    if params.haspowerup then

        love.graphics.draw(gTextures[‘main’], gFrames[‘powerups’][self.skin],

        self.x, self.y)

    end

end