Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Additional SpawnPositionType and Equip Behaviours #14419

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using System.Collections.Immutable;
using System.Linq;
using System.Xml.Linq;
using static Barotrauma.EntitySpawner;

namespace Barotrauma
{
Expand Down Expand Up @@ -151,7 +152,11 @@ public enum SpawnPositionType
/// <summary>
/// The position of the entity the StatusEffect is targeting. If there are multiple targets, an item is spawned at all of them.
/// </summary>
Target
Target,
/// <summary>
/// The inventories of the entities the StatusEffect is targeting. If there are multiple targets, the items are spawned in all of them.
/// </summary>
TargetInventory
}

public enum SpawnRotationType
Expand Down Expand Up @@ -2138,7 +2143,7 @@ entity is Item item &&

void ProcessItemSpawnInfo(ItemSpawnInfo spawnInfo)
{
if (spawnInfo.SpawnPosition == ItemSpawnInfo.SpawnPositionType.Target)
if (spawnInfo.SpawnPosition is ItemSpawnInfo.SpawnPositionType.Target or ItemSpawnInfo.SpawnPositionType.TargetInventory)
{
foreach (var target in targets)
{
Expand Down Expand Up @@ -2192,7 +2197,7 @@ void SpawnItem(ItemSpawnInfo chosenItemSpawnInfo, Entity entity, PhysicsBody sou
SetUser(parentItem.GetComponent<Projectile>()?.User);
}

if (chosenItemSpawnInfo.SpawnPosition == ItemSpawnInfo.SpawnPositionType.Target && targetEntity != null)
if (chosenItemSpawnInfo.SpawnPosition is ItemSpawnInfo.SpawnPositionType.Target or ItemSpawnInfo.SpawnPositionType.TargetInventory && targetEntity != null)
{
entity = targetEntity;
position = entity.WorldPosition;
Expand Down Expand Up @@ -2318,6 +2323,7 @@ void SpawnItem(ItemSpawnInfo chosenItemSpawnInfo, Entity entity, PhysicsBody sou
});
break;
case ItemSpawnInfo.SpawnPositionType.ThisInventory:
case ItemSpawnInfo.SpawnPositionType.TargetInventory:
{
Inventory inventory = null;
if (entity is Character character && character.Inventory != null)
Expand All @@ -2343,16 +2349,7 @@ void SpawnItem(ItemSpawnInfo chosenItemSpawnInfo, Entity entity, PhysicsBody sou
{
Entity.Spawner.AddItemToSpawnQueue(chosenItemSpawnInfo.ItemPrefab, inventory, spawnIfInventoryFull: chosenItemSpawnInfo.SpawnIfInventoryFull, onSpawned: item =>
{
if (chosenItemSpawnInfo.Equip && entity is Character character && character.Inventory != null)
{
//if the item is both pickable and wearable, try to wear it instead of picking it up
List<InvSlotType> allowedSlots =
item.GetComponents<Pickable>().Count() > 1 ?
new List<InvSlotType>(item.GetComponent<Wearable>()?.AllowedSlots ?? item.GetComponent<Pickable>().AllowedSlots) :
new List<InvSlotType>(item.AllowedSlots);
allowedSlots.Remove(InvSlotType.Any);
character.Inventory.TryPutItem(item, null, allowedSlots);
}
if (chosenItemSpawnInfo.Equip) TryEquipItem(item, inventory);
OnItemSpawned(item, chosenItemSpawnInfo);
});
}
Expand All @@ -2373,6 +2370,7 @@ void SpawnItem(ItemSpawnInfo chosenItemSpawnInfo, Entity entity, PhysicsBody sou
{
Entity.Spawner.AddItemToSpawnQueue(chosenItemSpawnInfo.ItemPrefab, inventory, spawnIfInventoryFull: chosenItemSpawnInfo.SpawnIfInventoryFull, onSpawned: (Item newItem) =>
{
if (chosenItemSpawnInfo.Equip) TryEquipItem(newItem, inventory);
OnItemSpawned(newItem, chosenItemSpawnInfo);
});
}
Expand Down Expand Up @@ -2419,6 +2417,18 @@ void SpawnItem(ItemSpawnInfo chosenItemSpawnInfo, Entity entity, PhysicsBody sou
}
break;
}

void TryEquipItem(Item newItem, Inventory inventory)
{
if (inventory is not CharacterInventory charInventory) { return; }

// If the item is wearable, try to wear it instead of picking it up.
IEnumerable<InvSlotType> wearableSlots = newItem.GetComponents<Wearable>().SelectMany(wearable => wearable.AllowedSlots).Distinct();
List<InvSlotType> allowedSlots = (wearableSlots.Any() ? wearableSlots : newItem.AllowedSlots).ToList();
allowedSlots.Remove(InvSlotType.Any);
charInventory.TryPutItem(newItem, null, allowedSlots);
}

void OnItemSpawned(Item newItem, ItemSpawnInfo itemSpawnInfo)
{
newItem.Condition = newItem.MaxCondition * itemSpawnInfo.Condition;
Expand Down