This commit is contained in:
Andros Fenollosa 2016-12-03 22:49:20 +01:00
parent abb1707543
commit bc036967fb
2 changed files with 46 additions and 5 deletions

View File

@ -29,6 +29,7 @@ function asteroids.update(dt, game)
for key, asteroid in pairs(asteroids.bodys) do for key, asteroid in pairs(asteroids.bodys) do
if asteroid.x + asteroid.img:getWidth() < 0 then if asteroid.x + asteroid.img:getWidth() < 0 then
table.remove(asteroids.bodys, key) table.remove(asteroids.bodys, key)
game.collisions:remove('asteroid' .. key)
end end
end end
-- Create asteroids -- Create asteroids
@ -55,6 +56,14 @@ function make_asteroid(pos, game, x_random)
if x_random then if x_random then
asteroids.bodys[pos].x = math.random(0, game.canvas.width - temp_img:getWidth()) asteroids.bodys[pos].x = math.random(0, game.canvas.width - temp_img:getWidth())
end 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 end
return asteroids return asteroids

View File

@ -1,6 +1,7 @@
local anim8 = require 'assets/scripts/vendor/anim8' local anim8 = require 'assets/scripts/vendor/anim8'
local spaceship = {} local spaceship = {}
local collision_debug = true
spaceship.y = 0 spaceship.y = 0
local press_button = false 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()) 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) light.animation = anim8.newAnimation(g('1-' .. light.num_frames, 1), 0.05)
-- Collision -- Collision
body.collision = {x=body.body:getX() + 40, y=body.body:getY()} body.collision = {}
game.collisions:add({name='spaceship'}, body.collision.x, body.collision.y, body.width, body.height) body.collision[1] = {x=66, y=35, width=28, height=5, name={name='spaceship_1'}}
game.collisions:move({name='spaceship'}, body.collision.x, body.collision.y) 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 end
function spaceship.update(dt, game) function spaceship.update(dt, game)
@ -59,17 +75,33 @@ function spaceship.update(dt, game)
love.event.push('quit') love.event.push('quit')
end end
-- Collision -- 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 end
function spaceship.draw() 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) light.animation:draw(light.img, light.x, light.y)
if press_button then if press_button then
body.animation_fire:draw(body.img, body.body:getX(), body.body:getY()) body.animation_fire:draw(body.img, body.body:getX(), body.body:getY())
else else
body.animation_stop:draw(body.img, body.body:getX(), body.body:getY()) body.animation_stop:draw(body.img, body.body:getX(), body.body:getY())
end 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 end
return spaceship return spaceship