NoiseIndexSystem.cs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. using System.Numerics;
  2. using Content.Server.Worldgen.Components;
  3. using Content.Server.Worldgen.Prototypes;
  4. using Robust.Shared.Prototypes;
  5. using Robust.Shared.Random;
  6. namespace Content.Server.Worldgen.Systems;
  7. /// <summary>
  8. /// This handles the noise index.
  9. /// </summary>
  10. public sealed class NoiseIndexSystem : EntitySystem
  11. {
  12. [Dependency] private readonly IPrototypeManager _prototype = default!;
  13. [Dependency] private readonly IRobustRandom _random = default!;
  14. /// <summary>
  15. /// Gets a particular noise channel from the index on the given entity.
  16. /// </summary>
  17. /// <param name="holder">The holder of the index</param>
  18. /// <param name="protoId">The channel prototype ID</param>
  19. /// <returns>An initialized noise generator</returns>
  20. public NoiseGenerator Get(EntityUid holder, string protoId)
  21. {
  22. var idx = EnsureComp<NoiseIndexComponent>(holder);
  23. if (idx.Generators.TryGetValue(protoId, out var generator))
  24. return generator;
  25. var proto = _prototype.Index<NoiseChannelPrototype>(protoId);
  26. var gen = new NoiseGenerator(proto, _random.Next());
  27. idx.Generators[protoId] = gen;
  28. return gen;
  29. }
  30. /// <summary>
  31. /// Attempts to evaluate the given noise channel using the generator on the given entity.
  32. /// </summary>
  33. /// <param name="holder">The holder of the index</param>
  34. /// <param name="protoId">The channel prototype ID</param>
  35. /// <param name="coords">The coordinates to evaluate at</param>
  36. /// <returns>The result of evaluation</returns>
  37. public float Evaluate(EntityUid holder, string protoId, Vector2 coords)
  38. {
  39. var gen = Get(holder, protoId);
  40. return gen.Evaluate(coords);
  41. }
  42. }