Модуль:Entity Sprite: различия между версиями
Pok (обсуждение | вклад) мНет описания правки |
Pok (обсуждение | вклад) мНет описания правки |
||
| (не показаны 3 промежуточные версии этого же участника) | |||
| Строка 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 | ||
-- Функция получения пути спрайта | -- Функция получения пути спрайта: сначала пытаем Icon.sprite, затем Sprite.sprite, затем первый layer.sprite | ||
local function getSpritePath(entry) | local function getSpritePath(entry) | ||
local | local iconBlock = findFieldInsensitive(entry, "Icon") | ||
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 | ||
-- Функция | -- Проверка 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 | local result = {} | ||
local function addState(state, sprite) | |||
table.insert(result, { state = state, sprite = sprite }) | |||
end | end | ||
local | local spritePath = getSpritePath(entry) | ||
local iconBlock = findFieldInsensitive(entry, "Icon") | |||
local | if iconBlock and iconBlock.state and isLayerVisibleAndShaded(iconBlock) then | ||
for _, layer in ipairs(spriteBlock.layers) do | local s = (type(iconBlock) == "table" and iconBlock.sprite) or spritePath | ||
addState(iconBlock.state, s) | |||
else | |||
local spriteBlock = findFieldInsensitive(entry, "Sprite") | |||
if spriteBlock and spriteBlock.layers then | |||
for _, layer in ipairs(spriteBlock.layers) do | |||
if layer.state and isLayerVisibleAndShaded(layer) then | |||
local s = layer.sprite or spritePath | |||
addState(layer.state, s) | |||
break | |||
end | |||
end | |||
elseif spriteBlock and spriteBlock.state and isLayerVisibleAndShaded(spriteBlock) then | |||
addState(spriteBlock.state, spritePath) | |||
end | end | ||
end | end | ||
local spriteBlock = findFieldInsensitive(entry, "Sprite") | |||
if spriteBlock and spriteBlock.layers then | |||
for _, layer in ipairs(spriteBlock.layers) do | |||
local alreadyAdded = false | |||
local | for _, r in ipairs(result) do | ||
if | if r.state == layer.state then | ||
alreadyAdded = true | |||
break | |||
end | |||
end | |||
if not alreadyAdded and layer.state and isStateFirstKey(layer) and isLayerVisibleAndShaded(layer) then | |||
local s = layer.sprite or spritePath | |||
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 | ||
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, ", ") .. ")" | ||