Add attribute system with core stats and gameplay components
This commit is contained in:
10
GameCore/Events/DamageDealtEvent.cs
Normal file
10
GameCore/Events/DamageDealtEvent.cs
Normal file
@@ -0,0 +1,10 @@
|
||||
using GameCore.ECS;
|
||||
using GameCore.Events.Interfaces;
|
||||
|
||||
namespace GameCore.Events;
|
||||
|
||||
public readonly struct DamageDealtEvent(Entity target, float amount) : IEvent
|
||||
{
|
||||
public readonly Entity Target = target;
|
||||
public readonly float Amount = amount;
|
||||
}
|
||||
9
GameCore/Events/EntityDiedEvent.cs
Normal file
9
GameCore/Events/EntityDiedEvent.cs
Normal file
@@ -0,0 +1,9 @@
|
||||
using GameCore.ECS;
|
||||
using GameCore.Events.Interfaces;
|
||||
|
||||
namespace GameCore.Events;
|
||||
|
||||
public readonly struct EntityDiedEvent(Entity entity) : IEvent
|
||||
{
|
||||
public readonly Entity Entity = entity;
|
||||
}
|
||||
11
GameCore/Events/EntitySpawnedEvent.cs
Normal file
11
GameCore/Events/EntitySpawnedEvent.cs
Normal file
@@ -0,0 +1,11 @@
|
||||
using GameCore.ECS;
|
||||
using GameCore.Events.Interfaces;
|
||||
|
||||
namespace GameCore.Events;
|
||||
|
||||
public readonly struct EntitySpawnedEvent(Entity newEntity, Entity owner, string archetypeId) : IEvent
|
||||
{
|
||||
public readonly Entity NewEntity = newEntity;
|
||||
public readonly Entity Owner = owner;
|
||||
public readonly string ArchetypeId = archetypeId;
|
||||
}
|
||||
46
GameCore/Events/EventBus.cs
Normal file
46
GameCore/Events/EventBus.cs
Normal file
@@ -0,0 +1,46 @@
|
||||
using GameCore.Events.Interfaces;
|
||||
|
||||
namespace GameCore.Events;
|
||||
|
||||
public class EventBus
|
||||
{
|
||||
private readonly Queue<IEvent> _eventQueue = new();
|
||||
private readonly Dictionary<Type, List<Action<IEvent>>> _subscribers = new();
|
||||
|
||||
public void Subscribe<T>(Action<T> listener) where T : IEvent
|
||||
{
|
||||
var eventType = typeof(T);
|
||||
if (!_subscribers.ContainsKey(eventType)) _subscribers[eventType] = [];
|
||||
|
||||
_subscribers[eventType].Add(e => listener((T)e));
|
||||
}
|
||||
|
||||
public void Unsubscribe<T>(Action<T> listener) where T : IEvent
|
||||
{
|
||||
var eventType = typeof(T);
|
||||
if (!_subscribers.TryGetValue(eventType, out var listeners)) return;
|
||||
|
||||
listeners.RemoveAll(e => e.Equals((Action<IEvent>)(e1 => listener((T)e1))));
|
||||
|
||||
if (listeners.Count == 0) _subscribers.Remove(eventType);
|
||||
}
|
||||
|
||||
public void Publish(IEvent gameEvent)
|
||||
{
|
||||
_eventQueue.Enqueue(gameEvent);
|
||||
}
|
||||
|
||||
public void ProcessEvents()
|
||||
{
|
||||
while (_eventQueue.Count > 0)
|
||||
{
|
||||
var gameEvent = _eventQueue.Dequeue();
|
||||
var eventType = gameEvent.GetType();
|
||||
|
||||
if (!_subscribers.TryGetValue(eventType, out var listeners)) continue;
|
||||
|
||||
foreach (var listener in listeners)
|
||||
listener.Invoke(gameEvent);
|
||||
}
|
||||
}
|
||||
}
|
||||
6
GameCore/Events/Interfaces/IEvent.cs
Normal file
6
GameCore/Events/Interfaces/IEvent.cs
Normal file
@@ -0,0 +1,6 @@
|
||||
namespace GameCore.Events.Interfaces;
|
||||
|
||||
public interface IEvent
|
||||
{
|
||||
|
||||
}
|
||||
20
GameCore/Events/SpawnEntityEvent.cs
Normal file
20
GameCore/Events/SpawnEntityEvent.cs
Normal file
@@ -0,0 +1,20 @@
|
||||
using GameCore.ECS;
|
||||
using GameCore.Events.Interfaces;
|
||||
using GameCore.Math;
|
||||
|
||||
namespace GameCore.Events;
|
||||
|
||||
public readonly struct SpawnEntityEvent(
|
||||
string archetypeId,
|
||||
Vector3 position,
|
||||
Vector3 rotation,
|
||||
Entity owner,
|
||||
Vector3? initialVelocity = null)
|
||||
: IEvent
|
||||
{
|
||||
public readonly string ArchetypeId = archetypeId;
|
||||
public readonly Vector3 Position = position;
|
||||
public readonly Vector3 Rotation = rotation;
|
||||
public readonly Entity Owner = owner;
|
||||
public readonly Vector3? InitialVelocity = initialVelocity;
|
||||
}
|
||||
11
GameCore/Events/WeaponFiredEvent.cs
Normal file
11
GameCore/Events/WeaponFiredEvent.cs
Normal file
@@ -0,0 +1,11 @@
|
||||
using GameCore.ECS;
|
||||
using GameCore.Events.Interfaces;
|
||||
using GameCore.Math;
|
||||
|
||||
namespace GameCore.Events;
|
||||
|
||||
public readonly struct WeaponFiredEvent(Entity owner, Vector3 muzzlePosition) : IEvent
|
||||
{
|
||||
public readonly Entity Owner = owner;
|
||||
public readonly Vector3 MuzzlePosition = muzzlePosition;
|
||||
}
|
||||
Reference in New Issue
Block a user