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