Client LuaCsForBarotrauma
BarotraumaClient/ClientSource/Items/Components/Holdable/Holdable.cs
2 using Microsoft.Xna.Framework;
3 using Microsoft.Xna.Framework.Graphics;
4 using System;
5 using System.Diagnostics.Tracing;
6 
8 {
9  partial class Holdable : IDrawableComponent
10  {
11  public Vector2 DrawSize
12  {
13  get { return item.Rect.Size.ToVector2(); }
14  }
15 
16  public void Draw(SpriteBatch spriteBatch, bool editing, float itemDepth = -1, Color? overrideColor = null)
17  {
19  {
20  Drawable = false;
21  return;
22  }
23 
24  Vector2 gridPos = picker.Position;
25  Vector2 roundedGridPos = new Vector2(
26  MathUtils.RoundTowardsClosest(picker.Position.X, Submarine.GridSize.X),
27  MathUtils.RoundTowardsClosest(picker.Position.Y, Submarine.GridSize.Y));
28  Vector2 attachPos = GetAttachPosition(picker);
29 
30  if (item.Submarine == null)
31  {
33  if (attachTarget != null)
34  {
35  if (attachTarget.Submarine != null)
36  {
37  //set to submarine-relative position
38  gridPos += attachTarget.Submarine.Position;
39  roundedGridPos += attachTarget.Submarine.Position;
40  attachPos += attachTarget.Submarine.Position;
41  }
42  }
43  }
44  else
45  {
46  gridPos += item.Submarine.Position;
47  roundedGridPos += item.Submarine.Position;
48  attachPos += item.Submarine.Position;
49  }
50 
51  Submarine.DrawGrid(spriteBatch, 14, gridPos, roundedGridPos, alpha: 0.4f);
52 
53  Sprite sprite = item.Sprite;
54  foreach (ContainedItemSprite containedSprite in item.Prefab.ContainedSprites)
55  {
56  if (containedSprite.UseWhenAttached)
57  {
58  sprite = containedSprite.Sprite;
59  break;
60  }
61  }
62 
63  sprite.Draw(
64  spriteBatch,
65  new Vector2(attachPos.X, -attachPos.Y),
66  item.SpriteColor * 0.5f,
68  item.Scale, SpriteEffects.None, 0.0f);
69 
70  GUI.DrawRectangle(spriteBatch, new Vector2(attachPos.X - 2, -attachPos.Y - 2), Vector2.One * 5, GUIStyle.Red, thickness: 3);
71  }
72 
73  public override bool ValidateEventData(NetEntityEvent.IData data)
74  => TryExtractEventData<EventData>(data, out _);
75 
76  public void ClientEventWrite(IWriteMessage msg, NetEntityEvent.IData extraData = null)
77  {
78  if (!attachable || body == null) { return; }
79 
80  var eventData = ExtractEventData<EventData>(extraData);
81 
82  Vector2 attachPos = eventData.AttachPos;
83  msg.WriteSingle(attachPos.X);
84  msg.WriteSingle(attachPos.Y);
85  }
86 
87  public override void ClientEventRead(IReadMessage msg, float sendingTime)
88  {
89  base.ClientEventRead(msg, sendingTime);
90 
91  bool readAttachData = msg.ReadBoolean();
92  if (!readAttachData) { return; }
93 
94  bool shouldBeAttached = msg.ReadBoolean();
95  Vector2 simPosition = new Vector2(msg.ReadSingle(), msg.ReadSingle());
96  UInt16 submarineID = msg.ReadUInt16();
97  Submarine sub = Entity.FindEntityByID(submarineID) as Submarine;
98 
99  if (shouldBeAttached)
100  {
101  if (!attached)
102  {
103  Drop(false, null);
104  item.SetTransform(simPosition, 0.0f);
105  item.Submarine = sub;
106  AttachToWall();
107  }
108  }
109  else
110  {
111  if (attached)
112  {
113  DropConnectedWires(null);
114  if (body != null)
115  {
116  item.body = body;
117  item.body.Enabled = true;
118  }
119  IsActive = false;
120 
122  }
123  else
124  {
125  item.SetTransform(simPosition, 0.0f);
126  item.Submarine = sub;
127  }
128  }
129  }
130  }
131 }
Submarine Submarine
Definition: Entity.cs:53
static Entity FindEntityByID(ushort ID)
Find an entity based on the ID
Definition: Entity.cs:204
void SetTransform(Vector2 simPosition, float rotation, bool findNewHull=true, bool setPrevTransform=true)
ImmutableArray< ContainedItemSprite > ContainedSprites
void ClientEventWrite(IWriteMessage msg, NetEntityEvent.IData extraData=null)
override bool ValidateEventData(NetEntityEvent.IData data)
void Draw(SpriteBatch spriteBatch, bool editing, float itemDepth=-1, Color? overrideColor=null)
override void Drop(Character dropper, bool setTransform=true)
a Character has dropped the item
void DropConnectedWires(Character character)
Definition: Pickable.cs:248
void Draw(ISpriteBatch spriteBatch, Vector2 pos, float rotate=0.0f, float scale=1.0f, SpriteEffects spriteEffect=SpriteEffects.None)
static Structure GetAttachTarget(Vector2 worldPosition)
Checks if there's a structure items can be attached to at the given position and returns it.
static void DrawGrid(SpriteBatch spriteBatch, int gridCells, Vector2 gridCenter, Vector2 roundedGridCenter, float alpha=1.0f)