Code: Select all
local sprites = {
['@'] = { drawable = nil, dark = nil, fname = 'hero.png' },
['.'] = { drawable = nil, dark = nil, fname = 'ground.png' },
['#'] = { drawable = nil, dark = nil, fname = 'wall.png' },
['f'] = { drawable = nil, dark = nil, fname = 'fungus.png' },
['F'] = { drawable = nil, dark = nil, fname = 'fungus-big.png' },
['>'] = { drawable = nil, dark = nil, fname = 'stairs-down.png' },
['<'] = { drawable = nil, dark = nil, fname = 'stairs-up.png' },
['b'] = { drawable = nil, dark = nil, fname = 'bat.png' },
}
local SpriteRenderer = class('SpriteRenderer')
-- ...
function SpriteRenderer:load()
for index, value in pairs(sprites) do
local image = love.graphics.newImage('data/' .. sprites[index].fname) -- load image
image:setFilter('nearest', 'nearest')
sprites[index].drawable = image
-- making "shadowed" copy
sprites[index].dark = love.graphics.newCanvas(self.spriteWidth, self.spriteHeight)
love.graphics.setCanvas(sprites[index].dark)
love.graphics.clear()
love.graphics.setColor(1, 1, 1, 1)
love.graphics.draw(image, 0, 0)
love.graphics.setColor(0, 0, 0, 0.5)
love.graphics.rectangle('fill', 0, 0, self.spriteWidth, self.spriteHeight)
love.graphics.setCanvas()
end
end
-- ...
function SpriteRenderer:draw(char, x, y, isDark)
local w = self:getSpriteWidth()
local h = self:getSpriteHeight()
if isDark then
love.graphics.draw(sprites[char].dark, x * w, y * h) -- using darker copy
else
love.graphics.draw(sprites[char].drawable, x * w, y * h)
end
end
P.S. Code is correctly create darkerk copy of sprites, but im not sure in correctly using of canvas.