mike: Rewrote paddle col detection; removed comments; less reliance on global variables
This commit is contained in:
parent
ff386b2c75
commit
beb04e1451
@ -12,6 +12,9 @@ function screen_col_detect(object, display)
|
|||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function create_object(start_x, start_y, width, height, speed)
|
function create_object(start_x, start_y, width, height, speed)
|
||||||
local object = {}
|
local object = {}
|
||||||
object.x = start_x
|
object.x = start_x
|
||||||
@ -29,7 +32,36 @@ function num_is_pos(number)
|
|||||||
end
|
end
|
||||||
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
|
local paddle_hit = false
|
||||||
ball.x = ball.x + ball.x_speed
|
ball.x = ball.x + ball.x_speed
|
||||||
ball.y = ball.y + ball.y_speed
|
ball.y = ball.y + ball.y_speed
|
||||||
@ -45,25 +77,27 @@ function ball_bounce(ball)
|
|||||||
end
|
end
|
||||||
|
|
||||||
--If ball hits paddle
|
--If ball hits paddle
|
||||||
if ball.y + 1 >= player.y and
|
|
||||||
ball.x >= player.x and
|
if hitbox_collision(ball.x, ball.y, ball.width, ball.height,
|
||||||
ball.x <= player.x + player.width then
|
player.x, player.y, player.width, player.height) then
|
||||||
ball.y_speed = ball.y_speed *-1
|
|
||||||
paddle_hit = true
|
|
||||||
end
|
|
||||||
|
|
||||||
if paddle_hit == true then
|
|
||||||
if ball.x < player.x + (player.width/2) and
|
-- We don't need to check left limits because we already know
|
||||||
num_is_pos(ball.x_speed) then
|
-- there was a collision, so just bounce left if the ball is
|
||||||
ball.x_speed = ball.x_speed *-1
|
-- left of center on the paddle
|
||||||
elseif ball.x > player.x + (player.width/2) and
|
if ball.x < player.x + (player.width /2) then
|
||||||
not num_is_pos(ball.x_speed) then
|
bounce("left", ball)
|
||||||
ball.x_speed = ball.x_speed *-1
|
|
||||||
end
|
-- If the ball is on the right side, bounce right
|
||||||
end
|
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
|
return ball
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -33,7 +33,7 @@ function half_speed(object_speed)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function check_keys(object)
|
function check_keys(object, screen)
|
||||||
local newObject = {}
|
local newObject = {}
|
||||||
newObject.x = object.x
|
newObject.x = object.x
|
||||||
newObject.y = object.y
|
newObject.y = object.y
|
||||||
@ -49,14 +49,6 @@ function check_keys(object)
|
|||||||
newObject.x = newObject.x - half_speed(newObject.speed)
|
newObject.x = newObject.x - half_speed(newObject.speed)
|
||||||
end
|
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
|
if screen_col_detect(newObject, screen) then
|
||||||
return newObject
|
return newObject
|
||||||
else
|
else
|
||||||
@ -64,3 +56,5 @@ function check_keys(object)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
--Inspired by https://love2d.org/wiki/BoundingBox.lua
|
||||||
|
--Returns true if boxes overlap
|
||||||
|
@ -10,18 +10,18 @@ function love.load()
|
|||||||
screen.height = love.graphics.getHeight()
|
screen.height = love.graphics.getHeight()
|
||||||
|
|
||||||
-- Create a table to hold all the player data
|
-- 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
|
-- Create a table to hold ball data
|
||||||
ball = create_object(screen.width /2, screen.height /2, 1, 1, 10)
|
ball = create_object(screen.width /2, screen.height /2, 1, 1, 10)
|
||||||
ball.x_speed = ball.speed /2
|
ball.x_speed = (ball.speed /2)
|
||||||
ball.y_speed = ball.speed /2
|
ball.y_speed = (ball.speed /2)
|
||||||
|
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function love.update(dt)
|
function love.update(dt)
|
||||||
player = check_keys(player)
|
player = check_keys(player, screen)
|
||||||
ball = ball_bounce(ball)
|
ball = ball_bounce(player, ball)
|
||||||
end
|
end
|
||||||
|
|
||||||
function love.draw()
|
function love.draw()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user