105 lines
3.1 KiB
Lua
105 lines
3.1 KiB
Lua
-------
|
|
-- Simple API for loading levels from image files
|
|
-- @module level_api
|
|
-- @alias level
|
|
-- @author Hugues Russ
|
|
|
|
local level = {
|
|
colors = {
|
|
[1] = {}, -- Red
|
|
[2] = {}, -- Green
|
|
[3] = {}, -- Yellow
|
|
[4] = {}, -- Blue
|
|
[5] = {}, -- Magenta
|
|
[6] = {}, -- Cyan
|
|
[7] = {}, -- White
|
|
},
|
|
|
|
pre_load = {},
|
|
post_load = {},
|
|
}
|
|
|
|
--- Register a callback to perform on pixels of a certain color channel.
|
|
-- The function will receive the average value of the color in a range from 0-1
|
|
-- @param self
|
|
-- @param channels A table of three numbers of 1 or 0. (eg. `{ 1, 1, 1 }` = white, `{ 0, 1, 0 }` = green)
|
|
-- @param handle_color The color callback (eg. `function on_color(self, x, y, value, level_data)`)
|
|
function level.register_color(self, channels, handle_color)
|
|
local channel = (channels[1] * 1) + (channels[2] * 2) + (channels[3] * 4)
|
|
table.insert(self.colors[channel], handle_color)
|
|
end
|
|
|
|
--- Register a callback to perform before loading a level.
|
|
-- This is useful for initialization
|
|
-- @param self
|
|
-- @param pre_load The pre-load callback (eg. function `on_pre_load(self, level_image, level_data)`)
|
|
function level.register_pre_load(self, pre_load)
|
|
table.insert(self.pre_load, pre_load)
|
|
end
|
|
|
|
--- Register a callback to perform before after a level.
|
|
-- This is useful for cleanup and registering loaded data
|
|
-- @param self
|
|
-- @param post_load The post-load callback (eg. `function on_post_load(self, level_image, level_data)`)
|
|
function level.register_post_load(self, post_load)
|
|
table.insert(self.post_load, post_load)
|
|
end
|
|
|
|
--- Load a level from an image file and data table
|
|
-- @param self
|
|
-- @param image An image representing the level
|
|
-- @param data A table containing additional data to pass to callbacks
|
|
-- @return A table representing the loaded level
|
|
function level.load(self, image, data)
|
|
local level_table = {}
|
|
|
|
for _,fn in ipairs(self.pre_load) do
|
|
fn(level_table, image, data)
|
|
end
|
|
|
|
local width, height = image:getDimensions()
|
|
|
|
for i=1,height do
|
|
for j=1,width do
|
|
-- Determine the channel from rgb
|
|
local r, g, b = image:getPixel(j - 1, i - 1)
|
|
local channel = 0
|
|
local count = 0
|
|
local value = 0
|
|
if r ~= 0 then
|
|
channel = channel + 1
|
|
count = count + 1
|
|
value = value + r
|
|
end
|
|
if g ~= 0 then
|
|
channel = channel + 2
|
|
count = count + 1
|
|
value = value + g
|
|
end
|
|
if b ~= 0 then
|
|
channel = channel + 4
|
|
count = count + 1
|
|
value = value + b
|
|
end
|
|
if channel == 0 then
|
|
channel = 7
|
|
count = 3
|
|
value = r + g + b
|
|
end -- Black = White channel
|
|
|
|
-- Handle each callback for the selected channel
|
|
for _,fn in ipairs(self.colors[channel]) do
|
|
fn(level_table, j, i, value / count, data)
|
|
end
|
|
end
|
|
end
|
|
|
|
for _,fn in ipairs(self.post_load) do
|
|
fn(level_table, image, data)
|
|
end
|
|
|
|
return level_table
|
|
end
|
|
|
|
return level
|