Skip to content

Commit

Permalink
Entity storage now holds air (space-wizards#11355)
Browse files Browse the repository at this point in the history
  • Loading branch information
mirrorcult authored Sep 16, 2022
1 parent edd41c0 commit 4e5def0
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 7 deletions.
13 changes: 11 additions & 2 deletions Content.Server/Storage/Components/EntityStorageComponent.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Content.Server.Atmos;
using Content.Shared.Physics;
using Content.Shared.Whitelist;
using Robust.Shared.Audio;
Expand All @@ -6,9 +7,10 @@
namespace Content.Server.Storage.Components;

[RegisterComponent]
public sealed class EntityStorageComponent : Component
public sealed class EntityStorageComponent : Component, IGasMixtureHolder
{
public readonly float MaxSize = 1.0f; // maximum width or height of an entity allowed inside the storage.
public const float GasMixVolume = 70f;

public static readonly TimeSpan InternalOpenAttemptDelay = TimeSpan.FromSeconds(0.5);
public TimeSpan LastInternalOpenAttempt;
Expand All @@ -35,7 +37,7 @@ public sealed class EntityStorageComponent : Component
[DataField("isCollidableWhenOpen")]
public bool IsCollidableWhenOpen;

//The offset for where items are emptied/vacuumed for the EntityStorage.
//The offset for where items are emptied/vacuumed for the EntityStorage.
[DataField("enteringOffset")]
public Vector2 EnteringOffset = new(0, 0);

Expand Down Expand Up @@ -77,6 +79,13 @@ public sealed class EntityStorageComponent : Component

[ViewVariables(VVAccess.ReadWrite)]
public bool IsWeldedShut;

/// <summary>
/// Gas currently contained in this entity storage.
/// None while open. Grabs gas from the atmosphere when closed, and exposes any entities inside to it.
/// </summary>
[ViewVariables(VVAccess.ReadWrite)]
public GasMixture Air { get; set; } = new (GasMixVolume);
}

public sealed class InsertIntoEntityStorageAttemptEvent : CancellableEntityEventArgs { }
Expand Down
10 changes: 10 additions & 0 deletions Content.Server/Storage/Components/InsideEntityStorageComponent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace Content.Server.Storage.Components;

/// <summary>
/// Added to entities contained within entity storage, for directed event purposes.
/// </summary>
[RegisterComponent]
public sealed class InsideEntityStorageComponent : Component
{
public EntityUid Storage;
}
96 changes: 91 additions & 5 deletions Content.Server/Storage/EntitySystems/EntityStorageSystem.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Linq;
using Content.Server.Atmos.EntitySystems;
using Content.Server.Construction;
using Content.Server.Construction.Components;
using Content.Server.Popups;
Expand Down Expand Up @@ -29,6 +30,8 @@ public sealed class EntityStorageSystem : EntitySystem
[Dependency] private readonly SharedInteractionSystem _interactionSystem = default!;
[Dependency] private readonly PlaceableSurfaceSystem _placeableSurface = default!;
[Dependency] private readonly PopupSystem _popupSystem = default!;
[Dependency] private readonly AtmosphereSystem _atmos = default!;
[Dependency] private readonly IMapManager _map = default!;

public const string ContainerName = "entity_storage";

Expand All @@ -41,6 +44,10 @@ public override void Initialize()
SubscribeLocalEvent<EntityStorageComponent, WeldableAttemptEvent>(OnWeldableAttempt);
SubscribeLocalEvent<EntityStorageComponent, WeldableChangedEvent>(OnWelded);
SubscribeLocalEvent<EntityStorageComponent, DestructionEventArgs>(OnDestroy);

SubscribeLocalEvent<InsideEntityStorageComponent, InhaleLocationEvent>(OnInsideInhale);
SubscribeLocalEvent<InsideEntityStorageComponent, ExhaleLocationEvent>(OnInsideExhale);
SubscribeLocalEvent<InsideEntityStorageComponent, AtmosExposedGetAirEvent>(OnInsideExposed);
}

private void OnInit(EntityUid uid, EntityStorageComponent component, ComponentInit args)
Expand All @@ -54,6 +61,13 @@ private void OnInit(EntityUid uid, EntityStorageComponent component, ComponentIn

if (TryComp<PlaceableSurfaceComponent>(uid, out var placeable))
_placeableSurface.SetPlaceable(uid, component.Open, placeable);

if (!component.Open)
{
// If we're closed on spawn, we need to pull some air into our environment from where we spawned,
// so that we have -something-. For example, if you bought an animal crate or something.
TakeGas(uid, component);
}
}

