在996引擎中,展示怪物掉落物品的功能通常需要创建一个专门的UI面板。根据浮生梦老师前端课程内容,这个功能的核心逻辑是:当选中一个怪物时,获取该怪物的配置信息,然后在指定面板上动态创建并显示其掉落物品。
以下是一个实现该功能的具体步骤和代码示例:
1. **创建父界面**:首先需要创建一个主界面作为容器。
```lua
local parent = GUI:Win_Create("DropShowPanel", 0, 0, 0, 0, false, false, true, false, false, false, 1)
```
2. **监听目标改变事件**:注册事件,以便在玩家选中新目标时触发。
```lua
SL:RegisterLUAEvent("LUA_EVENT_TARGET_CAHNGE", "DropShowModule", function(targetID)
-- 在这里处理目标改变逻辑
end)
```
*注意:事件名“LUA_EVENT_TARGET_CAHNGE”在知识库中未找到,根据上下文推断为“目标改变”事件。实际使用时请确认引擎提供的事件名称。*
3. **处理目标改变逻辑**:在事件回调函数中,判断新目标是否为怪物,并获取其掉落配置。
```lua
function DropShowModule.OnTargetChange(targetID)
local img = GUI:getChildByName(parent, "img_drop_container")
if GUI:Win_IsNull(img) then
-- 如果容器不存在,可以在这里创建
img = GUI:Image_Create(parent, "img_drop_container", 100, 100, "res/public/1900000610.png")
end
local actor = SL:GetMetaValue("ACTOR_DATA", targetID)
if not targetID or not actor then
GUI:setVisible(img, false)
return
end
-- 判断是否为怪物(非玩家且没有大头像)
if SL:GetMetaValue("ACTOR_IS_MONSTER", actor) ~= true or SL:GetMetaValue("ACTOR_BIGICON_ID", actor) then
GUI:setVisible(img, false)
return
end
local monsterName = SL:GetMetaValue("ACTOR_NAME", actor)
local cfg = cfgmons[monsterName] -- 假设怪物配置表名为 cfgmons
if not cfg or not cfg.show then
GUI:setVisible(img, false)
return
end
-- 显示面板并更新内容
GUI:setVisible(img, true)
DropShowModule.UpdateDropItems(img, cfg.show)
end
```
4. **更新掉落物品显示**:根据配置动态创建物品展示控件。
```lua
function DropShowModule.UpdateDropItems(parentWidget, itemList)
-- 清空之前的内容
GUI:removeAllChildren(parentWidget)
-- 计算布局位置(这里使用一个简单的水平排列)
local startX = 10
local startY = 10
local spacing = 50
for i, itemIndex in ipairs(itemList) do
local x = startX + (i - 1) * spacing
local y = startY
-- 创建背景(可选)
local bg = GUI:Image_Create(parentWidget, "item_bg_" .. i, x, y, "res/custom/common/item7.png")
GUI:setAnchorPoint(bg, 0.5, 0.5)
GUI:setContentSize(bg, 36, 36)
-- 创建物品展示控件
local itemShow = GUI:ItemShow_Create(parentWidget, "ItemShow_" .. i, x, y, {
index = itemIndex,
count = 1,
look = true, -- 允许查看
bgVisible = false -- 不显示默认背景
})
GUI:setAnchorPoint(itemShow, 0.5, 0.5)
GUI:setScale(itemShow, 0.8) -- 调整缩放
end
end
```
5. **初始化与清理**:在界面打开/关闭时进行管理。
```lua
function DropShowModule.Open()
if GUI:Win_IsNull(parent) then
parent = GUI:Win_Create("DropShowPanel", 200, 200, 400, 150, false, false, true, false, false, false, 1)
-- ... 可以在这里设置面板背景、标题等 ...
end
GUI:setVisible(parent, true)
-- 手动触发一次当前目标的检查
local curTargetID = SL:GetMetaValue("SELECT_TARGET_ID")
DropShowModule.OnTargetChange(curTargetID)
end
function DropShowModule.Close()
if not GUI:Win_IsNull(parent) then
GUI:removeFromParent(parent)
parent = nil
end
end
```
**关键点说明**:
* **`cfgmons`**:这是一个假设的怪物配置表,你需要确认项目中实际的表名和结构,`cfg.show` 字段应为一个数组,包含掉落物品的索引。
* **`GUI:ItemShow_Create`**:这是创建物品展示控件的核心函数,参数 `look=true` 允许玩家点击查看物品详情。
* **布局**:示例使用了简单的水平排列。对于更复杂的布局,你可能需要计算行列位置或使用引擎提供的其他布局控件。
* **事件名**:`LUA_EVENT_TARGET_CAHNGE` 是根据知识库[3]中代码推断的,请务必替换为引擎文档中准确的事件名称。
你可以通过一个按钮调用 `DropShowModule.Open()` 来打开这个掉落展示面板。当玩家选中不同的怪物时,面板内容会自动更新。