Client LuaCsForBarotrauma
CheckVisibilityAction.cs
1 #nullable enable
2 using Microsoft.Xna.Framework;
3 using System.Linq;
4 
5 namespace Barotrauma
6 {
11  {
12  [Serialize("", IsPropertySaveable.Yes, description: "Tag of the entity to do the visibility check from.")]
13  public Identifier EntityTag { get; set; }
14 
15  [Serialize("", IsPropertySaveable.Yes, description: "Entities that also have this tag are excluded.")]
16  public Identifier ExcludedEntityTag { get; set; }
17 
18  [Serialize("", IsPropertySaveable.Yes, description: "Tag of the entity to do the visibility check to.")]
19  public Identifier TargetTag { get; set; }
20 
21  [Serialize(false, IsPropertySaveable.Yes, description: "Does the entity need to be facing the target? Only valid if the entity is a character.")]
22  public bool CheckFacing { get; set; }
23 
24  [Serialize(1000.0f, IsPropertySaveable.Yes, description: "Maximum distance between the targets.")]
25  public float MaxDistance { get; set; }
26 
27  [Serialize("", IsPropertySaveable.Yes, description: "Tag to apply to the entity who saw the target when the check succeeds.")]
28  public Identifier ApplyTagToEntity { get; set; }
29 
30  [Serialize("", IsPropertySaveable.Yes, description: "Tag to apply to the entity that was seen when the check succeeds.")]
31  public Identifier ApplyTagToTarget { get; set; }
32 
33  [Serialize(true, IsPropertySaveable.Yes, description: "If both the seeing entity and the target are the same, does it count as success?")]
34  public bool AllowSameEntity { get; set; }
35 
36  public CheckVisibilityAction(ScriptedEvent parentEvent, ContentXElement element) : base(parentEvent, element)
37  {
38  }
39 
40  protected override bool? DetermineSuccess()
41  {
42  foreach (var entity in ParentEvent.GetTargets(EntityTag))
43  {
44  if (!ExcludedEntityTag.IsEmpty)
45  {
46  if (ParentEvent.GetTargets(ExcludedEntityTag).Contains(entity)) { continue; }
47  }
48 
49  foreach (var target in ParentEvent.GetTargets(TargetTag))
50  {
51  if (!AllowSameEntity && entity == target) { continue; }
52  if (Vector2.DistanceSquared(target.WorldPosition, entity.WorldPosition) > MaxDistance * MaxDistance) { continue; }
53  if (Character.IsTargetVisible(target, entity, seeThroughWindows: true, CheckFacing))
54  {
55  if (!ApplyTagToEntity.IsEmpty)
56  {
58  }
59  if (!ApplyTagToTarget.IsEmpty)
60  {
62  }
63  return true;
64  }
65  }
66  }
67 
68  return false;
69  }
70 
71  public override string ToDebugString()
72  {
73  return $"{ToolBox.GetDebugSymbol(HasBeenDetermined())} {nameof(CheckVisibilityAction)} -> (TargetTags: {EntityTag.ColorizeObject()}, {TargetTag.ColorizeObject()})";
74  }
75  }
76 }
static bool IsTargetVisible(ISpatialEntity target, ISpatialEntity seeingEntity, bool seeThroughWindows=false, bool checkFacing=false)
Check whether a specific entity is visible from the perspective of another entity.
override string ToDebugString()
Rich test to display in debugdraw
CheckVisibilityAction(ScriptedEvent parentEvent, ContentXElement element)
readonly ScriptedEvent ParentEvent
Definition: EventAction.cs:106
void AddTarget(Identifier tag, Entity target)
IEnumerable< Entity > GetTargets(Identifier tag)