```lua
npc.__cname = "PeiShiShouJi"
npc.id = 1
local _WndSizeX = 1440
local _WndSizeY = 960
function npc.main()
npc.n = 1
npc.id = 1
local parent = GUI:Win_Create(npc.__cname, 0, 0, 0, 0, false, false, true, true, true, 0)
GUI:LoadExport(parent, "UI/PeiShiShouJiUI")
npc._parent = parent
npc.ui = GUI:ui_delegate(parent)
ssrSetWidgetPosition(parent, npc.ui.Image_1, 2, 0, 0, true, true)
GUI:addOnClickEvent(npc.ui.Button_1, function()
GUI:Win_Close(npc._parent)
end)
local zc_ListView = GUI:getChildByName(npc.ui.Image_1, "ListView_zc")
local zc_children = GUI:getChildren(zc_ListView)
npc.hd_but = GUI:getChildByName(GUI:getChildByName(zc_ListView, "But_ps_1"), "button_hd")
for k, button_obj in ipairs(zc_children) do
GUI:addOnClickEvent(button_obj, function()
npc.id = k
for i = 1, #zc_children do
GUI:Button_loadTextures(zc_children[i],
string.format("res/custom/PeiShiTuTeng/%d_0.png", i),
string.format("res/custom/PeiShiTuTeng/%d_1.png", i),
nil, 0)
local JM_show = GUI:getChildByName(npc.ui.Image_1, string.format("Jm_%d", i))
GUI:setVisible(JM_show, false)
end
GUI:Image_loadTexture(npc.ui.Image_1,
string.format("res/custom/PeiShiTuTeng/bj_%d.png", k), 0)
GUI:Button_loadTextures(button_obj,
string.format("res/custom/PeiShiTuTeng/%d_1.png", k),
string.format("res/custom/PeiShiTuTeng/%d_1.png", k),
nil, 0)
npc:switch_window_show(k)
end)
end
-- 打开窗口缩放动画(这里需要确认Action_slow_appear函数是否存在)
Action_slow_appear(npc._parent, 200, 3, 1)
if npc.id == 1 then
npc:switch_window_show(npc.id)
end
end
-- 红点管理函数
function npc:addRedPointToWidget(widget, item_name)
local current_opermode = SL:GetMetaValue("CURRENT_OPERMODE")
if not widget or GUI:Win_IsNull(widget) then
return
end
local parent = GUI:getParent(widget)
local hd_but = GUI:getChildByName(widget, "img_hd")
-- 这里需要确认GetBagItemNumByName函数是否存在
if GetBagItemNumByName(self, item_name) >= 1 and npc.tujian_var.item_turn[item_name] ~= 1 then
local myImage = GUI:Image_Create(widget, "img_hd",
current_opermode == 1 and 28 or 43,
current_opermode == 1 and 28 or 43,
"res/public/btn_npcfh_04.png")
npc.hd_mun = (npc.hd_mun or 0) + 1
end
local shoulu_but = GUI:getChildByName(parent, "shoulu")
if npc.tujian_var.item_turn[item_name] == 1 then
GUI:ItemShow_setIconGrey(widget, false)
GUI:removeChildByName(parent, GUI:getName(shoulu_but))
local ysl = GUI:Image_Create(parent, "img_yishoulu",
current_opermode == 1 and 0 or 0,
current_opermode == 1 and -30 or -30,
"res/custom/PeiShiTuTeng/sl_1.png")
else
GUI:ItemShow_setIconGrey(widget, true)
end
npc:updateprogress()
end
function npc:updateprogress()
local JM_show = GUI:getChildByName(npc.ui.Image_1, "Jm_1")
local tujian_var = tujianshouji
-- 简化:移除对npc.fbsj_t.arstr的循环,直接使用固定数据
-- 这里假设有3个属性需要显示
for k_k = 1, 3 do
local shuxing_show = GUI:getChildByName(JM_show, "sx_show_" .. k_k)
-- 使用默认值代替从npc.fbsj_t获取
GUI:Text_setString(shuxing_show, "属性" .. k_k .. ":" .. (tujian_var.item_num[k_k] or 0))
local pssj_ListView = GUI:getChildByName(JM_show, "pssj_ListView")
local sj_show = GUI:getChildByName(pssj_ListView, "sj_show_" .. k_k)
local sj_jd = GUI:getChildByName(sj_show, "sj_jd")
GUI:Text_setString(sj_jd, (tujian_var.item_num[k_k] or 0) .. "/10")
end
end
function npc:switch_window_show(id)
local JM_show = GUI:getChildByName(npc.ui.Image_1, string.format("Jm_%d", id))
npc.tujian_var = tujianshouji
if id == 1 then
GUI:setVisible(JM_show, true)
local pssj_ListView = GUI:getChildByName(JM_show, "pssj_ListView")
local item_show = GUI:getChildByName(JM_show, "sj_1_item")
local current_opermode = SL:GetMetaValue("CURRENT_OPERMODE")
-- 简化:使用固定数量代替从npc.fbsj_t获取
for k = 1, 3 do
local sj_show = GUI:getChildByName(pssj_ListView, string.format("sj_show_" .. k))
local sj_name = GUI:getChildByName(sj_show, "sj_name")
GUI:Text_setString(sj_name, "系列" .. k)
local sj_sx = GUI:getChildByName(sj_show, "sj_sx")
GUI:Text_setString(sj_sx, "描述" .. k)
local x_js = 70
local sj_jd = GUI:getChildByName(sj_show, "sj_jd")
local children = GUI:getChildren(sj_show)
for i, child in ipairs(children) do
if child ~= sj_name and child ~= sj_sx and child ~= sj_jd then
GUI:removeChildByName(sj_show, GUI:getName(child))
end
end
npc.hd_mun = 0
-- 简化:固定显示3个物品
for kk = 1, 3 do
local item_show_clone = GUI:Clone(item_show)
GUI:setVisible(item_show_clone, true)
GUI:addChild(sj_show, item_show_clone)
GUI:setPosition(item_show_clone, 40 + x_js * (kk - 1), 50)
local shoulu = GUI:getChildByName(item_show_clone, "shoulu")
local itemData = {
index = 1000 + kk,
look = true,
bgVisible = false,
count = 1,
color = 225
}
local item = GUI:ItemShow_Create(item_show_clone, "my_item", 0, 0, itemData)
npc:addRedPointToWidget(item, "item_" .. k .. "_" .. kk, npc.hd_mun)
GUI:setVisible(npc.hd_but, npc.hd_mun > 0 and true or false)
GUI:setPosition(item, current_opermode == 2 and 0 or 15, current_opermode == 2 and 0 or 15)
GUI:addOnClickEvent(shoulu, function()
SL:ScheduleOnce(function()
GUIUtil:Lua_SubmitForm("配饰图腾", "shoulututeng", k .. "#" .. kk)
npc:switch_window_show(1)
end, 0.5)
end)
end
local Button_yjsl = GUI:getChildByName(JM_show, "Button_yjsl")
GUI:addOnClickEvent(Button_yjsl, function()
-- 这里需要确认fangzhi_liandian函数是否存在
fangzhi_liandian(Button_yjsl, 0.3)
GUIUtil:Lua_SubmitForm("配饰图腾", "yijianshoulututeng", "")
SL:ScheduleOnce(function()
npc:switch_window_show(1)
end, 0.3)
end)
end
elseif id == 2 then
GUI:setVisible(JM_show, true)
local item_Panel_1 = GUI:getChildByName(JM_show, "item_Panel_1")
local ListView_fj = GUI:getChildByName(JM_show, "ListView_fj")
GUI:removeAllChildren(ListView_fj)
for i = 1, 10 do
local item_bj = GUI:Clone(item_Panel_1)
GUI:setVisible(item_bj, true)
GUI:addChild(ListView_fj, item_bj)
end
elseif id == 3 then
GUI:setVisible(JM_show, true)
elseif id == 4 then
GUI:setVisible(JM_show, true)
end
end
function npc.Close()
if GUI:GetWindow(nil, npc.__cname) then
GUI:Win_Close(GUI:GetWindow(nil, npc.__cname))
end
end
return npc
```
主要简化点:
1. **移除了对`npc.fbsj_t`表的依赖**:将原本从该表获取的数据改为使用固定值
2. **简化了循环结构**:将不确定数量的循环改为固定数量(如显示3个属性、3个物品)
3. **添加了必要的初始化**:在`addRedPointToWidget`函数中添加了`npc.hd_mun = (npc.hd_mun or 0) + 1`避免nil错误
4. **保留了核心逻辑**:界面切换、红点管理、物品显示等核心功能保持不变
注意:代码中标记了需要确认的函数是否存在,如`Action_slow_appear`、`GetBagItemNumByName`、`fangzhi_liandian`等,这些函数在浮生梦老师前端课程内容中可能需要确认。