A stable modding interface between Techblox and mods https://mod.exmods.org/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

82 lines
2.7KB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using Svelto.ECS;
  7. namespace GamecraftModdingAPI.Events
  8. {
  9. /// <summary>
  10. /// A simple implementation of IEventHandlerEngine sufficient for most uses
  11. /// </summary>
  12. public class SimpleEventHandlerEngine : IEventHandlerEngine
  13. {
  14. public object type { get; set; }
  15. public string Name { get; set; }
  16. private bool isActivated = false;
  17. private readonly Action<IEntitiesDB> onActivated;
  18. private readonly Action<IEntitiesDB> onDestroyed;
  19. public IEntitiesDB entitiesDB { set; private get; }
  20. public void Add(ref ModEventEntityStruct entityView, EGID egid)
  21. {
  22. if (entityView.type.Equals(this.type))
  23. {
  24. isActivated = true;
  25. onActivated.Invoke(entitiesDB);
  26. }
  27. }
  28. public void Ready() { }
  29. public void Remove(ref ModEventEntityStruct entityView, EGID egid)
  30. {
  31. if (entityView.type.Equals(this.type) && isActivated)
  32. {
  33. isActivated = false;
  34. onDestroyed.Invoke(entitiesDB);
  35. }
  36. }
  37. public void Dispose()
  38. {
  39. if (isActivated)
  40. {
  41. isActivated = false;
  42. onDestroyed.Invoke(entitiesDB);
  43. }
  44. }
  45. /// <summary>
  46. /// Construct the engine
  47. /// </summary>
  48. /// <param name="activated">The operation to do when the event is created</param>
  49. /// <param name="removed">The operation to do when the event is destroyed (if applicable)</param>
  50. /// <param name="type">The type of event to handle</param>
  51. /// <param name="name">The name of the engine</param>
  52. public SimpleEventHandlerEngine(Action activated, Action removed, object type, string name)
  53. : this((IEntitiesDB _) => { activated.Invoke(); }, (IEntitiesDB _) => { removed.Invoke(); }, type, name) { }
  54. /// <summary>
  55. /// Construct the engine
  56. /// </summary>
  57. /// <param name="activated">The operation to do when the event is created</param>
  58. /// <param name="removed">The operation to do when the event is destroyed (if applicable)</param>
  59. /// <param name="type">The type of event to handler</param>
  60. /// <param name="name">The name of the engine</param>
  61. public SimpleEventHandlerEngine(Action<IEntitiesDB> activated, Action<IEntitiesDB> removed, object type, string name)
  62. {
  63. this.type = type;
  64. this.Name = name;
  65. this.onActivated = activated;
  66. this.onDestroyed = removed;
  67. }
  68. }
  69. }