Skip to content
This repository has been archived by the owner on Nov 1, 2024. It is now read-only.

Fix error on debris despawn #92

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 34 additions & 2 deletions Content.Server/Worldgen/Systems/LocalityLoaderSystem.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
using Content.Server.Worldgen.Components;
using System.Linq;
using Content.Server.Worldgen.Components;
using Content.Server.Worldgen.Components.Debris;
using Content.Shared.Humanoid;
using Content.Shared.Mobs.Components;
using Robust.Server.GameObjects;
using Robust.Shared.Spawners;

using EntityPosition = (Robust.Shared.GameObjects.EntityUid Entity, Robust.Shared.Map.EntityCoordinates Coordinates);

namespace Content.Server.Worldgen.Systems;

/// <summary>
Expand All @@ -14,12 +17,20 @@ public sealed class LocalityLoaderSystem : BaseWorldSystem
{
[Dependency] private readonly TransformSystem _xformSys = default!;

private EntityQuery<SpaceDebrisComponent> _debrisQuery;

private readonly List<(EntityUid Debris, List<EntityPosition> Entity)> _terminatingDebris = [];

// Duration to reset the despawn timer to when a debris is loaded into a player's view.
private const float DebrisActiveDuration = 300; // 5 минут Corvax.

public override void Initialize()
{
_debrisQuery = GetEntityQuery<SpaceDebrisComponent>();

SubscribeLocalEvent<SpaceDebrisComponent, EntityTerminatingEvent>(OnDebrisDespawn);

EntityManager.EntityDeleted += OnEntityDeleted;
}

/// <inheritdoc />
Expand Down Expand Up @@ -88,9 +99,30 @@ private void OnDebrisDespawn(EntityUid entity, SpaceDebrisComponent component, E
{
var mobQuery = AllEntityQuery<HumanoidAppearanceComponent, MobStateComponent, TransformComponent>();

List<EntityPosition> positions = [];

while (mobQuery.MoveNext(out var mob, out _, out _, out var xform))
if (xform.MapUid is not null && xform.GridUid == entity)
_xformSys.SetCoordinates(mob, new(xform.MapUid.Value, _xformSys.GetWorldPosition(xform)));
{
positions.Add((mob, new(xform.MapUid.Value, _xformSys.GetWorldPosition(xform))));

_xformSys.DetachParentToNull(mob, xform);
}

_terminatingDebris.Add((entity, positions));
}

private void OnEntityDeleted(Entity<MetaDataComponent> entity)
{
var debris = _terminatingDebris.FirstOrDefault(debris => debris.Debris == entity.Owner);

if (debris.Debris == EntityUid.Invalid)
return;

foreach (var position in debris.Entity)
_xformSys.SetCoordinates(position.Entity, position.Coordinates);

_terminatingDebris.Remove(debris);
}
}

Expand Down
Loading