- ItemContainers whose bodies are disabled don't set the positions of the contained items each frame (only once when the item is placed in the container)
- ItemContainers maintain a list of statuseffects that need to be applied on the contained items instead of constantly rechecking each item - deactivating ItemContainers if they have no body and there are no statuseffects to apply
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
using System.Xml.Linq;
|
using System.Xml.Linq;
|
||||||
using FarseerPhysics;
|
using FarseerPhysics;
|
||||||
using Microsoft.Xna.Framework;
|
using Microsoft.Xna.Framework;
|
||||||
@@ -14,7 +15,7 @@ namespace Barotrauma.Items.Components
|
|||||||
List<RelatedItem> containableItems;
|
List<RelatedItem> containableItems;
|
||||||
public ItemInventory Inventory;
|
public ItemInventory Inventory;
|
||||||
|
|
||||||
private bool hasStatusEffects;
|
private List<Pair<Item, StatusEffect>> itemsWithStatusEffects;
|
||||||
|
|
||||||
//how many items can be contained
|
//how many items can be contained
|
||||||
[HasDefaultValue(5, false)]
|
[HasDefaultValue(5, false)]
|
||||||
@@ -111,23 +112,38 @@ namespace Barotrauma.Items.Components
|
|||||||
RelatedItem containable = RelatedItem.Load(subElement);
|
RelatedItem containable = RelatedItem.Load(subElement);
|
||||||
if (containable == null) continue;
|
if (containable == null) continue;
|
||||||
|
|
||||||
foreach (StatusEffect effect in containable.statusEffects)
|
|
||||||
{
|
|
||||||
if (effect.type == ActionType.OnContaining) hasStatusEffects = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
containableItems.Add(containable);
|
containableItems.Add(containable);
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
IsActive = true;
|
itemsWithStatusEffects = new List<Pair<Item, StatusEffect>>();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void RemoveContained(Item item)
|
public void OnItemContained(Item item)
|
||||||
{
|
{
|
||||||
Inventory.RemoveItem(item);
|
item.SetContainedItemPositions();
|
||||||
|
|
||||||
|
RelatedItem ri = containableItems.Find(x => x.MatchesItem(item));
|
||||||
|
if (ri != null)
|
||||||
|
{
|
||||||
|
foreach (StatusEffect effect in ri.statusEffects)
|
||||||
|
{
|
||||||
|
itemsWithStatusEffects.Add(Pair<Item, StatusEffect>.Create(item, effect));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//no need to Update() if this item has no statuseffects and no physics body
|
||||||
|
IsActive = itemsWithStatusEffects.Count > 0 || item.body != null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void OnItemRemoved(Item item)
|
||||||
|
{
|
||||||
|
itemsWithStatusEffects.RemoveAll(i => i.First == item);
|
||||||
|
|
||||||
|
//deactivate if the inventory is empty
|
||||||
|
IsActive = itemsWithStatusEffects.Count > 0 || item.body != null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool CanBeContained(Item item)
|
public bool CanBeContained(Item item)
|
||||||
@@ -138,31 +154,24 @@ namespace Barotrauma.Items.Components
|
|||||||
|
|
||||||
public override void Update(float deltaTime, Camera cam)
|
public override void Update(float deltaTime, Camera cam)
|
||||||
{
|
{
|
||||||
if (item.body != null && item.body.FarseerBody.Awake)
|
if (item.body != null &&
|
||||||
|
item.body.Enabled &&
|
||||||
|
item.body.FarseerBody.Awake)
|
||||||
{
|
{
|
||||||
foreach (Item contained in Inventory.Items)
|
item.SetContainedItemPositions();
|
||||||
{
|
|
||||||
if (contained == null) continue;
|
|
||||||
contained.SetTransform(item.SimPosition, 0.0f);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!hasStatusEffects) return;
|
foreach (Pair<Item, StatusEffect> itemAndEffect in itemsWithStatusEffects)
|
||||||
|
|
||||||
foreach (Item contained in Inventory.Items)
|
|
||||||
{
|
{
|
||||||
if (contained == null || contained.Condition <= 0.0f) continue;
|
Item contained = itemAndEffect.First;
|
||||||
|
if (contained.Condition < 0.0f) continue;
|
||||||
|
|
||||||
RelatedItem ri = containableItems.Find(x => x.MatchesItem(contained));
|
StatusEffect effect = itemAndEffect.Second;
|
||||||
if (ri == null) continue;
|
|
||||||
|
|
||||||
foreach (StatusEffect effect in ri.statusEffects)
|
if (effect.Targets.HasFlag(StatusEffect.TargetType.This))
|
||||||
{
|
effect.Apply(ActionType.OnContaining, deltaTime, item, item.AllPropertyObjects);
|
||||||
if (effect.Targets.HasFlag(StatusEffect.TargetType.This)) effect.Apply(ActionType.OnContaining, deltaTime, item, item.AllPropertyObjects);
|
if (effect.Targets.HasFlag(StatusEffect.TargetType.Contained))
|
||||||
if (effect.Targets.HasFlag(StatusEffect.TargetType.Contained)) effect.Apply(ActionType.OnContaining, deltaTime, item, contained.AllPropertyObjects);
|
effect.Apply(ActionType.OnContaining, deltaTime, item, contained.AllPropertyObjects);
|
||||||
}
|
|
||||||
|
|
||||||
//contained.ApplyStatusEffects(ActionType.OnContained, deltaTime);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -232,14 +241,12 @@ namespace Barotrauma.Items.Components
|
|||||||
|
|
||||||
public override bool Combine(Item item)
|
public override bool Combine(Item item)
|
||||||
{
|
{
|
||||||
if (containableItems.Find(x => x.MatchesItem(item)) == null) return false;
|
if (!containableItems.Any(x => x.MatchesItem(item))) return false;
|
||||||
|
|
||||||
if (Inventory.TryPutItem(item))
|
if (Inventory.TryPutItem(item))
|
||||||
{
|
{
|
||||||
IsActive = true;
|
IsActive = true;
|
||||||
if (hideItems || (item.body!=null && !item.body.Enabled)) item.body.Enabled = false;
|
if (hideItems && item.body != null) item.body.Enabled = false;
|
||||||
|
|
||||||
//item.Container = this.item;
|
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -168,9 +168,8 @@ namespace Barotrauma.Items.Components
|
|||||||
|
|
||||||
public override void Update(float deltaTime, Camera cam)
|
public override void Update(float deltaTime, Camera cam)
|
||||||
{
|
{
|
||||||
base.Update(deltaTime, cam);
|
|
||||||
|
|
||||||
item.SetTransform(picker.SimPosition, 0.0f);
|
item.SetTransform(picker.SimPosition, 0.0f);
|
||||||
|
item.SetContainedItemPositions();
|
||||||
|
|
||||||
ApplyStatusEffects(ActionType.OnWearing, deltaTime, picker);
|
ApplyStatusEffects(ActionType.OnWearing, deltaTime, picker);
|
||||||
|
|
||||||
|
|||||||
@@ -44,6 +44,27 @@ namespace Barotrauma
|
|||||||
return (item!=null && Items[i]==null && container.CanBeContained(item));
|
return (item!=null && Items[i]==null && container.CanBeContained(item));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public override bool TryPutItem(Item item, System.Collections.Generic.List<InvSlotType> allowedSlots = null, bool createNetworkEvent = true)
|
||||||
|
{
|
||||||
|
bool wasPut = base.TryPutItem(item, allowedSlots, createNetworkEvent);
|
||||||
|
|
||||||
|
if (wasPut)
|
||||||
|
{
|
||||||
|
foreach (Character c in Character.CharacterList)
|
||||||
|
{
|
||||||
|
if (!c.HasSelectedItem(item)) continue;
|
||||||
|
|
||||||
|
item.Unequip(c);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
container.IsActive = true;
|
||||||
|
container.OnItemContained(item);
|
||||||
|
}
|
||||||
|
|
||||||
|
return wasPut;
|
||||||
|
}
|
||||||
|
|
||||||
public override bool TryPutItem(Item item, int i, bool allowSwapping, bool createNetworkEvent)
|
public override bool TryPutItem(Item item, int i, bool allowSwapping, bool createNetworkEvent)
|
||||||
{
|
{
|
||||||
bool wasPut = base.TryPutItem(item, i, allowSwapping, createNetworkEvent);
|
bool wasPut = base.TryPutItem(item, i, allowSwapping, createNetworkEvent);
|
||||||
@@ -57,11 +78,18 @@ namespace Barotrauma
|
|||||||
item.Unequip(c);
|
item.Unequip(c);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
//item.Container = container.Item;
|
|
||||||
container.IsActive = true;
|
container.IsActive = true;
|
||||||
|
container.OnItemContained(item);
|
||||||
}
|
}
|
||||||
|
|
||||||
return wasPut;
|
return wasPut;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public override void RemoveItem(Item item)
|
||||||
|
{
|
||||||
|
base.RemoveItem(item);
|
||||||
|
container.OnItemRemoved(item);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user