Fixed items attached mid-round by other clients or the host being impossible to interact with and occasionally being attached to an incorrect position. Closes #296

This commit is contained in:
Joonas Rikkonen
2018-02-28 14:01:38 +02:00
parent 2585cc9873
commit 74ef5c6ea6
5 changed files with 40 additions and 18 deletions
@@ -126,7 +126,15 @@ namespace Barotrauma.Items.Components
public override void Drop(Character dropper) public override void Drop(Character dropper)
{ {
DropConnectedWires(dropper); Drop(true, dropper);
}
private void Drop(bool dropConnectedWires, Character dropper)
{
if (dropConnectedWires)
{
DropConnectedWires(dropper);
}
if (attachable) if (attachable)
{ {
@@ -360,6 +368,12 @@ namespace Barotrauma.Items.Components
item.body.Dir = -item.body.Dir; item.body.Dir = -item.body.Dir;
} }
public override void OnItemLoaded()
{
if (item.Submarine != null && item.Submarine.Loading) return;
OnMapLoaded();
}
public override void OnMapLoaded() public override void OnMapLoaded()
{ {
if (!attachable) return; if (!attachable) return;
@@ -384,17 +398,20 @@ namespace Barotrauma.Items.Components
public void ServerWrite(NetBuffer msg, Client c, object[] extraData = null) public void ServerWrite(NetBuffer msg, Client c, object[] extraData = null)
{ {
if (!attachable) if (!attachable || body == null)
{ {
DebugConsole.ThrowError("Sent an attachment event for an item that's not attachable."); DebugConsole.ThrowError("Sent an attachment event for an item that's not attachable.");
} }
msg.Write(Attached); msg.Write(Attached);
msg.Write(body.SimPosition.X);
msg.Write(body.SimPosition.Y);
} }
public void ClientRead(ServerNetObject type, NetBuffer msg, float sendingTime) public void ClientRead(ServerNetObject type, NetBuffer msg, float sendingTime)
{ {
bool isAttached = msg.ReadBoolean(); bool isAttached = msg.ReadBoolean();
Vector2 simPosition = new Vector2(msg.ReadFloat(), msg.ReadFloat());
if (!attachable) if (!attachable)
{ {
@@ -404,10 +421,8 @@ namespace Barotrauma.Items.Components
if (isAttached) if (isAttached)
{ {
if (item.ParentInventory != null) Drop(false, null);
{ item.SetTransform(simPosition, 0.0f);
item.ParentInventory.RemoveItem(item);
}
AttachToWall(); AttachToWall();
} }
else else
@@ -208,7 +208,7 @@ namespace Barotrauma.Items.Components
if (item.body != null && !item.body.Enabled) if (item.body != null && !item.body.Enabled)
{ {
item.body.ResetDynamics(); item.body.ResetDynamics();
item.SetTransform(bodyDropPos, 0.0f); item.SetTransform(bodyDropPos, 0.0f);
item.body.Enabled = true; item.body.Enabled = true;
} }
@@ -575,8 +575,16 @@ namespace Barotrauma.Items.Components
} }
} }
/// <summary>
/// Called when all items have been loaded. Use to initialize connections between items.
/// </summary>
public virtual void OnMapLoaded() { } public virtual void OnMapLoaded() { }
/// <summary>
/// Called when all the components of the item have been loaded. Use to initialize connections between components and such.
/// </summary>
public virtual void OnItemLoaded() { }
public static ItemComponent Load(XElement element, Item item, string file, bool errorMessages = true) public static ItemComponent Load(XElement element, Item item, string file, bool errorMessages = true)
{ {
Type t; Type t;
@@ -23,12 +23,6 @@ namespace Barotrauma.Items.Components
{ {
get get
{ {
if (powerConnections == null)
{
var connections = Item.Connections;
powerConnections = connections == null ? new List<Connection>() : connections.FindAll(c => c.IsPower);
}
return powerConnections; return powerConnections;
} }
} }
@@ -338,15 +332,15 @@ namespace Barotrauma.Items.Components
connectionDirty[connection] = true; connectionDirty[connection] = true;
} }
public override void OnMapLoaded() public override void OnItemLoaded()
{ {
var connections = item.Connections; var connections = Item.Connections;
powerConnections = connections == null ? new List<Connection>() : connections.FindAll(c => c.IsPower);
if (connections == null) if (connections == null)
{ {
IsActive = false; IsActive = false;
return; return;
} }
SetAllConnectionsDirty(); SetAllConnectionsDirty();
} }
@@ -461,6 +461,11 @@ namespace Barotrauma
InsertToList(); InsertToList();
ItemList.Add(this); ItemList.Add(this);
foreach (ItemComponent ic in components)
{
ic.OnItemLoaded();
}
} }
public override MapEntity Clone() public override MapEntity Clone()