Inventories WiP

This commit is contained in:
PJB3005
2017-09-24 21:09:26 +02:00
parent 60ff292f14
commit f6e265bccb
7 changed files with 313 additions and 0 deletions

View File

@@ -56,6 +56,12 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="EntryPoint.cs" /> <Compile Include="EntryPoint.cs" />
<Compile Include="Interfaces\GameObjects\Components\Items\IHandsComponent.cs" />
<Compile Include="Interfaces\GameObjects\Components\Items\IInventoryComponent.cs" />
<Compile Include="Interfaces\GameObjects\Components\Items\IItemComponent.cs" />
<Compile Include="GameObjects\Components\Items\HandsComponent.cs" />
<Compile Include="GameObjects\Components\Items\InventoryComponent.cs" />
<Compile Include="GameObjects\Components\Items\ItemComponent.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

View File

@@ -0,0 +1,11 @@
using SS14.Shared.GameObjects;
using SS14.Shared.Interfaces.GameObjects;
using System.Collections.Generic;
namespace Content.Server.Interfaces.GameObjects
{
public class HandsComponent : Component, IHandsComponent
{
public override string Name => "Hands";
}
}

View File

@@ -0,0 +1,105 @@
using Content.Server.Interfaces.GameObjects;
using SS14.Server.GameObjects;
using SS14.Server.Interfaces.GameObjects;
using SS14.Shared.GameObjects;
using SS14.Shared.Interfaces.GameObjects;
using System;
using System.Collections.Generic;
namespace Content.Server.Interfaces.GameObjects
{
public class InventoryComponent : Component, IInventoryComponent
{
public override string Name => "Inventory";
private Dictionary<string, InventorySlot> slots = new Dictionary<string, InventorySlot>();
private TransformComponent transform;
// TODO: Make this container unique per-slot.
private IContainer container;
public override void Initialize()
{
transform = Owner.GetComponent<TransformComponent>();
base.Initialize();
}
public override void OnRemove()
{
transform = null;
base.OnRemove();
}
public IItemComponent Get(string slot)
{
return _GetSlot(slot).Item;
}
public IInventorySlot GetSlot(string slot)
{
return slots[slot];
}
// Private version that returns our concrete implementation.
private InventorySlot _GetSlot(string slot)
{
return slots[slot];
}
public bool PutInSlot(string slot, IItemComponent item)
{
var inventorySlot = _GetSlot(slot);
if (inventorySlot.Item != null)
{
return false;
}
inventorySlot.Item = item;
item.EquippedToSlot(inventorySlot);
return true;
}
public bool DropSlot(string slot)
{
var inventorySlot = _GetSlot(slot);
var item = inventorySlot.Item;
if (item == null || !container.Remove(item.Owner))
{
return false;
}
item.RemovedFromSlot();
inventorySlot.Item = null;
// TODO: The item should be dropped to the container our owner is in, if any.
var itemTransform = item.Owner.GetComponent<TransformComponent>();
itemTransform.LocalPosition = transform.LocalPosition;
return true;
}
public void AddSlot(string slot)
{
if (slots.ContainsKey(slot))
{
}
}
public void RemoveSlot(string slot)
{
throw new NotImplementedException();
}
private class InventorySlot : IInventorySlot
{
public IItemComponent Item { get; set; }
public string Name { get; }
public IInventoryComponent Owner { get; }
public InventorySlot(string name, IInventoryComponent owner)
{
Name = name;
Owner = owner;
}
}
}
}

View File

@@ -0,0 +1,35 @@
using Content.Server.Interfaces.GameObjects;
using SS14.Shared.GameObjects;
using SS14.Shared.Interfaces.GameObjects;
using System;
namespace Content.Server.Interfaces.GameObjects
{
public class ItemComponent : Component, IItemComponent
{
public override string Name => "Item";
/// <inheritdoc />
public IInventorySlot ContainingSlot { get; private set; }
public void RemovedFromSlot()
{
if (ContainingSlot == null)
{
throw new InvalidOperationException("Item is not in a slot.");
}
ContainingSlot = null;
}
public void EquippedToSlot(IInventorySlot slot)
{
if (ContainingSlot != null)
{
throw new InvalidOperationException("Item is already in a slot.");
}
ContainingSlot = slot;
}
}
}

View File

