Add final star

This commit is contained in:
Andros Fenollosa
2016-11-12 20:39:10 +01:00
parent 5fb3cd8d47
commit 5147e66de4
29 changed files with 104 additions and 20 deletions

View File

@ -0,0 +1,39 @@
local anim8 = require 'assets/scripts/vendor/anim8'
local tools = require 'assets/scripts/tools'
local background = {}
local stars = {}
function background.load()
num_stars = 100
local star = {}
star.num_frames = 9
star.ani_speed = 1
star.width_frame = 16
star.height_frame = 16
star.img = love.graphics.newImage('assets/sprites/background/star.png')
local g = anim8.newGrid(star.width_frame, star.height_frame, star.img:getWidth(), star.img:getHeight())
star.animation = anim8.newAnimation(g('1-' .. star.num_frames, 1), star.ani_speed)
for i = 1, num_stars do
local temp_star = tools.clone_table(star)
temp_star.animation = anim8.newAnimation(g('1-' .. star.num_frames, 1), math.random(star.ani_speed, star.ani_speed * 2))
temp_star.animation:gotoFrame(math.random(1, star.num_frames))
temp_star.x = math.random(-star.width_frame , canvas.width)
temp_star.y = math.random(-star.height_frame, canvas.height)
stars[i] = temp_star
end
end
function background.update(dt)
for key, star in pairs(stars) do
star.animation:update(dt)
end
end
function background.draw()
for key, star in pairs(stars) do
star.animation:draw(star.img, star.x, star.y)
end
end
return background

View File

@ -12,7 +12,7 @@ function spaceship.load(world)
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(98, 93, spaceship.img:getWidth(), spaceship.img:getHeight())
local g = anim8.newGrid(110, 103, spaceship.img:getWidth(), spaceship.img:getHeight())
spaceship.animation = anim8.newAnimation(g('1-1', 1), 0.1)
end

39
assets/scripts/tools.lua Normal file
View File

@ -0,0 +1,39 @@
local tools = {}
-- Get Table length
function tools.table_length(T)
local count = 0
for _ in pairs(T) do count = count + 1 end
return count
end
-- Pause system n seconds
local clock = os.clock
function tools.sleep(n) -- seconds
local t0 = clock()
while clock() - t0 <= n do end
end
-- Collision detection function
function tools.check_circle_collision(circle1, circle2)
local distance = math.sqrt((circle1.x - circle2.x) ^ 2 + (circle1.y - circle2.y) ^ 2)
return distance <= circle1.radius + circle2.radius
end
-- Deep copy table
function tools.clone_table(orig)
local orig_type = type(orig)
local copy
if orig_type == 'table' then
copy = {}
for orig_key, orig_value in next, orig, nil do
copy[tools.clone_table(orig_key)] = tools.clone_table(orig_value)
end
setmetatable(copy, tools.clone_table(getmetatable(orig)))
else -- number, string, boolean, etc
copy = orig
end
return copy
end
return tools

Binary file not shown.

Before

Width:  |  Height:  |  Size: 21 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 624 B

After

Width:  |  Height:  |  Size: 5.9 KiB