Add camera

This commit is contained in:
Andros Fenollosa
2016-11-27 12:31:56 +01:00
parent fc553ac58f
commit 8b012de054
29 changed files with 104 additions and 49 deletions

View File

@ -1,2 +1,15 @@
local cam = gamera.new(0,0,2000,2000)
cam:setWorld(0,0,2000,2000)
local gamera = require 'assets/scripts/vendor/gamera'
camera = {}
local limit = 200
function camera.load(game)
camera.gcam = gamera.new(0, 0, game.window.width, game.window.height)
camera.gcam:setWorld(0, 0, game.canvas.width, game.canvas.height)
end
function camera.update(game, spaceship)
camera.gcam:setPosition(0, spaceship.y + limit)
end
return camera

View File

@ -1,52 +1,54 @@
local anim8 = require 'assets/scripts/vendor/anim8'
local spaceship = {}
spaceship.y = 0
local press_button = false
function spaceship.load(game)
-- power origin 1000
spaceship = { x = game.canvas.width / 2, y = 0 , power = 400 , size_collition = 28, polygons_collition = 8 }
spaceship.width = 156
spaceship.height = 143
spaceship.img = love.graphics.newImage('assets/sprites/spaceship/body.png')
spaceship.body = love.physics.newBody(game.world, (game.canvas.width / 2) - (spaceship.img:getWidth() / 2) , spaceship.y, 'dynamic')
spaceship.shape = love.physics.newCircleShape(20)
spaceship.fixture = love.physics.newFixture(spaceship.body, spaceship.shape, 1)
spaceship.fixture:setRestitution(0.9)
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)
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)
-- Light
light = {
img = love.graphics.newImage('assets/sprites/spaceship/light.png'),
y = spaceship.y
y = body.y
}
light.x = spaceship.x + (spaceship.img:getWidth() / 2) + (light.img:getWidth() / 2)
light.num_frames = 9
light.width = 16
light.height = 16
light.x = body.x + (body.img:getWidth() / 2) + (light.img:getWidth() / 2)
light.num_frames = 11
light.width = 74
light.height = 66
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)
light.animation = anim8.newAnimation(g('1-' .. light.num_frames, 1), 0.05)
end
function spaceship.update(dt)
-- Spaceship
spaceship.animation_fire:update(dt)
body.animation_fire:update(dt)
press_button = false
spaceship.y = body.body:getY()
-- Light
light.animation:update(dt)
light.x = spaceship.body:getX() + (spaceship.width / 2) + (light.width / 2)
light.y = spaceship.body:getY()
light.x = body.body:getX() + 43
light.y = body.body:getY() - 15
-- Controls
if control_up then
spaceship.body:applyForce(0, -spaceship.power)
body.body:applyForce(0, -body.power)
press_button = true
end
if control_right then
spaceship.body:applyForce(spaceship.power, 0)
body.body:applyForce(body.power, 0)
press_button = true
elseif control_left then
spaceship.body:applyForce(-spaceship.power, 0)
body.body:applyForce(-body.power, 0)
press_button = true
end
if control_quit then
@ -57,9 +59,9 @@ end
function spaceship.draw()
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())
body.animation_fire:draw(body.img, body.body:getX(), body.body:getY())
else
spaceship.animation_stop:draw(spaceship.img, spaceship.body:getX(), spaceship.body:getY())
body.animation_stop:draw(body.img, body.body:getX(), body.body:getY())
end
end

View File

@ -36,4 +36,39 @@ function tools.clone_table(orig)
return copy
end
-- Print table
function tools.print_r(t)
local print_r_cache={}
local function sub_print_r(t,indent)
if (print_r_cache[tostring(t)]) then
print(indent.."*"..tostring(t))
else
print_r_cache[tostring(t)]=true
if (type(t)=="table") then
for pos,val in pairs(t) do
if (type(val)=="table") then
print(indent.."["..pos.."] => "..tostring(t).." {")
sub_print_r(val,indent..string.rep(" ",string.len(pos)+8))
print(indent..string.rep(" ",string.len(pos)+6).."}")
elseif (type(val)=="string") then
print(indent.."["..pos..'] => "'..val..'"')
else
print(indent.."["..pos.."] => "..tostring(val))
end
end
else
print(indent..tostring(t))
end
end
end
if (type(t)=="table") then
print(tostring(t).." {")
sub_print_r(t," ")
print("}")
else
sub_print_r(t," ")
end
print()
end
return tools