TechbloxModdingAPI/GamecraftModdingAPI/Events/SimpleEventHandlerEngine.cs

82 lines
2.7 KiB
C#
Raw Normal View History

using System;
2019-12-14 04:42:55 +00:00
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Svelto.ECS;
2019-12-14 04:42:55 +00:00
namespace GamecraftModdingAPI.Events
{
2019-12-14 18:52:24 +00:00
/// <summary>
/// A simple implementation of IEventHandlerEngine sufficient for most uses
/// </summary>
2019-12-15 07:20:20 +00:00
public class SimpleEventHandlerEngine : IEventHandlerEngine
2019-12-14 04:42:55 +00:00
{
public object type { get; set; }
public string Name { get; set; }
2019-12-14 18:52:24 +00:00
private bool isActivated = false;
private readonly Action<IEntitiesDB> onActivated;
private readonly Action<IEntitiesDB> onDestroyed;
2019-12-14 04:42:55 +00:00
public IEntitiesDB entitiesDB { set; private get; }
public void Add(ref ModEventEntityStruct entityView, EGID egid)
{
if (entityView.type.Equals(this.type))
{
2019-12-14 18:52:24 +00:00
isActivated = true;
onActivated.Invoke(entitiesDB);
2019-12-14 04:42:55 +00:00
}
}
public void Ready() { }
2019-12-14 18:52:24 +00:00
public void Remove(ref ModEventEntityStruct entityView, EGID egid)
{
if (entityView.type.Equals(this.type) && isActivated)
{
isActivated = false;
onDestroyed.Invoke(entitiesDB);
}
}
public void Dispose()
{
if (isActivated)
{
isActivated = false;
onDestroyed.Invoke(entitiesDB);
}
}
2019-12-14 04:42:55 +00:00
2019-12-14 18:52:24 +00:00
/// <summary>
2019-12-15 07:20:20 +00:00
/// Construct the engine
2019-12-14 18:52:24 +00:00
/// </summary>
2019-12-15 07:20:20 +00:00
/// <param name="activated">The operation to do when the event is created</param>
/// <param name="removed">The operation to do when the event is destroyed (if applicable)</param>
/// <param name="type">The type of event to handle</param>
/// <param name="name">The name of the engine</param>
2019-12-14 18:52:24 +00:00
public SimpleEventHandlerEngine(Action activated, Action removed, object type, string name)
: this((IEntitiesDB _) => { activated.Invoke(); }, (IEntitiesDB _) => { removed.Invoke(); }, type, name) { }
2019-12-15 07:20:20 +00:00
/// <summary>
/// Construct the engine
/// </summary>
/// <param name="activated">The operation to do when the event is created</param>
/// <param name="removed">The operation to do when the event is destroyed (if applicable)</param>
/// <param name="type">The type of event to handler</param>
/// <param name="name">The name of the engine</param>
2019-12-14 18:52:24 +00:00
public SimpleEventHandlerEngine(Action<IEntitiesDB> activated, Action<IEntitiesDB> removed, object type, string name)
2019-12-14 04:42:55 +00:00
{
this.type = type;
this.Name = name;
2019-12-14 18:52:24 +00:00
this.onActivated = activated;
this.onDestroyed = removed;
2019-12-14 04:42:55 +00:00
}
}
}