@@ -0,0 +1,50 @@
using SS14.Shared.Interfaces.GameObjects;
using System.Collections.Generic;
namespace Content.Server.Interfaces.GameObjects
{
public interface IHandsComponent : IComponent
{
/// <summary>
/// The hand index of the currently active hand.
/// </summary>
string ActiveIndex { get; }
/// <summary>
/// Enumerates over every held item.
/// </summary>
IEnumerable<IItemComponent> GetAllHands();
/// <summary>
/// Gets the item held by a hand.
/// </summary>
/// <param name="index">The index of the hand to get.</param>
/// <returns>The item in the held, null if no item is held</returns>
IItemComponent GetHand(string index);
/// <summary>
/// Puts an item into any empty hand, preferring the active hand.
/// </summary>
/// <param name="item">The item to put in a hand.</param>
/// <returns>True if the item was inserted, false otherwise.</returns>
bool PutInHand(IItemComponent item);
/// <summary>
/// Puts an item into a specific hand.
/// </summary>
/// <param name="item">The item to put in the hand.</param>
/// <param name="index">The index of the hand to put the item into.</param>
/// <param name="fallback">
/// If true and the provided hand is full, the method will fall back to <see cref="PutInHand(IItemComponent)" />
/// </param>
/// <returns>True if the item was inserted into a hand, false otherwise.</returns>
bool PutInHand(IItemComponent item, string index, bool fallback=true);
/// <summary>
/// Drops an item on the ground, removing it from the hand.
/// </summary>
/// <param name="index">The hand to drop from.</param>
/// <returns>True if an item was successfully dropped, false otherwise.</returns>
bool Drop(string index);
}
}

View File

@@ -0,0 +1,84 @@
using SS14.Server.Interfaces.GameObjects;
using SS14.Shared.Interfaces.GameObjects;
using System;
namespace Content.Server.Interfaces.GameObjects
{
public interface IInventoryComponent : IComponent
{
/// <summary>
/// Gets the item in the specified slot.
/// </summary>
/// <param name="slot">The slot to get the item for.</param>
/// <returns>Null if the slot is empty, otherwise the item.</returns>
IItemComponent Get(string slot);
/// <summary>
/// Gets the slot with specified name.
/// This gets the slot, NOT the item contained therein.
/// </summary>
/// <param name="slot">The name of the slot to get.</param>
IInventorySlot GetSlot(string slot);
/// <summary>
/// Puts an item in a slot.
/// </summary>
/// <remarks>
/// This will fail if there is already an item in the specified slot.
/// </remarks>
/// <param name="slot">The slot to put the item in.</param>
/// <param name="item">The item to insert into the slot.</param>
/// <returns>True if the item was successfully inserted, false otherwise.</returns>
bool PutInSlot(string slot, IItemComponent item);
/// <summary>
/// Drops the item in a slot.
/// </summary>
/// <param name="slot">The slot to drop the item from.</param>
/// <returns>True if an item was dropped, false otherwise.</returns>
bool DropSlot(string slot);
/// <summary>
/// Adds a new slot to this inventory component.
/// </summary>
/// <param name="slot">The name of the slot to add.</param>
/// <exception cref="InvalidOperationException">
/// Thrown if the slot with specified name already exists.
/// </exception>
void AddSlot(string slot);
/// <summary>
/// Removes a slot from this inventory component.
/// </summary>
/// <remarks>
/// If the slot contains an item, the item is dropped.
/// </remarks>
/// <param name="slot">The name of the slot to remove.</param>
void RemoveSlot(string slot);
/// <summary>
///
/// </summary>
/// <param name="slot"></param>
/// <returns></returns>
bool HasSlot(string slot);
}
public interface IInventorySlot
{
/// <summary>
/// The name of the slot.
/// </summary>
string Name { get; }
/// <summary>
/// The item contained in the slot, can be null.
/// </summary>
IItemComponent Item { get; }
/// <summary>
/// The component owning us.
/// </summary>
IInventoryComponent Owner { get; }
}
}

View File

@@ -0,0 +1,22 @@
using SS14.Shared.Interfaces.GameObjects;
namespace Content.Server.Interfaces.GameObjects
{
public interface IItemComponent : IComponent
{
/// <summary>
/// The inventory slot this item is stored in, if any.
/// </summary>
IInventorySlot ContainingSlot { get; }
/// <summary>
/// Called when the item is removed from its inventory slot.
/// </summary>
void RemovedFromSlot();
/// <summary>
/// Called when the item is inserted into a new inventory slot.
/// </summary>
void EquippedToSlot(IInventorySlot slot);
}
}