alunizaje/assets/scripts/gui/life.lua

88 lines
2.9 KiB
Lua
Raw Normal View History

2017-01-05 10:49:04 +01:00
local anim8 = require 'assets/scripts/vendor/anim8'
local tools = require 'assets/scripts/tools'
local life = {}
function life.load(game)
local PADDING = 50
-- Up
life.top = {
x = PADDING,
y = PADDING,
img = love.graphics.newImage('assets/sprites/gui/life_top.png'),
num_frames = 2
}
g = anim8.newGrid(life.top.img:getWidth() / life.top.num_frames, life.top.img:getHeight(), life.top.img:getWidth(), life.top.img:getHeight())
life.top.animation = anim8.newAnimation(g('1-' .. life.top.num_frames, 1), 1)
life.top.animation:gotoFrame(1)
life.top.animation:pause()
-- Down
life.down = {
img = love.graphics.newImage('assets/sprites/gui/life_down.png'),
x = PADDING,
y = life.top.y + life.top.img:getHeight()
}
-- Indicator
life.indicator = {
x_shaft = life.down.x + (life.down.img:getWidth() / 2),
y_shaft = life.down.y + (life.down.img:getHeight() / 2),
x_target = life.top.x + (life.top.img:getWidth() / 2 / life.top.num_frames),
y_target = life.top.y + (life.top.img:getHeight() / 2),
width = 10,
color = {0, 0, 0},
2017-01-06 21:36:57 +01:00
angle = 300,
2017-01-05 11:53:52 +01:00
move_up = false,
2017-01-06 21:36:57 +01:00
vel = 0.3,
limits = {310, 270, 230},
angle_variation = 3
2017-01-05 10:49:04 +01:00
}
life.indicator.radius = tools.distance(life.indicator.x_shaft, life.indicator.y_shaft, life.indicator.x_target, life.indicator.y_target)
love.graphics.setLineWidth(life.indicator.width)
2017-01-06 21:36:57 +01:00
-- Filter red
life.filter = {
color = {255, 255, 255},
temp_num_lifes = game.lifes
2017-01-05 11:53:52 +01:00
}
2017-01-05 10:49:04 +01:00
end
function life.update(dt, game)
life.top.animation:update(dt)
2017-01-05 11:53:52 +01:00
-- Direction
2017-01-06 21:36:57 +01:00
if life.indicator.limits[game.lifes] < life.indicator.angle then
2017-01-05 11:53:52 +01:00
life.indicator.move_up = false
2017-01-06 21:36:57 +01:00
elseif life.indicator.limits[game.lifes] - life.indicator.angle_variation > life.indicator.angle then
2017-01-05 11:53:52 +01:00
life.indicator.move_up = true
end
-- Increment
if life.indicator.move_up then
life.indicator.angle = life.indicator.angle + life.indicator.vel
else
life.indicator.angle = life.indicator.angle - life.indicator.vel
end
-- Calculate pos indicator
2017-01-05 10:49:04 +01:00
life.indicator.x_target, life.indicator.y_target = tools.circle_position(life.indicator.angle, life.indicator.radius, life.indicator.x_shaft, life.indicator.y_shaft)
2017-01-06 21:36:57 +01:00
-- Filter
if life.filter.temp_num_lifes ~= game.lifes then
life.filter.color = {255, 0, 0}
life.filter.temp_num_lifes = game.lifes
end
for key, color in pairs(life.filter.color) do
if color < 255 then
life.filter.color[key] = life.filter.color[key] + 1
end
end
2017-01-05 10:49:04 +01:00
end
2017-01-06 21:36:57 +01:00
function life.draw(game)
2017-01-05 10:49:04 +01:00
-- Background top
life.top.animation:draw(life.top.img, life.top.x, life.top.y)
-- Indicator
love.graphics.setColor(life.indicator.color)
love.graphics.line(life.indicator.x_shaft, life.indicator.y_shaft, life.indicator.x_target, life.indicator.y_target)
2017-01-06 21:36:57 +01:00
-- Filter
love.graphics.setColor(life.filter.color)
2017-01-05 10:49:04 +01:00
-- Background down
2017-01-06 21:36:57 +01:00
love.graphics.draw(life.down.img, life.down.x, life.down.y - 1)
2017-01-05 10:49:04 +01:00
end
return life