Client LuaCsForBarotrauma
BarotraumaClient/ClientSource/Items/DockingPort.cs
1 using Barotrauma.Lights;
3 using FarseerPhysics;
4 using FarseerPhysics.Collision;
5 using Microsoft.Xna.Framework;
6 using Microsoft.Xna.Framework.Graphics;
7 
9 {
10  partial class DockingPort : ItemComponent, IDrawableComponent, IServerSerializable, IClientSerializable
11  {
12  private GUIMessageBox autodockingVerification;
13 
14  private readonly ConvexHull[] convexHulls = new ConvexHull[2];
15 
16  public Vector2 DrawSize
17  {
18  //use the extents of the item as the draw size
19  get { return Vector2.Zero; }
20  }
21 
22  public void Draw(SpriteBatch spriteBatch, bool editing, float itemDepth = -1, Color? overrideColor = null)
23  {
24  if (dockingState == 0.0f) return;
25 
26  if (overlaySprite != null)
27  {
28  Vector2 drawPos = item.DrawPosition;
29  drawPos.Y = -drawPos.Y;
30 
31  var rect = overlaySprite.SourceRect;
32  if (IsHorizontal)
33  {
34  drawPos.Y -= rect.Height / 2;
35 
36  if (DockingDir == 1)
37  {
38  spriteBatch.Draw(overlaySprite.Texture,
39  drawPos,
40  new Rectangle(
41  rect.Center.X + (int)(rect.Width / 2 * (1.0f - dockingState)), rect.Y,
42  (int)(rect.Width / 2 * dockingState), rect.Height),
43  overrideColor ?? Color.White);
44 
45  }
46  else
47  {
48  spriteBatch.Draw(overlaySprite.Texture,
49  drawPos - Vector2.UnitX * (rect.Width / 2 * dockingState),
50  new Rectangle(
51  rect.X, rect.Y,
52  (int)(rect.Width / 2 * dockingState), rect.Height),
53  overrideColor ?? Color.White);
54  }
55  }
56  else
57  {
58  drawPos.X -= rect.Width / 2;
59 
60  if (DockingDir == 1)
61  {
62  spriteBatch.Draw(overlaySprite.Texture,
63  drawPos - Vector2.UnitY * (rect.Height / 2 * dockingState),
64  new Rectangle(
65  rect.X, rect.Y,
66  rect.Width, (int)(rect.Height / 2 * dockingState)),
67  overrideColor ?? Color.White);
68  }
69  else
70  {
71  spriteBatch.Draw(overlaySprite.Texture,
72  drawPos,
73  new Rectangle(
74  rect.X, rect.Y + rect.Height / 2 + (int)(rect.Height / 2 * (1.0f - dockingState)),
75  rect.Width, (int)(rect.Height / 2 * dockingState)),
76  overrideColor ?? Color.White);
77  }
78  }
79  }
80 
81  if (!GameMain.DebugDraw) { return; }
82 
83  if (bodies != null)
84  {
85  for (int i = 0; i < bodies.Length; i++)
86  {
87  var body = bodies[i];
88  if (body == null) continue;
89 
90  body.FixtureList[0].GetAABB(out AABB aabb, 0);
91 
92  Vector2 bodyDrawPos = ConvertUnits.ToDisplayUnits(new Vector2(aabb.LowerBound.X, aabb.UpperBound.Y));
93  if ((i == 1 || i == 3) && DockingTarget?.item?.Submarine != null) bodyDrawPos += DockingTarget.item.Submarine.Position;
94  if ((i == 0 || i == 2) && item.Submarine != null) bodyDrawPos += item.Submarine.Position;
95  bodyDrawPos.Y = -bodyDrawPos.Y;
96 
97  GUI.DrawRectangle(spriteBatch,
98  bodyDrawPos,
99  ConvertUnits.ToDisplayUnits(aabb.Extents * 2),
100  Color.Gray, false, 0.0f, 4);
101  }
102  }
103 
104  if (doorBody != null && doorBody.Enabled)
105  {
106  doorBody.FixtureList[0].GetAABB(out AABB aabb, 0);
107 
108  Vector2 bodyDrawPos = ConvertUnits.ToDisplayUnits(new Vector2(aabb.LowerBound.X, aabb.UpperBound.Y));
109  if (item?.Submarine != null) bodyDrawPos += item.Submarine.Position;
110  bodyDrawPos.Y = -bodyDrawPos.Y;
111 
112  GUI.DrawRectangle(spriteBatch,
113  bodyDrawPos,
114  ConvertUnits.ToDisplayUnits(aabb.Extents * 2),
115  Color.Gray, false, 0, 8);
116  }
117  }
118 
119  partial void RemoveConvexHulls()
120  {
121  for (int i = 0; i < convexHulls.Length; i++)
122  {
123  convexHulls[i]?.Remove();
124  convexHulls[i] = null;
125  }
126  }
127 
128  public void ClientEventRead(IReadMessage msg, float sendingTime)
129  {
130  bool isDocked = msg.ReadBoolean();
131 
132  for (int i = 0; i < 2; i++)
133  {
134  if (hulls[i] == null) continue;
135  item.linkedTo.Remove(hulls[i]);
136  hulls[i].Remove();
137  hulls[i] = null;
138  }
139 
140  if (gap != null)
141  {
142  item.linkedTo.Remove(gap);
143  gap.Remove();
144  gap = null;
145  }
146 
147  if (isDocked)
148  {
149  ushort dockingTargetID = msg.ReadUInt16();
150 
151  bool isLocked = msg.ReadBoolean();
152 
153  Entity targetEntity = Entity.FindEntityByID(dockingTargetID);
154  if (targetEntity == null || !(targetEntity is Item))
155  {
156  DebugConsole.ThrowError("Invalid docking port network event (can't dock to " + (targetEntity?.ToString() ?? "null") + ")");
157  return;
158  }
159 
160  DockingTarget = (targetEntity as Item).GetComponent<DockingPort>();
161  if (DockingTarget == null)
162  {
163  DebugConsole.ThrowError("Invalid docking port network event (" + targetEntity + " doesn't have a docking port component)");
164  return;
165  }
166 
168  if (joint == null)
169  {
170  string errorMsg = "Error while reading a docking port network event (Dock method did not create a joint between the ports)." +
171  " Submarine: " + (item.Submarine?.Info.Name ?? "null") +
172  ", target submarine: " + (DockingTarget.item.Submarine?.Info.Name ?? "null");
173  if (item.Submarine?.ConnectedDockingPorts.ContainsKey(DockingTarget.item.Submarine) ?? false)
174  {
175  errorMsg += "\nAlready docked.";
176  }
178  {
179  errorMsg += "\nTrying to dock the submarine to itself.";
180  }
181  GameAnalyticsManager.AddErrorEventOnce("DockingPort.ClientRead:JointNotCreated", GameAnalyticsManager.ErrorSeverity.Error, errorMsg);
182  }
183 
184  if (isLocked)
185  {
186  if (DockingTarget.joint != null)
187  {
188  DockingTarget.Lock(isNetworkMessage: true);
189  }
190  else
191  {
192  Lock(isNetworkMessage: true);
193  }
194  }
195  }
196  else
197  {
198  Undock();
199  }
200  }
201 
202  public void ClientEventWrite(IWriteMessage msg, NetEntityEvent.IData extraData = null)
203  {
204  msg.WriteByte((byte)allowOutpostAutoDocking);
205  }
206  }
207 }
virtual Vector2 DrawPosition
Definition: Entity.cs:51
Submarine Submarine
Definition: Entity.cs:53
static Entity FindEntityByID(ushort ID)
Find an entity based on the ID
Definition: Entity.cs:204
static bool DebugDraw
Definition: GameMain.cs:29
void ClientEventWrite(IWriteMessage msg, NetEntityEvent.IData extraData=null)
void Draw(SpriteBatch spriteBatch, bool editing, float itemDepth=-1, Color? overrideColor=null)
readonly Dictionary< Submarine, DockingPort > ConnectedDockingPorts
Interface for entities that the clients can send events to the server
Interface for entities that the server can send events to the clients