Skip to content

Commit

Permalink
Merge pull request SerbiaStrong-220#7 from stalengd/cult-corruption-c…
Browse files Browse the repository at this point in the history
…ontainers

Cult corruption containers
  • Loading branch information
SkaldetSkaeg authored Jul 28, 2024
2 parents f611918 + f707003 commit ed6fd6e
Showing 1 changed file with 36 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
using Robust.Shared.Serialization;
using Robust.Shared.Network;
using Robust.Shared.Prototypes;
using Robust.Shared.Containers;
using Robust.Shared.Random;

namespace Content.Shared.SS220.CultYogg.EntitySystems;

Expand All @@ -24,10 +26,14 @@ public sealed class SharedCultYoggCorruptedSystem : EntitySystem
[Dependency] private readonly SharedDoAfterSystem _doAfter = default!;
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
[Dependency] private readonly SharedStackSystem _stackSystem = default!;
[Dependency] private readonly SharedContainerSystem _containerSystem = default!;
[Dependency] private readonly SharedTransformSystem _transformSystem = default!;
[Dependency] private readonly IRobustRandom _random = default!;

private readonly TimeSpan _corruptionDuration = TimeSpan.FromSeconds(3);
private readonly Dictionary<ProtoId<EntityPrototype>, CultYoggCorruptedPrototype> _recipesBySourcePrototypeId = [];
private readonly Dictionary<ProtoId<StackPrototype>, CultYoggCorruptedPrototype> _recipesBySourceStackType = [];
private readonly List<EntityUid> _dropEntitiesBuffer = [];


public override void Initialize()
Expand Down Expand Up @@ -72,6 +78,8 @@ public bool IsCorrupted(EntityUid entity)
if (recipe is null)
return null;

TryDropAllContainedEntities(corruptedEntity);

var coords = Transform(corruptedEntity).Coordinates;
var normalEntity = Spawn(corruptedEntity.Comp.OriginalPrototypeId, coords);

Expand Down Expand Up @@ -229,7 +237,7 @@ private void InitializeRecipes()

_adminLogger.Add(LogType.EntitySpawn, LogImpact.Low, $"{ToPrettyString(user)} used corrupt on {ToPrettyString(entity)} and made {ToPrettyString(corruptedEntity)}");

//ToDo if object is a storage, it should drop all its items
TryDropAllContainedEntities(entity);

EnsureComp<CultYoggCorruptedComponent>(corruptedEntity, out var corrupted);

Expand Down Expand Up @@ -265,6 +273,33 @@ private bool TryTransformStack(CultYoggCorruptedPrototype recipe, EntityUid orig
}
return false;
}

/// <summary>
/// Drops entities from all attached containers
/// </summary>
private bool TryDropAllContainedEntities(EntityUid entity)
{
if (!TryComp<ContainerManagerComponent>(entity, out var containerManager))
return false;

_dropEntitiesBuffer.Clear();
var coords = Transform(entity).Coordinates;
foreach (var container in _containerSystem.GetAllContainers(entity, containerManager))
{
foreach (var item in container.ContainedEntities)
{
_dropEntitiesBuffer.Add(item);
}
}
foreach (var item in _dropEntitiesBuffer)
{
_transformSystem.AttachToGridOrMap(item);
_transformSystem.SetCoordinates(item, coords);
_transformSystem.SetWorldRotation(item, _random.NextAngle());
}
_dropEntitiesBuffer.Clear();
return true;
}
}

/// <summary>
Expand Down

0 comments on commit ed6fd6e

Please sign in to comment.