This commit is contained in:
2025-11-29 11:15:01 +08:00
parent 728d52259c
commit 14e018e312
13 changed files with 248 additions and 36 deletions

View File

@@ -13,12 +13,93 @@ namespace WulaFallenEmpire
{
return pawn.Reserve(job.targetA, job, 1, -1, null, errorOnFailed);
}
/// <summary>
/// 更换武器逻辑
/// </summary>
private void SwitchWeapon()
{
if (pawn == null || pawn.Destroyed || !pawn.Spawned)
return;
try
{
// 1. 扔掉当前武器
ThingWithComps currentWeapon = pawn.equipment?.Primary;
if (currentWeapon != null)
{
// 将武器扔在地上
pawn.equipment.TryDropEquipment(currentWeapon, out ThingWithComps droppedWeapon, pawn.Position, true);
}
// 2. 从PawnKind允许的武器中生成新武器
ThingDef newWeaponDef = GetRandomWeaponFromPawnKind();
if (newWeaponDef != null)
{
// 生成新武器
Thing newWeapon = ThingMaker.MakeThing(newWeaponDef);
if (newWeapon is ThingWithComps newWeaponWithComps)
{
// 使用 AddEquipment 方法装备新武器
pawn.equipment.AddEquipment(newWeaponWithComps);
if (Prefs.DevMode)
{
Log.Message($"[CompAutonomousMech] {pawn.LabelCap} equipped new weapon: {newWeaponDef.LabelCap}");
}
}
}
}
catch (System.Exception ex)
{
Log.Error($"[CompAutonomousMech] Error switching weapon for {pawn?.LabelCap}: {ex}");
}
}
/// <summary>
/// 从PawnKind允许的武器中随机获取一个武器定义
/// </summary>
private ThingDef GetRandomWeaponFromPawnKind()
{
if (pawn.kindDef?.weaponTags == null || pawn.kindDef.weaponTags.Count == 0)
return null;
// 收集所有匹配的武器
List<ThingDef> availableWeapons = new List<ThingDef>();
foreach (string weaponTag in pawn.kindDef.weaponTags)
{
foreach (ThingDef thingDef in DefDatabase<ThingDef>.AllDefs)
{
if (thingDef.IsWeapon && thingDef.weaponTags != null && thingDef.weaponTags.Contains(weaponTag))
{
availableWeapons.Add(thingDef);
}
}
}
if (availableWeapons.Count == 0)
return null;
// 随机选择一个武器
return availableWeapons.RandomElement();
}
protected override IEnumerable<Toil> MakeNewToils()
{
// 关键修改:在任务开始时立即更换武器
yield return new Toil
{
initAction = () =>
{
SwitchWeapon();
},
defaultCompleteMode = ToilCompleteMode.Instant
};
// 前往回收器
yield return Toils_Goto.GotoThing(TargetIndex.A, PathEndMode.InteractionCell);
// 进入回收器
yield return new Toil
{

View File

@@ -23,10 +23,10 @@ namespace WulaFallenEmpire
public bool affectPrisoners = false;
public bool affectSlaves = false;
// 传送设置 - 移除冷却时间
public int checkIntervalTicks = 30; // 检查间隔
public int stunTicks = 30; // 传送后眩晕时间
public int maxPositionAdjustRadius = 5; // 最大位置调整半径
// 传送设置
public int checkIntervalTicks = 30;
public int stunTicks = 30;
public int maxPositionAdjustRadius = 5;
// 效果设置
public EffecterDef entryEffecter;
@@ -39,6 +39,14 @@ namespace WulaFallenEmpire
public ClamorDef destClamorType;
public float destClamorRadius = 2f;
// 新增:科技需求
public ResearchProjectDef requiredResearch;
public bool requireResearchToUse = false;
// 新增:开关控制
public bool canBeToggled = true;
public bool defaultEnabled = true;
public CompProperties_AreaTeleporter()
{
compClass = typeof(ThingComp_AreaTeleporter);

View File

@@ -25,15 +25,27 @@ namespace WulaFallenEmpire
// 硬编码的工作排除表
private static readonly HashSet<JobDef> ExcludedJobs = new HashSet<JobDef>
{
JobDefOf.GotoWander // 排除闲逛工作
JobDefOf.GotoWander
};
// 新增:开关状态
private bool enabled = true;
// 新增:初始化时设置默认状态
public override void Initialize(CompProperties props)
{
base.Initialize(props);
enabled = Props.defaultEnabled;
RegisterToNetwork();
}
// 新增:保存和加载开关状态
public override void PostExposeData()
{
base.PostExposeData();
Scribe_Values.Look(ref enabled, "teleporterEnabled", Props.defaultEnabled);
}
public override void PostSpawnSetup(bool respawningAfterLoad)
{
base.PostSpawnSetup(respawningAfterLoad);
@@ -52,12 +64,46 @@ namespace WulaFallenEmpire
UnregisterFromNetwork();
}
/// <summary>
/// 检查是否满足科技需求
/// </summary>
public bool HasRequiredResearch
{
get
{
// 如果没有设置科技需求或者不要求科技则返回true
if (Props.requiredResearch == null || !Props.requireResearchToUse)
return true;
// 检查科技是否已完成
return Props.requiredResearch.IsFinished;
}
}
/// <summary>
/// 检查是否应该显示传送器功能
/// </summary>
public bool ShouldDisplayFunctionality
{
get
{
// 如果拥有者是玩家,检查科技需求
if (parent.Faction == Faction.OfPlayer)
{
return HasRequiredResearch;
}
// 非玩家派系总是显示
return true;
}
}
/// <summary>
/// 注册到网络
/// </summary>
private void RegisterToNetwork()
{
if (parent?.Map == null) return;
if (parent?.Map == null || !enabled || !ShouldDisplayFunctionality) return;
var map = parent.Map;
if (!teleporterNetworks.ContainsKey(map))
@@ -105,11 +151,12 @@ namespace WulaFallenEmpire
/// </summary>
private bool IsPositionInNetworkRange(IntVec3 position)
{
if (parent?.Map == null) return false;
if (parent?.Map == null || !enabled || !ShouldDisplayFunctionality) return false;
foreach (var teleporter in GetNetworkTeleporters())
{
if (teleporter.parent?.Spawned == true &&
teleporter.enabled && teleporter.ShouldDisplayFunctionality &&
position.DistanceTo(teleporter.parent.Position) <= teleporter.Props.teleportRadius)
{
return true;
@@ -123,7 +170,7 @@ namespace WulaFallenEmpire
/// </summary>
private IntVec3 FindSafePositionInNetwork(IntVec3 preferredPosition, Pawn pawn)
{
if (parent?.Map == null) return IntVec3.Invalid;
if (parent?.Map == null || !enabled || !ShouldDisplayFunctionality) return IntVec3.Invalid;
var map = parent.Map;
@@ -146,7 +193,8 @@ namespace WulaFallenEmpire
// 在整个网络范围内搜索安全位置
foreach (var teleporter in GetNetworkTeleporters())
{
if (teleporter.parent?.Spawned != true) continue;
if (teleporter.parent?.Spawned != true || !teleporter.enabled || !teleporter.ShouldDisplayFunctionality)
continue;
var teleporterPos = teleporter.parent.Position;
var searchRadius = teleporter.Props.teleportRadius;
@@ -171,7 +219,7 @@ namespace WulaFallenEmpire
{
base.CompTick();
if (parent == null || !parent.Spawned || parent.Map == null)
if (parent == null || !parent.Spawned || parent.Map == null || !enabled || !ShouldDisplayFunctionality)
return;
// 使用间隔检查优化性能
@@ -499,11 +547,61 @@ namespace WulaFallenEmpire
effecters.Clear();
}
// 新增:切换开关状态
private void ToggleEnabled()
{
bool oldEnabled = enabled;
enabled = !enabled;
if (oldEnabled != enabled)
{
if (enabled)
{
RegisterToNetwork();
Messages.Message("WULA_TeleporterEnabled".Translate(parent.Label), parent, MessageTypeDefOf.PositiveEvent);
}
else
{
UnregisterFromNetwork();
Messages.Message("WULA_TeleporterDisabled".Translate(parent.Label), parent, MessageTypeDefOf.NegativeEvent);
}
// 清理效果
CleanupAllEffects();
}
}
// 新增获取Gizmos
public override IEnumerable<Gizmo> CompGetGizmosExtra()
{
foreach (var gizmo in base.CompGetGizmosExtra())
{
yield return gizmo;
}
// 只有满足科技需求时才显示开关按钮
if (ShouldDisplayFunctionality && Props.canBeToggled)
{
yield return new Command_Toggle
{
defaultLabel = enabled ? "WULA_TeleporterDisable".Translate() : "WULA_TeleporterEnable".Translate(),
defaultDesc = enabled ? "WULA_TeleporterDisableDesc".Translate() : "WULA_TeleporterEnableDesc".Translate(),
icon = ContentFinder<Texture2D>.Get("UI/Commands/Teleport"),
isActive = () => enabled,
toggleAction = ToggleEnabled
};
}
}
// 调试方法:显示传送范围
public override void PostDraw()
{
base.PostDraw();
// 只有满足科技需求且启用时才绘制范围
if (!ShouldDisplayFunctionality || !enabled)
return;
if (Find.Selector.IsSelected(parent))
{
try
@@ -514,7 +612,7 @@ namespace WulaFallenEmpire
// 绘制网络范围(所有传送器的范围)
foreach (var teleporter in GetNetworkTeleporters())
{
if (teleporter != this && teleporter.parent.Spawned)
if (teleporter != this && teleporter.parent.Spawned && teleporter.enabled && teleporter.ShouldDisplayFunctionality)
{
GenDraw.DrawRadiusRing(teleporter.parent.Position, teleporter.Props.teleportRadius, new Color(0.3f, 0.7f, 1, 0.3f));
}

View File

@@ -374,9 +374,7 @@
<ItemGroup>
<Compile Include="Verb\Verb_Excalibur\Thing_ExcaliburBeam.cs" />
</ItemGroup>
<ItemGroup>
<Folder Include="BuildingComp\WULA_FlyOverBeacon\" />
</ItemGroup>
<ItemGroup />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- 自定义清理任务删除obj文件夹中的临时文件 -->
<Target Name="CleanDebugFiles" AfterTargets="Build">