SharedPloughingSystem.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. using Content.Shared.Interaction;
  2. using Content.Server.DoAfter;
  3. using Content.Shared.DoAfter;
  4. using Content.Shared.Maps;
  5. using Content.Shared.Popups;
  6. using Robust.Shared.Map;
  7. using Robust.Shared.Map.Components;
  8. using Robust.Shared.Timing;
  9. using Content.Shared.Farming;
  10. namespace Content.Server.Farming
  11. {
  12. public sealed partial class SharedPloughingSystem : EntitySystem
  13. {
  14. [Dependency] private readonly SharedTransformSystem _transform = default!;
  15. [Dependency] private readonly SharedMapSystem _map = default!;
  16. [Dependency] private readonly ITileDefinitionManager _tileManager = default!;
  17. [Dependency] private readonly DoAfterSystem _doAfter = default!;
  18. [Dependency] private readonly SharedPopupSystem _popup = default!;
  19. public override void Initialize()
  20. {
  21. base.Initialize();
  22. SubscribeLocalEvent<PloughToolComponent, AfterInteractEvent>(OnAfterInteract);
  23. SubscribeLocalEvent<PloughToolComponent, PloughDoAfterEvent>(OnDoAfter);
  24. }
  25. private void OnAfterInteract(Entity<PloughToolComponent> ent, ref AfterInteractEvent args)
  26. {
  27. if (args.Handled || args.Target != null || !args.CanReach)
  28. return;
  29. var comp = ent.Comp;
  30. var user = args.User;
  31. var clickLocation = args.ClickLocation;
  32. // Grass tiles that can be turned into dirt tiles
  33. var grassTiles = new HashSet<string>
  34. {
  35. "FloorGrass",
  36. "FloorSnowGrass",
  37. "FloorGrassJungle",
  38. "FloorGrassDark",
  39. "FloorGrassLight",
  40. "FloorPlanetGrass",
  41. };
  42. var ploughableTiles = new HashSet<string> // I'd rather do it in another way, but Asphalt has a FloorDirt BaseTurf, so I cant use it
  43. {
  44. "FloorDirt",
  45. "FloorSnowDirt",
  46. "FloorDirtRock",
  47. "FloorDirtDigged_1",
  48. "FloorDirtDigged_2",
  49. "FloorDirtDigged_3"
  50. };
  51. // Get clicked grid
  52. var gridUid = _transform.GetGrid(args.ClickLocation);
  53. if (!gridUid.HasValue || !TryComp<MapGridComponent>(gridUid.Value, out var grid))
  54. {
  55. return;
  56. }
  57. // Get tile coords
  58. var snapPos = grid.TileIndicesFor(clickLocation);
  59. var tileRef = _map.GetTileRef(gridUid.Value, grid, snapPos);
  60. var tileDef = (ContentTileDefinition)_tileManager[tileRef.Tile.TypeId];
  61. PloughActionType actionType;
  62. string popupMessage;
  63. // Ploughing dirt or grass?
  64. if (ploughableTiles.Contains(tileDef.ID))
  65. {
  66. actionType = PloughActionType.Plough;
  67. popupMessage = "You begin plowing the soil.";
  68. }
  69. else if (grassTiles.Contains(tileDef.ID))
  70. {
  71. actionType = PloughActionType.ClearGrass;
  72. popupMessage = "You begin clearing the grass.";
  73. }
  74. else
  75. {
  76. return; // Cant plough this tile
  77. }
  78. var delay = comp.Delay;
  79. var netGridUid = GetNetEntity(gridUid.Value);
  80. var doAfterArgs = new DoAfterArgs(EntityManager, user, delay, new PloughDoAfterEvent(netGridUid, snapPos, actionType), ent)
  81. {
  82. BreakOnMove = true,
  83. BreakOnDamage = true,
  84. NeedHand = true
  85. };
  86. if (_doAfter.TryStartDoAfter(doAfterArgs))
  87. {
  88. _popup.PopupEntity(popupMessage, ent, user);
  89. args.Handled = true;
  90. }
  91. }
  92. private void OnDoAfter(Entity<PloughToolComponent> ent, ref PloughDoAfterEvent args)
  93. {
  94. if (args.Cancelled || args.Handled)
  95. return;
  96. var gridUid = GetEntity(args.GridUid);
  97. if (!TryComp<MapGridComponent>(gridUid, out var grid))
  98. return;
  99. var snapPos = args.SnapPos;
  100. var coordinates = grid.GridTileToLocal(snapPos);
  101. if (args.ActionType == PloughActionType.Plough)
  102. {
  103. // Create entity ploughedField
  104. var ploughedField = Spawn("ploughedField", coordinates);
  105. _popup.PopupEntity("You finish plowing the field.", ent, args.User);
  106. }
  107. else if (args.ActionType == PloughActionType.ClearGrass)
  108. {
  109. // Turns grass fields into dirt
  110. var dirtTile = _tileManager["FloorDirt"];
  111. var newTile = new Tile(dirtTile.TileId);
  112. _map.SetTile(gridUid, grid, snapPos, newTile);
  113. _popup.PopupEntity("You finish clearing the grass, turning it into dirt.", ent, args.User);
  114. }
  115. args.Handled = true;
  116. }
  117. }
  118. }