diff --git a/love/mike/lib/functions.lua b/love/mike/lib/functions.lua index 191038b..095368c 100644 --- a/love/mike/lib/functions.lua +++ b/love/mike/lib/functions.lua @@ -12,6 +12,9 @@ function screen_col_detect(object, display) return true end + + + function create_object(start_x, start_y, width, height, speed) local object = {} object.x = start_x @@ -29,7 +32,36 @@ function num_is_pos(number) end end -function ball_bounce(ball) + +-- Inspired by https://love2d.org/wiki/BoundingBox.lua +-- Returns true if boxes overlap + +function hitbox_collision(x1, y1, width1, height1, + x2, y2, width2, height2) + return x1 < x2 + width2 and + x2 < x1 + width1 and + y1 < y2 + height2 and + y2 < y1 + height1 +end + + +--Takes a string "left" or "right" +function bounce(str, ball) + if str == "right" and + not num_is_pos(ball.x_speed) then + ball.x_speed = ball.x_speed *-1 + + elseif + str == "left" and + num_is_pos(ball.x_speed) then + ball.x_speed = ball.x_speed *-1 + end + return ball +end + + + +function ball_bounce(player, ball) local paddle_hit = false ball.x = ball.x + ball.x_speed ball.y = ball.y + ball.y_speed @@ -45,25 +77,27 @@ function ball_bounce(ball) end --If ball hits paddle - if ball.y + 1 >= player.y and - ball.x >= player.x and - ball.x <= player.x + player.width then - ball.y_speed = ball.y_speed *-1 - paddle_hit = true - end + + if hitbox_collision(ball.x, ball.y, ball.width, ball.height, + player.x, player.y, player.width, player.height) then - if paddle_hit == true then - if ball.x < player.x + (player.width/2) and - num_is_pos(ball.x_speed) then - ball.x_speed = ball.x_speed *-1 - elseif ball.x > player.x + (player.width/2) and - not num_is_pos(ball.x_speed) then - ball.x_speed = ball.x_speed *-1 - end - end + + -- We don't need to check left limits because we already know + -- there was a collision, so just bounce left if the ball is + -- left of center on the paddle + if ball.x < player.x + (player.width /2) then + bounce("left", ball) + + -- If the ball is on the right side, bounce right + elseif ball.x > player.x +(player.width /2) then + bounce("right", ball) + end + -- We don't really need to worry about the center + -- so we won't modify it. + + -- Bounce + ball.y_speed = ball.y_speed *-1 + end return ball end - - - diff --git a/love/mike/lib/keys.lua b/love/mike/lib/keys.lua index 00a2503..8edc20c 100644 --- a/love/mike/lib/keys.lua +++ b/love/mike/lib/keys.lua @@ -33,7 +33,7 @@ function half_speed(object_speed) end end -function check_keys(object) +function check_keys(object, screen) local newObject = {} newObject.x = object.x newObject.y = object.y @@ -49,14 +49,6 @@ function check_keys(object) newObject.x = newObject.x - half_speed(newObject.speed) end --- if love.keyboard.isDown("up") then --- newObject.y = newObject.y - newObject.speed --- end - --- if love.keyboard.isDown("down") then --- newObject.y = newObject.y + newObject.speed --- end - if screen_col_detect(newObject, screen) then return newObject else @@ -64,3 +56,5 @@ function check_keys(object) end end +--Inspired by https://love2d.org/wiki/BoundingBox.lua +--Returns true if boxes overlap diff --git a/love/mike/main.lua b/love/mike/main.lua index 903b6c1..3b8a070 100644 --- a/love/mike/main.lua +++ b/love/mike/main.lua @@ -10,18 +10,18 @@ function love.load() screen.height = love.graphics.getHeight() -- Create a table to hold all the player data - player = create_object(screen.width /2, screen.height - 50, 25, 5, 10) + player = create_object(screen.width /2, screen.height - 50, 30, 5, 10) -- Create a table to hold ball data ball = create_object(screen.width /2, screen.height /2, 1, 1, 10) - ball.x_speed = ball.speed /2 - ball.y_speed = ball.speed /2 + ball.x_speed = (ball.speed /2) + ball.y_speed = (ball.speed /2) end function love.update(dt) - player = check_keys(player) - ball = ball_bounce(ball) + player = check_keys(player, screen) + ball = ball_bounce(player, ball) end function love.draw()