Skip to content

Commit

Permalink
feat: refactor steam gift custom behavior, add patches
Browse files Browse the repository at this point in the history
  • Loading branch information
ryantanrk committed Jun 4, 2024
1 parent 68f12a8 commit dad4607
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 5 deletions.
7 changes: 3 additions & 4 deletions Behaviours/SteamGiftPhysicsProp.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Unity.Netcode;
using UnityEngine;
using UnityEngine;

namespace DingusThings.Behaviours
{
Expand All @@ -23,9 +22,9 @@ public override void ItemActivate(bool used, bool buttonDown = true)
{
AssetBundle? bundle = DingusThings.Bundle;
string itemName = "Steam Gift Card";

// find a terminal
Terminal terminal = FindFirstObjectByType<Terminal>();
Terminal terminal = DingusThings.GetTerminalInstance();
if (terminal != null)
{
// add scrap value to terminal
Expand Down
26 changes: 25 additions & 1 deletion DingusThings.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using BepInEx;
using BepInEx.Logging;
using DingusThings.Behaviours;
using DingusThings.Patches;
using HarmonyLib;
using LethalLib.Modules;
using System.IO;
Expand All @@ -21,6 +22,25 @@ public class DingusThings : BaseUnityPlugin

public static string PluginString = $"{MyPluginInfo.PLUGIN_NAME} v{MyPluginInfo.PLUGIN_VERSION}";

private static Terminal? terminalInstance;

public static void SetTerminalInstance(Terminal terminal)
{
terminalInstance = terminal;
}

public static Terminal? GetTerminalInstance()
{
if (terminalInstance != null)
{
return terminalInstance;
}
else
{
return null;
}
}

private void Awake()
{
Logger = base.Logger;
Expand All @@ -35,6 +55,10 @@ private void Awake()
return;
}

// load patches
Harmony.CreateAndPatchAll(typeof(TerminalPatch));
Harmony.CreateAndPatchAll(typeof(SteamGiftPatch));

/// My Heart
int myHeartRarity = 30;
Item myHeartItem = Bundle.LoadAsset<Item>("Assets/DingusThings/Items/MyHeart.asset");
Expand All @@ -54,7 +78,7 @@ private void Awake()
/// Steam Gift Card
int steamGiftRarity = 50;
Item steamGiftItem = Bundle.LoadAsset<Item>("Assets/DingusThings/Items/SteamGiftCard.asset");
steamGiftItem.toolTips = ["Inspect : [ Z ]", "Redeem : [ LMB ]"];
steamGiftItem.toolTips = ["Inspect : [ Z ]"];
SteamGiftPhysicsProp steamGiftPhysicsProp = steamGiftItem.spawnPrefab.AddComponent<SteamGiftPhysicsProp>();
steamGiftPhysicsProp.grabbable = true;
steamGiftPhysicsProp.grabbableToEnemies = true;
Expand Down
18 changes: 18 additions & 0 deletions Patches/SteamGiftPatch.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using HarmonyLib;

namespace DingusThings.Patches
{
internal class SteamGiftPatch
{
[HarmonyPatch(typeof(GrabbableObject), nameof(GrabbableObject.GrabItem))]
[HarmonyPostfix]
public static void GrabbableObject_GrabItem(GrabbableObject __instance)
{
if (__instance.itemProperties.itemName == "Steam Gift Card")
{
// change tooltip on grab
HUDManager.Instance.ChangeControlTip(2, __instance.scrapValue <= 0 ? "ALREADY REDEEMED" : "Redeem : [ LMB ]");
}
}
}
}
16 changes: 16 additions & 0 deletions Patches/TerminalPatch.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using HarmonyLib;

namespace DingusThings.Patches
{
internal class TerminalPatch
{
// tell Harmony which class method is being targeted
[HarmonyPatch(typeof(Terminal), nameof(Terminal.Start))]
// run after original method
[HarmonyPostfix]
public static void Terminal_Start(Terminal __instance)
{
DingusThings.SetTerminalInstance(__instance);
}
}
}

0 comments on commit dad4607

Please sign in to comment.