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.

74 lines
2.1KB

  1. using Svelto.ECS;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace GamecraftModdingAPI.Events
  8. {
  9. /// <summary>
  10. /// A simple implementation of IEventHandlerEngine sufficient for most uses
  11. /// </summary>
  12. 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. ///
  47. /// </summary>
  48. /// <param name="activated"></param>
  49. /// <param name="removed"></param>
  50. /// <param name="type"></param>
  51. /// <param name="name"></param>
  52. public SimpleEventHandlerEngine(Action activated, Action removed, object type, string name)
  53. : this((IEntitiesDB _) => { activated.Invoke(); }, (IEntitiesDB _) => { removed.Invoke(); }, type, name) { }
  54. public SimpleEventHandlerEngine(Action<IEntitiesDB> activated, Action<IEntitiesDB> removed, object type, string name)
  55. {
  56. this.type = type;
  57. this.Name = name;
  58. this.onActivated = activated;
  59. this.onDestroyed = removed;
  60. }
  61. }
  62. }