Refactor event management: remove old EventManager and EventExample scripts, add new Core/EventManager as singleton

This commit is contained in:
Will Stuckey
2025-10-28 13:02:48 -05:00
parent 3deeb42403
commit 873cc7dd17
8 changed files with 81 additions and 198 deletions

View File

@@ -0,0 +1,78 @@
using Godot;
using EinSoftworks.Events;
namespace Voider;
/// <summary>
/// Autoload singleton that provides access to the ConfigurableEventManager
/// </summary>
public partial class EventManager : Node
{
private static EventManager _instance;
public static EventManager Instance => _instance;
private ConfigurableEventManager _eventManager;
public override void _Ready()
{
_instance = this;
_eventManager = new ConfigurableEventManager();
}
/// <summary>
/// Raise a game event using the resource's EventName
/// </summary>
public void RaiseEvent(GameEventResource eventResource)
{
if (eventResource?.EventName != null)
{
_eventManager.Raise(eventResource.EventName);
}
}
/// <summary>
/// Raise a game event by name
/// </summary>
public void RaiseEvent(string eventName)
{
_eventManager.Raise(eventName);
}
/// <summary>
/// Subscribe to a game event using the resource's EventName
/// </summary>
public void Subscribe(GameEventResource eventResource, System.Action callback)
{
if (eventResource?.EventName != null)
{
_eventManager.Subscribe(eventResource.EventName, callback);
}
}
/// <summary>
/// Subscribe to a game event by name
/// </summary>
public void Subscribe(string eventName, System.Action callback)
{
_eventManager.Subscribe(eventName, callback);
}
/// <summary>
/// Unsubscribe from a game event using the resource's EventName
/// </summary>
public void Unsubscribe(GameEventResource eventResource, System.Action callback)
{
if (eventResource?.EventName != null)
{
_eventManager.Unsubscribe(eventResource.EventName, callback);
}
}
/// <summary>
/// Unsubscribe from a game event by name
/// </summary>
public void Unsubscribe(string eventName, System.Action callback)
{
_eventManager.Unsubscribe(eventName, callback);
}
}

View File

@@ -0,0 +1 @@
uid://yt5e67nfdc6y

View File

@@ -1,81 +0,0 @@
using Godot;
using EinSoftworks.Events;
namespace Voider;
/// <summary>
/// Example script demonstrating how to use the ConfigurableEventManager.
/// This would typically be attached to a Player, Enemy, or other game object.
/// </summary>
public partial class EventExample : Node
{
// Reference to the ConfigurableEventManager in the scene
private EventManager _eventManager;
public override void _Ready()
{
// Get reference to the ConfigurableEventManager node
// (Assumes it's named "EventManager" in your scene tree)
_eventManager = GetNode<EventManager>("/root/Main/EventManager");
// Subscribe to events
SubscribeToEvents();
}
private void SubscribeToEvents()
{
// Example: Subscribe to a simple event with no parameters
_eventManager.Subscribe("GameStarted", OnGameStarted);
// Example: Subscribe to an event with one parameter (int score)
_eventManager.Subscribe<int>("ScoreChanged", OnScoreChanged);
// Example: Subscribe to an event with two parameters (string playerName, int level)
_eventManager.Subscribe<string, int>("PlayerLevelUp", OnPlayerLevelUp);
}
// Event handlers
private void OnGameStarted()
{
GD.Print("Game has started!");
}
private void OnScoreChanged(int newScore)
{
GD.Print($"Score changed to: {newScore}");
}
private void OnPlayerLevelUp(string playerName, int newLevel)
{
GD.Print($"{playerName} leveled up to level {newLevel}!");
}
// Example of raising events from code
public void SomeGameAction()
{
// Raise an event with no parameters
_eventManager.Raise("GameStarted");
// Raise an event with one parameter
_eventManager.Raise("ScoreChanged", 100);
// Raise an event with two parameters
_eventManager.Raise("PlayerLevelUp", "Player1", 5);
}
public override void _ExitTree()
{
// Always unsubscribe when node is removed
UnsubscribeFromEvents();
}
private void UnsubscribeFromEvents()
{
if (_eventManager != null)
{
_eventManager.Unsubscribe("GameStarted", OnGameStarted);
_eventManager.Unsubscribe<int>("ScoreChanged", OnScoreChanged);
_eventManager.Unsubscribe<string, int>("PlayerLevelUp", OnPlayerLevelUp);
}
}
}

View File

@@ -1 +0,0 @@
uid://b4fnyaf5ok7hg

View File

@@ -1,15 +0,0 @@
using Godot;
using EinSoftworks.Events;
namespace Voider;
/// <summary>
/// Wrapper for ConfigurableEventManager that makes it available in this project.
/// This inherits from the library's ConfigurableEventManager.
/// </summary>
[GlobalClass]
public partial class EventManager : ConfigurableEventManager
{
// Inherits all functionality from ConfigurableEventManager
// You can add project-specific event manager functionality here if needed
}

View File

@@ -1 +0,0 @@
uid://butot2dkursrs