init
This commit is contained in:
@@ -0,0 +1,24 @@
|
||||
using RPGCore.Core.Objects;
|
||||
|
||||
namespace RPGCore.ObjectModules.EventObjectModule
|
||||
{
|
||||
/// <summary>
|
||||
/// Extend this to create new type of event for <<typeparamref name="T"/>> type of object.<br/><br/>
|
||||
/// This event is called once by <see cref="EventModule"/>.<see cref="EventModule.Invoke{T}"/>:
|
||||
/// </summary>
|
||||
/// <typeparam name="T"><see cref="BaseObject"/> (or any of its children) that this event can be attached to.</typeparam>
|
||||
/// <seealso cref="BasePreventableEvent{T}"/>
|
||||
public abstract class BaseEvent<T> : BaseEvent where T : BaseObject
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// <b>DO NOT EXTEND THIS</b>, use <see cref="BaseEvent{T}"/> instead!
|
||||
/// </summary>
|
||||
public abstract class BaseEvent
|
||||
{
|
||||
protected internal BaseEvent()
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 71103e95ef1449e096cf7e813cc339dc
|
||||
timeCreated: 1761865102
|
||||
@@ -0,0 +1,27 @@
|
||||
using RPGCore.Core.Objects;
|
||||
|
||||
namespace RPGCore.ObjectModules.EventObjectModule
|
||||
{
|
||||
/// <summary>
|
||||
/// Extend this to create new type of preventable event for <<typeparamref name="T"/>> type of object.<br/><br/>
|
||||
/// This event can be called twice by <see cref="EventModule"/>:
|
||||
/// <ul>
|
||||
/// <li><see cref="EventModule.InvokeBefore{T}"/></li>
|
||||
/// <li><see cref="EventModule.InvokeAfter{T}"/> - if not prevented in before</li>
|
||||
/// </ul>
|
||||
/// </summary>
|
||||
/// <typeparam name="T"><see cref="BaseObject"/> (or any of its children) that this event can be attached to.</typeparam>
|
||||
/// <seealso cref="BaseEvent{T}"/>
|
||||
public abstract class BasePreventableEvent<T> : BasePreventableEvent where T : BaseObject
|
||||
{
|
||||
}
|
||||
|
||||
public abstract class BasePreventableEvent
|
||||
{
|
||||
public bool isPrevented;
|
||||
|
||||
protected internal BasePreventableEvent()
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ff4239f29db24fc787c11af5bc1fdaca
|
||||
timeCreated: 1761865162
|
||||
@@ -0,0 +1,84 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using RPGCore.Core;
|
||||
using RPGCore.Core.Objects;
|
||||
|
||||
namespace RPGCore.ObjectModules.EventObjectModule
|
||||
{
|
||||
[Serializable]
|
||||
public class EventModule : ObjectModule<BaseObject>
|
||||
{
|
||||
// TODO: tutaj dodać serializowane eventy w postaci SerializableDictionary<SerializableType, UnityAction>
|
||||
// TODO: dodatkowo mozna zmienić preventableEvents na dwa osobne słowniki before i after
|
||||
|
||||
internal readonly Dictionary<Type, Delegate> events = new();
|
||||
internal readonly Dictionary<Type, Delegate[]> preventableEvents = new();
|
||||
|
||||
public void Invoke<T>(T baseEvent) where T : BaseEvent
|
||||
{
|
||||
events.TryAdd(typeof(T), null);
|
||||
(events[typeof(T)] as Action<T>)?.Invoke(baseEvent);
|
||||
}
|
||||
|
||||
public void Register<T>(Action<T> action) where T : BaseEvent
|
||||
{
|
||||
events.TryAdd(typeof(T), null);
|
||||
var temp = (Action<T>)events[typeof(T)];
|
||||
temp += action;
|
||||
events[typeof(T)] = temp;
|
||||
}
|
||||
|
||||
public void Unregister<T>(Action<T> action) where T : BaseEvent
|
||||
{
|
||||
events.TryAdd(typeof(T), null);
|
||||
var temp = (Action<T>)events[typeof(T)];
|
||||
temp -= action;
|
||||
events[typeof(T)] = temp;
|
||||
}
|
||||
|
||||
public void InvokeBefore<T>(T basePreventableEvent) where T : BasePreventableEvent
|
||||
{
|
||||
preventableEvents.TryAdd(typeof(T), new Delegate[2]);
|
||||
(preventableEvents[typeof(T)][0] as Action<T>)?.Invoke(basePreventableEvent);
|
||||
}
|
||||
|
||||
public void RegisterBefore<T>(Action<T> action) where T : BasePreventableEvent
|
||||
{
|
||||
preventableEvents.TryAdd(typeof(T), new Delegate[2]);
|
||||
var temp = (Action<T>)preventableEvents[typeof(T)][0];
|
||||
temp += action;
|
||||
preventableEvents[typeof(T)][0] = temp;
|
||||
}
|
||||
|
||||
public void UnregisterBefore<T>(Action<T> action) where T : BasePreventableEvent
|
||||
{
|
||||
preventableEvents.TryAdd(typeof(T), new Delegate[2]);
|
||||
var temp = (Action<T>)preventableEvents[typeof(T)][0];
|
||||
temp -= action;
|
||||
preventableEvents[typeof(T)][0] = temp;
|
||||
}
|
||||
|
||||
public void InvokeAfter<T>(T basePreventableEvent) where T : BasePreventableEvent
|
||||
{
|
||||
if (basePreventableEvent.isPrevented) throw new EventPreventedException("Event is prevented and can't be invoked!");
|
||||
preventableEvents.TryAdd(typeof(T), new Delegate[2]);
|
||||
(preventableEvents[typeof(T)][1] as Action<T>)?.Invoke(basePreventableEvent);
|
||||
}
|
||||
|
||||
public void RegisterAfter<T>(Action<T> action) where T : BasePreventableEvent
|
||||
{
|
||||
preventableEvents.TryAdd(typeof(T), new Delegate[2]);
|
||||
var temp = (Action<T>)preventableEvents[typeof(T)][1];
|
||||
temp += action;
|
||||
preventableEvents[typeof(T)][1] = temp;
|
||||
}
|
||||
|
||||
public void UnregisterAfter<T>(Action<T> action) where T : BasePreventableEvent
|
||||
{
|
||||
preventableEvents.TryAdd(typeof(T), new Delegate[2]);
|
||||
var temp = (Action<T>)preventableEvents[typeof(T)][1];
|
||||
temp -= action;
|
||||
preventableEvents[typeof(T)][1] = temp;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 3fa533ce2d5945ec9f533cfd956a5394
|
||||
timeCreated: 1761863930
|
||||
@@ -0,0 +1,11 @@
|
||||
using System;
|
||||
|
||||
namespace RPGCore.ObjectModules.EventObjectModule
|
||||
{
|
||||
public class EventPreventedException : Exception
|
||||
{
|
||||
public EventPreventedException(string message) : base(message)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: a647e5d4d4ed44c28fd6961d60943fe4
|
||||
timeCreated: 1761917482
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 579fbe50273b4fa78b63536a5c316b68
|
||||
timeCreated: 1762712676
|
||||
@@ -0,0 +1,9 @@
|
||||
using RPGCore.Core.Objects;
|
||||
|
||||
namespace RPGCore.ObjectModules.EventObjectModule.Events
|
||||
{
|
||||
public class RemoveEvent : BaseEvent<BaseObject>
|
||||
{
|
||||
public BaseObject obj;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 11c3329262c8465b9a3010eea4f08e69
|
||||
timeCreated: 1761916133
|
||||
@@ -0,0 +1,9 @@
|
||||
using RPGCore.Core.Objects;
|
||||
|
||||
namespace RPGCore.ObjectModules.EventObjectModule.Events
|
||||
{
|
||||
public class SpawnEvent : BaseEvent<BaseObject>
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: d57d2e777dd44799a419a6a81dc1558a
|
||||
timeCreated: 1761913423
|
||||
@@ -0,0 +1,11 @@
|
||||
using RPGCore.Core;
|
||||
using RPGCore.Core.Objects;
|
||||
|
||||
namespace RPGCore.ObjectModules.EventObjectModule.Events
|
||||
{
|
||||
public class TriggerEnterEvent : BaseEvent<BaseObject>
|
||||
{
|
||||
public BaseObject target;
|
||||
public ITrigger trigger;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 3a95611010304d37ad43daf0b18dd30c
|
||||
timeCreated: 1761916203
|
||||
@@ -0,0 +1,11 @@
|
||||
using RPGCore.Core;
|
||||
using RPGCore.Core.Objects;
|
||||
|
||||
namespace RPGCore.ObjectModules.EventObjectModule.Events
|
||||
{
|
||||
public class TriggerExitEvent : BaseEvent<BaseObject>
|
||||
{
|
||||
public BaseObject target;
|
||||
public ITrigger trigger;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: d36459dce15b4f69b33ddcf21b6bdc3d
|
||||
timeCreated: 1762710866
|
||||
Reference in New Issue
Block a user