From ef1e3cdd0fa32683117bec254f4f7ea5d8c89da0 Mon Sep 17 00:00:00 2001 From: Vegapnk Date: Mon, 17 Jul 2023 06:58:00 +0200 Subject: [PATCH] Added generic DefModExtension for Distance and Ticks --- Source/Common/Defs/DistanceExtension.cs | 20 +++++++++ Source/Common/Defs/ModExtensionHelper.cs | 48 +++++++++++++++++++++ Source/Common/Defs/TickIntervalExtension.cs | 18 ++++++++ 3 files changed, 86 insertions(+) create mode 100644 Source/Common/Defs/DistanceExtension.cs create mode 100644 Source/Common/Defs/ModExtensionHelper.cs create mode 100644 Source/Common/Defs/TickIntervalExtension.cs diff --git a/Source/Common/Defs/DistanceExtension.cs b/Source/Common/Defs/DistanceExtension.cs new file mode 100644 index 0000000..48d82e6 --- /dev/null +++ b/Source/Common/Defs/DistanceExtension.cs @@ -0,0 +1,20 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Verse; + +namespace RJW_Genes +{ + + /// + /// Small Extension that covers distances for Genes. + /// This allows to set distance in the XMLs. + /// The distance is measured in tiles, so a distance of ~25 means 25 tiles away from the pawn in any direction. + /// + public class DistanceExtension : DefModExtension + { + public int Distance; + } +} diff --git a/Source/Common/Defs/ModExtensionHelper.cs b/Source/Common/Defs/ModExtensionHelper.cs new file mode 100644 index 0000000..e8fc545 --- /dev/null +++ b/Source/Common/Defs/ModExtensionHelper.cs @@ -0,0 +1,48 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Verse; + +namespace RJW_Genes +{ + internal class ModExtensionHelper + { + + public static int GetDistanceFromModExtension(GeneDef defOf, int fallback) + { + DistanceExtension distanceExt = defOf.GetModExtension(); + + int potentialDistance = distanceExt?.Distance ?? fallback; + + if (potentialDistance > 0) + { + return potentialDistance; + } else { + ModLog.Warning($"Retrieved a bad distance ({potentialDistance}) reading the RJW_Genes.DistanceExtension for {defOf.defName}"); + return 1; + } + } + + + public static int GetTickIntervalFromModExtension(GeneDef defOf, int fallback) + { + TickIntervalExtension tickExt = defOf.GetModExtension(); + + int potentialTickInterval = tickExt?.tickInterval ?? fallback; + + if (potentialTickInterval > 0) + { + return potentialTickInterval; + } + else + { + ModLog.Warning($"Retrieved a bad distance ({potentialTickInterval}) reading the RJW_Genes.DistanceExtension for {defOf.defName}"); + return 1; + } + } + + + } +} diff --git a/Source/Common/Defs/TickIntervalExtension.cs b/Source/Common/Defs/TickIntervalExtension.cs new file mode 100644 index 0000000..3e8184c --- /dev/null +++ b/Source/Common/Defs/TickIntervalExtension.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Verse; + +namespace RJW_Genes +{ + /// + /// General DefModExtension to cover various genes that need to tick regularly. + /// Defining it like this allows to adjust things in XML. + /// + public class TickIntervalExtension : DefModExtension + { + public int tickInterval; + } +}