Skip to content

Commit

Permalink
chore: code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
xhayper committed Aug 25, 2024
1 parent bec40da commit 3d4a5a8
Show file tree
Hide file tree
Showing 12 changed files with 124 additions and 133 deletions.
6 changes: 3 additions & 3 deletions COTL_API/COTL_API.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@
<PackageReadmeFile>README.md</PackageReadmeFile>
</PropertyGroup>

<Import Project="../COTL_API.Common.props" />
<Import Project="../COTL_API.Common.props"/>

<ItemGroup>
<PackageReference Include="CultOfTheLamb.GameLibs" Version="1.4.4.590-r.0"/>
<Reference Include="UnifyLibrary">
<HintPath>..\lib\UnifyLibrary.dll</HintPath>
</Reference>

<None Include="../README.md" Pack="true" PackagePath="/" />
<None Include="../LICENSE" Pack="true" PackagePath="/" />
<None Include="../README.md" Pack="true" PackagePath="/"/>
<None Include="../LICENSE" Pack="true" PackagePath="/"/>
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using COTL_API.CustomInventory;
using HarmonyLib;
using Lamb.UI.FollowerInteractionWheel;
using COTL_API.CustomInventory;

namespace COTL_API.CustomFollowerCommand;

Expand Down Expand Up @@ -142,7 +142,8 @@ private static bool FontImageNames_IconForCommand(FollowerCommands followerComma
var command0 = followerCommands;
if (CustomItemManager.CustomMealList.Values.Any(x => x.FollowerCommand == command0))
{
__result = CustomItemManager.CustomMealList.Values.First(x => x.FollowerCommand == command0).InventoryStringIcon();
__result = CustomItemManager.CustomMealList.Values.First(x => x.FollowerCommand == command0)
.InventoryStringIcon();
return false;
}

Expand Down
11 changes: 5 additions & 6 deletions COTL_API/CustomInventory/CustomItemManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public enum ItemRarity
COMMON,
RARE
}

public static Dictionary<InventoryItem.ITEM_TYPE, CustomInventoryItem> CustomItemList { get; } = [];
public static Dictionary<InventoryItem.ITEM_TYPE, CustomMeal> CustomMealList { get; } = [];

Expand All @@ -42,14 +42,14 @@ public static InventoryItem.ITEM_TYPE Add(CustomInventoryItem item)

if (!StructuresData.AllStructures.Contains(structureType)) StructuresData.AllStructures.Add(structureType);

CustomMealList.Add(itemType,meal);
CustomMealList.Add(itemType, meal);
}

CustomItemList.Add(itemType, item);

return itemType;
}

/// <summary>
/// A method to return whether to drop loot or not based on the custom items chances to drop. Defaults to the items
/// DungeonChestSpawnChance unless a custom chance is provided.
Expand All @@ -64,9 +64,8 @@ public static bool DropLoot(CustomInventoryItem customInventoryItem, float custo
? customChance
: customInventoryItem.DungeonChestSpawnChance +
customInventoryItem.DungeonChestSpawnChance * DataManager.Instance.GetLuckMultiplier();
if (Plugin.Instance != null && Plugin.Instance.Debug)
LogDebug(
$"{customInventoryItem.InternalObjectName} Roll/Chance: {roll} / {chance}: Win? {roll <= chance}");
LogDebug(
$"{customInventoryItem.InternalObjectName} Roll/Chance: {roll} / {chance}: Win? {roll <= chance}");
return roll <= chance;
}

Expand Down
19 changes: 10 additions & 9 deletions COTL_API/CustomInventory/CustomMeal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,35 +9,36 @@ public abstract class CustomMeal : CustomInventoryItem
internal FollowerCommands FollowerCommand { get; set; }

/// <summary>
/// This Meal's Recipe
/// This Meal's Recipe
/// </summary>
public abstract List<List<InventoryItem>> Recipe { get; }

/// <summary>
/// A list of the effects that will occur when eating this meal
/// A list of the effects that will occur when eating this meal
/// </summary>
public virtual MealEffect[] MealEffects { get; } = [];

public override bool IsFood => true;
public override InventoryItem.ITEM_TYPE ItemPickUpToImitate { get; } = InventoryItem.ITEM_TYPE.MEAL_BERRIES;

