Add collision

This commit is contained in:
Andros Fenollosa 2016-10-17 23:41:41 +02:00
parent 784c64d47e
commit e17d7b8139

View File

@ -4,15 +4,41 @@ function table_length(T)
return count return count
end end
function love.load(arg) local clock = os.clock
function sleep(n) -- seconds
local t0 = clock()
while clock() - t0 <= n do end
end
-- Collision detection function.
-- Returns true if two boxes overlap, false if they don't
-- x1,y1 are the left-top coords of the first box, while w1,h1 are its width and height
-- x2,y2,w2 & h2 are the same, but for the second box
function CheckCollision(x1,y1,w1,h1, x2,y2,w2,h2)
return x1 < x2+w2 and
x2 < x1+w1 and
y1 < y2+h2 and
y2 < y1+h1
end
function love.load()
-- Configuración -- Configuración
math.randomseed(os.time()) math.randomseed(os.time())
love.window.setMode(1280, 720, {resizable=false}) camara_width = 1280
love.window.setTitle( 'Alunizaje' ) camara_height = 720
debug = false
play = true
win = false
font = love.graphics.newFont(40)
love.graphics.setFont(font)
love.window.setMode(camara_width, camara_height, {resizable=false})
-- love.window.setFullscreen(true)
love.window.setTitle('Alunizaje')
fondo = { x = 0, y = 0, img = love.graphics.newImage('assets/img/fondo.jpg') } fondo = { x = 0, y = 0, img = love.graphics.newImage('assets/img/fondo.jpg') }
game_height = 2880 game_height = 2880
game_width = 1280 game_width = 1280
gravedad = 2 gravedad = 2
distancia_luna = 100
-- Fisicas -- Fisicas
love.physics.setMeter(64) -- Altura del mundo en metros love.physics.setMeter(64) -- Altura del mundo en metros
world = love.physics.newWorld(0, gravedad * 64, true) -- Crea el mundo físico world = love.physics.newWorld(0, gravedad * 64, true) -- Crea el mundo físico
@ -40,7 +66,7 @@ function love.load(arg)
for i=1, num_asteroides do for i=1, num_asteroides do
temp_img = img_asteroide[math.random(1, table_length(img_asteroide))] temp_img = img_asteroide[math.random(1, table_length(img_asteroide))]
asteroides[i] = { x = math.random(0, game_width - temp_img:getWidth()), asteroides[i] = { x = math.random(0, game_width - temp_img:getWidth()),
y = math.random(0, game_height - temp_img:getHeight()), y = math.random(200, game_height - temp_img:getHeight()),
speed = math.random(1, max_speed), speed = math.random(1, max_speed),
img = temp_img, img = temp_img,
angle = math.random(0, 90)} angle = math.random(0, 90)}
@ -48,6 +74,7 @@ function love.load(arg)
end end
function love.update(dt) function love.update(dt)
if play then
-- Fisicas del mundo -- Fisicas del mundo
world:update(dt) world:update(dt)
@ -93,17 +120,49 @@ function love.update(dt)
img = temp_img, img = temp_img,
angle = math.random(0, 90)} angle = math.random(0, 90)}
end end
-- Camara
camara_y = 0
if camara_height / 2 <= nave.body:getY() then -- Superior
camara_y = -nave.body:getY() + (camara_height / 2)
end
if game_height - camara_height / 2 < nave.body:getY() then --Inferior
camara_y = -game_height + camara_height
end
-- Colision
if nave.body:getY() <= 0 then -- Arriba del juego
x, y = nave.body:getLinearVelocity()
nave.body:setLinearVelocity(x, -y)
end
if nave.body:getX() + nave.img:getWidth() > game_width then -- Derecha del juego
x, y = nave.body:getLinearVelocity()
nave.body:setLinearVelocity(-x, y)
end
if nave.body:getX() < 0 then -- Izquierda del juego
x, y = nave.body:getLinearVelocity()
nave.body:setLinearVelocity(-x, y)
end
if nave.body:getY() + nave.img:getHeight() + distancia_luna >= game_height then -- Abajo del juego
nave.body:setLinearVelocity(0, 0)
win = true
play = false
end
for key, value in pairs(asteroides) do -- Con asteroides
if CheckCollision(asteroides[key].x - (asteroides[key].img:getWidth() / 2), asteroides[key].y - (asteroides[key].img:getHeight() / 2), asteroides[key].img:getWidth(), asteroides[key].img:getHeight(), nave.body:getX(), nave.body:getY(), nave.img:getWidth(), nave.img:getHeight()) then
play = false
end
end
else
sleep(2)
love.load()
end
end end
function love.draw() function love.draw()
-- Camara -- Camara
camara_y = -nave.body:getY() + (game_height / 8) - nave.img:getHeight()
if (game_height / 8) - nave.img:getHeight() <= nave.body:getY() then
love.graphics.translate(0, camara_y) love.graphics.translate(0, camara_y)
end
if game_height - (game_height / 6) < nave.body:getY() then
love.graphics.translate(0, -game_height)
end
-- Fondo -- Fondo
love.graphics.draw(fondo.img, fondo.x, fondo.y) love.graphics.draw(fondo.img, fondo.x, fondo.y)
@ -111,9 +170,22 @@ function love.draw()
-- Asteroides -- Asteroides
for key, value in pairs(asteroides) do for key, value in pairs(asteroides) do
love.graphics.draw(value.img, value.x, value.y, value.angle, 1, 1, value.img:getWidth() / 2, value.img:getHeight() / 2) love.graphics.draw(value.img, value.x, value.y, value.angle, 1, 1, value.img:getWidth() / 2, value.img:getHeight() / 2)
if debug then
love.graphics.rectangle("line", value.x - (value.img:getWidth() / 2), value.y - (value.img:getHeight() / 2), value.img:getWidth(), value.img:getHeight())
end
end end
-- Nave -- Nave
love.graphics.draw(nave.img, nave.body:getX(), nave.body:getY()) love.graphics.draw(nave.img, nave.body:getX(), nave.body:getY())
end if debug then
love.graphics.rectangle("line", nave.body:getX(), nave.body:getY(), nave.img:getWidth(), nave.img:getHeight())
end
-- Textos
if not play and not win then
love.graphics.print('Game over', (camara_width / 2), -camara_y + (camara_height / 2))
end
if not play and win then
love.graphics.print('You win!', (camara_width / 2), -camara_y + (camara_height / 2))
end
end