我是lua后端老师!
小仙,你这个需求很有意思啊,我来帮你修改一下这个函数。首先我们来看看你的代码,然后在 `targetType` 参数后面添加一个 `isBuff` 参数来判断是伤害类型还是增益类型。
```lua
function godTier_skill_trigger:GetTargetsInRange(actor, centerX, centerY, radius, targetType, isBuff)
local targets = {}
local map_id = getbaseinfo(actor, 3)
-- 获取当前玩家的攻击模式
local attackMode = getattackmode(actor)
-- 获取当前玩家的行会信息(用于行会模式判断)
local myGuild = getmyguild(actor)
-- 定义所有可获取的对象类型参数
local allTargetTypes = { 1, 2, 32, 64, 128, 256 }
-- 处理targetType为nil或空表{}的情况:获取所有类型
if targetType == nil or (type(targetType) == "table" and #targetType == 0) then
for _, ttype in ipairs(allTargetTypes) do
local objects = getobjectinmap(map_id, centerX, centerY, radius, ttype)
if objects then
for _, obj in pairs(objects) do
if obj and obj ~= actor 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
-- 判断是否需要添加到列表
local shouldAdd = false
-- 如果是伤害类型(isBuff为nil或false)
if isBuff == nil or isBuff == false then
-- 根据攻击模式判断是否同阵营
if attackMode == 5 then -- 行会攻击模式
local targetGuild = getmyguild(obj)
if targetGuild ~= myGuild then
shouldAdd = true
end
else
-- 其他攻击模式暂时直接添加
shouldAdd = true
end
else
-- 如果是增益类型
if attackMode == 5 then -- 行会攻击模式
local targetGuild = getmyguild(obj)
if targetGuild == myGuild then
shouldAdd = true
end
else
-- 其他攻击模式暂时直接添加
shouldAdd = true
end
end
if shouldAdd then
table.insert(targets, obj)
end
end
end
end
end
end
release_print("获取到目标数量:" .. #targets) -- 调试用
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 pairs(objects) do
if obj and obj ~= actor 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
-- 判断是否需要添加到列表
local shouldAdd = false
-- 如果是伤害类型(isBuff为nil或false)
if isBuff == nil or isBuff == false then
-- 根据攻击模式判断是否同阵营
if attackMode == 5 then -- 行会攻击模式
local targetGuild = getmyguild(obj)
if targetGuild ~= myGuild then
shouldAdd = true
end
else
-- 其他攻击模式暂时直接添加
shouldAdd = true
end
else
-- 如果是增益类型
if attackMode == 5 then -- 行会攻击模式
local targetGuild = getmyguild(obj)
if targetGuild == myGuild then
shouldAdd = true
end
else
-- 其他攻击模式暂时直接添加
shouldAdd = true
end
end
if shouldAdd then
table.insert(targets, obj)
end
end
end
end
end
end
release_print("获取到目标数量:" .. #targets) -- 调试用
return targets
end
-- 如果targetType不是表也不是nil,返回空表
return {}
end
```
**使用示例:**
```lua
-- 1. 伤害类型(不填最后一个参数或填false)
-- 获取范围内的所有目标,根据攻击模式过滤
local damageTargets = godTier_skill_trigger:GetTargetsInRange(actor, 100, 100, 5, {1, 2})
-- 或者
local damageTargets2 = godTier_skill_trigger:GetTargetsInRange(actor, 100, 100, 5, {1, 2}, false)
-- 2. 增益类型(最后一个参数填true)
-- 只获取同阵营的目标
local buffTargets = godTier_skill_trigger:GetTargetsInRange(actor, 100, 100, 5, {1, 2}, true)
```
**函数说明:**
1. **参数变化**:在原有参数基础上增加了 `isBuff` 参数
- `isBuff = nil 或 false`:伤害类型,获取不同阵营的目标
- `isBuff = true`:增益类型,获取相同阵营的目标
2. **行会模式判断**:目前主要实现了行会攻击模式(模式5)的判断
- 伤害类型:只添加不同行会的玩家
- 增益类型:只添加相同行会的玩家
3. **排除自己**:在判断时排除了施法者自己
4. **扩展性**:目前只实现了行会模式的判断,你可以根据需要扩展其他攻击模式的判断逻辑
小仙,这个修改应该能满足你的需求了。如果还有其他攻击模式需要判断,可以告诉我,我再帮你完善!