Модуль:Entity Sprite: различия между версиями

мНет описания правки
мНет описания правки
 
(не показаны 2 промежуточные версии этого же участника)
Строка 9: Строка 9:
end
end


-- Общая функция нечувствительного к регистру поиска поля
-- Функция нечувствительного к регистру поиска поля
local function findFieldInsensitive(tbl, fieldName)
local function findFieldInsensitive(tbl, fieldName)
     for key, value in pairs(tbl) do
     for key, value in pairs(tbl) do
Строка 19: Строка 19:
end
end


-- Функция получения пути спрайта (берёт основной sprite или первый найденный в layers)
-- Функция получения пути спрайта: сначала пытаем Icon.sprite, затем Sprite.sprite, затем первый layer.sprite
local function getSpritePath(entry)
local function getSpritePath(entry)
     local spriteBlock = findFieldInsensitive(entry, "Icon")
     local iconBlock = findFieldInsensitive(entry, "Icon")
                      or findFieldInsensitive(entry, "Sprite")
    if type(iconBlock) == "table" and iconBlock.sprite then
        return iconBlock.sprite
    end
 
    local spriteBlock = findFieldInsensitive(entry, "Sprite")
     if not spriteBlock then return nil end
     if not spriteBlock then return nil end


Строка 44: Строка 48:
end
end


-- Функция получения списка состояний с учётом переопределяющего sprite в слое
-- Проверка visible и shader
local function isLayerVisibleAndShaded(layer)
    if layer.visible == false then return false end
    if layer.shader and layer.shader == "unshaded" then return false end
    return true
end
 
-- Функция определения, что поле state первое
local function isStateFirstKey(layer)
local function isStateFirstKey(layer)
     for k, _ in pairs(layer) do
     for k, _ in pairs(layer) do
Строка 52: Строка 63:
end
end


-- Функция получения списка состояний с учётом переопределяющего sprite в слое
local function getSpriteStates(entry)
local function getSpriteStates(entry)
     local result = {}
     local result = {}
Строка 60: Строка 72:


     local spritePath = getSpritePath(entry)
     local spritePath = getSpritePath(entry)
    local iconBlock = findFieldInsensitive(entry, "Icon")


     local iconBlock = findFieldInsensitive(entry, "Icon")
     if iconBlock and iconBlock.state and isLayerVisibleAndShaded(iconBlock) then
    if iconBlock and iconBlock.state then
        local s = (type(iconBlock) == "table" and iconBlock.sprite) or spritePath
         addState(iconBlock.state, getSpritePath(entry))
         addState(iconBlock.state, s)
     else
     else
         local spriteBlock = findFieldInsensitive(entry, "Sprite")
         local spriteBlock = findFieldInsensitive(entry, "Sprite")
         if spriteBlock and spriteBlock.layers then
         if spriteBlock and spriteBlock.layers then
             for _, layer in ipairs(spriteBlock.layers) do
             for _, layer in ipairs(spriteBlock.layers) do
                 if layer.state and layer.visible ~= false then
                 if layer.state and isLayerVisibleAndShaded(layer) then
                     local s = layer.sprite or spritePath
                     local s = layer.sprite or spritePath
                     addState(layer.state, s)
                     addState(layer.state, s)
Строка 74: Строка 87:
                 end
                 end
             end
             end
         elseif spriteBlock and spriteBlock.state then
         elseif spriteBlock and spriteBlock.state and isLayerVisibleAndShaded(spriteBlock) then
             addState(spriteBlock.state, getSpritePath(entry))
             addState(spriteBlock.state, spritePath)
         end
         end
     end
     end
Строка 89: Строка 102:
                 end
                 end
             end
             end
 
             if not alreadyAdded and layer.state and isStateFirstKey(layer) and isLayerVisibleAndShaded(layer) then
             if not alreadyAdded and layer.state and isStateFirstKey(layer) and layer.visible ~= false then
                 local s = layer.sprite or spritePath
                 local s = layer.sprite or spritePath
                 addState(layer.state, s)
                 addState(layer.state, s)
             end
             end
         end
         end
    end
    if #result == 0 and spritePath then
        addState("icon", spritePath)
     end
     end


Строка 127: Строка 143:
                     local spritePath = item.sprite
                     local spritePath = item.sprite
                     local stateName = item.state
                     local stateName = item.state
                     local url = baseUrl .. spritePath .. "/" .. stateName .. ".png"
                     if spritePath then
                    table.insert(links, "[" .. url .. " " .. stateName .. "]")
                        local url = baseUrl .. spritePath .. "/" .. stateName .. ".png"
                        table.insert(links, "[" .. url .. " " .. stateName .. "]")
                    else
                        table.insert(links, stateName .. " Error: sprite not found")
                    end
                 end
                 end
                 return "(state: " .. table.concat(links, ", ") .. ")"
                 return "(state: " .. table.concat(links, ", ") .. ")"