Skip to content

Commit

Permalink
feat: Add new overload for RegisterCustomEvent story goal (#476)
Browse files Browse the repository at this point in the history
* fix: StoryGoal actions receive the name of the unlocked goal

* Revert "fix: StoryGoal actions receive the name of the unlocked goal"

This reverts commit df9b622.

* feat: Add new overload for registering custom StoryGoal events
  • Loading branch information
tinyhoot authored Sep 28, 2023
1 parent 4330e5e commit 9f27720
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
20 changes: 19 additions & 1 deletion Nautilus/Handlers/StoryGoalHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,12 +137,21 @@ public static void RegisterOnGoalUnlockData(string goal, UnlockBlueprintData[] b
/// Registers a given <see cref="Action"/> to be performed when its associated goal is completed.
/// </summary>
/// <param name="key">The key of the goal that triggers the <paramref name="customEventCallback"/>.</param>
/// <param name="customEventCallback">The method that is called when the associated goal is completed. The name of the goal will be passed as a parameter.</param>
/// <param name="customEventCallback">The method that is called when the associated goal is completed.</param>
public static void RegisterCustomEvent(string key, Action customEventCallback)
{
CustomStoryGoalManager.StoryGoalCustomEvents.GetOrAddNew(key).Add(customEventCallback);
}

/// <summary>
/// Registers a given <see cref="Action"/> to be performed when a goal is completed.
/// </summary>
/// <param name="customEventCallback">The method that is called when any goal is completed. The name of the goal will be passed as a parameter.</param>
public static void RegisterCustomEvent(Action<string> customEventCallback)
{
CustomStoryGoalManager.StoryGoalCustomKeyHandlerEvents.Add(customEventCallback);
}

/// <summary>
/// Unregisters a custom event.
/// </summary>
Expand All @@ -155,4 +164,13 @@ public static void UnregisterCustomEvent(string key, Action customEventCallback)
callbacks.Remove(customEventCallback);
}
}

/// <summary>
/// Unregisters a custom event.
/// </summary>
/// <param name="customEventCallback">The method to unregister.</param>
public static void UnregisterCustomEvent(Action<string> customEventCallback)
{
CustomStoryGoalManager.StoryGoalCustomKeyHandlerEvents.Remove(customEventCallback);
}
}
2 changes: 2 additions & 0 deletions Nautilus/MonoBehaviours/CustomStoryGoalManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ internal class CustomStoryGoalManager : MonoBehaviour, IStoryGoalListener
public static CustomStoryGoalManager Instance { get; private set; }

internal static readonly Dictionary<string, List<Action>> StoryGoalCustomEvents = new();
internal static readonly List<Action<string>> StoryGoalCustomKeyHandlerEvents = new();

private ItemGoalTracker _itemGoalTracker;
private BiomeGoalTracker _biomeGoalTracker;
Expand Down Expand Up @@ -70,6 +71,7 @@ void IStoryGoalListener.NotifyGoalComplete(string key)
if (key == customEvent.Key)
customEvent.Value.ForEach(x => x?.Invoke());
}
StoryGoalCustomKeyHandlerEvents.ForEach(x => x?.Invoke(key));
}

#if BELOWZERO
Expand Down

0 comments on commit 9f27720

Please sign in to comment.