diff --git a/assets/scripts/asteroids.lua b/assets/scripts/asteroids.lua index e9b422e..b028442 100644 --- a/assets/scripts/asteroids.lua +++ b/assets/scripts/asteroids.lua @@ -29,6 +29,7 @@ function asteroids.update(dt, game) for key, asteroid in pairs(asteroids.bodys) do if asteroid.x + asteroid.img:getWidth() < 0 then table.remove(asteroids.bodys, key) + game.collisions:remove('asteroid' .. key) end end -- Create asteroids @@ -55,6 +56,14 @@ function make_asteroid(pos, game, x_random) if x_random then asteroids.bodys[pos].x = math.random(0, game.canvas.width - temp_img:getWidth()) end + -- Collision + game.collisions:add( + 'asteroid' .. pos, + asteroids.bodys[pos].x, + asteroids.bodys[pos].y, + asteroids.bodys[pos].img:getWidth(), + asteroids.bodys[pos].img:getHeight() + ) end return asteroids \ No newline at end of file diff --git a/assets/scripts/spaceship.lua b/assets/scripts/spaceship.lua index d3d3002..7701e39 100644 --- a/assets/scripts/spaceship.lua +++ b/assets/scripts/spaceship.lua @@ -1,6 +1,7 @@ local anim8 = require 'assets/scripts/vendor/anim8' local spaceship = {} +local collision_debug = true spaceship.y = 0 local press_button = false @@ -29,9 +30,24 @@ function spaceship.load(game) g = anim8.newGrid(light.width, light.height, light.img:getWidth(), light.img:getHeight()) light.animation = anim8.newAnimation(g('1-' .. light.num_frames, 1), 0.05) -- Collision - body.collision = {x=body.body:getX() + 40, y=body.body:getY()} - game.collisions:add({name='spaceship'}, body.collision.x, body.collision.y, body.width, body.height) - game.collisions:move({name='spaceship'}, body.collision.x, body.collision.y) + body.collision = {} + body.collision[1] = {x=66, y=35, width=28, height=5, name={name='spaceship_1'}} + body.collision[2] = {x=56, y=40, width=48, height=5, name={name='spaceship_2'}} + body.collision[3] = {x=54, y=45, width=54, height=10, name={name='spaceship_3'}} + body.collision[4] = {x=50, y=55, width=60, height=10, name={name='spaceship_4'}} + body.collision[5] = {x=46, y=65, width=66, height=10, name={name='spaceship_5'}} + body.collision[6] = {x=44, y=75, width=70, height=10, name={name='spaceship_6'}} + body.collision[7] = {x=40, y=85, width=78, height=20, name={name='spaceship_7'}} + body.collision[8] = {x=53, y=105, width=50, height=4, name={name='spaceship_8'}} + for key, value in pairs(body.collision) do + game.collisions:add( + body.collision[key].name, + body.body:getX() + body.collision[key].x, + body.body:getY() + body.collision[key].y, + body.collision[key].width, + body.collision[key].height + ) + end end function spaceship.update(dt, game) @@ -59,17 +75,33 @@ function spaceship.update(dt, game) love.event.push('quit') end -- Collision - --game.collisions:move({name='spaceship'}, body.collision.x, body.collision.y) + for key, value in pairs(body.collision) do + game.collisions:move( + body.collision[key].name, + body.body:getX() + body.collision[key].x, + body.body:getY() + body.collision[key].y + ) + end end function spaceship.draw() - love.graphics.rectangle('fill', body.collision.x, body.collision.y, body.width, body.height) light.animation:draw(light.img, light.x, light.y) if press_button then body.animation_fire:draw(body.img, body.body:getX(), body.body:getY()) else body.animation_stop:draw(body.img, body.body:getX(), body.body:getY()) end + if collision_debug then + for key, value in pairs(body.collision) do + love.graphics.rectangle( + 'fill', + body.body:getX() + body.collision[key].x, + body.body:getY() + body.collision[key].y, + body.collision[key].width, + body.collision[key].height + ) + end + end end return spaceship \ No newline at end of file