MappingSystem.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. using Content.Client.Actions;
  2. using Content.Shared.Actions;
  3. using Content.Shared.Mapping;
  4. using Content.Shared.Maps;
  5. using Robust.Client.Placement;
  6. using Robust.Shared.Map;
  7. using Robust.Shared.Utility;
  8. using static Robust.Shared.Utility.SpriteSpecifier;
  9. namespace Content.Client.Mapping;
  10. public sealed partial class MappingSystem : EntitySystem
  11. {
  12. [Dependency] private readonly IPlacementManager _placementMan = default!;
  13. [Dependency] private readonly ITileDefinitionManager _tileMan = default!;
  14. [Dependency] private readonly MetaDataSystem _metaData = default!;
  15. /// <summary>
  16. /// The icon to use for space tiles.
  17. /// </summary>
  18. private readonly SpriteSpecifier _spaceIcon = new Texture(new ("Tiles/cropped_parallax.png"));
  19. /// <summary>
  20. /// The icon to use for entity-eraser.
  21. /// </summary>
  22. private readonly SpriteSpecifier _deleteIcon = new Texture(new ("Interface/VerbIcons/delete.svg.192dpi.png"));
  23. public override void Initialize()
  24. {
  25. base.Initialize();
  26. SubscribeLocalEvent<FillActionSlotEvent>(OnFillActionSlot);
  27. SubscribeLocalEvent<StartPlacementActionEvent>(OnStartPlacementAction);
  28. }
  29. /// <summary>
  30. /// This checks if the placement manager is currently active, and attempts to copy the placement information for
  31. /// some entity or tile into an action. This is somewhat janky, but it seem to work well enough. Though I'd
  32. /// prefer if it were to function more like DecalPlacementSystem.
  33. /// </summary>
  34. private void OnFillActionSlot(FillActionSlotEvent ev)
  35. {
  36. if (!_placementMan.IsActive)
  37. return;
  38. if (ev.Action != null)
  39. return;
  40. var actionEvent = new StartPlacementActionEvent();
  41. ITileDefinition? tileDef = null;
  42. if (_placementMan.CurrentPermission != null)
  43. {
  44. actionEvent.EntityType = _placementMan.CurrentPermission.EntityType;
  45. actionEvent.PlacementOption = _placementMan.CurrentPermission.PlacementOption;
  46. if (_placementMan.CurrentPermission.IsTile)
  47. {
  48. tileDef = _tileMan[_placementMan.CurrentPermission.TileType];
  49. actionEvent.TileId = tileDef.ID;
  50. }
  51. }
  52. else if (_placementMan.Eraser)
  53. {
  54. actionEvent.Eraser = true;
  55. }
  56. else
  57. return;
  58. InstantActionComponent action;
  59. string name;
  60. if (tileDef != null)
  61. {
  62. if (tileDef is not ContentTileDefinition contentTileDef)
  63. return;
  64. var tileIcon = contentTileDef.MapAtmosphere
  65. ? _spaceIcon
  66. : new Texture(contentTileDef.Sprite!.Value);
  67. action = new InstantActionComponent
  68. {
  69. ClientExclusive = true,
  70. CheckCanInteract = false,
  71. Event = actionEvent,
  72. Icon = tileIcon
  73. };
  74. name = Loc.GetString(tileDef.Name);
  75. }
  76. else if (actionEvent.Eraser)
  77. {
  78. action = new InstantActionComponent
  79. {
  80. ClientExclusive = true,
  81. CheckCanInteract = false,
  82. Event = actionEvent,
  83. Icon = _deleteIcon,
  84. };
  85. name = Loc.GetString("action-name-mapping-erase");
  86. }
  87. else
  88. {
  89. if (string.IsNullOrWhiteSpace(actionEvent.EntityType))
  90. return;
  91. action = new InstantActionComponent
  92. {
  93. ClientExclusive = true,
  94. CheckCanInteract = false,
  95. Event = actionEvent,
  96. Icon = new EntityPrototype(actionEvent.EntityType),
  97. };
  98. name = actionEvent.EntityType;
  99. }
  100. var actionId = Spawn(null);
  101. AddComp<Component>(actionId, action);
  102. _metaData.SetEntityName(actionId, name);
  103. ev.Action = actionId;
  104. }
  105. private void OnStartPlacementAction(StartPlacementActionEvent args)
  106. {
  107. if (args.Handled)
  108. return;
  109. args.Handled = true;
  110. _placementMan.BeginPlacing(new()
  111. {
  112. EntityType = args.EntityType,
  113. IsTile = args.TileId != null,
  114. TileType = args.TileId != null ? _tileMan[args.TileId].TileId : (ushort) 0,
  115. PlacementOption = args.PlacementOption,
  116. });
  117. if (_placementMan.Eraser != args.Eraser)
  118. _placementMan.ToggleEraser();
  119. }
  120. }