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.

101 lines
3.6KB

  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 int type { get; set; }
  15. public string Name { get; set; }
  16. private bool isActivated = false;
  17. private readonly Action<EntitiesDB> onActivated;
  18. private readonly Action<EntitiesDB> onDestroyed;
  19. public EntitiesDB entitiesDB { set; private get; }
  20. public bool isRemovable => true;
  21. public void Add(ref ModEventEntityStruct entityView, EGID egid)
  22. {
  23. if (entityView.type.Equals(this.type))
  24. {
  25. isActivated = true;
  26. onActivated.Invoke(entitiesDB);
  27. }
  28. }
  29. /// <summary>
  30. /// Manually activate the EventHandler.
  31. /// Once activated, the next remove event will not be ignored.
  32. /// </summary>
  33. /// <param name="handle">Whether to invoke the activated action</param>
  34. public void Activate(bool handle = false)
  35. {
  36. isActivated = true;
  37. }
  38. public void Ready() { }
  39. public void Remove(ref ModEventEntityStruct entityView, EGID egid)
  40. {
  41. if (entityView.type.Equals(this.type) && isActivated)
  42. {
  43. isActivated = false;
  44. onDestroyed.Invoke(entitiesDB);
  45. }
  46. }
  47. public void Dispose()
  48. {
  49. if (isActivated)
  50. {
  51. isActivated = false;
  52. onDestroyed.Invoke(entitiesDB);
  53. }
  54. }
  55. /// <summary>
  56. /// Construct the engine
  57. /// </summary>
  58. /// <param name="activated">The operation to do when the event is created</param>
  59. /// <param name="removed">The operation to do when the event is destroyed (if applicable)</param>
  60. /// <param name="type">The type of event to handle</param>
  61. /// <param name="name">The name of the engine</param>
  62. /// <param name="simple">A useless parameter to use to avoid Python overload resolution errors</param>
  63. public SimpleEventHandlerEngine(Action activated, Action removed, int type, string name, bool simple = true)
  64. : this((EntitiesDB _) => { activated.Invoke(); }, (EntitiesDB _) => { removed.Invoke(); }, type, name) { }
  65. /// <summary>
  66. /// Construct the engine
  67. /// </summary>
  68. /// <param name="activated">The operation to do when the event is created</param>
  69. /// <param name="removed">The operation to do when the event is destroyed (if applicable)</param>
  70. /// <param name="type">The type of event to handler</param>
  71. /// <param name="name">The name of the engine</param>
  72. public SimpleEventHandlerEngine(Action<EntitiesDB> activated, Action<EntitiesDB> removed, int type, string name)
  73. {
  74. this.type = type;
  75. this.Name = name;
  76. this.onActivated = activated;
  77. this.onDestroyed = removed;
  78. }
  79. public SimpleEventHandlerEngine(Action activated, Action removed, EventType type, string name, bool simple = true)
  80. : this((EntitiesDB _) => { activated.Invoke(); }, (EntitiesDB _) => { removed.Invoke(); }, (int)type, name) { }
  81. public SimpleEventHandlerEngine(Action<EntitiesDB> activated, Action<EntitiesDB> removed, EventType type, string name, bool simple = true)
  82. : this(activated, removed, (int)type, name) { }
  83. }
  84. }