TechbloxModdingAPI/GamecraftModdingAPI/Events/SimpleEventHandlerEngine.cs
2019-12-14 10:52:24 -08:00

73 lines
2.1 KiB
C#

using Svelto.ECS;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace GamecraftModdingAPI.Events
{
/// <summary>
/// A simple implementation of IEventHandlerEngine sufficient for most uses
/// </summary>
class SimpleEventHandlerEngine : IEventHandlerEngine
{
public object type { get; set; }
public string Name { get; set; }
private bool isActivated = false;
private readonly Action<IEntitiesDB> onActivated;
private readonly Action<IEntitiesDB> onDestroyed;
public IEntitiesDB entitiesDB { set; private get; }
public void Add(ref ModEventEntityStruct entityView, EGID egid)
{
if (entityView.type.Equals(this.type))
{
isActivated = true;
onActivated.Invoke(entitiesDB);
}
}
public void Ready() { }
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);
}
}
/// <summary>
///
/// </summary>
/// <param name="activated"></param>
/// <param name="removed"></param>
/// <param name="type"></param>
/// <param name="name"></param>
public SimpleEventHandlerEngine(Action activated, Action removed, object type, string name)
: this((IEntitiesDB _) => { activated.Invoke(); }, (IEntitiesDB _) => { removed.Invoke(); }, type, name) { }
public SimpleEventHandlerEngine(Action<IEntitiesDB> activated, Action<IEntitiesDB> removed, object type, string name)
{
this.type = type;
this.Name = name;
this.onActivated = activated;
this.onDestroyed = removed;
}
}
}