2019-11-23 16:10:05 -05:00
|
|
|
|
using Content.Server.GameObjects.Components.Stack;
|
2020-11-06 12:38:41 +01:00
|
|
|
|
using Content.Shared.Audio;
|
2020-07-18 22:51:56 -07:00
|
|
|
|
using Content.Shared.Interfaces.GameObjects.Components;
|
2019-11-23 16:10:05 -05:00
|
|
|
|
using Content.Shared.Maps;
|
2020-08-30 11:37:06 +02:00
|
|
|
|
using Content.Shared.Utility;
|
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;
|
2019-11-23 16:10:05 -05:00
|
|
|
|
using Robust.Shared.Serialization;
|
2020-11-20 16:58:06 +02:00
|
|
|
|
using System.Collections.Generic;
|
2020-12-17 10:45:04 +03:00
|
|
|
|
using System.Threading.Tasks;
|
2021-02-11 01:13:03 -08:00
|
|
|
|
using Robust.Server.GameObjects;
|
2019-11-23 16:10:05 -05:00
|
|
|
|
|
|
|
|
|
|
namespace Content.Server.GameObjects.Components.Items
|
|
|
|
|
|
{
|
|
|
|
|
|
[RegisterComponent]
|
2020-05-23 17:23:25 +02:00
|
|
|
|
public class FloorTileItemComponent : Component, IAfterInteract
|
2019-11-23 16:10:05 -05:00
|
|
|
|
{
|
2020-08-24 14:10:28 +02:00
|
|
|
|
[Dependency] private readonly ITileDefinitionManager _tileDefinitionManager = default!;
|
2019-11-23 16:10:05 -05:00
|
|
|
|
|
|
|
|
|
|
public override string Name => "FloorTile";
|
2020-11-20 16:58:06 +02:00
|
|
|
|
private List<string> _outputTiles;
|
|
|
|
|
|
|
2019-11-23 16:10:05 -05:00
|
|
|
|
|
|
|
|
|
|
public override void ExposeData(ObjectSerializer serializer)
|
|
|
|
|
|
{
|
|
|
|
|
|
base.ExposeData(serializer);
|
2020-11-20 16:58:06 +02:00
|
|
|
|
serializer.DataField(ref _outputTiles, "outputs", null);
|
2019-11-23 16:10:05 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override void Initialize()
|
|
|
|
|
|
{
|
|
|
|
|
|
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)
|
|
|
|
|
|
{
|
|
|
|
|
|
mapGrid.SetTile(location.Offset(new Vector2(offset, offset)), new Tile(tileId));
|
|
|
|
|
|
EntitySystem.Get<AudioSystem>().PlayAtCoords("/Audio/Items/genhit.ogg", location, AudioHelpers.WithVariation(0.125f));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-02-04 17:50:28 +01:00
|
|
|
|
async Task<bool> IAfterInteract.AfterInteract(AfterInteractEventArgs eventArgs)
|
2019-11-23 16:10:05 -05:00
|
|
|
|
{
|
2021-02-03 14:05:31 +01:00
|
|
|
|
if (!eventArgs.InRangeUnobstructed(ignoreInsideBlocker: true, popup: true))
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
|
|
if (!Owner.TryGetComponent(out StackComponent stack))
|
|
|
|
|
|
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();
|
|
|
|
|
|
var locationMap = location.ToMap(Owner.EntityManager);
|
2020-11-20 16:58:06 +02:00
|
|
|
|
mapManager.TryGetGrid(location.GetGridId(Owner.EntityManager), out var mapGrid);
|
|
|
|
|
|
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];
|
|
|
|
|
|
|
|
|
|
|
|
if (HasBaseTurf(currentTileDefinition, baseTurf.Name) && stack.Use(1))
|
|
|
|
|
|
{
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|