Add Item.Rarity

This commit is contained in:
Van
2026-02-01 20:51:23 +03:00
parent 3dd28858f5
commit 865f46b734
5 changed files with 82 additions and 4 deletions

View File

@@ -5,6 +5,33 @@ local require = modInfo.require
local ItemFactory = Mod.getInfo('smc__core__item_factory').require('ItemFactory')
---Determines whether common rarity will be shown in item descriptions or not.
local SHOW_COMMON_RARITY = true
---Colors corresponding to the rarity color in the item description.
---@type table<Item.Rarity, ColorString>
local RARITY_COLORS = {
['common'] = '#C0C0C0',
['uncommon'] = '#7cd2e2',
['distinguished'] = '#7ce2da',
['eminent'] = '#44d1a9',
['prime'] = '#9CF0B1',
['unique'] = '#f4c95a'
}
---Whether to show the numeric index in the rarity description.
local SHOW_RARITY_INDEX = true
---@type table<Item.Rarity, integer>
local RARITY_INDEX = {
['common'] = 1,
['uncommon'] = 2,
['distinguished'] = 3,
['eminent'] = 4,
['prime'] = 5,
['unique'] = 0
}
---@class Item
---@field itemDef Item.ItemDefinition
---@field luantiDef table
@@ -18,20 +45,42 @@ local Item = {
description = '',
titleColor = '#FFFFFF',
descriptionColor = '#C0C0C0',
rarity = 'common',
addModName = true,
},
callbacks = {}
}
}
---Adds an item's rarity tag to the description.
---@param rarity Item.Rarity
---@param desc string
---@return string
local function addRarityToDesc(rarity, desc)
if not SHOW_COMMON_RARITY and rarity == 'common' then
return desc
end
local S = core.get_translator('item')
local color = RARITY_COLORS[rarity]
local rarityString = S('Rarity')..': '..S(rarity)
if SHOW_RARITY_INDEX then
rarityString = rarityString..' ['..RARITY_INDEX[rarity]..']'
end
return desc..'\n'..core.colorize(color, rarityString)
end
---The alias `table.merge` for EmmyLua
---@type fun(...):(NodeDefinition|ItemDefinition)
local defMerge = function(...)
return table.merge(...)
end
---@param itemDef Item.ItemDefinition
---@return NodeDefinition|ItemDefinition
---@param itemDef Item.ItemDefinition
---@return NodeDefinition|ItemDefinition
local function itemDefToLuantiDef(itemDef)
local modName = Mod.getInfo().shortName
local S = core.get_translator(modName)
@@ -62,7 +111,11 @@ local function itemDefToLuantiDef(itemDef)
error('Item description color parameters cannot be empty.')
end
s.title, s.description = nil, description
assert(s.rarity ~= nil, 'The rarity of an item cannot be empty.')
description = addRarityToDesc(s.rarity, description)
s.title, s.description = nil, description..core.get_background_escape_sequence('#110F13')
local luantiDef = defMerge(s, c)