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.

EmitterBuilder.cs 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. using System;
  2. using Svelto.ECS;
  3. namespace GamecraftModdingAPI.Events
  4. {
  5. public class EmitterBuilder
  6. {
  7. private string name;
  8. private int? type;
  9. /// <summary>
  10. /// Create a new event emitter builder.
  11. /// </summary>
  12. public EmitterBuilder()
  13. {
  14. }
  15. /// <summary>
  16. /// Create a new event emitter builder.
  17. /// This is equivalent to new <code>EmitterBuilder().Name(name)</code>
  18. /// </summary>
  19. /// <param name="name">The emitter name.</param>
  20. public EmitterBuilder(string name)
  21. {
  22. this.name = name;
  23. }
  24. /// <summary>
  25. /// Create and return an event emitter builder.
  26. /// </summary>
  27. /// <returns>The builder.</returns>
  28. public static EmitterBuilder Builder()
  29. {
  30. return new EmitterBuilder();
  31. }
  32. /// <summary>
  33. /// Create and return an event emitter builder.
  34. /// This is equivalent to <code>Builder().Name(name)</code>
  35. /// </summary>
  36. /// <returns>The builder.</returns>
  37. /// <param name="name">The emitter name.</param>
  38. public static EmitterBuilder Builder(string name)
  39. {
  40. return new EmitterBuilder(name);
  41. }
  42. /// <summary>
  43. /// Name the event emitter.
  44. /// </summary>
  45. /// <returns>The builder.</returns>
  46. /// <param name="name">The event emitter name.</param>
  47. public EmitterBuilder Name(string name)
  48. {
  49. this.name = name;
  50. return this;
  51. }
  52. /// <summary>
  53. /// Set the type of event to handle.
  54. /// </summary>
  55. /// <returns>The builder.</returns>
  56. /// <param name="eventType">The event type.</param>
  57. public EmitterBuilder Handle(EventType eventType)
  58. {
  59. return Handle((int)eventType);
  60. }
  61. /// <summary>
  62. /// Set the type of event to handle.
  63. /// </summary>
  64. /// <returns>The builder.</returns>
  65. /// <param name="eventType">The event type.</param>
  66. public EmitterBuilder Handle(int eventType)
  67. {
  68. this.type = eventType;
  69. return this;
  70. }
  71. /// <summary>
  72. /// Build the event emitter.
  73. /// </summary>
  74. /// <returns>The event emitter.</returns>
  75. /// <param name="register">Automatically register the event emitter with EventManager.AddEventemitter().</param>
  76. public IEventEmitterEngine Build(bool register = true)
  77. {
  78. if (string.IsNullOrWhiteSpace(name))
  79. {
  80. throw new EventParameterMissingException("Event emitter name must be defined before Build() is called");
  81. }
  82. if (!type.HasValue)
  83. {
  84. throw new EventParameterMissingException("Event emitter event type must be defined before Build() is called");
  85. }
  86. SimpleEventEmitterEngine result = new SimpleEventEmitterEngine(type.Value, name);
  87. if (register)
  88. {
  89. EventManager.AddEventEmitter(result);
  90. }
  91. return result;
  92. }
  93. }
  94. }