Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom Traits #105

Merged
merged 6 commits into from
Jan 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
zelzmiy marked this conversation as resolved.
Show resolved Hide resolved
{
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();
}
}
Loading