Skip to content

Commit

Permalink
Merge pull request #105 from zelzmiy/CustomTraits
Browse files Browse the repository at this point in the history
Custom Traits
  • Loading branch information
InfernoDragon0 authored Jan 16, 2025
2 parents 1dfbcec + 036bfbb commit 121d6cb
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -604,5 +604,6 @@ FodyWeavers.xsd

!COTL_API/Debug/
build/
.idea

global.json
39 changes: 39 additions & 0 deletions COTL_API/CustomTraits/CustomTrait.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using I2.Loc;
using UnityEngine;

namespace FoodPlus.CustomTraits;

public abstract class CustomTrait
{
public abstract string InternalName { get; }

internal FollowerTrait.TraitType TraitType;
internal string ModPrefix = "";

public virtual string LocalizedTitle() => LocalizationManager.GetTranslation($"Traits/{ModPrefix}.{InternalName}");

public virtual string LocalizedDescription() => LocalizationManager.GetTranslation($"Traits/{InternalName}.description");

public virtual bool IsTraitUnavailable() => false;

public virtual TraitFlags TraitFlags => TraitFlags.NONE;

public virtual List<FollowerTrait.TraitType> ExclusiveTraits => [];
public virtual Sprite Icon => TextureHelper.CreateSpriteFromPath(PluginPaths.ResolveAssetPath("placeholder.png"));
public virtual bool Positive => true;

}

[Flags]
public enum TraitFlags
{
NONE = 0,
STARTING_TRAIT = 1 << 0,
FAITHFUL_TRAIT = 1 << 1,
RARE_STARTING_TRAIT = 1 << 2,
SINGLE_TRAIT = 1 << 3,
EXCLUDE_FROM_MATING = 1 << 4,
SIN_TRAIT = 1 << 5,
PURE_BLOOD_TRAIT = 1 << 6,
REQUIRES_ONBOARDING_COMPLETE = 1 << 7,
}
51 changes: 51 additions & 0 deletions COTL_API/CustomTraits/CustomTraitManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using System.Reflection;
using COTL_API.Guid;

namespace FoodPlus.CustomTraits;

public static partial class CustomTraitManager
{
private static Dictionary<FollowerTrait.TraitType, CustomTrait> CustomTraitList { get; } = [];

public static FollowerTrait.TraitType Add(CustomTrait trait)
{
var guid = TypeManager.GetModIdFromCallstack(Assembly.GetCallingAssembly());

var traitType = GuidManager.GetEnumValue<FollowerTrait.TraitType>(guid, trait.InternalName);
trait.TraitType = traitType;
trait.ModPrefix = guid;

HandleTraitFlags(trait);
foreach (var exclusive in trait.ExclusiveTraits)
{
FollowerTrait.ExclusiveTraits.Add(traitType, exclusive);
}

CustomTraitList.Add(traitType, trait);

return traitType;
}

private static void HandleTraitFlags(CustomTrait item)
{
var flagActions = new Dictionary<TraitFlags, Action>
{
{ TraitFlags.STARTING_TRAIT, () => FollowerTrait.StartingTraits.Add(item.TraitType) },
{ TraitFlags.FAITHFUL_TRAIT, () => FollowerTrait.FaithfulTraits.Add(item.TraitType) },
{ TraitFlags.RARE_STARTING_TRAIT, () => FollowerTrait.RareStartingTraits.Add(item.TraitType) },
{ TraitFlags.SINGLE_TRAIT, () => FollowerTrait.SingleTraits.Add(item.TraitType) },
{ TraitFlags.SIN_TRAIT, () => FollowerTrait.SinTraits.Add(item.TraitType) },
{ TraitFlags.EXCLUDE_FROM_MATING, () => FollowerTrait.ExcludedFromMating.Add(item.TraitType) },
{ TraitFlags.PURE_BLOOD_TRAIT, () => FollowerTrait.PureBloodTraits.Add(item.TraitType) },
{ TraitFlags.REQUIRES_ONBOARDING_COMPLETE, () => FollowerTrait.RequiresOnboardingCompleted.Add(item.TraitType) }
};

foreach (var flagAction in flagActions)
{
if (item.TraitFlags.HasFlag(flagAction.Key))
{
flagAction.Value();
}
}
}
}
48 changes: 48 additions & 0 deletions COTL_API/CustomTraits/CustomTraitPatches.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using HarmonyLib;
using UnityEngine;

namespace FoodPlus.CustomTraits;

[HarmonyPatch]
public static partial class CustomTraitManager
{
[HarmonyPatch(typeof(FollowerTrait), nameof(FollowerTrait.GetIcon))]
[HarmonyPostfix]
private static void FollowerTrait_GetIcon(FollowerTrait.TraitType Type, ref Sprite __result)
{
if (!CustomTraitList.TryGetValue(Type, out var value)) return;
__result = value.Icon;
}

[HarmonyPatch(typeof(FollowerTrait), nameof(FollowerTrait.IsPositiveTrait))]
[HarmonyPostfix]
private static void FollowerTrait_IsPositiveTrait(FollowerTrait.TraitType traitType, ref bool __result)
{
if (!CustomTraitList.TryGetValue(traitType, out var value)) return;
__result = value.Positive;
}

[HarmonyPatch(typeof(FollowerTrait), nameof(FollowerTrait.GetLocalizedTitle))]
[HarmonyPostfix]
private static void FollowerTrait_GetLocalizedTitle(FollowerTrait.TraitType Type, ref string __result)
{
if (!CustomTraitList.TryGetValue(Type, out var value)) return;
__result = value.LocalizedTitle();
}

[HarmonyPatch(typeof(FollowerTrait), nameof(FollowerTrait.GetLocalizedDescription))]
[HarmonyPostfix]
private static void FollowerTrait_GetLocalizedDescription(FollowerTrait.TraitType Type, ref string __result)
{
if (!CustomTraitList.TryGetValue(Type, out var value)) return;
__result = value.LocalizedDescription();
}

[HarmonyPatch(typeof(FollowerTrait), nameof(FollowerTrait.IsTraitUnavailable))]
[HarmonyPostfix]
private static void FollowerTrait_IsTraitUnavailable(FollowerTrait.TraitType trait, ref bool __result)
{
if (!CustomTraitList.TryGetValue(trait, out var value)) return;
__result = value.IsTraitUnavailable();
}
}

0 comments on commit 121d6cb

Please sign in to comment.