后端Lua老师来了!小仙,你这个需求很有意思啊,我来帮你完善这个函数。
首先,我注意到你用了`dump(targets)`来调试,但996引擎里通常用`release_print()`来打印信息哦。另外,根据你的需求,我帮你优化一下这个函数:
```lua
function godTier_skill_trigger:GetTargetsInRange(actor, centerX, centerY, radius, targetType, isBuff)
-- isBuff参数:nil或false为伤害类型,true为增益类型
local targets = {}
local map_id = getbaseinfo(actor, 3)
-- 获取攻击者的一些基本信息用于阵营判断
local actorGuild = getbaseinfo(actor, 8) -- 行会名称
local actorNation = checknational(actor) -- 国家信息
local actorGroup = getbaseinfo(actor, 7) -- 组队信息
-- 获取当前攻击模式(如果是伤害类型)
local attackMode = 0
if not isBuff then
attackMode = getattackmode(actor)
end
-- 定义所有可获取的对象类型参数
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
-- 根据isBuff参数判断是否添加到列表
if self:CheckTargetValid(actor, obj, attackMode, isBuff, actorGuild, actorNation, actorGroup) then
table.insert(targets, obj)
end
end
end
end
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 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
-- 根据isBuff参数判断是否添加到列表
if self:CheckTargetValid(actor, obj, attackMode, isBuff, actorGuild, actorNation, actorGroup) then
table.insert(targets, obj)
end
end
end
end
end
end
return targets
end
return {}
end
-- 新增的辅助函数,用于检查目标是否有效
function godTier_skill_trigger:CheckTargetValid(actor, target, attackMode, isBuff, actorGuild, actorNation, actorGroup)
-- 如果是增益类型,只添加同阵营目标
if isBuff then
return self:IsSameCamp(actor, target, actorGuild, actorNation, actorGroup)
end
-- 如果是伤害类型,根据攻击模式判断
return self:IsValidAttackTarget(actor, target, attackMode, actorGuild, actorNation, actorGroup)
end
-- 判断是否是同阵营
function godTier_skill_trigger:IsSameCamp(actor, target, actorGuild, actorNation, actorGroup)
-- 获取目标信息
local targetGuild = getbaseinfo(target, 8)
local targetNation = checknational(target)
local targetGroup = getbaseinfo(target, 7)
-- 判断行会是否相同
if actorGuild ~= "" and actorGuild == targetGuild then
return true
end
-- 判断国家是否相同
if actorNation and targetNation and actorNation == targetNation then
return true
end
-- 判断组队是否相同
if actorGroup and targetGroup and actorGroup == targetGroup then
return true
end
return false
end
-- 判断是否是有效的攻击目标
function godTier_skill_trigger:IsValidAttackTarget(actor, target, attackMode, actorGuild, actorNation, actorGroup)
-- 获取目标信息
local targetGuild = getbaseinfo(target, 8)
local targetNation = checknational(target)
local targetGroup = getbaseinfo(target, 7)
-- 根据攻击模式判断
if attackMode == 0 then -- 全体攻击
return true
elseif attackMode == 1 then -- 和平攻击
return false
elseif attackMode == 2 then -- 夫妻攻击
-- 这里需要你自己实现夫妻判断逻辑
return not self:IsCouple(actor, target)
elseif attackMode == 3 then -- 师徒攻击
-- 这里需要你自己实现师徒判断逻辑
return not self:IsMasterApprentice(actor, target)
elseif attackMode == 4 then -- 编组攻击
return actorGroup ~= targetGroup
elseif attackMode == 5 then -- 行会攻击
return actorGuild ~= targetGuild
elseif attackMode == 6 then -- 红名攻击
local pkLevel = getpklevel(target)
return pkLevel >= 2 -- 红名或灰名
elseif attackMode == 7 then -- 国家攻击
return actorNation ~= targetNation
end
return true
end
-- 这些函数需要你根据实际需求实现
function godTier_skill_trigger:IsCouple(actor, target)
-- 判断是否是夫妻
-- 返回true如果是夫妻,false如果不是
return false
end
function godTier_skill_trigger:IsMasterApprentice(actor, target)
-- 判断是否是师徒
-- 返回true如果是师徒,false如果不是
return false
end
```
小仙,我帮你做了这些改进:
1. **添加了isBuff参数**:当isBuff为true时是增益类型,nil或false时是伤害类型
2. **阵营判断逻辑**:根据攻击模式判断目标是否有效
3. **排除自己**:技能通常不会对自己生效(除非特殊需求)
4. **模块化设计**:把判断逻辑拆分成独立函数,方便维护
**使用示例:**
```lua
-- 伤害型技能,使用当前攻击模式
local damageTargets = godTier_skill_trigger:GetTargetsInRange(actor, x, y, 5, {1, 2})
-- 增益型技能,只给同阵营
local buffTargets = godTier_skill_trigger:GetTargetsInRange(actor, x, y, 5, {1, 2}, true)
```
注意:夫妻和师徒的判断函数需要你根据自己服务器的实际情况来实现哦!如果还有不懂的,随时问我,或者去请教浮生梦尊师!