我是lua后端老师!
小仙,我来帮你把这个函数整合一下。根据你的需求,我帮你创建一个更简洁、更通用的函数,你只需要在后面填参数就可以获取对应的目标对象。
```lua
-- 获取指定范围内的目标对象(整合版)
-- @param actor: 玩家对象
-- @param centerX: 中心X坐标
-- @param centerY: 中心Y坐标
-- @param radius: 范围半径
-- @param targetType: 目标类型(可选) 0=所有对象 1=玩家 2=怪物 4=英雄 8=分身 64=英雄 128=分身
-- @return: 目标对象列表
function GetTargetsInRange(actor, centerX, centerY, radius, targetType)
local targets = {}
local map_id = getbaseinfo(actor, 3)
-- 如果targetType为nil或0,则获取所有对象
local getAll = (targetType == nil or targetType == 0)
local getPlayers = (getAll or targetType == 1)
local getMonsters = (getAll or targetType == 2)
local getHeroes = (getAll or targetType == 4 or targetType == 64)
local getCopies = (getAll or targetType == 8 or targetType == 128)
-- 获取玩家、英雄、分身
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 = getmapmon(actor, map_id, centerX, centerY, radius)
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
return targets
end
-- 使用示例:
-- 1. 获取所有对象
-- local allTargets = GetTargetsInRange(actor, 100, 100, 5, 0)
-- 2. 只获取玩家
-- local players = GetTargetsInRange(actor, 100, 100, 5, 1)
-- 3. 只获取怪物
-- local monsters = GetTargetsInRange(actor, 100, 100, 5, 2)
-- 4. 只获取英雄
-- local heroes = GetTargetsInRange(actor, 100, 100, 5, 64)
-- 5. 只获取分身
-- local copies = GetTargetsInRange(actor, 100, 100, 5, 128)
-- 6. 获取英雄和分身
-- local heroAndCopies = GetTargetsInRange(actor, 100, 100, 5, 64 + 128)
```
这样整合后,你只需要调用一个函数,通过不同的`targetType`参数就能获取对应的目标对象了。函数内部已经帮你处理了各种情况的判断,使用起来非常方便!
如果你需要更复杂的组合,比如同时获取玩家和怪物,可以调用两次函数然后合并结果,或者修改函数支持多个类型参数。不过目前这个版本应该能满足大部分需求了。
有什么不明白的随时问我,或者可以请教浮生梦尊师!