From 445419b7c673b1d8212827285b477b6e102d8900 Mon Sep 17 00:00:00 2001 From: FN Date: Tue, 30 Apr 2024 01:07:37 +0700 Subject: [PATCH] Fix error on debris despawn --- .../Worldgen/Systems/LocalityLoaderSystem.cs | 36 +++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/Content.Server/Worldgen/Systems/LocalityLoaderSystem.cs b/Content.Server/Worldgen/Systems/LocalityLoaderSystem.cs index 4dbf23ba65d..1aa3a03b6fb 100644 --- a/Content.Server/Worldgen/Systems/LocalityLoaderSystem.cs +++ b/Content.Server/Worldgen/Systems/LocalityLoaderSystem.cs @@ -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; /// @@ -14,12 +17,20 @@ public sealed class LocalityLoaderSystem : BaseWorldSystem { [Dependency] private readonly TransformSystem _xformSys = default!; + private EntityQuery _debrisQuery; + + private readonly List<(EntityUid Debris, List 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(); + SubscribeLocalEvent(OnDebrisDespawn); + + EntityManager.EntityDeleted += OnEntityDeleted; } /// @@ -88,9 +99,30 @@ private void OnDebrisDespawn(EntityUid entity, SpaceDebrisComponent component, E { var mobQuery = AllEntityQuery(); + List 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 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); } }