Floor tile ECS (#8577)

This commit is contained in:
Rane
2022-06-03 06:08:09 -04:00
committed by GitHub
parent 299645eb28
commit 40d6f690dd
3 changed files with 111 additions and 104 deletions

View File

@@ -0,0 +1,20 @@
using Content.Shared.Maps;
using Content.Shared.Sound;
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype.List;
namespace Content.Server.Tiles
{
/// <summary>
/// This gives items floor tile behavior, but it doesn't have to be a literal floor tile.
/// A lot of materials use this too. Note that the AfterInteract will fail without a stack component on the item.
/// </summary>
[RegisterComponent]
public sealed class FloorTileComponent : Component
{
[DataField("outputs", customTypeSerializer: typeof(PrototypeIdListSerializer<ContentTileDefinition>))]
public List<string>? OutputTiles;
[DataField("placeTileSound")]
public SoundSpecifier PlaceTileSound = new SoundPathSpecifier("/Audio/Items/genhit.ogg");
}
}

View File

@@ -1,104 +0,0 @@
using System.Threading.Tasks;
using Content.Server.Stack;
using Content.Shared.Audio;
using Content.Shared.Interaction;
using Content.Shared.Maps;
using Content.Shared.Sound;
using Robust.Shared.Audio;
using Robust.Shared.Map;
using Robust.Shared.Player;
using Robust.Shared.Random;
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype.List;
namespace Content.Server.Tiles
{
[RegisterComponent]
[ComponentProtoName("FloorTile")]
public sealed class FloorTileItemComponent : Component, IAfterInteract
{
[Dependency] private readonly IEntityManager _entMan = default!;
[Dependency] private readonly ITileDefinitionManager _tileDefinitionManager = default!;
[Dependency] private readonly IRobustRandom _random = default!;
[DataField("outputs", customTypeSerializer: typeof(PrototypeIdListSerializer<ContentTileDefinition>))]
private List<string>? _outputTiles;
[DataField("placeTileSound")] SoundSpecifier _placeTileSound = new SoundPathSpecifier("/Audio/Items/genhit.ogg");
protected override void Initialize()
{
base.Initialize();
Owner.EnsureComponent<StackComponent>();
}
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)
{
var variant = _random.Pick(((ContentTileDefinition) _tileDefinitionManager[tileId]).PlacementVariants);
mapGrid.SetTile(location.Offset(new Vector2(offset, offset)), new Tile(tileId, 0, variant));
SoundSystem.Play(Filter.Pvs(location), _placeTileSound.GetSound(), location, AudioHelpers.WithVariation(0.125f));
}
async Task<bool> IAfterInteract.AfterInteract(AfterInteractEventArgs eventArgs)
{
if (!eventArgs.CanReach)
return true;
if (!_entMan.TryGetComponent(Owner, out StackComponent? stack))
return true;
var mapManager = IoCManager.Resolve<IMapManager>();
var location = eventArgs.ClickLocation.AlignWithClosestGridTile();
var locationMap = location.ToMap(_entMan);
if (locationMap.MapId == MapId.Nullspace)
return true;
mapManager.TryGetGrid(location.GetGridId(_entMan), out var mapGrid);
if (_outputTiles == null)
return true;
foreach (var currentTile in _outputTiles)
{
var currentTileDefinition = (ContentTileDefinition) _tileDefinitionManager[currentTile];
if (mapGrid != null)
{
var tile = mapGrid.GetTileRef(location);
var baseTurf = (ContentTileDefinition) _tileDefinitionManager[tile.Tile.TypeId];
if (HasBaseTurf(currentTileDefinition, baseTurf.ID))
{
if (!EntitySystem.Get<StackSystem>().Use(Owner, 1, stack))
continue;
PlaceAt(mapGrid, location, currentTileDefinition.TileId);
break;
}
}
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;
}
}
return true;
}
}
}

View File

@@ -0,0 +1,91 @@
using Content.Server.Stack;
using Content.Shared.Audio;
using Content.Shared.Interaction;
using Content.Shared.Maps;
using Content.Shared.Sound;
using Robust.Shared.Audio;
using Robust.Shared.Map;
using Robust.Shared.Player;
using Robust.Shared.Random;
namespace Content.Server.Tiles
{
public sealed class FloorTileSystem : EntitySystem
{
[Dependency] private readonly ITileDefinitionManager _tileDefinitionManager = default!;
[Dependency] private readonly IMapManager _mapManager = default!;
[Dependency] private readonly IRobustRandom _random = default!;
[Dependency] private readonly StackSystem _stackSystem = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<FloorTileComponent, AfterInteractEvent>(OnAfterInteract);
}
private void OnAfterInteract(EntityUid uid, FloorTileComponent component, AfterInteractEvent args)
{
if (!args.CanReach)
return;
if (!TryComp<StackComponent>(uid, out var stack))
return;
if (component.OutputTiles == null)
return;
// this looks a bit sussy but it might be because it needs to be able to place off of grids and expand them
var location = args.ClickLocation.AlignWithClosestGridTile();
var locationMap = location.ToMap(EntityManager);
if (locationMap.MapId == MapId.Nullspace)
return;
_mapManager.TryGetGrid(location.GetGridId(EntityManager), out var mapGrid);
foreach (var currentTile in component.OutputTiles)
{
var currentTileDefinition = (ContentTileDefinition) _tileDefinitionManager[currentTile];
if (mapGrid != null)
{
var tile = mapGrid.GetTileRef(location);
var baseTurf = (ContentTileDefinition) _tileDefinitionManager[tile.Tile.TypeId];
if (HasBaseTurf(currentTileDefinition, baseTurf.ID))
{
if (!_stackSystem.Use(uid, 1, stack))
continue;
PlaceAt(mapGrid, location, currentTileDefinition.TileId, component.PlaceTileSound);
return;
}
}
if (HasBaseTurf(currentTileDefinition, "space"))
{
mapGrid = _mapManager.CreateGrid(locationMap.MapId);
mapGrid.WorldPosition = locationMap.Position;
location = new EntityCoordinates(mapGrid.GridEntityId, Vector2.Zero);
PlaceAt(mapGrid, location, _tileDefinitionManager[component.OutputTiles[0]].TileId, component.PlaceTileSound, mapGrid.TileSize / 2f);
return;
}
}
}
public 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, SoundSpecifier placeSound, float offset = 0)
{
var variant = _random.Pick(((ContentTileDefinition) _tileDefinitionManager[tileId]).PlacementVariants);
mapGrid.SetTile(location.Offset(new Vector2(offset, offset)), new Tile(tileId, 0, variant));
SoundSystem.Play(Filter.Pvs(location), placeSound.GetSound(), location, AudioHelpers.WithVariation(0.125f, _random));
}
}
}