Skip to content

Commit

Permalink
API method to grab all huggables
Browse files Browse the repository at this point in the history
  • Loading branch information
VioVayo committed Apr 19, 2024
1 parent bafaff7 commit ed5e65c
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 6 deletions.
6 changes: 5 additions & 1 deletion HugMod/HugModApi.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
using OWML.Common;
using System;
using System.Linq;
using UnityEngine;

namespace HugMod
{
public class HugModApi : IHugModApi
{
public void AddHugComponent(GameObject hugObject, bool initialiseImmediately)
public GameObject[] GetAllHuggables() => Resources.FindObjectsOfTypeAll<HugComponent>().Select(obj => obj.gameObject).ToArray();


public void AddHugComponent(GameObject hugObject, bool initialiseImmediately)
{
if (hugObject.GetComponent<HugComponent>() == null)
{
Expand Down
4 changes: 2 additions & 2 deletions HugMod/HugPatches.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ public class HugPatches
[HarmonyPatch(typeof(PlayerCharacterController), nameof(PlayerCharacterController.UpdateMovement))]
public static IEnumerable<CodeInstruction> PlayerCharacterController_UpdateMovement_Transpiler(IEnumerable<CodeInstruction> instructions)
{
var matcher = new CodeMatcher(instructions).MatchForward(true,
var matcher = new CodeMatcher(instructions).MatchForward(true, //matching to the part where it accepts player input
new CodeMatch(OpCodes.Call, AccessTools.Method(typeof(OWInput), nameof(OWInput.GetAxisValue), new Type[] { typeof(IInputCommands), typeof(InputMode) })),
new CodeMatch(OpCodes.Stloc_0)
);

matcher.Insert(
matcher.Insert( //sneakily swapping out the value on top of the stack before it gets stored
Transpilers.EmitDelegate<Func<Vector2, Vector2>>((input) =>
{
if (WalkingTowardsHugTarget) return new Vector2(0, WalkingTowardsHugSpeed);
Expand Down
8 changes: 8 additions & 0 deletions HugMod/IHugModApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ namespace HugMod
{
public interface IHugModApi
{
/// <summary>
/// Returns an array of all GameObjects that have a hug script attached<br/>
/// This may falsely return null if used directly on scene load, so it's good to wait a frame first
/// </summary>
/// <returns>The array of GameObjects, including deactivated ones</returns>
public GameObject[] GetAllHuggables();


/// <summary>
/// Makes a suitable GameObject huggable
/// </summary>
Expand Down
4 changes: 2 additions & 2 deletions HugMod/Targets/TargetManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ private static void HugReenable(Target target) //prevent loss of huggability aft
{
var hugComponent = target.GetHugComponent() ?? AddHugComponent(target);
if (hugComponent == null) return;
var receiver = hugComponent.gameObject.GetComponentInChildren<InteractReceiver>();
if (receiver._owCollider._active) return; //this is meant to reenable the receiver without reenabling dialogue, so don't do the thing if everything is still enabled
var receiver = hugComponent.HugReceiver;
if (receiver == null || receiver._owCollider._active) return; //this is meant to reenable the receiver without reenabling dialogue, so don't do the thing if everything is still enabled
receiver.OnPressInteract -= hugComponent.gameObject.GetComponentInChildren<CharacterDialogueTree>().OnPressInteract;
receiver.OnGainFocus += () => { Locator.GetPromptManager().RemoveScreenPrompt(receiver._screenPrompt); };
receiver.EnableInteraction();
Expand Down
2 changes: 1 addition & 1 deletion HugMod/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"author": "VioVayo",
"name": "Hug Mod",
"uniqueName": "VioVayo.HugMod",
"version": "1.0.3",
"version": "1.1.0",
"owmlVersion": "2.9.0",
"dependencies": [],
"pathsToPreserve": [ "settings.json" ]
Expand Down

0 comments on commit ed5e65c

Please sign in to comment.