Client LuaCsForBarotrauma
BarotraumaShared/SharedSource/Items/Components/Holdable/LevelResource.cs
2 using FarseerPhysics;
3 using Microsoft.Xna.Framework;
4 using System;
5 using System.Linq;
6 
8 {
10  {
11  private PhysicsBody trigger;
12 
13  private Holdable holdable;
14 
15  private float deattachTimer;
16 
17  [Serialize(1.0f, IsPropertySaveable.No, description: "How long it takes to deattach the item from the level walls (in seconds).")]
18  public float DeattachDuration
19  {
20  get;
21  set;
22  }
23 
24  [Serialize(0.0f, IsPropertySaveable.No, description: "How far along the item is to being deattached. When the timer goes above DeattachDuration, the item is deattached.")]
25  public float DeattachTimer
26  {
27  get { return deattachTimer; }
28  set
29  {
30  //clients don't deattach the item until the server says so (handled in ClientRead)
31  if (GameMain.NetworkMember != null && GameMain.NetworkMember.IsClient)
32  {
33  return;
34  }
35 
36  if (holdable == null) { return; }
37 
38  deattachTimer = Math.Max(0.0f, value);
39 #if SERVER
40  if (deattachTimer >= DeattachDuration)
41  {
42  if (holdable.Attached) { item.CreateServerEvent(this); }
43  holdable.DeattachFromWall();
44  }
45  else if (Math.Abs(lastSentDeattachTimer - deattachTimer) > 0.1f)
46  {
47  item.CreateServerEvent(this);
48  lastSentDeattachTimer = deattachTimer;
49  }
50 #else
51  if (deattachTimer >= DeattachDuration)
52  {
53  if (holdable.Attached)
54  {
55  GameAnalyticsManager.AddDesignEvent("ResourceCollected:" + (GameMain.GameSession?.GameMode?.Preset.Identifier.Value ?? "none") + ":" + item.Prefab.Identifier);
56  holdable.DeattachFromWall();
57  }
58  trigger.Enabled = false;
59  }
60 #endif
61  }
62  }
63 
64  [Serialize(1.0f, IsPropertySaveable.No, description: "How much the position of the item can vary from the wall the item spawns on.")]
65  public float RandomOffsetFromWall
66  {
67  get;
68  set;
69  }
70 
71  public bool Attached
72  {
73  get { return holdable != null && holdable.Attached; }
74  }
75 
76  public LevelResource(Item item, ContentXElement element) : base(item, element)
77  {
78  IsActive = true;
79  }
80 
81  public override void Move(Vector2 amount, bool ignoreContacts = false)
82  {
83  if (trigger != null && amount.LengthSquared() > 0.00001f)
84  {
85  if (ignoreContacts)
86  {
88  }
89  else
90  {
91  trigger.SetTransform(item.SimPosition, 0.0f);
92  }
93  }
94  }
95 
96  public override void Update(float deltaTime, Camera cam)
97  {
98  if (holdable != null && !holdable.Attached)
99  {
100  if (trigger != null)
101  {
102  trigger.Enabled = false;
103  }
104  IsActive = false;
105  }
106  else
107  {
108  if (trigger == null)
109  {
110  CreateTriggerBody();
111  }
112  if (trigger != null && Vector2.DistanceSquared(item.SimPosition, trigger.SimPosition) > 0.01f)
113  {
114  trigger.SetTransform(item.SimPosition, 0.0f);
115  }
116  IsActive = false;
117  }
118  }
119 
120  public override void OnItemLoaded()
121  {
122  holdable = item.GetComponent<Holdable>();
123  if (holdable == null)
124  {
125  IsActive = false;
126  return;
127  }
128  holdable.Reattachable = false;
129  if (RequiredItems.Any())
130  {
131  holdable.PickingTime = float.MaxValue;
132  }
133  }
134 
135  private void CreateTriggerBody()
136  {
137  System.Diagnostics.Debug.Assert(trigger == null, "LevelResource trigger already created!");
138  var body = item.body ?? holdable?.Body;
139  if (body != null && Attached)
140  {
141  trigger = new PhysicsBody(body.Width, body.Height, body.Radius,
142  body.Density,
143  BodyType.Static,
144  Physics.CollisionWall,
145  Physics.CollisionNone,
146  findNewContacts: false)
147  {
148  UserData = item
149  };
150  trigger.FarseerBody.SetIsSensor(true);
151  }
152  }
153 
154  protected override void RemoveComponentSpecific()
155  {
156  if (trigger != null)
157  {
158  trigger.Remove();
159  trigger = null;
160  }
161  }
162  }
163 }
static GameSession?? GameSession
Definition: GameMain.cs:88
static NetworkMember NetworkMember
Definition: GameMain.cs:190
readonly Identifier Identifier
The base class for components holding the different functionalities of the item
Dictionary< RelatedItem.RelationType, List< RelatedItem > > RequiredItems
override void OnItemLoaded()
Called when all the components of the item have been loaded. Use to initialize connections between co...
bool SetTransform(Vector2 simPosition, float rotation, bool setPrevTransform=true)
bool SetTransformIgnoreContacts(Vector2 simPosition, float rotation, bool setPrevTransform=true)
readonly Identifier Identifier
Definition: Prefab.cs:34
Interface for entities that the server can send events to the clients