Skip to content
This repository has been archived by the owner on Dec 30, 2024. It is now read-only.

Commit

Permalink
🐛 Redo the interactable object highlighting patch
Browse files Browse the repository at this point in the history
Replace the `PlayfieldObject.EnterDetails` patch with a `PlayfieldObject.get_interactable` patch. The former's vanilla method is way too fine-tuned to simply replace it.
  • Loading branch information
Chasmical committed Jun 19, 2022
1 parent 61f9a21 commit ea5cd80
Show file tree
Hide file tree
Showing 10 changed files with 35 additions and 132 deletions.
17 changes: 10 additions & 7 deletions RogueLibsCore/Interactions/InteractionModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public void StopInteraction(bool forced)
}
shouldStop = false;
forcedStop = true;
OriginalStopInteraction(Instance);
if (!fakingInteraction) OriginalStopInteraction(Instance);
}

protected override void Initialize() { }
Expand Down Expand Up @@ -184,24 +184,27 @@ private void OnPressedButton2(string buttonName)
// refresh the buttons (restarts the cycle)
Agent.worldSpaceGUI?.StartCoroutine(Agent.worldSpaceGUI.RefreshObjectButtons2(Object));
}
public bool ShouldBeHighlighted(Agent agent)

private static string? lastLog;
private bool fakingInteraction;
public bool IsInteractable(Agent agent)
{
Agent prevAgent = Instance.interactingAgent;
Agent? prevAgent = Instance.interactingAgent;
try
{
fakingInteraction = true;
RogueLibsPlugin.useModelStopInteraction = true;
Instance.interactingAgent = agent;
bool res = ShouldBeHighlighted2();
// RogueFramework.LogWarning($"{Instance} will{(res ? null : " not")} be highlighted.");
return res;
return IsInteractable2();
}
finally
{
fakingInteraction = false;
RogueLibsPlugin.useModelStopInteraction = false;
Instance.interactingAgent = prevAgent;
}
}
private bool ShouldBeHighlighted2()
private bool IsInteractable2()
{
// reset state
interactions.Clear();
Expand Down
5 changes: 0 additions & 5 deletions RogueLibsCore/Interactions/VanillaInteractions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,6 @@ public static void PatchInteractFar<T>() where T : PlayfieldObject
{
patcher.Prefix(typeof(T), nameof(PlayfieldObject.InteractFar), nameof(RogueLibsPlugin.InteractFarHook));
}
public static void MakeInteractable<T>() where T : PlayfieldObject
{
patcher.Postfix(typeof(T), "Awake", nameof(RogueLibsPlugin.AwakeInteractableHook));
patcher.Postfix(typeof(T), nameof(PlayfieldObject.RecycleAwake), nameof(RogueLibsPlugin.RecycleAwakeInteractableHook));
}

[AttributeUsage(AttributeTargets.Method), MeansImplicitUse]
internal class IncludeAttribute : Attribute { }
Expand Down
1 change: 0 additions & 1 deletion RogueLibsCore/Interactions/VanillaInteractions/Altar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ private static void Patch_Altar()
Patch<Altar>(Params2);
PatchInteract<Altar>();
PatchInteractFar<Altar>();
MakeInteractable<Altar>();

RogueInteractions.CreateProvider<Altar>(static h =>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ private static void Patch_ArcadeGame()
Patch<ArcadeGame>(Params1);
PatchInteract<ArcadeGame>();
PatchInteractFar<ArcadeGame>();
MakeInteractable<ArcadeGame>();

RogueInteractions.CreateProvider<ArcadeGame>(static h =>
{
Expand Down
1 change: 0 additions & 1 deletion RogueLibsCore/Interactions/VanillaInteractions/Bars.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ public static partial class VanillaInteractions
private static void Patch_Bars()
{
PatchInteract<Bars>();
MakeInteractable<Bars>();

RogueLibs.CreateCustomName("InteractWithAgent", NameTypes.Interface, new CustomNameInfo
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ private static void Patch_Television()
{
Patch<Television>(Params1);
PatchInteractFar<Television>();
MakeInteractable<Television>();

RogueInteractions.CreateProvider<Television>(static h =>
{
Expand Down
1 change: 0 additions & 1 deletion RogueLibsCore/Interactions/VanillaInteractions/TrapDoor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ private static void Patch_TrapDoor()
Patch<TrapDoor>(Params1);
PatchInteract<TrapDoor>();
PatchInteractFar<TrapDoor>();
MakeInteractable<TrapDoor>();

RogueInteractions.CreateProvider<TrapDoor>(static h =>
{
Expand Down
1 change: 0 additions & 1 deletion RogueLibsCore/Interactions/VanillaInteractions/Turret.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ private static void Patch_Turret()
Patch<Turret>(Params2);
PatchInteract<Turret>();
PatchInteractFar<Turret>();
MakeInteractable<Turret>();

RogueInteractions.CreateProvider<Turret>(static h =>
{
Expand Down
52 changes: 0 additions & 52 deletions RogueLibsCore/Interactions/VanillaInteractions/_NoInteractions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,67 +5,15 @@ public static partial class VanillaInteractions
[Include]
private static void Patch_NoInteractions()
{
MakeInteractable<BarbedWire>();
MakeInteractable<BarStool>();
MakeInteractable<Bathtub>();
MakeInteractable<Boulder>();
MakeInteractable<BoulderSmall>();
MakeInteractable<Chair>();
MakeInteractable<Desk>();

MakeInteractable<ExplodingBarrel>();
PatchInteract<ExplodingBarrel>();

MakeInteractable<Fireplace>();

MakeInteractable<FireSpewer>();
PatchInteract<FireSpewer>();

MakeInteractable<FlameGrate>();
PatchInteract<FlameGrate>();

MakeInteractable<FlamingBarrel>();

MakeInteractable<GasVent>();
PatchInteract<GasVent>();

MakeInteractable<Gravestone>();

MakeInteractable<KillerPlant>();
MakeInteractable<Lamp>();

MakeInteractable<LockdownWall>();
MakeInteractable<Manhole>();

MakeInteractable<MineCart>();
PatchInteract<MineCart>();

MakeInteractable<MovieScreen>();
MakeInteractable<Plant>();
MakeInteractable<PoolTable>();

MakeInteractable<SawBlade>();
PatchInteract<SawBlade>();

MakeInteractable<Shelf>();
MakeInteractable<SlimeBarrel>();
MakeInteractable<SlimePuddle>();
MakeInteractable<Speaker>();
MakeInteractable<Stove>();
MakeInteractable<Table>();
MakeInteractable<TableBig>();

MakeInteractable<Train>();
PatchInteract<Train>();

MakeInteractable<Tree>();

MakeInteractable<Tube>();
PatchInteract<Tube>();

MakeInteractable<VendorCart>();


}
}
}
87 changes: 25 additions & 62 deletions RogueLibsCore/Patches/Patches_Interactions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,18 @@ public void PatchInteractions()

Patcher.Prefix(typeof(PlayfieldObject), nameof(PlayfieldObject.StopInteraction), new Type[1] { typeof(bool) });

Patcher.Prefix(typeof(InteractionHelper), "EnterDetails");
Patcher.Prefix(typeof(InteractionHelper), "EnterDetails", nameof(InteractionHelper_EnterDetails_Prefix));
Patcher.Finalizer(typeof(InteractionHelper), "EnterDetails", nameof(InteractionHelper_EnterDetails_Finalizer));
Patcher.Prefix(typeof(InteractionHelper), nameof(InteractionHelper.UpdateInteractionHelper),
nameof(InteractionHelper_UpdateInteractionHelper_Prefix));
Patcher.Finalizer(typeof(InteractionHelper), nameof(InteractionHelper.UpdateInteractionHelper),
nameof(InteractionHelper_UpdateInteractionHelper_Finalizer));

MethodInfo interactableGetter = AccessTools.PropertyGetter(typeof(PlayfieldObject), nameof(PlayfieldObject.interactable));
Harmony harmony = Patcher.GetHarmony();
harmony.Patch(interactableGetter, new HarmonyMethod(AccessTools.Method(typeof(RogueLibsPlugin), nameof(PlayfieldObject_get_interactable))));

Patcher.Prefix(typeof(InteractionHelper), nameof(InteractionHelper.canInteractCounter));

VanillaInteractions.PatchAll();

Expand Down Expand Up @@ -149,14 +160,6 @@ public static bool InteractFarHook(PlayfieldObject __instance, Agent agent)
__instance.ShowObjectButtons();
return false;
}
public static void AwakeInteractableHook(PlayfieldObject __instance)
{
__instance.interactable = true;
}
public static void RecycleAwakeInteractableHook(PlayfieldObject __instance)
{
__instance.interactable = true;
}

internal static bool useModelStopInteraction;
public static bool PlayfieldObject_StopInteraction(PlayfieldObject __instance)
Expand All @@ -170,63 +173,23 @@ public static bool PlayfieldObject_StopInteraction(PlayfieldObject __instance)
}

private static readonly FieldInfo interactionHelperAgentField = AccessTools.Field(typeof(InteractionHelper), "agent");
public static bool InteractionHelper_EnterDetails(InteractionHelper __instance, Collider2D other)
private static Agent? useModelInteractable;
public static void InteractionHelper_EnterDetails_Prefix(InteractionHelper __instance)
=> useModelInteractable = (Agent)interactionHelperAgentField.GetValue(__instance);
public static void InteractionHelper_EnterDetails_Finalizer() => useModelInteractable = null;
public static void InteractionHelper_UpdateInteractionHelper_Prefix(InteractionHelper __instance)
=> useModelInteractable = (Agent)interactionHelperAgentField.GetValue(__instance);
public static void InteractionHelper_UpdateInteractionHelper_Finalizer() => useModelInteractable = null;

public static bool PlayfieldObject_get_interactable(PlayfieldObject __instance, ref bool __result)
{
__instance.EnterRange(other.gameObject);

Agent agent = (Agent)interactionHelperAgentField.GetValue(__instance);
Vector3 pointPos = agent.tr.position;

float itemDistance = float.MaxValue;
float objectDistance = float.MaxValue;
__instance.closestItem = null;
__instance.closestObject = null;
foreach (GameObject obj in __instance.TriggerList)
{
PlayfieldObject playfieldObj = obj.GetComponent<PlayfieldObject>()
?? obj.GetComponent<ObjectSprite>()?.playFieldObject
?? obj.transform.parent.GetComponent<PlayfieldObject>()
?? obj.transform.parent.GetComponent<ObjectSprite>().playFieldObject;
InteractionModel model = GetOrCreateModel(playfieldObj);

float dist = Vector3.Distance(playfieldObj.tr.position, pointPos);
if (obj.CompareTag("AgentSprite") || obj.CompareTag("ObjectRealSprite"))
{
if (dist < objectDistance && model.ShouldBeHighlighted(agent))
{
objectDistance = dist;
__instance.closestObject = obj;
}
}
else if (obj.CompareTag("ItemImage"))
{
if (dist < itemDistance && model.ShouldBeHighlighted(agent))
{
itemDistance = dist;
__instance.closestItem = obj;
}
}
}
foreach (GameObject obj in __instance.TriggerList)
{
PlayfieldObject playfieldObj = obj.GetComponent<PlayfieldObject>()
?? obj.GetComponent<ObjectSprite>()?.playFieldObject
?? obj.transform.parent.GetComponent<PlayfieldObject>()
?? obj.transform.parent.GetComponent<ObjectSprite>().playFieldObject;

if (obj == __instance.closestItem || obj == __instance.closestObject)
{
try { playfieldObj.objectSprite.SetHighlight("Normal", agent); } catch { }
}
else
{
try { playfieldObj.objectSprite.SetHighlight("Off", agent); } catch { }
}
}

if (useModelInteractable is null) return true;
InteractionModel model = GetOrCreateModel(__instance);
__result = model.IsInteractable(useModelInteractable);
return false;
}

public static bool InteractionHelper_canInteractCounter() => true;

}
}

0 comments on commit ea5cd80

Please sign in to comment.