level_api.lua 3.15 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
-------
-- 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