From 1a84b6cae72a024f0c424775bbb8afb4fc519755 Mon Sep 17 00:00:00 2001 From: Joonas Rikkonen Date: Wed, 30 May 2018 19:15:55 +0300 Subject: [PATCH] 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 --- .../Source/Items/Components/ItemContainer.cs | 37 +++++++++++++++++- .../BarotraumaShared/Source/Items/Item.cs | 38 ++----------------- 2 files changed, 40 insertions(+), 35 deletions(-) diff --git a/Barotrauma/BarotraumaShared/Source/Items/Components/ItemContainer.cs b/Barotrauma/BarotraumaShared/Source/Items/Components/ItemContainer.cs index e9fae6c86..416082a19 100644 --- a/Barotrauma/BarotraumaShared/Source/Items/Components/ItemContainer.cs +++ b/Barotrauma/BarotraumaShared/Source/Items/Components/ItemContainer.cs @@ -179,7 +179,6 @@ namespace Barotrauma.Items.Components return (picker != null); } - public override bool Combine(Item item) { if (!containableItems.Any(x => x.MatchesItem(item))) return false; @@ -195,6 +194,42 @@ namespace Barotrauma.Items.Components 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() { if (itemIds == null) return; diff --git a/Barotrauma/BarotraumaShared/Source/Items/Item.cs b/Barotrauma/BarotraumaShared/Source/Items/Item.cs index 21548bfcb..3248be891 100644 --- a/Barotrauma/BarotraumaShared/Source/Items/Item.cs +++ b/Barotrauma/BarotraumaShared/Source/Items/Item.cs @@ -602,7 +602,7 @@ namespace Barotrauma foreach (Item item in ItemList) item.FindHull(); } - public virtual Hull FindHull() + public Hull FindHull() { if (parentInventory != null && parentInventory.Owner != null) { @@ -648,39 +648,9 @@ namespace Barotrauma public void SetContainedItemPositions() { - if (ownInventory == null) return; - - Vector2 simPos = SimPosition; - Vector2 displayPos = Position; - - foreach (Item contained in ownInventory.Items) + foreach (ItemComponent component in components) { - 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 = Submarine; - contained.CurrentHull = CurrentHull; - - contained.SetContainedItemPositions(); + (component as ItemContainer)?.SetContainedItemPositions(); } } @@ -804,7 +774,7 @@ namespace Barotrauma private bool IsInWater() { if (CurrentHull == null) return true; - + float surfaceY = CurrentHull.Surface; return CurrentHull.WaterVolume > 0.0f && Position.Y < surfaceY;