-
Notifications
You must be signed in to change notification settings - Fork 661
Expand file tree
/
Copy pathMotionDetectorIgnoreHolderSystem.cs
More file actions
114 lines (93 loc) · 3.38 KB
/
Copy pathMotionDetectorIgnoreHolderSystem.cs
File metadata and controls
114 lines (93 loc) · 3.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
using Content.Shared._Mono.MotionDetector.Components;
using Content.Shared._Mono.Company;
using Content.Shared.Hands.Components;
using Content.Shared.ProximityDetection;
using Robust.Shared.Containers;
using Robust.Shared.Physics.Components;
using Content.Shared.Tag;
using Robust.Shared.Prototypes;
namespace Content.Shared._Mono.MotionDetector.Systems;
/// <summary>
/// Prevents motion detectors from detecting the holder.
/// </summary>
public sealed partial class MotionDetectorIgnoreHolderSystem : EntitySystem
{
[Dependency] private SharedContainerSystem _containerSystem = default!;
[Dependency] private TagSystem _tag = default!;
private static readonly ProtoId<TagPrototype> ScreenedTag = "Screened";
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<MetaDataComponent, ProximityDetectionAttemptEvent>(OnProximityDetectionAttempt);
}
private void OnProximityDetectionAttempt(EntityUid targetEntity, MetaDataComponent component, ref ProximityDetectionAttemptEvent args)
{
var detectorEntity = args.Detector.Owner;
if (!HasComp<MotionDetectorIgnoreHolderComponent>(detectorEntity))
return;
var holder = GetEntityHolder(detectorEntity);
if (holder != null && holder == targetEntity)
{
args.Cancel = true;
return;
}
if (holder != null && ShouldIgnoreCompanyMember(holder.Value, targetEntity))
{
args.Cancel = true;
return;
}
if (!IsEntityMoving(targetEntity))
{
args.Cancel = true;
}
// Prevents motion detector from detecting screened players
if (_tag.HasTag(targetEntity, ScreenedTag))
{
args.Cancel = true;
}
}
/// <summary>
/// Checks if the target entity is actually moving.
/// </summary>
private bool IsEntityMoving(EntityUid entity)
{
if (!TryComp<PhysicsComponent>(entity, out var physics))
return false;
const float minVelocityThreshold = 0.1f;
var velocity = physics.LinearVelocity;
var speed = velocity.Length();
return speed > minVelocityThreshold;
}
/// <summary>
/// Checks if the target entity should be ignored based on company affiliation.
/// </summary>
private bool ShouldIgnoreCompanyMember(EntityUid holder, EntityUid target)
{
if (!TryComp<CompanyComponent>(holder, out var holderCompany))
return false;
if (!TryComp<CompanyComponent>(target, out var targetCompany))
return false;
if (string.IsNullOrEmpty(holderCompany.CompanyName) ||
string.IsNullOrEmpty(targetCompany.CompanyName) ||
holderCompany.CompanyName == "None" ||
targetCompany.CompanyName == "None")
{
return false;
}
return holderCompany.CompanyName == targetCompany.CompanyName;
}
/// <summary>
/// Finds the holder.
/// </summary>
private EntityUid? GetEntityHolder(EntityUid item)
{
if (!_containerSystem.TryGetContainingContainer((item, null, null), out var container))
return null;
var containerOwner = container.Owner;
if (HasComp<HandsComponent>(containerOwner))
{
return containerOwner;
}
return GetEntityHolder(containerOwner);
}
}