TimedSpawnerComponent.cs 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. using System.Threading;
  2. using Robust.Shared.Prototypes;
  3. using Robust.Shared.Serialization;
  4. namespace Content.Server.Spawners.Components;
  5. /// <summary>
  6. /// Spawns entities at a set interval.
  7. /// Can configure the set of entities, spawn timing, spawn chance,
  8. /// and min/max number of entities to spawn.
  9. /// </summary>
  10. [RegisterComponent, EntityCategory("Spawner")]
  11. public sealed partial class TimedSpawnerComponent : Component, ISerializationHooks
  12. {
  13. /// <summary>
  14. /// List of entities that can be spawned by this component. One will be randomly
  15. /// chosen for each entity spawned. When multiple entities are spawned at once,
  16. /// each will be randomly chosen separately.
  17. /// </summary>
  18. [DataField]
  19. public List<EntProtoId> Prototypes = [];
  20. /// <summary>
  21. /// Chance of an entity being spawned at the end of each interval.
  22. /// </summary>
  23. [DataField]
  24. public float Chance = 1.0f;
  25. /// <summary>
  26. /// Length of the interval between spawn attempts.
  27. /// </summary>
  28. [DataField]
  29. public int IntervalSeconds = 60;
  30. /// <summary>
  31. /// The minimum number of entities that can be spawned when an interval elapses.
  32. /// </summary>
  33. [DataField]
  34. public int MinimumEntitiesSpawned = 1;
  35. /// <summary>
  36. /// The maximum number of entities that can be spawned when an interval elapses.
  37. /// </summary>
  38. [DataField]
  39. public int MaximumEntitiesSpawned = 1;
  40. public CancellationTokenSource? TokenSource;
  41. void ISerializationHooks.AfterDeserialization()
  42. {
  43. if (MinimumEntitiesSpawned > MaximumEntitiesSpawned)
  44. throw new ArgumentException("MaximumEntitiesSpawned can't be lower than MinimumEntitiesSpawned!");
  45. }
  46. }