Client LuaCsForBarotrauma
BarotraumaShared/SharedSource/Items/Components/Scanner.cs
1 using System;
2 using System.Xml.Linq;
3 
5 {
6  partial class Scanner : ItemComponent
7  {
8  [Serialize(1.0f, IsPropertySaveable.No, description: "How long it takes for the scan to be completed.")]
9  public float ScanDuration { get; set; }
10  [Serialize(0.0f, IsPropertySaveable.No, description: "How far along the scan is. When the timer goes above ScanDuration, the scan is completed.")]
11  public float ScanTimer
12  {
13  get
14  {
15  return scanTimer;
16  }
17  set
18  {
19  if (GameMain.NetworkMember != null && GameMain.NetworkMember.IsClient) { return; }
20  if (Holdable == null) { return; }
21  bool wasScanCompletedPreviously = IsScanCompleted;
22  scanTimer = Math.Max(0.0f, value);
23  if (!wasScanCompletedPreviously && IsScanCompleted)
24  {
25  OnScanCompleted?.Invoke(this);
26  }
27 #if SERVER
28  if (wasScanCompletedPreviously != IsScanCompleted || Math.Abs(LastSentScanTimer - scanTimer) > 0.1f)
29  {
30  item.CreateServerEvent(this);
31  LastSentScanTimer = scanTimer;
32  }
33 #endif
34  }
35  }
36  [Serialize(1.0f, IsPropertySaveable.No, description: "How far the scanner can be from the target for the scan to be successful.")]
37  public float ScanRadius { get; set; }
38  [Serialize(true, IsPropertySaveable.No, description: "Should the progress bar always be displayed when the item has been attached.")]
39  public bool AlwaysDisplayProgressBar { get; set; }
40 
41  private Holdable Holdable { get; set; }
45  public bool DisplayProgressBar { get; set; } = false;
46  private bool IsScanCompleted => scanTimer >= ScanDuration;
47 
48  private float scanTimer;
49 
50  public Action<Scanner> OnScanStarted, OnScanCompleted;
51 
52  public Scanner(Item item, ContentXElement element) : base(item, element)
53  {
54  IsActive = true;
55  }
56 
57  public override void Update(float deltaTime, Camera cam)
58  {
59  if (Holdable != null && Holdable.Attachable && Holdable.Attached)
60  {
61  if (ScanTimer <= 0.0f)
62  {
63  OnScanStarted?.Invoke(this);
64  }
65  ScanTimer += deltaTime;
66  item.AiTarget?.IncreaseSoundRange(deltaTime, speed: 2.0f);
67  ApplyStatusEffects(ActionType.OnActive, deltaTime);
68  }
69  else
70  {
71  ScanTimer = 0.0f;
72  DisplayProgressBar = false;
73  }
74  UpdateProjSpecific();
75  }
76 
77  partial void UpdateProjSpecific();
78 
79  public override void OnItemLoaded()
80  {
81  base.OnItemLoaded();
82  Holdable = item.GetComponent<Holdable>();
83  if (Holdable == null || !Holdable.Attachable)
84  {
85  DebugConsole.ThrowError("Error in initializing a Scanner component: an attachable Holdable component is required on the same item and none was found",
86  contentPackage: item.Prefab.ContentPackage);
87  IsActive = false;
88  }
89  }
90  }
91 }
void IncreaseSoundRange(float deltaTime, float speed=1)
AITarget AiTarget
Definition: Entity.cs:55
static NetworkMember NetworkMember
Definition: GameMain.cs:190
The base class for components holding the different functionalities of the item
void ApplyStatusEffects(ActionType type, float deltaTime, Character character=null, Limb targetLimb=null, Entity useTarget=null, Character user=null, Vector2? worldPosition=null, float afflictionMultiplier=1.0f)
bool DisplayProgressBar
Should the progress bar be displayed. Use when AlwaysDisplayProgressBar is set to false.
override void OnItemLoaded()
Called when all the components of the item have been loaded. Use to initialize connections between co...
ContentPackage? ContentPackage
Definition: Prefab.cs:37
ActionType
ActionTypes define when a StatusEffect is executed.
Definition: Enums.cs:19