Refactor event management: remove old EventManager and EventExample scripts, add new Core/EventManager as singleton
This commit is contained in:
78
Scripts/Core/EventManager.cs
Normal file
78
Scripts/Core/EventManager.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
1
Scripts/Core/EventManager.cs.uid
Normal file
1
Scripts/Core/EventManager.cs.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://yt5e67nfdc6y
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1 +0,0 @@
|
||||
uid://b4fnyaf5ok7hg
|
||||
@@ -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
|
||||
}
|
||||
@@ -1 +0,0 @@
|
||||
uid://butot2dkursrs
|
||||
Reference in New Issue
Block a user