function godTier_skill_trigger:GetTargetsInRange(actor, centerX, centerY, radius, targetType, damagetype)
local targets = {}
local map_id = getbaseinfo(actor, 3)
local actor_mode = getattackmode(actor) -- 获取当前玩家的攻击模式
local group_m
我是lua后端老师!
小仙,你这代码写得很用心啊!不过我看到你在处理攻击模式逻辑时,有些地方需要优化,特别是最后那个注释里提到的“检测如果获取到对象为宝宝或者分身或者英雄时就判断他的主人是是否是跟我同阵营的”这个需求,你的代码里还没有实现。
让我帮你分析一下:
1. **攻击模式逻辑问题**:你在`actor_mode == 4`(编组攻击)的循环里,每次循环都会插入目标,这会导致重复添加。应该先判断目标是否在编组内。
2. **缺少阵营判断**:你需要使用`getcamp`接口来获取阵营ID,然后进行比较。
3. **对象类型判断**:需要使用`isplaymon`判断宝宝,`ishero`判断英雄,`ismob`判断分身。
下面是修改后的代码,我帮你添加了阵营判断逻辑:
```lua
function godTier_skill_trigger:GetTargetsInRange(actor, centerX, centerY, radius, targetType, damagetype)
local targets = {}
local map_id = getbaseinfo(actor, 3)
local actor_mode = getattackmode(actor) -- 获取当前玩家的攻击模式
local group_members = getgroupmember(actor) -- 获取当前玩家的编组成员对象
local actor_guild = getmyguild(actor) -- 获取自己的行会ID
local actor_camp = getcamp(actor) -- 获取自己的阵营ID -- 新增
-- 攻击模式: 0-全部攻击 1-和平攻击 2-夫妻攻击 3-师徒攻击 4-编组攻击 5-行会攻击 6-红名攻击 7-国家攻击
local mode = { 0, 1, 2, 3, 4, 5, 6, 7 }
-- 定义所有可获取的对象类型参数
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 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
if not damagetype then -- 判断是伤害性技能
if actor_mode == 0 then -- 全部攻击
table.insert(targets, obj)
elseif actor_mode == 1 then -- 平和攻击
targets = {}
elseif actor_mode == 2 then -- 夫妻模式 ---当前未实现为全部模式
table.insert(targets, obj)
elseif actor_mode == 3 then -- 师徒攻击 ---当前未实现为全部模式
table.insert(targets, obj)
elseif actor_mode == 4 then -- 编组攻击
local inGroup = false
for _, member in pairs(group_members) do -- 获取编组成员对象是否在我编组内
if obj == member then
inGroup = true
break
end
end
if not inGroup then
table.insert(targets, obj)
end
elseif actor_mode == 5 then -- 行会攻击
local obj_guild = getmyguild(obj) -- 获取目标的行会对象
local isnotat = false
-- 首先检查目标是否有行会
if obj_guild == "" or obj_guild == nil then
isnotat = true -- 无行会玩家可以被攻击
else
-- 判断是否是不同行会的玩家
if actor_guild ~= obj_guild then
-- 还需要检查是否是同盟行会
local isAlly = isallyguild(actor_guild, obj_guild) -- 判断是否结盟
if not isAlly then
isnotat = true
end
end
end
if isnotat then
table.insert(targets, obj)
end
elseif actor_mode == 6 then -- 红名攻击
local obj_pklevel = getpklevel(obj) -- 获取目标的PK等级
if obj_pklevel > 0 then -- 有PK等级玩家可以被攻击
table.insert(targets, obj)
end
elseif actor_mode == 7 then -- 国家攻击
table.insert(targets, obj)
end
else
-- 非伤害性技能,直接添加目标
table.insert(targets, obj)
end
-- 新增:检测宝宝、分身、英雄的阵营判断
if damagetype then -- 如果是伤害性技能
local shouldAdd = true
-- 判断是否为宝宝
if isplaymon(obj) then
local master = getmonmaster(obj) -- 获取宝宝的主人
if master then
local master_camp = getcamp(master)
if master_camp == actor_camp then
shouldAdd = false -- 同阵营,不攻击
end
end
-- 判断是否为英雄
elseif ishero(obj) then
local hero_owner = gethero(obj) -- 获取英雄的主人
if hero_owner then
local owner_camp = getcamp(hero_owner)
if owner_camp == actor_camp then
shouldAdd = false -- 同阵营,不攻击
end
end
-- 判断是否为分身
elseif ismob(obj) then
local mob_owner = getmonmaster(obj) -- 获取分身的主人
if mob_owner then
local owner_camp = getcamp(mob_owner)
if owner_camp == actor_camp then
shouldAdd = false -- 同阵营,不攻击
end
end
end
-- 如果shouldAdd为false,从targets中移除
if not shouldAdd then
for i = #targets, 1, -1 do
if targets[i] == obj then
table.remove(targets, i)
break
end
end
end
end
end
end
end
end
end
-- dump(targets) -- 调试用,可注释掉
return targets
end
end
```
**主要修改点:**
1. **修复编组攻击逻辑**:添加了`inGroup`变量来判断目标是否在编组内
2. **添加阵营获取**:使用`getcamp(actor)`获取玩家阵营
3. **添加阵营判断逻辑**:
- 使用`isplaymon`判断宝宝
- 使用`ishero`判断英雄
- 使用`ismob`判断分身
- 通过`getmonmaster`获取宝宝/分身的主人
- 通过`gethero`获取英雄的主人
- 比较阵营ID,同阵营则不攻击
小仙,这样修改后,你的技能就能正确处理宝宝、分身和英雄的阵营关系了。如果还有不明白的地方,随时问我哦!