fix wallmounted (#585)

This commit is contained in:
Ed
2024-11-15 23:54:58 +03:00
committed by GitHub
parent 5bcc378bbe
commit 7a0fb9fc15
3 changed files with 85 additions and 9 deletions

View File

@@ -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;
}

View File

@@ -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<TagPrototype>[] WallTags = {"Wall", "Window"};
@@ -17,18 +21,70 @@ public sealed class CP14WallmountSystem : EntitySystem
{
base.Initialize();
SubscribeLocalEvent<CP14WallmountComponent, MapInitEvent>(OnMapInit);
SubscribeLocalEvent<CP14WallmountedComponent, ComponentShutdown>(OnWallmountShutdown);
SubscribeLocalEvent<CP14WallmountedComponent, AnchorStateChangedEvent>(OnWallmountAnchorChanged);
}
private void OnMapInit(Entity<CP14WallmountComponent> ent, ref MapInitEvent args)
private void OnWallmountAnchorChanged(Entity<CP14WallmountedComponent> ent, ref AnchorStateChangedEvent args)
{
var grid = Transform(ent).GridUid;
if (!args.Anchored)
ClearWallmounts(ent);
}
private void OnWallmountShutdown(Entity<CP14WallmountedComponent> ent, ref ComponentShutdown args)
{
ClearWallmounts(ent);
}
public override void Update(float frameTime)
{
base.Update(frameTime);
var query = EntityQueryEnumerator<CP14WallmountComponent>();
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<CP14WallmountComponent>(uid);
}
}
}
private void ClearWallmounts(Entity<CP14WallmountedComponent> ent)
{
foreach (var attached in ent.Comp.Attached)
{
QueueDel(attached);
}
ent.Comp.Attached.Clear();
}
private bool TryAttachWallmount(Entity<CP14WallmountComponent> wallmount)
{
var grid = Transform(wallmount).GridUid;
if (grid == null || !TryComp<MapGridComponent>(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<CP14WallmountedComponent>(entt, out var wallmounted);
if (!wallmounted.Attached.Contains(wallmount))
wallmounted.Attached.Add(wallmount);
hasParent = true;
break;
}
if (!hasParent)
QueueDel(ent);
return hasParent;
}
}

View File

@@ -0,0 +1,11 @@
namespace Content.Shared._CP14.Wallmount;
/// <summary>
/// Stores a list of all entities that are “attached” to this object. Destroying this object will destroy all attached entities
/// </summary>
[RegisterComponent, Access(typeof(CP14WallmountSystem))]
public sealed partial class CP14WallmountedComponent : Component
{
[DataField]
public HashSet<EntityUid> Attached = new();
}