-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #105 from zelzmiy/CustomTraits
Custom Traits
- Loading branch information
Showing
4 changed files
with
139 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -604,5 +604,6 @@ FodyWeavers.xsd | |
|
||
!COTL_API/Debug/ | ||
build/ | ||
.idea | ||
|
||
global.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |