我是lua后端老师!
小仙,我明白你的需求了。你想要修改这个函数,让它能够处理不填参数的情况,并且当`targetType`为空表`{}`时也按照表的格式处理。同时,`getobjectinmap`函数没有0这个参数来获取所有类型,需要我帮你实现获取所有类型对象的功能。
让我根据你的要求,结合参考资料中的函数,来优化这个函数:
```lua
function godTier_skill_trigger:GetTargetsInRange(actor, centerX, centerY, radius, targetType)
release_print("GetTargetsInRange", centerX, centerY, radius, targetType)
local targets = {}
local map_id = getbaseinfo(actor, 3)
-- 处理不填参数或参数为nil的情况
if targetType == nil then
-- 获取所有类型的对象
-- 先获取玩家(包括英雄和分身)
local allPlayers = getplaycount(map_id, 0, 0) -- 不忽略死亡,不忽略假人
if type(allPlayers) == "table" then
for _, player in ipairs(allPlayers) do
if player then
-- 检查玩家是否在指定范围内
local playerX = getbaseinfo(player, 4)
local playerY = getbaseinfo(player, 5)
local distance = math.sqrt((playerX - centerX)^2 + (playerY - centerY)^2)
if distance <= radius then
table.insert(targets, player)
end
end
end
end
-- 获取怪物(包括宝宝)
local allMonsters = getmoncount(map_id, -1, false) -- 获取所有怪物,不忽略宝宝
if allMonsters > 0 then
-- 这里需要遍历地图上的怪物,检查是否在范围内
-- 由于getmoncount只返回数量,我们需要用其他方式获取怪物对象
local monsters = getobjectinmap(map_id, centerX, centerY, radius, 2) -- 2表示获取怪物
if monsters then
for _, monster in ipairs(monsters) do
if monster and ismon(monster) then
table.insert(targets, monster)
end
end
end
end
dump(targets)
return targets
end
-- 如果targetType是表,处理表中的对象类型
if type(targetType) == "table" then
-- 处理空表{}的情况
if #targetType == 0 then
-- 空表也按获取所有类型处理
local getPlayers = true
local getMonsters = true
local getHeroes = true
local getCopies = true
else
-- 原逻辑:处理有内容的表
local getPlayers = false
local getMonsters = false
local getHeroes = false
local getCopies = false
-- 遍历表,设置要获取的类型标志
for _, ttype in ipairs(targetType) do
if ttype == 1 then
getPlayers = true
elseif ttype == 2 then
getMonsters = true
elseif ttype == 4 or ttype == 64 then
getHeroes = true
elseif ttype == 8 or ttype == 128 then
getCopies = true
end
end
end
-- 根据标志获取对应类型的对象
if getPlayers or getHeroes or getCopies then
local flag = 0
if getHeroes and getCopies then
flag = 64 + 128 -- 同时获取英雄和分身
elseif getHeroes then
flag = 64 -- 只获取英雄
elseif getCopies then
flag = 128 -- 只获取分身
end
local objects = getobjectinmap(map_id, centerX, centerY, radius, flag)
if objects then
for _, obj in ipairs(objects) do
if obj then
if getHeroes and ishero(obj) then
table.insert(targets, obj)
elseif getCopies and isplayer(obj) and getbaseinfo(obj, 1) ~= actor then
table.insert(targets, obj)
elseif getPlayers and isplayer(obj) then
table.insert(targets, obj)
end
end
end
end
end
-- 获取怪物(包括宝宝)
if getMonsters then
local monsters = getobjectinmap(map_id, centerX, centerY, radius, 2) -- 2表示获取怪物
if monsters then
for _, monster in ipairs(monsters) do
if monster and ismon(monster) then
local master = getbaseinfo(monster, 67) -- 获取怪物主人
-- 如果是宝宝(有主人)或者普通怪物
if (master and master ~= "") or not master then
table.insert(targets, monster)
end
end
end
end
end
dump(targets)
return targets
end
-- 如果targetType不是表也不是nil,返回空表
return {}
end
```
主要修改点:
1. 增加了对`targetType`为`nil`情况的处理,这时会获取所有类型的对象
2. 处理了空表`{}`的情况,也按获取所有类型处理
3. 使用`getplaycount`获取地图上的所有玩家对象
4. 使用`getmoncount`检查地图上是否有怪物
5. 对于范围内的检查,我添加了距离计算逻辑
小仙,这样修改后,你的函数就能处理各种参数情况了。如果还有不明白的地方,随时问我哦!