Skip to content

Commit

Permalink
Add rando connection
Browse files Browse the repository at this point in the history
  • Loading branch information
flibber-hk committed Feb 10, 2024
1 parent e617464 commit b13839e
Show file tree
Hide file tree
Showing 10 changed files with 354 additions and 12 deletions.
6 changes: 5 additions & 1 deletion ModDependencies.txt
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
Vasi
Vasi
Randomizer 4
RandomizerCore
ItemChanger
MenuChanger
44 changes: 36 additions & 8 deletions MylaFlower/Dialogue.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,35 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Modding;
using Modding;

namespace MylaFlower
{
internal static class Dialogue
{
internal delegate string TextEditHandler(string key, string sheetTitle, string orig);

internal static event TextEditHandler OnGetMylaText;

internal static void Hook()
{
ModHooks.LanguageGetHook += ChangeMylaDialogue;
}

private static string ChangeMylaDialogue(string key, string sheetTitle, string orig)
{
if (!TryChangeMylaDialogueInternal(key, sheetTitle, orig, out string newText))
{
return orig;
}

if (OnGetMylaText is null) return newText;

foreach (TextEditHandler handler in OnGetMylaText?.GetInvocationList())
{
newText = handler(key, sheetTitle, newText);
}
return newText;
}

private static bool TryChangeMylaDialogueInternal(string key, string sheetTitle, string orig, out string newText)
{
if (sheetTitle == Consts.CustomLanguageSheet)
{
Expand All @@ -22,7 +39,8 @@ private static string ChangeMylaDialogue(string key, string sheetTitle, string o
case "FLOWER_OFFER_YN":
case "GIVEN_FLOWER":
case "NOT_GIVEN_FLOWER":
return Localization.GetText(key);
newText = Localization.GetText(key);
return true;
}

MylaFlower.instance.LogWarn($"Unrecognized key for myla flower sheet: {key}");
Expand All @@ -32,18 +50,28 @@ private static string ChangeMylaDialogue(string key, string sheetTitle, string o
&& key == "MINER_DREAM_2"
&& MylaFlower.GetMylaState() == MylaState.Crazy)
{
return Localization.GetText("CRAZY_DREAM");
newText = Localization.GetText("CRAZY_DREAM");
return true;
}

if (sheetTitle == "Enemy Dreams"
&& key.StartsWith("MYLA"))
{
newText = orig; // Could be changed if I had some text; need to return true so rando gets to see it
return true;
}

if (sheetTitle == "Minor NPC"
&& key == "MINER_EARLY_3"
&& MylaFlower.GetMylaState() == MylaState.Normal)
{
return Localization.GetText("GIVEN_DIALOGUE");
newText = Localization.GetText("GIVEN_DIALOGUE");
return true;
// return "Oh hello there friend! Look, your present makes a w-wonderful headlamp. Now I can always have it with me. Thank you again!";
}

return orig;
newText = orig;
return false;
}
}
}
18 changes: 16 additions & 2 deletions MylaFlower/MylaFlower.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,27 @@
using System.Linq;
using HutongGames.PlayMaker;
using HutongGames.PlayMaker.Actions;
using ItemChanger.Internal;
using Modding;
using UnityEngine;
using UnityEngine.SceneManagement;
using Vasi;

namespace MylaFlower
{
public class MylaFlower : Mod, ILocalSettings<SaveSettings>
public class MylaFlower : Mod, ILocalSettings<SaveSettings>, IGlobalSettings<GlobalSettings>
{
internal static MylaFlower instance;


public static SaveSettings LS { get; internal set; } = new();
public void OnLoadLocal(SaveSettings s) => LS = s;
public SaveSettings OnSaveLocal() => LS;


public static GlobalSettings GS { get; internal set; } = new();
public void OnLoadGlobal(GlobalSettings s) => GS = s;
public GlobalSettings OnSaveGlobal() => GS;

public MylaFlower() : base(null)
{
instance = this;
Expand All @@ -27,6 +32,11 @@ public MylaFlower() : base(null)

public override string GetVersion() => VersionUtil.GetVersion<MylaFlower>();

// Events used by the rando connection

public static event Action OnGiveFlower;


public override void Initialize()
{
Log("Initializing Mod...");
Expand All @@ -42,6 +52,9 @@ public override void Initialize()

// Change sprite
UnityEngine.SceneManagement.SceneManager.activeSceneChanged += ReplaceMylaSprite;

// Hook rando
RandoConnection.RandoInterop.Hook();
}

private void ReplaceMylaSprite(Scene _, Scene scene)
Expand Down Expand Up @@ -159,6 +172,7 @@ private void EditCrazyMylaFsm(PlayMakerFSM fsm)
{
LS.DeliveredFlower = true;
PlayerData.instance.SetBool(nameof(PlayerData.hasXunFlower), false);
OnGiveFlower?.Invoke();
});


Expand Down
19 changes: 19 additions & 0 deletions MylaFlower/MylaFlower.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,25 @@
<Reference Include="Vasi">
<HintPath>$(HollowKnightRefs)\Mods\Vasi\Vasi.dll</HintPath>
</Reference>

<Reference Include="ItemChanger">
<HintPath>$(HollowKnightRefs)\Mods\ItemChanger\ItemChanger.dll</HintPath>
</Reference>
<Reference Include="MenuChanger">
<HintPath>$(HollowKnightRefs)\Mods\MenuChanger\MenuChanger.dll</HintPath>
</Reference>
<Reference Include="RandomizerCore">
<HintPath>$(HollowKnightRefs)\Mods\RandomizerCore\RandomizerCore.dll</HintPath>
</Reference>
<Reference Include="RandomizerCore.Json">
<HintPath>$(HollowKnightRefs)\Mods\RandomizerCore.Json\RandomizerCore.Json.dll</HintPath>
</Reference>
<Reference Include="RandomizerMod" Condition="Exists('$(HollowKnightRefs)\Mods\RandomizerMod')">
<HintPath>$(HollowKnightRefs)\Mods\RandomizerMod\RandomizerMod.dll</HintPath>
</Reference>
<Reference Include="RandomizerMod" Condition="Exists('$(HollowKnightRefs)\Mods\Randomizer 4')">
<HintPath>$(HollowKnightRefs)\Mods\Randomizer 4\RandomizerMod.dll</HintPath>
</Reference>
</ItemGroup>

<Target Name="ClearReferenceCopyLocalPaths" AfterTargets="ResolveAssemblyReferences">
Expand Down
93 changes: 93 additions & 0 deletions MylaFlower/RandoConnection/MylaFlowerLocation.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
using ItemChanger;
using ItemChanger.Locations;
using ItemChanger.Extensions;
using ItemChanger.FsmStateActions;
using UnityEngine;
using Modding;

namespace MylaFlower.RandoConnection
{
public class MylaFlowerLocation : AutoLocation, ILocalHintLocation
{
public bool HintActive { get; set; }

protected override void OnLoad()
{
MylaFlower.OnGiveFlower += GiveItemOnGiveFlower;
Events.AddFsmEdit(sceneName, new(Consts.NormalMyla, "Conversation Control"), GiveRespawnedItems);
Dialogue.OnGetMylaText += ModifyMylaText;
On.HealthManager.TakeDamage += ProtectMyla;
}

protected override void OnUnload()
{
MylaFlower.OnGiveFlower -= GiveItemOnGiveFlower;
Events.RemoveFsmEdit(sceneName, new(Consts.NormalMyla, "Conversation Control"), GiveRespawnedItems);
Dialogue.OnGetMylaText -= ModifyMylaText;
On.HealthManager.TakeDamage -= ProtectMyla;
}

private void ProtectMyla(On.HealthManager.orig_TakeDamage orig, HealthManager self, HitInstance hitInstance)
{
if (self.gameObject.name != Consts.ZombieMyla || self.gameObject.scene.name != sceneName)
{
orig(self, hitInstance);
return;
}

int num = Mathf.RoundToInt(hitInstance.DamageDealt * hitInstance.Multiplier);
if (num >= self.hp)
{
self.hp = 50 + num;
ShowPreviewConvo(self);
}

orig(self, hitInstance);
return;
}

private void ShowPreviewConvo(HealthManager hm)
{
EnemyDreamnailReaction edr = hm.gameObject.GetComponent<EnemyDreamnailReaction>();
ReflectionHelper.CallMethod(edr, "ShowConvo");
}

private string ModifyMylaText(string key, string sheetTitle, string orig)
{
if (!this.GetItemHintActive()) return orig;

if (key == "FLOWER_OFFER_YN" && sheetTitle == Consts.CustomLanguageSheet)
{
string text = Placement.GetUIName();
Placement.OnPreview(text);
return string.Format(Localization.GetText("FLOWER_OFFER_YN_RANDO"), text);
}

if (key.StartsWith("MYLA") && sheetTitle == "Enemy Dreams")
{
string text = Placement.GetUIName();
Placement.OnPreview(text);
return string.Format(Localization.GetText("ZOMBIE_DREAM_RANDO"), text);
}

return orig;
}

private void GiveRespawnedItems(PlayMakerFSM fsm)
{
fsm.GetState("Talk Finish").AddFirstAction(new Lambda(() =>
{
if (Placement.CheckVisitedAll(VisitState.Accepted) && !Placement.AllObtained())
{
GiveAll();
}
}));
}

private void GiveItemOnGiveFlower()
{
Placement.AddVisitFlag(VisitState.Accepted);
GiveAll();
}
}
}
Loading

0 comments on commit b13839e

Please sign in to comment.