/// <summary>
/// "Star" level of this food
/// Range: 0-3
/// "Star" level of this food
/// Range: 0-3
/// </summary>
public abstract int SatiationLevel { get; }

/// <summary>
/// This is the amount that "Hunger Circle" is filled in when cooking the meal.
/// Range: 0-1
/// This is the amount that "Hunger Circle" is filled in when cooking the meal.
/// Range: 0-1
/// </summary>
public abstract float TummyRating { get; }

public virtual MealQuality Quality { get; } = MealQuality.NORMAL;
}

public enum MealQuality
{
{
BAD,
NORMAL,
GOOD,
}
GOOD
}
38 changes: 24 additions & 14 deletions COTL_API/CustomInventory/CustomMealEffect.cs
Original file line number Diff line number Diff line change
@@ -1,34 +1,44 @@
using I2.Loc;

namespace COTL_API.CustomInventory;

public abstract class CustomMealEffect
{
internal string _internalObjectName = "";
internal CookingData.MealEffectType _mealEffect;
internal string _modPrefix = "";
internal string InternalObjectName = "";

internal CookingData.MealEffectType MealEffect;
internal string ModPrefix = "";
public abstract string InternalName { get; }
public virtual bool EffectEnabled() => true;

/// <summary>
/// This is the effect that will occur when the follower eats this dish.
/// </summary>
public abstract Action<FollowerBrain> Effect { get; }

public virtual bool EffectEnabled()
{
return true;
}

public virtual string Description()
{
return $"CookingData/{InternalName}/Description";
return LocalizationManager.GetTranslation($"CookingData/{ModPrefix}.{InternalName}/Description");
}

/// <summary>
/// Used for adding a suffix to the description, normally for when the effect is disabled
/// Used for adding a suffix to the description, normally for when the effect is disabled
/// </summary>
/// <returns>The suffix to the description</returns>
public virtual string DescriptionSuffix()
{
return "";
return LocalizationManager.GetTranslation($"CookingData/{ModPrefix}.{InternalName}/DescriptionSuffix");
}

/// <summary>
/// Whether this MealEffect is positive or negative (The arrow on the cooking screen)
/// </summary>
public virtual bool Positive() => true;

/// <summary>
/// This is the effect that will occur when the follower eats this dish.
/// Whether this MealEffect is positive or negative (The arrow on the cooking screen)
/// </summary>
public abstract Action<FollowerBrain> Effect { get; }
public virtual bool Positive()
{
return true;
}
}
21 changes: 7 additions & 14 deletions COTL_API/CustomInventory/CustomMealEffectManager.cs
Original file line number Diff line number Diff line change
@@ -1,30 +1,23 @@
using COTL_API.Guid;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using COTL_API.Guid;

namespace COTL_API.CustomInventory;

