From 7a0fb9fc1519c7c57ed9209af90a698709b6eb0b Mon Sep 17 00:00:00 2001 From: Ed <96445749+TheShuEd@users.noreply.github.com> Date: Fri, 15 Nov 2024 23:54:58 +0300 Subject: [PATCH] fix wallmounted (#585) --- .../_CP14/Wallmount/CP14WallmountComponent.cs | 5 ++ .../_CP14/Wallmount/CP14WallmountSystem.cs | 78 ++++++++++++++++--- .../Wallmount/CP14WallmountedComponent.cs | 11 +++ 3 files changed, 85 insertions(+), 9 deletions(-) create mode 100644 Content.Shared/_CP14/Wallmount/CP14WallmountedComponent.cs diff --git a/Content.Shared/_CP14/Wallmount/CP14WallmountComponent.cs b/Content.Shared/_CP14/Wallmount/CP14WallmountComponent.cs index ce13734d1c..ff42f57c56 100644 --- a/Content.Shared/_CP14/Wallmount/CP14WallmountComponent.cs +++ b/Content.Shared/_CP14/Wallmount/CP14WallmountComponent.cs @@ -6,4 +6,9 @@ namespace Content.Shared._CP14.Wallmount; [RegisterComponent, Access(typeof(CP14WallmountSystem))] public sealed partial class CP14WallmountComponent : Component { + [DataField] + public int AttachAttempts = 3; + + [DataField] + public TimeSpan NextAttachTime = TimeSpan.Zero; } diff --git a/Content.Shared/_CP14/Wallmount/CP14WallmountSystem.cs b/Content.Shared/_CP14/Wallmount/CP14WallmountSystem.cs index f440b6c062..d8f8941b92 100644 --- a/Content.Shared/_CP14/Wallmount/CP14WallmountSystem.cs +++ b/Content.Shared/_CP14/Wallmount/CP14WallmountSystem.cs @@ -1,7 +1,9 @@ using Content.Shared.Tag; using Robust.Shared.Map; using Robust.Shared.Map.Components; +using Robust.Shared.Network; using Robust.Shared.Prototypes; +using Robust.Shared.Timing; namespace Content.Shared._CP14.Wallmount; @@ -10,6 +12,8 @@ public sealed class CP14WallmountSystem : EntitySystem [Dependency] private readonly SharedTransformSystem _transform = default!; [Dependency] private readonly SharedMapSystem _map = default!; [Dependency] private readonly TagSystem _tag = default!; + [Dependency] private readonly IGameTiming _timing = default!; + [Dependency] private readonly INetManager _net = default!; public static readonly ProtoId[] WallTags = {"Wall", "Window"}; @@ -17,18 +21,70 @@ public sealed class CP14WallmountSystem : EntitySystem { base.Initialize(); - SubscribeLocalEvent(OnMapInit); + SubscribeLocalEvent(OnWallmountShutdown); + SubscribeLocalEvent(OnWallmountAnchorChanged); } - private void OnMapInit(Entity ent, ref MapInitEvent args) + private void OnWallmountAnchorChanged(Entity ent, ref AnchorStateChangedEvent args) { - var grid = Transform(ent).GridUid; + if (!args.Anchored) + ClearWallmounts(ent); + } + + private void OnWallmountShutdown(Entity ent, ref ComponentShutdown args) + { + ClearWallmounts(ent); + } + + public override void Update(float frameTime) + { + base.Update(frameTime); + + var query = EntityQueryEnumerator(); + while (query.MoveNext(out var uid, out var wallmount)) + { + if (!wallmount.Initialized) + continue; + + if (_timing.CurTime < wallmount.NextAttachTime) + continue; + + if (wallmount.AttachAttempts <= 0) + { + if (_net.IsServer) + QueueDel(uid); + continue; + } + + wallmount.NextAttachTime = _timing.CurTime + TimeSpan.FromSeconds(0.5f); + wallmount.AttachAttempts--; + + if (TryAttachWallmount((uid, wallmount))) + { + RemComp(uid); + } + } + } + + private void ClearWallmounts(Entity ent) + { + foreach (var attached in ent.Comp.Attached) + { + QueueDel(attached); + } + + ent.Comp.Attached.Clear(); + } + + private bool TryAttachWallmount(Entity wallmount) + { + var grid = Transform(wallmount).GridUid; if (grid == null || !TryComp(grid, out var gridComp)) - return; + return false; //Try found a wall in neighbour tile - var offset = Transform(ent).LocalRotation.ToWorldVec(); - var targetPos = new EntityCoordinates(grid.Value,Transform(ent).LocalPosition - offset); + var offset = Transform(wallmount).LocalRotation.ToWorldVec().Normalized(); + var targetPos = new EntityCoordinates(grid.Value, Transform(wallmount).LocalPosition - offset); var anchored = _map.GetAnchoredEntities(grid.Value, gridComp, targetPos); bool hasParent = false; @@ -37,11 +93,15 @@ public sealed class CP14WallmountSystem : EntitySystem if (!_tag.HasAnyTag(entt, WallTags)) continue; - _transform.SetParent(ent, entt); + EnsureComp(entt, out var wallmounted); + + if (!wallmounted.Attached.Contains(wallmount)) + wallmounted.Attached.Add(wallmount); + hasParent = true; break; } - if (!hasParent) - QueueDel(ent); + + return hasParent; } } diff --git a/Content.Shared/_CP14/Wallmount/CP14WallmountedComponent.cs b/Content.Shared/_CP14/Wallmount/CP14WallmountedComponent.cs new file mode 100644 index 0000000000..05a00495b3 --- /dev/null +++ b/Content.Shared/_CP14/Wallmount/CP14WallmountedComponent.cs @@ -0,0 +1,11 @@ +namespace Content.Shared._CP14.Wallmount; + +/// +/// Stores a list of all entities that are “attached” to this object. Destroying this object will destroy all attached entities +/// +[RegisterComponent, Access(typeof(CP14WallmountSystem))] +public sealed partial class CP14WallmountedComponent : Component +{ + [DataField] + public HashSet Attached = new(); +}