chroma_solstice/sprite_manager.lua

113 lines
2.9 KiB
Lua
Raw Normal View History

sprites = {
2015-03-25 01:38:53 -04:00
player = love.graphics.newImage("art/game/player.png"),
wall = love.graphics.newImage("art/game/static_wall.png"),
box = {
2015-03-25 01:38:53 -04:00
red = love.graphics.newImage("art/game/wall_r.png"),
green = love.graphics.newImage("art/game/wall_g.png"),
blue = love.graphics.newImage("art/game/wall_b.png")
},
door = {
2015-03-25 01:38:53 -04:00
white = love.graphics.newImage("art/game/door_locked_w.png"),
red = love.graphics.newImage("art/game/door_locked_r.png"),
green = love.graphics.newImage("art/game/door_locked_g.png"),
blue = love.graphics.newImage("art/game/door_locked_b.png"),
unlocked = love.graphics.newImage("art/game/door_unlocked.png")
},
switch = {
2015-03-25 01:38:53 -04:00
red = love.graphics.newImage("art/game/switch_r.png"),
green = love.graphics.newImage("art/game/switch_g.png"),
blue = love.graphics.newImage("art/game/switch_b.png")
},
water = {
2015-03-16 07:39:54 -04:00
red = love.graphics.newImage("art/nature/water_r.png"),
green = love.graphics.newImage("art/nature/water_g.png"),
blue = love.graphics.newImage("art/nature/water_b.png")
},
2015-03-25 01:38:53 -04:00
mac_smaller = love.graphics.newImage("art/tech/mac_smaller_r.png"),
2015-03-16 07:39:54 -04:00
tree = {
red = love.graphics.newImage("art/nature/palm_scaled_r.png"),
green = love.graphics.newImage("art/nature/palm_scaled_g.png"),
blue = love.graphics.newImage("art/nature/palm_scaled_b.png")
},
npcs = {
},
}
spriteCells = {}
function processSprites()
getCellsFromSprite(sprites.player)
for name, spriteObj in pairs(sprites) do
if type(spriteObj) == "table" then
spriteCells[name] = {}
--this is multicolored so...
for color, sprite in pairs(spriteObj) do
2015-03-16 07:39:54 -04:00
spriteCells[name][color] = getCellsFromSprite(sprite)
end
else
--assume it's an image obj
spriteCells[name] = getCellsFromSprite(spriteObj)
end
end
end
function getCellsFromSprite(sprite)
local w, h = sprite:getWidth(), sprite:getHeight()
local box = {w = w / 16, h = h / 16}
local boxCells = cellShapeFromBox(box)
if box.w < 1 or box.h < 1 then
return boxCells
end
local img = sprite:getData()
local cellsToRemove = {}
local stop
for _, cell in pairs(boxCells) do
local startPixel = {x = (cell.x - 1) * 16, y = (cell.y - 1) * 16}
local endPixel = {x = (cell.x * 16) - 1, y = (cell.y * 16) - 1}
local empty = true
for i = startPixel.x, endPixel.x do
for j = startPixel.y, endPixel.y do
local r, g, b, a = img:getPixel(i, j)
if a > 0 and (r > 0 or g > 0 or b > 0) then
--this cell has color so
empty = false
end
end
end
if empty then
cellsToRemove[cell] = cell --references boxCells cell so comparison should wor_
end
end
local i = 1
while i <= #boxCells do
if boxCells[i] == cellsToRemove[boxCells[i]] then
table.remove(boxCells, i)
else
i = i + 1
end
end
shallowTablePrint(boxCells)
return boxCells
end
function cellShapeFromBox(box)
local cells = {}
for i = 1, box.w do
for j = 1, box.h do
table.insert(cells, {x = i, y = j})
end
end
return cells
end