LimbSlot combinations for pickable items ("Any,RightHand+LeftHand"...)

This commit is contained in:
Regalis
2015-11-19 21:24:49 +02:00
parent a46f724f1f
commit 8ec7fd44ff
18 changed files with 272 additions and 152 deletions

View File

@@ -481,8 +481,10 @@ namespace Barotrauma
if (info.Job.EquipSpawnItem[i])
{
inventory.TryPutItem(item,
item.AllowedSlots.HasFlag(LimbSlot.Any) ? item.AllowedSlots & ~LimbSlot.Any : item.AllowedSlots, false);
List<LimbSlot> allowedSlots = new List<LimbSlot>(item.AllowedSlots);
allowedSlots.Remove(LimbSlot.Any);
inventory.TryPutItem(item, allowedSlots, false);
}
else
{

View File

@@ -11,7 +11,7 @@ namespace Barotrauma
[Flags]
public enum LimbSlot
{
Any = 1, RightHand = 2, LeftHand = 4, Head = 8, Torso = 16, Legs = 32, BothHands = 64
None = 0, Any = 1, RightHand = 2, LeftHand = 4, Head = 8, Torso = 16, Legs = 32
};
class CharacterInventory : Inventory
@@ -100,56 +100,69 @@ namespace Barotrauma
/// <summary>
/// If there is room, puts the item in the inventory and returns true, otherwise returns false
/// </summary>
public override bool TryPutItem(Item item, LimbSlot allowedSlots, bool createNetworkEvent = true)
public override bool TryPutItem(Item item, List<LimbSlot> allowedSlots, bool createNetworkEvent = true)
{
for (int i = 0; i < capacity; i++)
{
//item is already in the inventory!
if (items[i] == item) return true;
}
//for (int i = 0; i < capacity; i++)
//{
// //item is already in the inventory!
// if (items[i] == item) return true;
//}
if (allowedSlots.HasFlag(LimbSlot.Any))
//try to place the item in LimBlot.Any slot if that's allowed
if (allowedSlots.Contains(LimbSlot.Any))
{
for (int i = 0; i < capacity; i++)
{
if (items[i] != null) continue;
if (limbSlots[i] != LimbSlot.Any) continue;
if (items[i] != null || limbSlots[i] != LimbSlot.Any) continue;
PutItem(item, i, createNetworkEvent);
item.Unequip(character);
return true;
}
}
for (int i = 0; i < capacity; i++)
{
if (allowedSlots.HasFlag(limbSlots[i]) && items[i]!=null) return false;
}
bool placed = false;
for (int i = 0; i < capacity; i++)
foreach (LimbSlot allowedSlot in allowedSlots)
{
if (allowedSlots.HasFlag(limbSlots[i]) && items[i] == null)
//check if all the required slots are free
bool free = true;
for (int i = 0; i < capacity; i++)
{
PutItem(item, i, createNetworkEvent, !placed);
item.Equip(character);
placed = true;
if (allowedSlot.HasFlag(limbSlots[i]) && items[i]!=null && items[i]!=item)
{
free = false;
break;
}
}
if (!free) continue;
for (int i = 0; i < capacity; i++)
{
if (allowedSlot.HasFlag(limbSlots[i]) && items[i] == null)
{
PutItem(item, i, createNetworkEvent, !placed);
item.Equip(character);
placed = true;
}
}
if (placed) return true;
//if (allowedSlots.HasFlag(LimbSlot.BothHands)) TryPutItem(item, 3, createNetworkEvent);
}
if (placed) return true;
if (allowedSlots.HasFlag(LimbSlot.BothHands)) TryPutItem(item, 3, createNetworkEvent);
return false;
return placed;
}
public override bool TryPutItem(Item item, int i, bool createNetworkEvent)
public override bool TryPutItem(Item item, int index, bool createNetworkEvent)
{
LimbSlot usedSlots = item.AllowedSlots;
//there's already an item in the slot
if (items[i] != null)
if (items[index] != null)
{
if (items[index] == item) return false;
bool combined = false;
//if (item.Combine(items[i]))
//{
@@ -157,11 +170,16 @@ namespace Barotrauma
// combined = true;
//}
//else
if (items[i].Combine(item))
if (items[index].Combine(item))
{
//PutItem(items[i], i, false, false);
Inventory otherInventory = items[i].inventory;
if (otherInventory!=null && createNetworkEvent)
if (items[index]==null)
{
System.Diagnostics.Debug.Assert(false);
return false;
}
Inventory otherInventory = items[index].inventory;
if (otherInventory != null && createNetworkEvent)
{
new Networking.NetworkEvent(Networking.NetworkEventType.InventoryUpdate, otherInventory.Owner.ID, true, true);
}
@@ -169,65 +187,157 @@ namespace Barotrauma
combined = true;
}
if (!combined) return false;
return combined;
}
if (usedSlots.HasFlag(LimbSlot.BothHands))
{
if (limbSlots[i] == LimbSlot.LeftHand)
{
PutItem(item, FindLimbSlot(LimbSlot.RightHand), createNetworkEvent, false);
}
else if (limbSlots[i] == LimbSlot.RightHand)
{
PutItem(item, FindLimbSlot(LimbSlot.LeftHand), createNetworkEvent, false);
}
}
if (limbSlots[i] == LimbSlot.Any) item.Unequip(character);
if (limbSlots[index] == LimbSlot.Any)
{
if (!item.AllowedSlots.Contains(LimbSlot.Any)) return false;
if (items[index] != null) return items[index] == item;
PutItem(item, index, createNetworkEvent, true);
return true;
}
LimbSlot placeToSlots = LimbSlot.None;
bool slotsFree = true;
List<LimbSlot> allowedSlots = item.AllowedSlots;
foreach (LimbSlot allowedSlot in allowedSlots)
{
if (!allowedSlot.HasFlag(limbSlots[index])) continue;
for (int i = 0; i < capacity; i++)
{
if (allowedSlot.HasFlag(limbSlots[i]) && items[i] != null && items[i] != item)
{
slotsFree = false;
break;
}
placeToSlots = allowedSlot;
}
}
if (!slotsFree) return false;
if (limbSlots[i]==LimbSlot.Any)
{
if (usedSlots.HasFlag(LimbSlot.Any))
{
item.Unequip(character);
PutItem(item, i, createNetworkEvent);
return true;
}
else
{
return false;
}
}
else
{
return TryPutItem(item, new List<LimbSlot>() {placeToSlots}, createNetworkEvent);
if (limbSlots[i] != LimbSlot.Any && usedSlots.HasFlag(limbSlots[i]) && items[i] == null)
{
item.Unequip(character);
PutItem(item, i, createNetworkEvent);
item.Equip(character);
return true;
}
////there's already an item in the slot
//if (items[i] != null)
//{
// bool combined = false;
// //if (item.Combine(items[i]))
// //{
// // //PutItem(item, i, false, false);
// // combined = true;
// //}
// //else
// if (items[i].Combine(item))
// {
// //PutItem(items[i], i, false, false);
// Inventory otherInventory = items[i].inventory;
// if (otherInventory!=null && createNetworkEvent)
// {
// new Networking.NetworkEvent(Networking.NetworkEventType.InventoryUpdate, otherInventory.Owner.ID, true, true);
// }
// combined = true;
// }
// if (!combined) return false;
// //if (usedSlots.HasFlag(LimbSlot.BothHands))
// //{
// // if (limbSlots[i] == LimbSlot.LeftHand)
// // {
// // PutItem(item, FindLimbSlot(LimbSlot.RightHand), createNetworkEvent, false);
// // }
// // else if (limbSlots[i] == LimbSlot.RightHand)
// // {
// // PutItem(item, FindLimbSlot(LimbSlot.LeftHand), createNetworkEvent, false);
// // }
// //}
// if (limbSlots[i] == LimbSlot.Any)
// {
// item.Unequip(character);
// return true;
// }
//}
//bool placed = false;
//foreach (LimbSlot allowedSlot in usedSlots)
//{
// if ()
//}
//foreach (LimbSlot allowedSlot in usedSlots)
//{
// //check if all the required slots are free
// for (int n = 0; n < capacity; i++)
// {
// if (allowedSlot.HasFlag(limbSlots[n]) && items[n] != null && items[n] != item) continue;
// }
// for (int n = 0; n < capacity; n++)
// {
// if (allowedSlot.HasFlag(limbSlots[i]) && items[i] == null)
// {
// PutItem(item, i, createNetworkEvent, !placed);
// item.Equip(character);
// placed = true;
// }
// }
// if (placed) return true;
// //if (allowedSlots.HasFlag(LimbSlot.BothHands)) TryPutItem(item, 3, createNetworkEvent);
//}
//if (limbSlots[i]==LimbSlot.Any)
//{
// if (usedSlots.HasFlag(LimbSlot.Any))
// {
// item.Unequip(character);
// PutItem(item, i, createNetworkEvent);
// return true;
// }
// else
// {
// return false;
// }
//}
//else
//{
// if (limbSlots[i] != LimbSlot.Any && usedSlots.HasFlag(limbSlots[i]) && items[i] == null)
// {
// item.Unequip(character);
// PutItem(item, i, createNetworkEvent);
// item.Equip(character);
// return true;
// }
if (usedSlots.HasFlag(LimbSlot.BothHands) && (limbSlots[i]==LimbSlot.LeftHand || limbSlots[i]==LimbSlot.RightHand))
{
int rightHandSlot = FindLimbSlot(LimbSlot.LeftHand);
int leftHandSlot = FindLimbSlot(LimbSlot.RightHand);
// if (usedSlots.HasFlag(LimbSlot.BothHands) && (limbSlots[i]==LimbSlot.LeftHand || limbSlots[i]==LimbSlot.RightHand))
// {
// int rightHandSlot = FindLimbSlot(LimbSlot.LeftHand);
// int leftHandSlot = FindLimbSlot(LimbSlot.RightHand);
if (items[rightHandSlot] != null) return false;
if (items[leftHandSlot] != null) return false;
// if (items[rightHandSlot] != null) return false;
// if (items[leftHandSlot] != null) return false;
PutItem(item, rightHandSlot, createNetworkEvent, true);
PutItem(item, leftHandSlot, createNetworkEvent, false);
item.Equip(character);
return true;
}
// PutItem(item, rightHandSlot, createNetworkEvent, true);
// PutItem(item, leftHandSlot, createNetworkEvent, false);
// item.Equip(character);
// return true;
// }
return false;
}
// return false;
//}
}

View File

@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Xml.Linq;
namespace Barotrauma.Items.Components
@@ -7,9 +8,9 @@ namespace Barotrauma.Items.Components
{
protected Character picker;
protected LimbSlot allowedSlots;
protected List<LimbSlot> allowedSlots;
public LimbSlot AllowedSlots
public List<LimbSlot> AllowedSlots
{
get { return allowedSlots; }
}
@@ -22,11 +23,26 @@ namespace Barotrauma.Items.Components
public Pickable(Item item, XElement element)
: base(item, element)
{
allowedSlots = new List<LimbSlot>();
string slotString = ToolBox.GetAttributeString(element, "slots", "Any");
string[] slots = slotString.Split(',');
foreach (string slot in slots)
string[] slotCombinations = slotString.Split(',');
foreach (string slotCombination in slotCombinations)
{
allowedSlots = allowedSlots | (LimbSlot)Enum.Parse(typeof(LimbSlot), slot.Trim());
string[] slots = slotCombination.Split('+');
LimbSlot allowedSlot = LimbSlot.None;
foreach (string slot in slots)
{
if (slot.ToLower()=="bothhands")
{
allowedSlot = LimbSlot.LeftHand | LimbSlot.RightHand;
}
else
{
allowedSlot = allowedSlot | (LimbSlot)Enum.Parse(typeof(LimbSlot), slot.Trim());
}
}
allowedSlots.Add(allowedSlot);
}
canBePicked = true;

View File

@@ -89,7 +89,7 @@ namespace Barotrauma
/// <summary>
/// If there is room, puts the item in the inventory and returns true, otherwise returns false
/// </summary>
public virtual bool TryPutItem(Item item, LimbSlot allowedSlots = 0, bool createNetworkEvent = true)
public virtual bool TryPutItem(Item item, List<LimbSlot> allowedSlots = null, bool createNetworkEvent = true)
{
int slot = FindAllowedSlot(item);
if (slot < 0) return false;
@@ -238,48 +238,46 @@ namespace Barotrauma
if (selectedSlot == slotIndex && !isSubSlot)
{
System.Diagnostics.Debug.WriteLine("sdfg: "+selectedSlot+" - "+slotIndex);
selectedSlot = -1;
if (item == null) return;
int itemCapacity = item.Capacity;
if (itemCapacity == 0) return;
int itemCapacity = item==null ? 0 : item.Capacity;
if (itemCapacity > 0)
{
#if DEBUG
System.Diagnostics.Debug.Assert(slotIndex>=0 && slotIndex<items.Length);
System.Diagnostics.Debug.Assert(slotIndex >= 0 && slotIndex < items.Length);
#else
if (slotIndex<0 || slotIndex>=items.Length) return;
#endif
Rectangle containerRect = new Rectangle(rect.X - 5, rect.Y - (40 + 10) * itemCapacity - 5,
rect.Width + 10, rect.Height + (40 + 10) * itemCapacity + 10);
Rectangle containerRect = new Rectangle(rect.X - 5, rect.Y - (40 + 10) * itemCapacity - 5,
rect.Width + 10, rect.Height + (40 + 10) * itemCapacity + 10);
Rectangle subRect = rect;
subRect.Height = 40;
Rectangle subRect = rect;
subRect.Height = 40;
selectedSlot = containerRect.Contains(PlayerInput.MousePosition) ? slotIndex : -1;
System.Diagnostics.Debug.WriteLine(selectedSlot);
selectedSlot = containerRect.Contains(PlayerInput.MousePosition) ? slotIndex : -1;
System.Diagnostics.Debug.WriteLine(selectedSlot);
GUI.DrawRectangle(spriteBatch, containerRect, Color.Black*0.8f, true);
GUI.DrawRectangle(spriteBatch, containerRect, Color.White);
GUI.DrawRectangle(spriteBatch, containerRect, Color.Black * 0.8f, true);
GUI.DrawRectangle(spriteBatch, containerRect, Color.White);
Item[] containedItems = null;
if (items[slotIndex] != null) containedItems = items[slotIndex].ContainedItems;
Item[] containedItems = null;
if (items[slotIndex] != null) containedItems = items[slotIndex].ContainedItems;
if (containedItems != null)
{
for (int i = 0; i < itemCapacity; i++)
if (containedItems != null)
{
subRect.Y = subRect.Y - subRect.Height - 10;
UpdateSlot(spriteBatch, subRect, selectedSlot, i < containedItems.Count() ? containedItems[i] : null, true);
for (int i = 0; i < itemCapacity; i++)
{
subRect.Y = subRect.Y - subRect.Height - 10;
UpdateSlot(spriteBatch, subRect, selectedSlot, i < containedItems.Count() ? containedItems[i] : null, true);
}
}
}
}
DrawSlot(spriteBatch, rect, (draggingItem == item && !mouseOn) ? null : item, mouseOn, isSubSlot, drawItem);

View File

@@ -186,12 +186,12 @@ namespace Barotrauma
}
//which type of inventory slots (head, torso, any, etc) the item can be placed in
public LimbSlot AllowedSlots
public List<LimbSlot> AllowedSlots
{
get
{
Pickable p = GetComponent<Pickable>();
return (p==null) ? LimbSlot.Any : p.AllowedSlots;
return (p==null) ? new List<LimbSlot>() { LimbSlot.Any } : p.AllowedSlots;
}
}

View File

@@ -257,7 +257,8 @@ namespace Barotrauma
Vector2 rectCenter = new Vector2(rect.Center.X, rect.Center.Y);
Vector2 offset = -currentLocation.MapPosition;
iceTexture.DrawTiled(spriteBatch, new Vector2(rect.X, rect.Y), new Vector2(rect.Width, rect.Height), offset, Color.White);
iceTexture.DrawTiled(spriteBatch, new Vector2(rect.X, rect.Y), new Vector2(rect.Width, rect.Height), Vector2.Zero, Color.White);
GUI.DrawRectangle(spriteBatch, rect, Color.White);
//spriteBatch.Draw(iceTexture, offset, rect, null, null, 0f, null, Color.White, SpriteEffects.None, 0.0f);
//Vector2 scale = new Vector2((float)rect.Width/ size, (float)rect.Height/size);

View File

@@ -270,16 +270,6 @@ namespace Barotrauma.Particles
prefab.Sprites[spriteIndex].origin, drawRotation,
drawSize, SpriteEffects.None, prefab.Sprites[spriteIndex].Depth);
//spriteBatch.Draw(
// prefab.sprite.Texture,
// drawPosition,
// null,
// color*alpha,
// drawRotation,
// prefab.sprite.origin,
// size,
// SpriteEffects.None, prefab.sprite.Depth);
prevPosition = position;
prevRotation = rotation;
}

View File

@@ -28,19 +28,19 @@ namespace Barotrauma
{
using (var game = new GameMain())
{
//#if !DEBUG
#if !DEBUG
try
{
//#endif
#endif
game.Run();
//#if !DEBUG
#if !DEBUG
}
catch (Exception e)
{
CrashDump(game, "crashreport.txt", e);
}
//#endif
#endif
}
}

View File

@@ -203,7 +203,7 @@ namespace Barotrauma
spriteBatch.End();
spriteBatch.Begin(SpriteSortMode.BackToFront,
BlendState.NonPremultiplied,
BlendState.AlphaBlend,
SamplerState.LinearWrap, DepthStencilState.Default, null, null,
cam.Transform);
@@ -249,7 +249,7 @@ namespace Barotrauma
spriteBatch.End();
spriteBatch.Begin(SpriteSortMode.Deferred,
BlendState.NonPremultiplied,
BlendState.AlphaBlend,
null, DepthStencilState.Default, null, null,
cam.Transform);
GameMain.ParticleManager.Draw(spriteBatch, true, Particles.ParticleBlendState.AlphaBlend);
@@ -272,7 +272,7 @@ namespace Barotrauma
spriteBatch.End();
spriteBatch.Begin(SpriteSortMode.Deferred,
BlendState.NonPremultiplied,
BlendState.AlphaBlend,
null, DepthStencilState.DepthRead, null, null,
cam.Transform);
GameMain.ParticleManager.Draw(spriteBatch, false, Particles.ParticleBlendState.AlphaBlend);