2021-03-16 15:50:20 +01:00
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Threading.Tasks;
|
2021-06-09 22:19:39 +02:00
|
|
|
using Content.Server.Stack;
|
2020-11-06 12:38:41 +01:00
|
|
|
using Content.Shared.Audio;
|
2021-06-09 22:19:39 +02:00
|
|
|
using Content.Shared.Interaction;
|
|
|
|
|
using Content.Shared.Interaction.Helpers;
|
2019-11-23 16:10:05 -05:00
|
|
|
using Content.Shared.Maps;
|
2021-07-10 17:35:33 +02:00
|
|
|
using Content.Shared.Sound;
|
2021-06-09 22:19:39 +02:00
|
|
|
using Robust.Shared.Audio;
|
2019-11-23 16:10:05 -05:00
|
|
|
using Robust.Shared.GameObjects;
|
|
|
|
|
using Robust.Shared.IoC;
|
|
|
|
|
using Robust.Shared.Map;
|
2020-11-06 12:38:41 +01:00
|
|
|
using Robust.Shared.Maths;
|
2021-03-21 09:12:03 -07:00
|
|
|
using Robust.Shared.Player;
|
2022-03-09 13:59:44 -06:00
|
|
|
using Robust.Shared.Random;
|
2021-03-05 01:08:38 +01:00
|
|
|
using Robust.Shared.Serialization.Manager.Attributes;
|
2021-05-27 11:39:30 +02:00
|
|
|
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype.List;
|
2019-11-23 16:10:05 -05:00
|
|
|
|
2021-06-09 22:19:39 +02:00
|
|
|
namespace Content.Server.Tiles
|
2019-11-23 16:10:05 -05:00
|
|
|
{
|
|
|
|
|
[RegisterComponent]
|
2022-02-01 19:35:40 -08:00
|
|
|
[ComponentProtoName("FloorTile")]
|
2022-02-16 00:23:23 -07:00
|
|
|
public sealed class FloorTileItemComponent : Component, IAfterInteract
|
2019-11-23 16:10:05 -05:00
|
|
|
{
|
2021-12-08 17:32:32 +01:00
|
|
|
[Dependency] private readonly IEntityManager _entMan = default!;
|
2020-08-24 14:10:28 +02:00
|
|
|
[Dependency] private readonly ITileDefinitionManager _tileDefinitionManager = default!;
|
2022-03-09 13:59:44 -06:00
|
|
|
[Dependency] private readonly IRobustRandom _random = default!;
|
2019-11-23 16:10:05 -05:00
|
|
|
|
2021-07-31 19:52:33 +02:00
|
|
|
[DataField("outputs", customTypeSerializer: typeof(PrototypeIdListSerializer<ContentTileDefinition>))]
|
2021-03-16 15:50:20 +01:00
|
|
|
private List<string>? _outputTiles;
|
2020-11-20 16:58:06 +02:00
|
|
|
|
2021-07-10 17:35:33 +02:00
|
|
|
[DataField("placeTileSound")] SoundSpecifier _placeTileSound = new SoundPathSpecifier("/Audio/Items/genhit.ogg");
|
|
|
|
|
|
2021-06-19 19:41:26 -07:00
|
|
|
protected override void Initialize()
|
2019-11-23 16:10:05 -05:00
|
|
|
{
|
|
|
|
|
base.Initialize();
|
2020-08-22 22:29:20 +02:00
|
|
|
Owner.EnsureComponent<StackComponent>();
|
2019-11-23 16:10:05 -05:00
|
|
|
}
|
2020-08-22 22:29:20 +02:00
|
|
|
|
2020-11-06 12:38:41 +01:00
|
|
|
private bool HasBaseTurf(ContentTileDefinition tileDef, string baseTurf)
|
|
|
|
|
{
|
|
|
|
|
foreach (var tileBaseTurf in tileDef.BaseTurfs)
|
|
|
|
|
{
|
|
|
|
|
if (baseTurf == tileBaseTurf)
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void PlaceAt(IMapGrid mapGrid, EntityCoordinates location, ushort tileId, float offset = 0)
|
|
|
|
|
{
|
2022-03-09 13:59:44 -06:00
|
|
|
var variant = _random.Pick(((ContentTileDefinition) _tileDefinitionManager[tileId]).PlacementVariants);
|
|
|
|
|
mapGrid.SetTile(location.Offset(new Vector2(offset, offset)), new Tile(tileId, 0, variant));
|
2021-07-31 19:52:33 +02:00
|
|
|
SoundSystem.Play(Filter.Pvs(location), _placeTileSound.GetSound(), location, AudioHelpers.WithVariation(0.125f));
|
2020-11-06 12:38:41 +01:00
|
|
|
}
|
|
|
|
|
|
2021-02-04 17:50:28 +01:00
|
|
|
async Task<bool> IAfterInteract.AfterInteract(AfterInteractEventArgs eventArgs)
|
2019-11-23 16:10:05 -05:00
|
|
|
{
|
2022-02-05 15:39:01 +13:00
|
|
|
if (!eventArgs.CanReach)
|
2021-02-03 14:05:31 +01:00
|
|
|
return true;
|
|
|
|
|
|
2021-12-08 17:32:32 +01:00
|
|
|
if (!_entMan.TryGetComponent(Owner, out StackComponent? stack))
|
2021-02-03 14:05:31 +01:00
|
|
|
return true;
|
|
|
|
|
|
2020-11-20 16:58:06 +02:00
|
|
|
var mapManager = IoCManager.Resolve<IMapManager>();
|
2020-05-23 02:27:31 -07:00
|
|
|
|
2020-11-06 12:38:41 +01:00
|
|
|
var location = eventArgs.ClickLocation.AlignWithClosestGridTile();
|
2021-12-08 17:32:32 +01:00
|
|
|
var locationMap = location.ToMap(_entMan);
|
2021-03-06 09:43:32 -08:00
|
|
|
if (locationMap.MapId == MapId.Nullspace)
|
|
|
|
|
return true;
|
2021-12-08 17:32:32 +01:00
|
|
|
mapManager.TryGetGrid(location.GetGridId(_entMan), out var mapGrid);
|
2021-03-16 15:50:20 +01:00
|
|
|
|
|
|
|
|
if (_outputTiles == null)
|
|
|
|
|
return true;
|
|
|
|
|
|
2020-11-20 16:58:06 +02:00
|
|
|
foreach (var currentTile in _outputTiles)
|
2019-11-23 16:10:05 -05:00
|
|
|
{
|
2020-11-20 16:58:06 +02:00
|
|
|
var currentTileDefinition = (ContentTileDefinition) _tileDefinitionManager[currentTile];
|
2019-11-23 16:10:05 -05:00
|
|
|
|
2020-11-20 16:58:06 +02:00
|
|
|
if (mapGrid != null)
|
2020-11-06 12:38:41 +01:00
|
|
|
{
|
2020-11-20 16:58:06 +02:00
|
|
|
var tile = mapGrid.GetTileRef(location);
|
|
|
|
|
var baseTurf = (ContentTileDefinition) _tileDefinitionManager[tile.Tile.TypeId];
|
|
|
|
|
|
2022-01-04 16:26:07 -08:00
|
|
|
if (HasBaseTurf(currentTileDefinition, baseTurf.ID))
|
2020-11-20 16:58:06 +02:00
|
|
|
{
|
2021-12-03 15:53:09 +01:00
|
|
|
if (!EntitySystem.Get<StackSystem>().Use(Owner, 1, stack))
|
2021-05-26 10:20:57 +02:00
|
|
|
continue;
|
|
|
|
|
|
2020-11-20 16:58:06 +02:00
|
|
|
PlaceAt(mapGrid, location, currentTileDefinition.TileId);
|
|
|
|
|
break;
|
|
|
|
|
}
|
2020-11-06 12:38:41 +01:00
|
|
|
}
|
2020-11-20 16:58:06 +02:00
|
|
|
else if (HasBaseTurf(currentTileDefinition, "space"))
|
|
|
|
|
{
|
|
|
|
|
mapGrid = mapManager.CreateGrid(locationMap.MapId);
|
|
|
|
|
mapGrid.WorldPosition = locationMap.Position;
|
|
|
|
|
location = new EntityCoordinates(mapGrid.GridEntityId, Vector2.Zero);
|
|
|
|
|
PlaceAt(mapGrid, location, _tileDefinitionManager[_outputTiles[0]].TileId, mapGrid.TileSize / 2f);
|
|
|
|
|
break;
|
|
|
|
|
}
|
2020-11-06 12:38:41 +01:00
|
|
|
}
|
2020-05-23 02:27:31 -07:00
|
|
|
|
2021-02-03 14:05:31 +01:00
|
|
|
return true;
|
2019-11-23 16:10:05 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|