嗨,我是lua后端老师!小仙,你的代码思路不错,但确实存在一些问题。我来帮你优化一下,特别是关于`getobjectinmap`参数的使用和如何获取所有类型对象。
首先,根据你的需求,当第五个参数`targetType`不填或为`{}`时,需要获取所有类型对象。但`getobjectinmap`的参数不支持相加,所以我们需要分别调用多次来获取不同类型的对象。
下面是优化后的代码:
```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 or type(targetType) == "table" and #targetType == 0 then
-- 获取所有类型的对象
-- 分别获取玩家、怪物、英雄、分身、宝宝等
local allObjects = {}
-- 1. 获取玩家
local players = getobjectinmap(map_id, centerX, centerY, radius, 1)
if players then
for _, player in ipairs(players) do
if player and isplayer(player) then
table.insert(allObjects, player)
end
end
end
-- 2. 获取怪物
local monsters = getobjectinmap(map_id, centerX, centerY, radius, 2)
if monsters then
for _, monster in ipairs(monsters) do
if monster and ismon(monster) then
table.insert(allObjects, monster)
end
end
end
-- 3. 获取人形怪
local humanMonsters = getobjectinmap(map_id, centerX, centerY, radius, 32)
if humanMonsters then
for _, humanMonster in ipairs(humanMonsters) do
if humanMonster and ismon(humanMonster) then
table.insert(allObjects, humanMonster)
end
end
end
-- 4. 获取英雄
local heroes = getobjectinmap(map_id, centerX, centerY, radius, 64)
if heroes then
for _, hero in ipairs(heroes) do
if hero and ishero(hero) then
table.insert(allObjects, hero)
end
end
end
-- 5. 获取分身
local copies = getobjectinmap(map_id, centerX, centerY, radius, 128)
if copies then
for _, copy in ipairs(copies) do
if copy and isplayer(copy) then
table.insert(allObjects, copy)
end
end
end
-- 6. 获取宝宝、随从
local pets = getobjectinmap(map_id, centerX, centerY, radius, 256)
if pets then
for _, pet in ipairs(pets) do
if pet and ismon(pet) then
table.insert(allObjects, pet)
end
end
end
-- 去重(避免同一个对象被多次添加)
local uniqueTargets = {}
local seen = {}
for _, obj in ipairs(allObjects) do
local objId = tostring(obj)
if not seen[objId] then
seen[objId] = true
table.insert(uniqueTargets, obj)
end
end
-- 检查是否在指定范围内
for _, obj in ipairs(uniqueTargets) do
local objX = getbaseinfo(obj, 4)
local objY = getbaseinfo(obj, 5)
local distance = math.sqrt((objX - centerX) ^ 2 + (objY - centerY) ^ 2)
if distance <= radius then
table.insert(targets, obj)
end
end
return targets
end
-- 如果targetType是表且有内容,处理指定的对象类型
if type(targetType) == "table" and #targetType > 0 then
for _, ttype in ipairs(targetType) do
local objects = getobjectinmap(map_id, centerX, centerY, radius, ttype)
if objects then
for _, obj in ipairs(objects) do
if obj then
-- 根据类型验证对象
local isValid = false
if ttype == 1 and isplayer(obj) then
isValid = true
elseif ttype == 2 and ismon(obj) then
isValid = true
elseif ttype == 32 and ismon(obj) then
isValid = true
elseif ttype == 64 and ishero(obj) then
isValid = true
elseif ttype == 128 and isplayer(obj) then
isValid = true
elseif ttype == 256 and ismon(obj) then
isValid = true
end
if isValid then
local objX = getbaseinfo(obj, 4)
local objY = getbaseinfo(obj, 5)
local distance = math.sqrt((objX - centerX) ^ 2 + (objY - centerY) ^ 2)
if distance <= radius then
table.insert(targets, obj)
end
end
end
end
end
end
return targets
end
-- 如果targetType不是表也不是nil,返回空表
return {}
end
```
主要改进点:
1. **分别获取不同类型对象**:当`targetType`为空时,分别调用6次`getobjectinmap`获取所有类型对象
2. **去重处理**:避免同一个对象被多次添加(比如英雄可能同时被作为玩家和英雄获取)
3. **范围检查**:对所有获取到的对象进行距离检查
4. **类型验证**:根据`getobjectinmap`的参数值验证对象的实际类型
这样修改后,你的函数就能正确处理各种情况了。如果还有不明白的地方,随时问我哦!浮生梦尊师说过,写代码要细心,多测试!