TechbloxModdingAPI/GamecraftModdingAPI/Events/SimpleEventHandlerEngine.cs
NorbiPeti b4e70fcebe
Update to GC 2020.03.11.11.02
Fixed compile issues and added new blocks
2020-03-12 23:38:32 +01:00

92 lines
3.1 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Svelto.ECS;
namespace GamecraftModdingAPI.Events
{
/// <summary>
/// A simple implementation of IEventHandlerEngine sufficient for most uses
/// </summary>
public class SimpleEventHandlerEngine : IEventHandlerEngine
{
public object type { get; set; }
public string Name { get; set; }
private bool isActivated = false;
private readonly Action<EntitiesDB> onActivated;
private readonly Action<EntitiesDB> onDestroyed;
public EntitiesDB entitiesDB { set; private get; }
public void Add(ref ModEventEntityStruct entityView, EGID egid)
{
if (entityView.type.Equals(this.type))
{
isActivated = true;
onActivated.Invoke(entitiesDB);
}
}
/// <summary>
/// Manually activate the EventHandler.
/// Once activated, the next remove event will not be ignored.
/// </summary>
/// <param name="handle">Whether to invoke the activated action</param>
public void Activate(bool handle = false)
{
isActivated = true;
}
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>
/// 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 handle</param>
/// <param name="name">The name of the engine</param>
/// <param name="simple">A useless parameter to use to avoid Python overload resolution errors</param>
public SimpleEventHandlerEngine(Action activated, Action removed, object type, string name, bool simple = true)
: this((EntitiesDB _) => { activated.Invoke(); }, (EntitiesDB _) => { removed.Invoke(); }, type, name) { }
/// <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>
public SimpleEventHandlerEngine(Action<EntitiesDB> activated, Action<EntitiesDB> removed, object type, string name)
{
this.type = type;
this.Name = name;
this.onActivated = activated;
this.onDestroyed = removed;
}
}
}