public static partial class CustomMealEffectManager
public static partial class CustomMealEffectManager
{
public static Dictionary<CookingData.MealEffectType, CustomMealEffect> CustomEffectList { get; } = new();
public static Dictionary<CookingData.MealEffectType, CustomMealEffect> CustomEffectList { get; } = [];

public static CookingData.MealEffectType Add(CustomMealEffect effect)
{
var guid = TypeManager.GetModIdFromCallstack(Assembly.GetCallingAssembly());

var effectType = GuidManager.GetEnumValue<CookingData.MealEffectType>(guid, effect.InternalName);
LogInfo("Regestering effect with effecType " + effectType);
effect._mealEffect = effectType;
effect._modPrefix = guid;
effect._internalObjectName = $"CustomEffect_{effect.InternalName}";
effect.MealEffect = effectType;
effect.ModPrefix = guid;
effect.InternalObjectName = $"CustomMealEffect_{effect.InternalName}";

CustomEffectList.Add(effectType, effect);

return effectType;
}
}

}
6 changes: 0 additions & 6 deletions COTL_API/CustomInventory/Patches/CustomItemSpawnPatches.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
using COTL_API.CustomStructures;
using HarmonyLib;
using MMBiomeGeneration;
using MMRoomGeneration;
using MMTools;
using Sirenix.Serialization.Utilities;
using UnityEngine;
using UnityEngine.AddressableAssets;
using Object = UnityEngine.Object;
Expand All @@ -27,7 +24,6 @@ private static bool Prefix(InventoryItem.ITEM_TYPE type, int quantity, Vector3 p
ref Action<PickUp> result, ref PickUp __result)
{
if (!CustomItemList.ContainsKey(type)) return true;
LogWarning($"Running custom spawn. Item type = {type}, Qty: {quantity}");

var gameObject = GameObject.FindGameObjectWithTag("Unit Layer");
var transform = gameObject != null ? gameObject.transform : null;
Expand Down Expand Up @@ -116,7 +112,6 @@ private static void Prefix(ref string path)
if (myObject != null) myObject.SetActive(true);
myObject = Object.Instantiate(ItemPickUp.GetItemPickUpObject(item.Value.ItemPickUpToImitate), null,
instantiateInWorldSpace: false) as GameObject;
LogWarning($"myObject is NULL? {myObject == null}");
myObject!.GetComponentInChildren<SpriteRenderer>().sprite = item.Value.Sprite;
myObject.name = item.Value.InternalObjectName;
myObject.transform.localScale = item.Value.LocalScale;
Expand All @@ -140,7 +135,6 @@ public static GameObject GetCustomObject(CustomInventoryItem item)

_myObject = Object.Instantiate(ItemPickUp.GetItemPickUpObject(item.ItemPickUpToImitate), null,
instantiateInWorldSpace: false) as GameObject;
LogWarning($"_myObject is NULL? {_myObject == null}");
_myObject!.GetComponentInChildren<SpriteRenderer>().sprite = item.Sprite;
_myObject.name = item.InternalObjectName;
_myObject.transform.localScale = item.LocalScale;
Expand Down
36 changes: 14 additions & 22 deletions COTL_API/CustomInventory/Patches/CustomMealEffectPatches.cs
Original file line number Diff line number Diff line change
@@ -1,34 +1,30 @@
using HarmonyLib;
using src.UI.InfoCards;
using UnityEngine;
using Random = UnityEngine.Random;

namespace COTL_API.CustomInventory;

[HarmonyPatch]
public static partial class CustomMealEffectManager
{
[HarmonyPatch(typeof(CookingData),nameof(CookingData.DoMealEffect)), HarmonyPostfix]
public static void CookingData_DoMealEffect(InventoryItem.ITEM_TYPE meal, FollowerBrain follower)
[HarmonyPatch(typeof(CookingData), nameof(CookingData.DoMealEffect))]
[HarmonyPostfix]
public static void CookingData_DoMealEffect(InventoryItem.ITEM_TYPE meal, FollowerBrain follower)
{
var customMealEffects = CookingData.GetMealEffects(meal).Where(x => CustomEffectList.Keys.Contains(x.MealEffectType));
var customMealEffects = CookingData.GetMealEffects(meal)
.Where(x => CustomEffectList.Keys.Contains(x.MealEffectType));
foreach (var effect in customMealEffects)
{
var chance = UnityEngine.Random.Range(0, 100);
var chance = Random.Range(0, 100);

if (chance >= effect.Chance)
continue;

CustomEffectList[effect.MealEffectType].Effect(follower);
}
}

[HarmonyPatch(typeof(RecipeInfoCard), nameof(RecipeInfoCard.Configure)), HarmonyPostfix]
private static void RecipeInfoCard_Configure(RecipeInfoCard __instance, InventoryItem.ITEM_TYPE config)
{
var mealEffects = CookingData.GetMealEffects(config);
}

[HarmonyPatch(typeof(CookingData), nameof(CookingData.GetEffectDescription)), HarmonyPostfix]

[HarmonyPatch(typeof(CookingData), nameof(CookingData.GetEffectDescription))]
[HarmonyPostfix]
private static void CookingData_GetEffectDescription(ref CookingData.MealEffect mealEffect, ref string __result)
{
if (!CustomEffectList.Keys.Contains(mealEffect.MealEffectType)) return;
Expand All @@ -41,13 +37,9 @@ private static void CookingData_GetEffectDescription(ref CookingData.MealEffect
var enabled = effect.EffectEnabled();

var formattedDescription = $"{icon} <color=#FFD201>{mealEffect.Chance}%</color> {description}";

if (!enabled)
{
formattedDescription = $"<s>{formattedDescription}</s>";
}


if (!enabled) formattedDescription = $"<s>{formattedDescription}</s>";

__result = formattedDescription + descriptionSuffix;
}

}
}
Loading

0 comments on commit 3d4a5a8

Please sign in to comment.