只在范围内有敌人时才进行伤害

This commit is contained in:
2026-02-05 16:21:22 +08:00
parent ec20ce79f1
commit fae09fcd38
6 changed files with 80 additions and 3 deletions

View File

@@ -27,7 +27,7 @@ namespace ArachnaeSwarm
public override void CompTick()
{
base.CompTick();
if (!parent.Spawned || !enabled)
return;
@@ -37,6 +37,18 @@ namespace ArachnaeSwarm
CleanupOldEffecters();
}
// 如果设置为只在有敌人时才伤害,检查是否有敌人
if (Props.onlyDamageWhenEnemiesPresent)
{
// 如果没有敌人,重置计时并返回
if (!HasEnemiesInRange())
{
ticksUntilNextDamage = Props.damageIntervalTicks;
return;
}
}
// 只有在有敌人时才进行计时和伤害
ticksUntilNextDamage--;
if (ticksUntilNextDamage <= 0)
{
@@ -79,6 +91,12 @@ namespace ArachnaeSwarm
if (map == null)
return;
// 如果设置为只在有敌人时才伤害,检查是否有敌人
if (Props.onlyDamageWhenEnemiesPresent && !HasEnemiesInRange())
{
return;
}
// 获取范围内的所有物体
List<Thing> thingsInRange = new List<Thing>();
foreach (IntVec3 cell in GenRadial.RadialCellsAround(parent.Position, Props.radius, true))
@@ -174,6 +192,61 @@ namespace ArachnaeSwarm
return thing is Building || thing is Pawn;
}
/// <summary>
/// 检查范围内是否有敌人
/// </summary>
private bool HasEnemiesInRange()
{
Map map = parent.Map;
if (map == null)
return false;
foreach (IntVec3 cell in GenRadial.RadialCellsAround(parent.Position, Props.radius, true))
{
if (!cell.InBounds(map))
continue;
List<Thing> thingList = cell.GetThingList(map);
foreach (Thing thing in thingList)
{
if (IsValidTargetType(thing) && IsHostileTarget(thing))
{
return true;
}
}
}
return false;
}
/// <summary>
/// 检查目标是否为敌对目标
/// </summary>
private bool IsHostileTarget(Thing thing)
{
Faction parentFaction = parent.Faction;
Faction targetFaction = thing.Faction;
// 如果父物体没有派系,所有有派系的目标都视为敌对
if (parentFaction == null)
{
return targetFaction != null && !targetFaction.IsPlayer;
}
// 如果目标是Pawn且敌对
if (thing is Pawn pawn)
{
return pawn.Faction != null && pawn.Faction.HostileTo(parentFaction);
}
// 如果目标是建筑且属于敌对派系
else if (thing is Building building)
{
return building.Faction != null && building.Faction.HostileTo(parentFaction);
}
return false;
}
private bool IsValidTarget(Thing thing)
{
// 首先检查是否为建筑或Pawn双重检查

View File

@@ -50,6 +50,9 @@ namespace ArachnaeSwarm
public string toggleDescription = "Enable or disable the area damage effect"; // 开关描述
public string toggleIconPath; // 开关图标路径
// 优化设置
public bool onlyDamageWhenEnemiesPresent = false; // 是否只在范围内有敌人时才进行伤害
public CompProperties_AreaDamage()
{
compClass = typeof(CompAreaDamage);