private void OnInteract(EntityUid uid, EntityStorageComponent component, ActivateInWorldEvent args)
Expand Down Expand Up @@ -117,11 +131,12 @@ public void EmptyContents(EntityUid uid, EntityStorageComponent? component = nul
var containedArr = component.Contents.ContainedEntities.ToArray();
foreach (var contained in containedArr)
{
if (component.Contents.Remove(contained))
{
Transform(contained).WorldPosition =
uidXform.WorldPosition + uidXform.WorldRotation.RotateVec(component.EnteringOffset);
}
if (!component.Contents.Remove(contained))
continue;

RemComp<InsideEntityStorageComponent>(contained);
Transform(contained).WorldPosition =
uidXform.WorldPosition + uidXform.WorldRotation.RotateVec(component.EnteringOffset);
}
}

Expand All @@ -134,6 +149,7 @@ public void OpenStorage(EntityUid uid, EntityStorageComponent? component = null)
EmptyContents(uid, component);
ModifyComponents(uid, component);
SoundSystem.Play(component.OpenSound.GetSound(), Filter.Pvs(component.Owner), component.Owner);
ReleaseGas(uid, component);
RaiseLocalEvent(uid, new StorageAfterOpenEvent());
}

Expand Down Expand Up @@ -161,11 +177,15 @@ public void CloseStorage(EntityUid uid, EntityStorageComponent? component = null
if (!AddToContents(entity, uid, component))
continue;

var inside = EnsureComp<InsideEntityStorageComponent>(entity);
inside.Storage = uid;

count++;
if (count >= component.Capacity)
break;
}

TakeGas(uid, component);
ModifyComponents(uid, component);
SoundSystem.Play(component.CloseSound.GetSound(), Filter.Pvs(uid), uid);
component.LastInternalOpenAttempt = default;
Expand Down Expand Up @@ -365,4 +385,70 @@ public void ModifyComponents(EntityUid uid, EntityStorageComponent? component =
appearance.SetData(StorageVisuals.HasContents, component.Contents.ContainedEntities.Any());
}
}

private void TakeGas(EntityUid uid, EntityStorageComponent component)
{
var tile = GetOffsetTileRef(uid, component);

if (tile != null && _atmos.GetTileMixture(tile.Value.GridUid, null, tile.Value.GridIndices, true) is {} environment)
{
_atmos.Merge(component.Air, environment.RemoveVolume(EntityStorageComponent.GasMixVolume));
}
}

private void ReleaseGas(EntityUid uid, EntityStorageComponent component)
{
var tile = GetOffsetTileRef(uid, component);

if (tile != null && _atmos.GetTileMixture(tile.Value.GridUid, null, tile.Value.GridIndices, true) is {} environment)
{
_atmos.Merge(environment, component.Air);
component.Air.Clear();
}
}

private TileRef? GetOffsetTileRef(EntityUid uid, EntityStorageComponent component)
{
var targetCoordinates = new EntityCoordinates(uid, component.EnteringOffset).ToMap(EntityManager);

if (_map.TryFindGridAt(targetCoordinates, out var grid))
{
return grid.GetTileRef(targetCoordinates);
}

return null;
}

#region Gas mix event handlers

private void OnInsideInhale(EntityUid uid, InsideEntityStorageComponent component, InhaleLocationEvent args)
{
if (TryComp<EntityStorageComponent>(component.Storage, out var storage))
{
args.Gas = storage.Air;
}
}

private void OnInsideExhale(EntityUid uid, InsideEntityStorageComponent component, ExhaleLocationEvent args)
{
if (TryComp<EntityStorageComponent>(component.Storage, out var storage))
{
args.Gas = storage.Air;
}
}

private void OnInsideExposed(EntityUid uid, InsideEntityStorageComponent component, ref AtmosExposedGetAirEvent args)
{
if (args.Handled)
return;

if (TryComp<EntityStorageComponent>(component.Storage, out var storage))
{
args.Gas = storage.Air;
}

args.Handled = true;
}

#endregion
}

0 comments on commit 4e5def0

Please sign in to comment.