Skip to content

Commit

Permalink
Restrict stacks by pattern and refactor comps
Browse files Browse the repository at this point in the history
  • Loading branch information
eth0net committed Jan 17, 2023
1 parent 1939ce6 commit d55bbdd
Show file tree
Hide file tree
Showing 10 changed files with 61 additions and 54 deletions.
Binary file modified Assemblies/NamedSubcores.dll
Binary file not shown.
11 changes: 5 additions & 6 deletions Source/NamedSubcores/Comps/BasePatternComp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
namespace NamedSubcores
{
/// <summary>
/// BasePatternComp implements the common features for the subcore and mech components.
/// BasePatternComp implements the common features for the pattern components.
/// </summary>
public class BasePatternComp : ThingComp
{
/// <summary>
/// PatternName tracks the name of the pawn scanned.
/// </summary>
public Name PatternName = null;
public Name PatternName;

/// <summary>
/// PostExposeData is used to save our component state.
Expand All @@ -22,12 +22,11 @@ public override void PostExposeData()
}

/// <summary>
/// CompInspectStringExtra adds to the item inspection pane.
/// Reset allows the component to be reset for reuse.
/// </summary>
/// <returns></returns>
public override string CompInspectStringExtra()
public void Reset()
{
return "Pattern".Translate() + ": " + (PatternName?.ToStringShort ?? "Unknown".Translate());
PatternName = null;
}
}
}
19 changes: 19 additions & 0 deletions Source/NamedSubcores/Comps/InspectPatternComp.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using Verse;

namespace NamedSubcores
{
/// <summary>
/// InspectPatternComp implements the common inspect method for pattern components.
/// </summary>
public class InspectPatternComp : BasePatternComp
{
/// <summary>
/// CompInspectStringExtra adds to the item inspection pane.
/// </summary>
/// <returns></returns>
public override string CompInspectStringExtra()
{
return "Pattern".Translate() + ": " + (PatternName?.ToStringShort ?? "Unknown".Translate());
}
}
}
31 changes: 3 additions & 28 deletions Source/NamedSubcores/Comps/MechGestatorComp.cs
Original file line number Diff line number Diff line change
@@ -1,32 +1,7 @@
using Verse;

namespace NamedSubcores
namespace NamedSubcores
{
/// <summary>
/// MechGestatorComp is added to mech gestators and allows us to track the subcore component during gestation.
/// MechGestatorComp is added to mech gestators and allows us to track the subcore pattern during gestation.
/// </summary>
public class MechGestatorComp : ThingComp
{
/// <summary>
/// SubcoreComp tracks the subcore component.
/// </summary>
public SubcorePatternComp SubcoreComp;

/// <summary>
/// PostExposeData is used to save our component state.
/// </summary>
public override void PostExposeData()
{
Scribe_Deep.Look(ref SubcoreComp, "subcoreComp");
base.PostExposeData();
}

/// <summary>
/// Reset allows the component to be reset for reuse.
/// </summary>
public void Reset()
{
SubcoreComp = null;
}
}
public class MechGestatorComp : BasePatternComp { }
}
2 changes: 1 addition & 1 deletion Source/NamedSubcores/Comps/MechPatternComp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
/// <summary>
/// MechPatternComp is added to mechanoids and is used to track the pawn scanned into the subcore.
/// </summary>
public class MechPatternComp : BasePatternComp { }
public class MechPatternComp : InspectPatternComp { }
}
21 changes: 19 additions & 2 deletions Source/NamedSubcores/Comps/SubcorePatternComp.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,24 @@
namespace NamedSubcores
using Verse;

