Fixed items not being placed in the same hull and sub as the item they're inside if the ItemContainer is not the first of the parent's ItemContainer components (e.g. fabricator output inventory). Closes #430

This commit is contained in:
Joonas Rikkonen
2018-05-30 19:15:55 +03:00
parent d1ec246f28
commit 1a84b6cae7
2 changed files with 40 additions and 35 deletions
@@ -179,7 +179,6 @@ namespace Barotrauma.Items.Components
return (picker != null); return (picker != null);
} }
public override bool Combine(Item item) public override bool Combine(Item item)
{ {
if (!containableItems.Any(x => x.MatchesItem(item))) return false; if (!containableItems.Any(x => x.MatchesItem(item))) return false;
@@ -195,6 +194,42 @@ namespace Barotrauma.Items.Components
return false; return false;
} }
public void SetContainedItemPositions()
{
Vector2 simPos = item.SimPosition;
Vector2 displayPos = item.Position;
foreach (Item contained in Inventory.Items)
{
if (contained == null) continue;
if (contained.body != null)
{
try
{
contained.body.FarseerBody.SetTransformIgnoreContacts(ref simPos, 0.0f);
}
catch (Exception e)
{
#if DEBUG
DebugConsole.ThrowError("SetTransformIgnoreContacts threw an exception in SetContainedItemPositions", e);
#endif
}
}
contained.Rect =
new Rectangle(
(int)(displayPos.X - contained.Rect.Width / 2.0f),
(int)(displayPos.Y + contained.Rect.Height / 2.0f),
contained.Rect.Width, contained.Rect.Height);
contained.Submarine = item.Submarine;
contained.CurrentHull = item.CurrentHull;
contained.SetContainedItemPositions();
}
}
public override void OnMapLoaded() public override void OnMapLoaded()
{ {
if (itemIds == null) return; if (itemIds == null) return;
@@ -602,7 +602,7 @@ namespace Barotrauma
foreach (Item item in ItemList) item.FindHull(); foreach (Item item in ItemList) item.FindHull();
} }
public virtual Hull FindHull() public Hull FindHull()
{ {
if (parentInventory != null && parentInventory.Owner != null) if (parentInventory != null && parentInventory.Owner != null)
{ {
@@ -648,39 +648,9 @@ namespace Barotrauma
public void SetContainedItemPositions() public void SetContainedItemPositions()
{ {
if (ownInventory == null) return; foreach (ItemComponent component in components)
Vector2 simPos = SimPosition;
Vector2 displayPos = Position;
foreach (Item contained in ownInventory.Items)
{ {
if (contained == null) continue; (component as ItemContainer)?.SetContainedItemPositions();
if (contained.body != null)
{
try
{
contained.body.FarseerBody.SetTransformIgnoreContacts(ref simPos, 0.0f);
}
catch (Exception e)
{
#if DEBUG
DebugConsole.ThrowError("SetTransformIgnoreContacts threw an exception in SetContainedItemPositions", e);
#endif
}
}
contained.Rect =
new Rectangle(
(int)(displayPos.X - contained.Rect.Width / 2.0f),
(int)(displayPos.Y + contained.Rect.Height / 2.0f),
contained.Rect.Width, contained.Rect.Height);
contained.Submarine = Submarine;
contained.CurrentHull = CurrentHull;
contained.SetContainedItemPositions();
} }
} }