SpiderSystem.cs 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. using System.Linq;
  2. using Content.Server.Popups;
  3. using Content.Shared.Spider;
  4. using Content.Shared.Maps;
  5. using Robust.Server.GameObjects;
  6. using Robust.Shared.Map;
  7. namespace Content.Server.Spider;
  8. public sealed class SpiderSystem : SharedSpiderSystem
  9. {
  10. [Dependency] private readonly PopupSystem _popup = default!;
  11. public override void Initialize()
  12. {
  13. base.Initialize();
  14. SubscribeLocalEvent<SpiderComponent, SpiderWebActionEvent>(OnSpawnNet);
  15. }
  16. private void OnSpawnNet(EntityUid uid, SpiderComponent component, SpiderWebActionEvent args)
  17. {
  18. if (args.Handled)
  19. return;
  20. var transform = Transform(uid);
  21. if (transform.GridUid == null)
  22. {
  23. _popup.PopupEntity(Loc.GetString("spider-web-action-nogrid"), args.Performer, args.Performer);
  24. return;
  25. }
  26. var coords = transform.Coordinates;
  27. // TODO generic way to get certain coordinates
  28. var result = false;
  29. // Spawn web in center
  30. if (!IsTileBlockedByWeb(coords))
  31. {
  32. Spawn(component.WebPrototype, coords);
  33. result = true;
  34. }
  35. // Spawn web in other directions
  36. for (var i = 0; i < 4; i++)
  37. {
  38. var direction = (DirectionFlag) (1 << i);
  39. coords = transform.Coordinates.Offset(direction.AsDir().ToVec());
  40. if (!IsTileBlockedByWeb(coords))
  41. {
  42. Spawn(component.WebPrototype, coords);
  43. result = true;
  44. }
  45. }
  46. if (result)
  47. {
  48. _popup.PopupEntity(Loc.GetString("spider-web-action-success"), args.Performer, args.Performer);
  49. args.Handled = true;
  50. }
  51. else
  52. _popup.PopupEntity(Loc.GetString("spider-web-action-fail"), args.Performer, args.Performer);
  53. }
  54. private bool IsTileBlockedByWeb(EntityCoordinates coords)
  55. {
  56. foreach (var entity in coords.GetEntitiesInTile())
  57. {
  58. if (HasComp<SpiderWebObjectComponent>(entity))
  59. return true;
  60. }
  61. return false;
  62. }
  63. }