namespace NamedSubcores
{
/// <summary>
/// MechPatternComp is added to subcores and is used to track the pawn scanned into the subcore.
/// </summary>
public class SubcorePatternComp : BasePatternComp { }
public class SubcorePatternComp : InspectPatternComp {
/// <summary>
/// AllowStackWith ensures that subcores can only be stacked with others of the same pattern.
/// </summary>
/// <param name="other"></param>
/// <returns></returns>
public override bool AllowStackWith(Thing other)
{
if (base.AllowStackWith(other) == false) { return false; };

SubcorePatternComp otherComp = other?.TryGetComp<SubcorePatternComp>();
if (otherComp == null) { return false; }

return PatternName == otherComp.PatternName;
}
}
}
12 changes: 3 additions & 9 deletions Source/NamedSubcores/Comps/SubcoreScannerComp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,29 @@ namespace NamedSubcores
/// <summary>
/// SubcoreScannerComp is added to subcore scanners and allows us to track when a subcore is ejected.
/// </summary>
public class SubcoreScannerComp : ThingComp
public class SubcoreScannerComp : BasePatternComp
{
/// <summary>
/// Ejected tracks whether a subcore has just been ejected.
/// </summary>
public bool Ejected = false;

/// <summary>
/// PatternName tracks the name of the pawn in the scanner.
/// </summary>
public Name OccupantName;

/// <summary>
/// PostExposeData is used to save our component state.
/// </summary>
public override void PostExposeData()
{
Scribe_Values.Look(ref Ejected, "ejected");
Scribe_Deep.Look(ref OccupantName, "occupantName");
base.PostExposeData();
}

/// <summary>
/// Reset allows the component to be reset for reuse.
/// </summary>
public void Reset()
public new void Reset()
{
base.Reset();
Ejected = false;
OccupantName = null;
}
}
}
12 changes: 7 additions & 5 deletions Source/NamedSubcores/Harmony/Harmony_Building_MechGestator.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using HarmonyLib;
using RimWorld;
using System;
using System.Linq;
using Verse;

Expand All @@ -15,7 +14,7 @@ internal class Harmony_Building_MechGestator
internal static class Harmony_Building_MechGestator_Notify_AllGestationCyclesCompleted
{
/// <summary>
/// Prefix stores the subcore component for later use.
/// Prefix stores the subcore pattern for later use.
/// </summary>
/// <param name="__instance"></param>
internal static void Prefix(Building_MechGestator __instance)
Expand All @@ -24,10 +23,13 @@ internal static void Prefix(Building_MechGestator __instance)
Thing subcore = __instance.innerContainer.FirstOrDefault(hasNamedSubcoreComp);
if (subcore == null) { return; }

SubcorePatternComp subcoreComp = subcore.TryGetComp<SubcorePatternComp>();
if (subcoreComp == null) { return; }

MechGestatorComp gestatorComp = __instance.GetComp<MechGestatorComp>();
if (gestatorComp == null) { return; }

gestatorComp.SubcoreComp = subcore.TryGetComp<SubcorePatternComp>();
gestatorComp.PatternName = subcoreComp.PatternName;
}

/// <summary>
Expand All @@ -39,10 +41,10 @@ internal static void Postfix(Building_MechGestator __instance)
MechGestatorComp gestatorComp = __instance.GetComp<MechGestatorComp>();
if (gestatorComp == null) { return; }

SubcorePatternComp mechComp = __instance.GestatingMech.GetComp<SubcorePatternComp>();
MechPatternComp mechComp = __instance.GestatingMech.GetComp<MechPatternComp>();
if (mechComp == null) { return; }

mechComp.PatternName = gestatorComp?.SubcoreComp?.PatternName;
mechComp.PatternName = gestatorComp.PatternName;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ internal static class Harmony_Building_SubcoreScanner_Tick
internal static void Prefix(Building_SubcoreScanner __instance)
{
SubcoreScannerComp scannerComp = __instance.GetComp<SubcoreScannerComp>();
scannerComp.OccupantName = __instance?.Occupant?.Name ?? null;
scannerComp.PatternName = __instance?.Occupant?.Name ?? null;
}

/// <summary>
Expand All @@ -48,7 +48,7 @@ internal static void Postfix(Building_SubcoreScanner __instance)
SubcorePatternComp subcoreComp = TryGetSubcoreComp(__instance);
if (subcoreComp != null)
{
subcoreComp.PatternName = scannerComp.OccupantName;
subcoreComp.PatternName = scannerComp.PatternName;
}

scannerComp.Reset();
Expand Down
3 changes: 2 additions & 1 deletion Source/NamedSubcores/NamedSubcores.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,9 @@
<ItemGroup>
<Compile Include="NamedSubcores.cs" />
<Compile Include="Comps\BasePatternComp.cs" />
<Compile Include="Comps\MechPatternComp.cs" />
<Compile Include="Comps\InspectPatternComp.cs" />
<Compile Include="Comps\MechGestatorComp.cs" />
<Compile Include="Comps\MechPatternComp.cs" />
<Compile Include="Comps\SubcorePatternComp.cs" />
<Compile Include="Comps\SubcoreScannerComp.cs" />
<Compile Include="Harmony\Harmony_Building_MechGestator.cs" />
Expand Down

0 comments on commit d55bbdd

Please sign in to comment.