Files
Delta-v/Content.Server/ReplaceOnSpawn/ReplaceOnSpawnSystem.cs
T
Vyacheslav Kovalevskyandmetalgearsloth 0c3c4d2d32 replace boring items with fun items on spawn (#26087)
* ReplaceRandom

* maybe use polymorph

* Revert "maybe use polymorph"

This reverts commit 28ecd64d122a335dd999d52a9795afc1df09c991.

* handle containers also

* better name

* chance to grow hot potato too

* replace banana peel with explosive

* fix containers

* more fun

* even more fun

* extra fun

* Update Content.Server/ReplaceOnSpawn/ReplaceOnSpawnSystem.cs

---------

Co-authored-by: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com>
2024-03-31 16:54:45 +02:00

50 lines
1.8 KiB
C#

using Content.Shared.Buckle;
using Robust.Server.Containers;
using Robust.Server.GameObjects;
using Robust.Shared.Collections;
using Robust.Shared.Random;
namespace Content.Server.ReplaceOnSpawn;
public sealed class ReplaceOnSpawn : EntitySystem
{
[Dependency] private readonly IRobustRandom _random = default!;
[Dependency] private readonly SharedBuckleSystem _buckle = default!;
[Dependency] private readonly TransformSystem _transform = default!;
[Dependency] private readonly ContainerSystem _container = default!;
public override void Update(float deltaTime)
{
// because OnMapInit is called when entity is spawned it can be put in container later which breaks stuff
// so have to do it this way instead
var toReplace = new ValueList<(EntityUid, ReplaceOnSpawnComponent)>();
var query = EntityQueryEnumerator<ReplaceOnSpawnComponent>();
while (query.MoveNext(out var uid, out var comp))
{
if (!_random.Prob(comp.Chance))
{
RemCompDeferred<ReplaceOnSpawnComponent>(uid);
continue;
}
toReplace.Add((uid, comp));
}
foreach (var (uid, comp) in toReplace)
{
// mostly stolen code from polymorph system
_buckle.TryUnbuckle(uid, uid, true);
var targetTransformComp = Transform(uid);
var child = Spawn(comp.Prototype, targetTransformComp.Coordinates);
var childXform = Transform(child);
_transform.SetLocalRotation(child, targetTransformComp.LocalRotation, childXform);
if (_container.TryGetContainingContainer(uid, out var cont))
{
_container.Remove(uid, cont, force: true);
_container.Insert(child, cont, force: true);
}
QueueDel(uid);
}
}
}