Skip to content

Commit

Permalink
Сумки можно прятать под плитки пола. (#115)
Browse files Browse the repository at this point in the history
* HideAndSeek

* Update Resources/Prototypes/Entities/Clothing/Back/backpacks.yml

Co-authored-by: FN <[email protected]>

* Update Resources/Prototypes/Entities/Clothing/Back/backpacks.yml

Co-authored-by: FN <[email protected]>

* Fixes

* Fixes

* Fixes

* Fixes

---------

Co-authored-by: FN <[email protected]>
Co-authored-by: FN <[email protected]>
  • Loading branch information
3 people authored Dec 4, 2024
1 parent 1ea862f commit d022445
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace Content.Server._CorvaxNext.Storage;

/// <summary>
/// This is used for restricting anchor operations on storage (one bag max per tile)
/// and ejecting sapient contents on anchor.
/// </summary>
[RegisterComponent]
public sealed partial class AnchorableStorageComponent : Component;
99 changes: 99 additions & 0 deletions Content.Server/_CorvaxNext/Storage/AnchorableStorageSystem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
using System.Linq;
using Content.Server.Popups;
using Content.Shared.Construction.Components;
using Content.Shared.Mind.Components;
using Content.Shared.Storage;
using Robust.Server.GameObjects;
using Robust.Shared.Containers;
using Robust.Shared.Map.Components;

namespace Content.Server._CorvaxNext.Storage;

/// <summary>
/// This is used for restricting anchor operations on storage (one bag max per tile)
/// and ejecting living contents on anchor.
/// </summary>
public sealed class AnchorableStorageSystem : EntitySystem
{
[Dependency] private readonly MapSystem _map = default!;
[Dependency] private readonly PopupSystem _popup = default!;
[Dependency] private readonly TransformSystem _xform = default!;
[Dependency] private readonly SharedContainerSystem _container = default!;

/// <inheritdoc/>
public override void Initialize()
{
SubscribeLocalEvent<AnchorableStorageComponent, AnchorStateChangedEvent>(OnAnchorStateChanged);
SubscribeLocalEvent<AnchorableStorageComponent, AnchorAttemptEvent>(OnAnchorAttempt);
SubscribeLocalEvent<AnchorableStorageComponent, ContainerIsInsertingAttemptEvent>(OnInsertAttempt);
}

private void OnAnchorStateChanged(Entity<AnchorableStorageComponent> ent, ref AnchorStateChangedEvent args)
{
if (!args.Anchored)
return;

var transform = Transform(ent);

if (CheckOverlap(ent, transform))
{
_popup.PopupEntity(Loc.GetString("anchored-storage-already-present"), ent);
_xform.Unanchor(ent, transform);
return;
}

if (!TryComp<StorageComponent>(ent.Owner, out var storage))
return;

foreach (var item in storage.StoredItems.Keys.ToArray())
if (HasComp<MindContainerComponent>(item))
_container.RemoveEntity(ent, item);
}

private void OnAnchorAttempt(Entity<AnchorableStorageComponent> ent, ref AnchorAttemptEvent args)
{
if (args.Cancelled)
return;

if (!CheckOverlap(ent))
return;

_popup.PopupEntity(Loc.GetString("anchored-storage-already-present"), ent, args.User);
args.Cancel();
}

private void OnInsertAttempt(Entity<AnchorableStorageComponent> ent, ref ContainerIsInsertingAttemptEvent args)
{
if (args.Cancelled)
return;

if (!HasComp<MindContainerComponent>(args.EntityUid))
return;

if (Transform(ent).Anchored)
args.Cancel();
}

public bool CheckOverlap(EntityUid entity, TransformComponent? transform = null)
{
if (!Resolve(entity, ref transform))
return false;

if (transform.GridUid is not { } grid || !TryComp<MapGridComponent>(grid, out var gridComp))
return false;

var indices = _map.TileIndicesFor(grid, gridComp, transform.Coordinates);
var enumerator = _map.GetAnchoredEntitiesEnumerator(grid, gridComp, indices);

while (enumerator.MoveNext(out var otherEnt))
{
if (otherEnt == entity)
continue;

if (HasComp<AnchorableStorageComponent>(otherEnt))
return true;
}

return false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
anchored-storage-already-present = There's already a bag anchored here!
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
anchored-storage-already-present = Здесь уже что-то спрятано!
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@
ClothingHandsGlovesColorBlack: 2
ClothingHandsGlovesLatex: 2
ClothingHeadsetSecurity: 2
trayScanner: 2 # Corvax-Next-Stashes
10 changes: 10 additions & 0 deletions Resources/Prototypes/Entities/Clothing/Back/backpacks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,16 @@
delay: 0.5
- type: ExplosionResistance
damageCoefficient: 0.9
# Corvax-Next-Stashes-Start
- type: Appearance
- type: SubFloorHide
- type: Anchorable
- type: CollideOnAnchor
enable: false
- type: Transform
anchored: false
- type: AnchorableStorage
# Corvax-Next-Stashes-End

- type: entity
parent: ClothingBackpack
Expand Down

0 comments on commit d022445

Please sign in to comment.