2016-11-12 13:49:24 +01:00
|
|
|
local anim8 = require 'assets/scripts/vendor/anim8'
|
|
|
|
|
|
|
|
local spaceship = {}
|
2016-12-03 22:49:20 +01:00
|
|
|
local collision_debug = true
|
2016-11-27 12:31:56 +01:00
|
|
|
spaceship.y = 0
|
2016-11-27 11:06:22 +01:00
|
|
|
local press_button = false
|
2016-11-12 13:49:24 +01:00
|
|
|
|
2016-11-14 00:35:14 +01:00
|
|
|
function spaceship.load(game)
|
2016-11-12 13:49:24 +01:00
|
|
|
-- power origin 1000
|
2016-11-27 12:31:56 +01:00
|
|
|
body = { x = game.canvas.width / 2, y = 0 , power = 400 , size_collition = 28, polygons_collition = 8 }
|
|
|
|
body.width = 156
|
|
|
|
body.height = 143
|
|
|
|
body.img = love.graphics.newImage('assets/sprites/spaceship/body.png')
|
|
|
|
body.body = love.physics.newBody(game.world, (game.canvas.width / 2) - (body.img:getWidth() / 2) , body.y, 'dynamic')
|
|
|
|
body.shape = love.physics.newCircleShape(20)
|
|
|
|
body.fixture = love.physics.newFixture(body.body, body.shape, 1)
|
|
|
|
body.fixture:setRestitution(0.9)
|
|
|
|
local g = anim8.newGrid(body.width, body.height, body.img:getWidth(), body.img:getHeight())
|
|
|
|
body.animation_stop = anim8.newAnimation(g('1-1', 1), 0.1)
|
|
|
|
body.animation_fire = anim8.newAnimation(g('2-5', 1), 0.01)
|
2016-11-27 11:06:22 +01:00
|
|
|
-- Light
|
|
|
|
light = {
|
|
|
|
img = love.graphics.newImage('assets/sprites/spaceship/light.png'),
|
2016-11-27 12:31:56 +01:00
|
|
|
y = body.y
|
2016-11-27 11:06:22 +01:00
|
|
|
}
|
2016-11-27 12:31:56 +01:00
|
|
|
light.x = body.x + (body.img:getWidth() / 2) + (light.img:getWidth() / 2)
|
|
|
|
light.num_frames = 11
|
|
|
|
light.width = 74
|
|
|
|
light.height = 66
|
2016-11-27 11:06:22 +01:00
|
|
|
g = anim8.newGrid(light.width, light.height, light.img:getWidth(), light.img:getHeight())
|
2016-11-27 12:31:56 +01:00
|
|
|
light.animation = anim8.newAnimation(g('1-' .. light.num_frames, 1), 0.05)
|
2016-12-01 21:23:35 +01:00
|
|
|
-- Collision
|
2016-12-03 22:49:20 +01:00
|
|
|
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
|
2016-11-12 13:49:24 +01:00
|
|
|
end
|
|
|
|
|
2016-12-01 21:23:35 +01:00
|
|
|
function spaceship.update(dt, game)
|
2016-11-27 11:06:22 +01:00
|
|
|
-- Spaceship
|
2016-11-27 12:31:56 +01:00
|
|
|
body.animation_fire:update(dt)
|
2016-11-27 11:06:22 +01:00
|
|
|
press_button = false
|
2016-11-27 12:31:56 +01:00
|
|
|
spaceship.y = body.body:getY()
|
2016-11-27 11:06:22 +01:00
|
|
|
-- Light
|
|
|
|
light.animation:update(dt)
|
2016-11-27 12:31:56 +01:00
|
|
|
light.x = body.body:getX() + 43
|
|
|
|
light.y = body.body:getY() - 15
|
2016-11-12 13:49:24 +01:00
|
|
|
-- Controls
|
|
|
|
if control_up then
|
2016-11-27 12:31:56 +01:00
|
|
|
body.body:applyForce(0, -body.power)
|
2016-11-27 11:06:22 +01:00
|
|
|
press_button = true
|
2016-11-12 13:49:24 +01:00
|
|
|
end
|
|
|
|
if control_right then
|
2016-11-27 12:31:56 +01:00
|
|
|
body.body:applyForce(body.power, 0)
|
2016-11-27 11:06:22 +01:00
|
|
|
press_button = true
|
2016-11-12 13:49:24 +01:00
|
|
|
elseif control_left then
|
2016-11-27 12:31:56 +01:00
|
|
|
body.body:applyForce(-body.power, 0)
|
2016-11-27 11:06:22 +01:00
|
|
|
press_button = true
|
2016-11-12 13:49:24 +01:00
|
|
|
end
|
|
|
|
if control_quit then
|
|
|
|
love.event.push('quit')
|
|
|
|
end
|
2016-12-01 21:23:35 +01:00
|
|
|
-- Collision
|
2016-12-03 22:49:20 +01:00
|
|
|
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
|
2016-11-12 13:49:24 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
function spaceship.draw()
|
2016-11-27 11:06:22 +01:00
|
|
|
light.animation:draw(light.img, light.x, light.y)
|
|
|
|
if press_button then
|
2016-11-27 12:31:56 +01:00
|
|
|
body.animation_fire:draw(body.img, body.body:getX(), body.body:getY())
|
2016-11-27 11:06:22 +01:00
|
|
|
else
|
2016-11-27 12:31:56 +01:00
|
|
|
body.animation_stop:draw(body.img, body.body:getX(), body.body:getY())
|
2016-11-27 11:06:22 +01:00
|
|
|
end
|
2016-12-03 22:49:20 +01:00
|
|
|
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
|
2016-11-12 13:49:24 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
return spaceship
|