1
0

MiningSystem.cs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. using Content.Shared.Destructible;
  2. using Content.Shared.Mining;
  3. using Content.Shared.Mining.Components;
  4. using Content.Shared.Random;
  5. using Content.Shared.Random.Helpers;
  6. using Robust.Shared.Prototypes;
  7. using Robust.Shared.Random;
  8. namespace Content.Server.Mining;
  9. /// <summary>
  10. /// This handles creating ores when the entity is destroyed.
  11. /// </summary>
  12. public sealed class MiningSystem : EntitySystem
  13. {
  14. [Dependency] private readonly IPrototypeManager _proto = default!;
  15. [Dependency] private readonly IRobustRandom _random = default!;
  16. /// <inheritdoc/>
  17. public override void Initialize()
  18. {
  19. base.Initialize();
  20. SubscribeLocalEvent<OreVeinComponent, MapInitEvent>(OnMapInit);
  21. SubscribeLocalEvent<OreVeinComponent, DestructionEventArgs>(OnDestruction);
  22. }
  23. private void OnDestruction(EntityUid uid, OreVeinComponent component, DestructionEventArgs args)
  24. {
  25. if (component.CurrentOre == null)
  26. return;
  27. var proto = _proto.Index<OrePrototype>(component.CurrentOre);
  28. if (proto.OreEntity == null)
  29. return;
  30. var coords = Transform(uid).Coordinates;
  31. var toSpawn = _random.Next(proto.MinOreYield, proto.MaxOreYield+1);
  32. for (var i = 0; i < toSpawn; i++)
  33. {
  34. Spawn(proto.OreEntity, coords.Offset(_random.NextVector2(0.2f)));
  35. }
  36. }
  37. private void OnMapInit(EntityUid uid, OreVeinComponent component, MapInitEvent args)
  38. {
  39. if (component.CurrentOre != null || component.OreRarityPrototypeId == null || !_random.Prob(component.OreChance))
  40. return;
  41. component.CurrentOre = _proto.Index<WeightedRandomOrePrototype>(component.OreRarityPrototypeId).Pick(_random);
  42. }
  43. }