2016-11-12 13:49:24 +01:00
|
|
|
local anim8 = require 'assets/scripts/vendor/anim8'
|
2016-12-08 20:02:27 +01:00
|
|
|
local HC = require 'assets/scripts/vendor/HC'
|
2016-11-12 13:49:24 +01:00
|
|
|
|
|
|
|
local spaceship = {}
|
2016-12-08 20:02:27 +01:00
|
|
|
local collision_debug = false
|
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)
|
2017-01-06 21:36:57 +01:00
|
|
|
body = {
|
|
|
|
x = 0,
|
|
|
|
y = 0,
|
2017-01-06 22:10:25 +01:00
|
|
|
power = 200,
|
2017-01-06 21:36:57 +01:00
|
|
|
size_collition = 28,
|
2017-01-06 22:10:25 +01:00
|
|
|
polygons_collition = 8,
|
|
|
|
visible = true,
|
|
|
|
blink = {
|
|
|
|
active = false,
|
|
|
|
num_repeat = 5,
|
|
|
|
duration = 0.05,
|
|
|
|
num_count = 0,
|
|
|
|
time = 0
|
|
|
|
}
|
2017-01-06 21:36:57 +01:00
|
|
|
}
|
2016-11-27 12:31:56 +01:00
|
|
|
body.width = 156
|
|
|
|
body.height = 143
|
2017-01-06 21:36:57 +01:00
|
|
|
body.pos_center_x = (game.canvas.width / 2) - (body.width / 2)
|
|
|
|
body.x = body.pos_center_x
|
2016-11-27 12:31:56 +01:00
|
|
|
body.img = love.graphics.newImage('assets/sprites/spaceship/body.png')
|
2016-12-08 20:02:27 +01:00
|
|
|
body.num_frames = 5
|
2017-01-06 21:36:57 +01:00
|
|
|
body.body = love.physics.newBody(game.world, body.x , body.y, 'dynamic')
|
2016-11-27 12:31:56 +01:00
|
|
|
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)
|
2016-12-08 20:02:27 +01:00
|
|
|
body.animation_fire = anim8.newAnimation(g('2-' .. body.num_frames, 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-08 20:49:38 +01:00
|
|
|
-- Explosion
|
|
|
|
explosion = {
|
|
|
|
img = love.graphics.newImage('assets/sprites/spaceship/explosion.png'),
|
|
|
|
num_frames = 10,
|
|
|
|
speed = 0.05,
|
|
|
|
x = -game.canvas.width,
|
|
|
|
y = -game.canvas.height
|
|
|
|
}
|
|
|
|
g = anim8.newGrid(explosion.img:getWidth() / explosion.num_frames, explosion.img:getHeight(), explosion.img:getWidth(), explosion.img:getHeight())
|
|
|
|
explosion.animation = anim8.newAnimation(g('1-' .. explosion.num_frames, 1), explosion.speed, 'pauseAtEnd')
|
|
|
|
explosion.animation:pause()
|
|
|
|
explosion.claim_x = 45
|
|
|
|
explosion.claim_y = 40
|
2016-12-01 21:23:35 +01:00
|
|
|
-- Collision
|
2016-12-08 20:02:27 +01:00
|
|
|
body.collision = {}
|
|
|
|
body.collision.claim = 79
|
|
|
|
body.collision.vertices = {
|
|
|
|
104, 40,
|
|
|
|
55, 40,
|
|
|
|
35, 100,
|
|
|
|
78, 110,
|
|
|
|
119, 100
|
|
|
|
}
|
|
|
|
body.collision.hc = HC.polygon(
|
|
|
|
body.collision.vertices[1],
|
|
|
|
body.collision.vertices[2],
|
|
|
|
body.collision.vertices[3],
|
|
|
|
body.collision.vertices[4],
|
|
|
|
body.collision.vertices[5],
|
|
|
|
body.collision.vertices[6],
|
|
|
|
body.collision.vertices[7],
|
|
|
|
body.collision.vertices[8],
|
|
|
|
body.collision.vertices[9],
|
|
|
|
body.collision.vertices[10]
|
|
|
|
)
|
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-12-08 20:49:38 +01:00
|
|
|
-- Explosion
|
|
|
|
explosion.animation:update(dt)
|
2016-11-12 13:49:24 +01:00
|
|
|
-- Controls
|
2017-01-03 22:05:46 +01:00
|
|
|
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
|
2017-01-03 22:05:46 +01:00
|
|
|
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
|
2017-01-03 22:05:46 +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
|
2017-01-03 22:05:46 +01:00
|
|
|
if CONTROL_QUIT then
|
2016-11-12 13:49:24 +01:00
|
|
|
love.event.push('quit')
|
|
|
|
end
|
2016-12-01 21:23:35 +01:00
|
|
|
-- Collision
|
2016-12-08 20:02:27 +01:00
|
|
|
-- Move polygon
|
|
|
|
body.collision.hc:moveTo(body.body:getX() + body.collision.claim, body.body:getY() + body.collision.claim)
|
|
|
|
-- Check for collisions
|
|
|
|
for shape, delta in pairs(HC.collisions(body.collision.hc)) do
|
2017-01-06 21:36:57 +01:00
|
|
|
if game.die == false then
|
|
|
|
-- Lose life
|
|
|
|
if game.lifes > 1 then
|
|
|
|
game.lifes = game.lifes - 1
|
|
|
|
end
|
|
|
|
-- Die
|
|
|
|
game.die = true
|
|
|
|
game.play = false
|
|
|
|
explosion.x, explosion.y, explosion.enable = body.body:getX() - explosion.claim_x, body.body:getY() - explosion.claim_y, true
|
|
|
|
explosion.animation:resume()
|
|
|
|
end
|
2016-12-08 20:02:27 +01:00
|
|
|
end
|
2016-12-08 20:49:38 +01:00
|
|
|
if not game.play then
|
|
|
|
body.body:setLinearVelocity(0, 0)
|
|
|
|
end
|
2016-12-08 20:02:27 +01:00
|
|
|
-- Check game limits
|
|
|
|
if body.body:getY() <= 0 then -- Top game
|
|
|
|
x, y = body.body:getLinearVelocity()
|
|
|
|
body.body:setLinearVelocity(x, -y)
|
|
|
|
end
|
|
|
|
if body.body:getX() + (body.img:getWidth() / body.num_frames) - (body.collision.claim / 2) > game.canvas.width then -- Right game
|
|
|
|
x, y = body.body:getLinearVelocity()
|
|
|
|
body.body:setLinearVelocity(-x, y)
|
|
|
|
end
|
|
|
|
if body.body:getX() + (body.collision.claim / 2) < 0 then -- Left game
|
|
|
|
x, y = body.body:getLinearVelocity()
|
|
|
|
body.body:setLinearVelocity(-x, y)
|
|
|
|
end
|
2017-01-06 22:10:25 +01:00
|
|
|
-- Blink
|
|
|
|
if body.blink.active then
|
|
|
|
body.blink.time = body.blink.time + dt
|
|
|
|
if body.blink.time >= body.blink.duration then
|
|
|
|
body.blink.time = 0
|
|
|
|
if body.visible then
|
|
|
|
body.visible = false
|
|
|
|
if body.blink.num_count < body.blink.num_repeat then
|
|
|
|
body.blink.num_count = body.blink.num_count + 1
|
|
|
|
else
|
|
|
|
body.blink.num_count = 0
|
|
|
|
body.blink.active = false
|
|
|
|
body.visible = true
|
|
|
|
end
|
|
|
|
else
|
|
|
|
body.visible = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-01-06 21:36:57 +01:00
|
|
|
-- Check if this ingame
|
|
|
|
local x, y = body.body:getPosition()
|
|
|
|
if y < 0 then
|
|
|
|
body.body:setPosition(x, 0)
|
|
|
|
end
|
|
|
|
if y > game.canvas.height then
|
|
|
|
game.play = false
|
|
|
|
end
|
|
|
|
-- Restart position
|
|
|
|
if not game.play then
|
|
|
|
body.body:setLinearVelocity(0, -1000)
|
|
|
|
if y <= 0 then
|
|
|
|
local y = 0
|
|
|
|
body.body:setLinearVelocity(0, 0)
|
|
|
|
body.body:setPosition(body.pos_center_x, y)
|
|
|
|
game.play = true
|
|
|
|
game.die = false
|
2017-01-06 22:10:25 +01:00
|
|
|
body.blink.active = true
|
2017-01-06 21:36:57 +01:00
|
|
|
end
|
|
|
|
end
|
2016-11-12 13:49:24 +01:00
|
|
|
end
|
|
|
|
|
2016-12-08 20:49:38 +01:00
|
|
|
function spaceship.draw(game)
|
2017-01-06 22:10:25 +01:00
|
|
|
if game.play and body.visible then
|
2016-12-08 20:49:38 +01:00
|
|
|
-- Lignt
|
|
|
|
light.animation:draw(light.img, light.x, light.y)
|
|
|
|
-- Spaceship
|
|
|
|
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
|
2016-11-27 11:06:22 +01:00
|
|
|
end
|
2016-12-08 20:49:38 +01:00
|
|
|
-- Explosion
|
|
|
|
explosion.animation:draw(explosion.img, explosion.x, explosion.y)
|
2016-12-08 20:02:27 +01:00
|
|
|
-- Collision
|
2016-12-03 22:49:20 +01:00
|
|
|
if collision_debug then
|
2016-12-08 20:02:27 +01:00
|
|
|
body.collision.hc:draw('fill')
|
2016-12-03 22:49:20 +01:00
|
|
|
end
|
2016-11-12 13:49:24 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
return spaceship
|