Bag = {}
function Bag.Init(isWin32)
-- 网格配置
Bag._ScrollHeight = isWin32 and 214 or 320 -- 容器滚动区域的高度
Bag._PWidth = isWin32 and 338 or 500 -- 容器可见区域 宽
Bag._PHeight = isWin32 and 214 or 320 -- 容器可见区域 高
Bag._IW
```lua
-- 新增:自动装备检测函数
function Bag.CheckAndAutoEquip()
-- 获取背包物品数据
local bagItems = SL:GetMetaValue("BAG_ITEMS")
-- 装备ID与名称映射
local equipMap = {
[12256] = "九天玄气ツ阴阳逆乱",
[12257] = "乾坤定星盘",
[12258] = "鬼新娘の呼唤",
[12259] = "帝乙木敕令",
[12260] = "无极剑魂·道天一心"
}
-- 检查每个装备ID
for equipId, equipName in pairs(equipMap) do
-- 检查背包中是否有该装备
local hasEquip = false
if bagItems then
for _, item in ipairs(bagItems) do
if item.id == equipId then
hasEquip = true
break
end
end
end
-- 如果背包中有装备且装备槽位为空,则自动装备
if hasEquip then
local emptySlot = Bag.FindEmptyEquipmentSlot()
if emptySlot then
-- 添加到装备列表
Bag._equipmentList[emptySlot] = equipName
-- 发送穿戴装备消息到服务端
SL:SendMsgToServer("WEAR_EQUIPMENT", {
equipId = equipId,
slotIndex = emptySlot
})
SL:Print("自动装备:" .. equipName .. " 到槽位" .. emptySlot)
end
end
end
-- 更新装备显示
Bag.UpdateEquipmentDisplay()
end
-- 新增:查找空装备槽位
function Bag.FindEmptyEquipmentSlot()
for i = 1, 10 do
if not Bag._equipmentList[i] then
return i
end
end
return nil
end
-- 修改Bag.main函数,添加自动装备检测
function Bag.main(page)
local parent = GUI:Attach_Parent()
local isWin32 = SL:GetMetaValue("WINPLAYMODE")
GUI:LoadExport(parent, isWin32 and "bag/bag_panel_win32" or "bag/bag_panel")
Bag._ui = GUI:ui_delegate(parent)
Bag._UI_ScrollView = Bag._ui["ScrollView_items"]
-- 初始化数据
Bag.Init(isWin32)
-- 新增:初始化装备滚动容器
Bag.InitEquipmentContainer()
-- 适配
GUI:setPositionY(Bag._ui["Panel_1"], isWin32 and SL:GetMetaValue("PC_POS_Y") or SL:GetMetaValue("SCREEN_HEIGHT") / 2)
-- 界面拖动
GUI:Win_SetDrag(parent, Bag._ui["Image_bg"])
-- 界面浮起
GUI:Win_SetZPanel(parent, Bag._ui["Image_bg"])
GUI:addOnClickEvent(Bag._ui["Button_close"], function()
SL:CloseBagUI()
end)
-- 新增:打开背包时自动检测装备
Bag.CheckAndAutoEquip()
-- 存入英雄背包
local Button_store_hero_bag = Bag._ui["Button_store_hero_bag"]
GUI:addOnClickEvent(Button_store_hero_bag, function()
local changeStoreMode = not Bag._changeStoreMode
if changeStoreMode then
local isActiveHero = SL:GetMetaValue("HERO_IS_ACTIVE")
if not isActiveHero then
return SL:ShowSystemTips("英雄还未激活")
end
local isCallHero = SL:GetMetaValue("HERO_IS_ALIVE")
if not isCallHero then
return SL:ShowSystemTips("英雄还未召唤")
end
end
Bag._changeStoreMode = changeStoreMode
GUI:Button_setGrey(Button_store_hero_bag, changeStoreMode)
end)
GUI:setVisible(Button_store_hero_bag, SL:GetMetaValue("USEHERO"))
-- 初始化左侧背包页签
Bag.InitPage()
Bag.PageTo(page or 1)
Bag.OnUpdateGold()
Bag.RegisterEvent()
end
-- 修改注册事件,添加背包物品更新监听
function Bag.RegisterEvent()
SL:RegisterLUAEvent(LUA_EVENT_MONEYCHANGE, "Bag", Bag.OnUpdateGold)
SL:RegisterLUAEvent(LUA_EVENT_CLOSEWIN, "Bag", Bag.OnClose)
-- 注册背包物品更新事件
SL:RegisterLUAEvent(LUA_EVENT_BAGUPDATE, "Bag", function(data)
-- 背包物品更新时重新检测装备
Bag.CheckAndAutoEquip()
end)
-- 注册装备更新事件
SL:RegisterLUAEvent("EQUIPMENT_UPDATE", "Bag", function(data)
Bag.UpdateEquipmentDisplay()
end)
end
-- 修改UpdateEquipmentDisplay函数,使用实际装备图标
function Bag.UpdateEquipmentDisplay()
for i = 1, 10 do
local slot = Bag._equipmentSlots[i]
if slot then
if Bag._equipmentList[i] then
-- 有装备的情况
local equipmentName = Bag._equipmentList[i]
GUI:Text_setString(slot.name, equipmentName)
GUI:Text_setTextColor(slot.name, "#ffffff")
-- 设置装备图标可见
GUI:setVisible(slot.icon, true)
-- 根据装备名称设置不同的图标颜色或效果
if equipmentName == "九天玄气ツ阴阳逆乱" then
GUI:setColor(slot.icon, cc.c3b(255, 0, 0)) -- 红色
elseif equipmentName == "乾坤定星盘" then
GUI:setColor(slot.icon, cc.c3b(0, 255, 0)) -- 绿色
elseif equipmentName == "鬼新娘の呼唤" then
GUI:setColor(slot.icon, cc.c3b(255, 0, 255)) -- 紫色
else
GUI:setColor(slot.icon, cc.c3b(255, 255, 255)) -- 白色
end
else
-- 空槽位
GUI:Text_setString(slot.name, "空槽位")
GUI:Text_setTextColor(slot.name, "#888888")
GUI:setVisible(slot.icon, false)
end
end
end
end
```
这段代码实现了以下功能:
1. **自动装备检测**:打开背包时自动检查是否有指定装备,并放入右侧装备槽
2. **装备ID映射**:将装备ID与名称对应,方便识别
3. **空槽位查找**:自动找到第一个可用的装备槽位
4. **服务端通信**:装备时发送消息到服务端记录
5. **装备显示优化**:根据装备类型设置不同的图标颜色效果
**注意**:装备图标路径不需要特别设置,因为装备本身在背包中已有图标。这里通过设置颜色来区分不同装备的视觉效果。实际项目中,装备图标会从物品配置中自动获取。