Add ore bag area pickups (#19358)

This commit is contained in:
metalgearsloth
2023-09-12 22:34:04 +10:00
committed by GitHub
parent 7d9550bc55
commit 7064f262b4
18 changed files with 256 additions and 83 deletions

View File

@@ -0,0 +1,105 @@
using Content.Server.Storage.Components;
using Content.Shared.Hands;
using Content.Shared.Inventory;
using Content.Shared.Stacks;
using Robust.Shared.Map;
using Robust.Shared.Physics.Components;
using Robust.Shared.Player;
using Robust.Shared.Timing;
namespace Content.Shared.Storage.EntitySystems;
/// <summary>
/// <see cref="MagnetPickupComponent"/>
/// </summary>
public sealed class MagnetPickupSystem : EntitySystem
{
[Dependency] private readonly IGameTiming _timing = default!;
[Dependency] private readonly EntityLookupSystem _lookup = default!;
[Dependency] private readonly InventorySystem _inventory = default!;
[Dependency] private readonly SharedTransformSystem _transform = default!;
[Dependency] private readonly SharedStorageSystem _storage = default!;
private static readonly TimeSpan ScanDelay = TimeSpan.FromSeconds(1);
private EntityQuery<PhysicsComponent> _physicsQuery;
public override void Initialize()
{
base.Initialize();
_physicsQuery = GetEntityQuery<PhysicsComponent>();
SubscribeLocalEvent<MagnetPickupComponent, MapInitEvent>(OnMagnetMapInit);
SubscribeLocalEvent<MagnetPickupComponent, EntityUnpausedEvent>(OnMagnetUnpaused);
}
private void OnMagnetUnpaused(EntityUid uid, MagnetPickupComponent component, ref EntityUnpausedEvent args)
{
component.NextScan += args.PausedTime;
}
private void OnMagnetMapInit(EntityUid uid, MagnetPickupComponent component, MapInitEvent args)
{
component.NextScan = _timing.CurTime;
}
public override void Update(float frameTime)
{
base.Update(frameTime);
var query = EntityQueryEnumerator<MagnetPickupComponent, StorageComponent, TransformComponent>();
var currentTime = _timing.CurTime;
while (query.MoveNext(out var uid, out var comp, out var storage, out var xform))
{
if (comp.NextScan < currentTime)
continue;
comp.NextScan += ScanDelay;
// No space
if (storage.StorageUsed >= storage.StorageCapacityMax)
continue;
if (!_inventory.TryGetContainingSlot(uid, out var slotDef))
continue;
if ((slotDef.SlotFlags & comp.SlotFlags) == 0x0)
continue;
var parentUid = xform.ParentUid;
var playedSound = false;
var finalCoords = xform.Coordinates;
var moverCoords = _transform.GetMoverCoordinates(uid, xform);
foreach (var near in _lookup.GetEntitiesInRange(uid, comp.Range, LookupFlags.Dynamic | LookupFlags.Sundries))
{
if (comp.Whitelist?.IsValid(near, EntityManager) == false)
continue;
if (!_physicsQuery.TryGetComponent(near, out var physics) || physics.BodyStatus != BodyStatus.OnGround)
continue;
if (near == parentUid)
continue;
// TODO: Probably move this to storage somewhere when it gets cleaned up
// TODO: This sucks but you need to fix a lot of stuff to make it better
// the problem is that stack pickups delete the original entity, which is fine, but due to
// game state handling we can't show a lerp animation for it.
var nearXform = Transform(near);
var nearMap = nearXform.MapPosition;
var nearCoords = EntityCoordinates.FromMap(moverCoords.EntityId, nearMap, _transform, EntityManager);
if (!_storage.Insert(uid, near, out var stacked, storageComp: storage, playSound: !playedSound))
continue;
// Play pickup animation for either the stack entity or the original entity.
if (stacked != null)
_storage.PlayPickupAnimation(stacked.Value, nearCoords, finalCoords, nearXform.LocalRotation);
else
_storage.PlayPickupAnimation(near, nearCoords, finalCoords, nearXform.LocalRotation);
playedSound = true;
}
}
}
}

View File

@@ -4,6 +4,7 @@ using Content.Shared.CombatMode;
using Content.Shared.Containers.ItemSlots;
using Content.Shared.Destructible;
using Content.Shared.DoAfter;
using Content.Shared.Hands;
using Content.Shared.Hands.Components;
using Content.Shared.Hands.EntitySystems;
using Content.Shared.Implants.Components;
@@ -37,7 +38,7 @@ public abstract class SharedStorageSystem : EntitySystem
[Dependency] private readonly SharedAppearanceSystem _appearance = default!;
[Dependency] protected readonly SharedAudioSystem Audio = default!;
[Dependency] private readonly SharedCombatModeSystem _combatMode = default!;
[Dependency] private readonly SharedTransformSystem _transform = default!;
[Dependency] protected readonly SharedTransformSystem _transform = default!;
[Dependency] private readonly SharedStackSystem _stack = default!;
[Dependency] protected readonly UseDelaySystem UseDelay = default!;
@@ -435,7 +436,7 @@ public abstract class SharedStorageSystem : EntitySystem
foreach (var entity in entities.ToArray())
{
Insert(target, entity, user, targetComp, playSound: false);
Insert(target, entity, out _, user: user, targetComp, playSound: false);
}
Audio.PlayPredicted(sourceComp.StorageInsertSound, target, user);
@@ -495,8 +496,10 @@ public abstract class SharedStorageSystem : EntitySystem
/// Inserts into the storage container
/// </summary>
/// <returns>true if the entity was inserted, false otherwise</returns>
public bool Insert(EntityUid uid, EntityUid insertEnt, EntityUid? user = null, StorageComponent? storageComp = null, bool playSound = true)
public bool Insert(EntityUid uid, EntityUid insertEnt, out EntityUid? stackedEntity, EntityUid? user = null, StorageComponent? storageComp = null, bool playSound = true)
{
stackedEntity = null;
if (!Resolve(uid, ref storageComp) || !CanInsert(uid, insertEnt, out _, storageComp))
return false;
@@ -522,6 +525,7 @@ public abstract class SharedStorageSystem : EntitySystem
if (!_stack.TryAdd(insertEnt, ent, insertStack, containedStack))
continue;
stackedEntity = ent;
var remaining = insertStack.Count;
toInsertCount -= toInsertCount - remaining;
@@ -596,11 +600,17 @@ public abstract class SharedStorageSystem : EntitySystem
if (!Resolve(uid, ref storageComp) || !_sharedInteractionSystem.InRangeUnobstructed(player, uid))
return false;
if (!Insert(uid, toInsert, player, storageComp))
if (!Insert(uid, toInsert, out _, user: player, storageComp))
{
_popupSystem.PopupClient(Loc.GetString("comp-storage-cant-insert"), uid, player);
return false;
}
return true;
}
/// <summary>
/// Plays a clientside pickup animation for the specified uid.
/// </summary>
public abstract void PlayPickupAnimation(EntityUid uid, EntityCoordinates initialCoordinates,
EntityCoordinates finalCoordinates, Angle initialRotation, EntityUid? user = null);
}