More inventory work. APIs implemented.

Still need to do prototype loading and test cases.
This commit is contained in:
PJB3005
2017-09-24 23:19:47 +02:00
parent 66483bdd72
commit 73ae408e20
5 changed files with 148 additions and 8 deletions

View File

@@ -1,4 +1,8 @@
using SS14.Shared.ContentPack; using Content.Server.GameObjects;
using Content.Server.Interfaces.GameObjects;
using SS14.Shared.ContentPack;
using SS14.Shared.Interfaces.GameObjects;
using SS14.Shared.IoC;
namespace Content.Server namespace Content.Server
{ {
@@ -6,7 +10,16 @@ namespace Content.Server
{ {
public override void Init() public override void Init()
{ {
// TODO: Anything at all. var factory = IoCManager.Resolve<IComponentFactory>();
factory.Register<HandsComponent>();
factory.RegisterReference<HandsComponent, IHandsComponent>();
factory.Register<InventoryComponent>();
factory.RegisterReference<InventoryComponent, IInventoryComponent>();
factory.Register<ItemComponent>();
factory.RegisterReference<ItemComponent, IItemComponent>();
} }
} }
} }

View File

@@ -1,11 +1,131 @@
using Content.Server.Interfaces.GameObjects;
using SS14.Shared.GameObjects; using SS14.Shared.GameObjects;
using SS14.Shared.Interfaces.GameObjects; using SS14.Shared.Interfaces.GameObjects;
using System.Collections.Generic; using System.Collections.Generic;
using System;
using System.Linq;
namespace Content.Server.Interfaces.GameObjects namespace Content.Server.GameObjects
{ {
public class HandsComponent : Component, IHandsComponent public class HandsComponent : Component, IHandsComponent
{ {
public override string Name => "Hands"; public override string Name => "Hands";
private string activeIndex;
public string ActiveIndex
{
get => activeIndex;
set
{
if (!hands.ContainsKey(value))
{
throw new ArgumentException($"No hand '{value}'");
}
activeIndex = value;
}
}
private Dictionary<string, IInventorySlot> hands = new Dictionary<string, IInventorySlot>();
private IInventoryComponent inventory;
public override void Initialize()
{
inventory = Owner.GetComponent<IInventoryComponent>();
}
public IEnumerable<IItemComponent> GetAllHands()
{
foreach (var slot in hands.Values)
{
if (slot.Item != null)
{
yield return slot.Item;
}
}
}
public IItemComponent GetHand(string index)
{
var slot = hands[index];
return slot.Item;
}
/// <summary>
/// Enumerates over the hand keys, returning the active hand first.
/// </summary>
private IEnumerable<string> ActivePriorityEnumerable()
{
yield return ActiveIndex;
foreach (var hand in hands.Keys)
{
if (hand == ActiveIndex)
{
continue;
}
yield return hand;
}
}
public bool PutInHand(IItemComponent item)
{
foreach (var hand in ActivePriorityEnumerable())
{
if (PutInHand(item, hand, fallback: false))
{
return true;
}
}
return false;
}
public bool PutInHand(IItemComponent item, string index, bool fallback = true)
{
if (!CanPutInHand(item, index))
{
return fallback && PutInHand(item);
}
var slot = hands[index];
return slot.Owner.Insert(slot.Name, item);
}
public bool CanPutInHand(IItemComponent item)
{
foreach (var hand in ActivePriorityEnumerable())
{
if (CanPutInHand(item, hand))
{
return true;
}
}
return false;
}
public bool CanPutInHand(IItemComponent item, string index)
{
var slot = hands[index];
return slot.Owner.CanInsert(slot.Name, item);
}
public bool Drop(string index)
{
if (!CanDrop(index))
{
return false;
}
var slot = hands[index];
return slot.Owner.Drop(slot.Name);
}
public bool CanDrop(string index)
{
var slot = hands[index];
return slot.Item != null && slot.Owner.CanDrop(slot.Name);
}
} }
} }

View File

@@ -1,12 +1,13 @@
using Content.Server.Interfaces.GameObjects; using Content.Server.Interfaces.GameObjects;
using SS14.Server.GameObjects; using SS14.Server.GameObjects;
using SS14.Server.GameObjects.Components.Container;
using SS14.Server.Interfaces.GameObjects; using SS14.Server.Interfaces.GameObjects;
using SS14.Shared.GameObjects; using SS14.Shared.GameObjects;
using SS14.Shared.Interfaces.GameObjects; using SS14.Shared.Interfaces.GameObjects;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
namespace Content.Server.Interfaces.GameObjects namespace Content.Server.GameObjects
{ {
public class InventoryComponent : Component, IInventoryComponent public class InventoryComponent : Component, IInventoryComponent
{ {
@@ -20,12 +21,18 @@ namespace Content.Server.Interfaces.GameObjects
public override void Initialize() public override void Initialize()
{ {
transform = Owner.GetComponent<TransformComponent>(); transform = Owner.GetComponent<TransformComponent>();
container = Container.Create("inventory", Owner);
base.Initialize(); base.Initialize();
} }
public override void OnRemove() public override void OnRemove()
{ {
foreach (var slot in slots.Keys)
{
RemoveSlot(slot);
}
transform = null; transform = null;
container = null;
base.OnRemove(); base.OnRemove();
} }
@@ -53,7 +60,7 @@ namespace Content.Server.Interfaces.GameObjects
} }
var inventorySlot = _GetSlot(slot); var inventorySlot = _GetSlot(slot);
if (!CanInsert(slot, item)) if (!CanInsert(slot, item) || !container.Insert(item.Owner))
{ {
return false; return false;
} }
@@ -66,7 +73,7 @@ namespace Content.Server.Interfaces.GameObjects
public bool CanInsert(string slot, IItemComponent item) public bool CanInsert(string slot, IItemComponent item)
{ {
var inventorySlot = _GetSlot(slot); var inventorySlot = _GetSlot(slot);
return inventorySlot.Item == null; return inventorySlot.Item == null && container.CanInsert(item.Owner);
} }
public bool Drop(string slot) public bool Drop(string slot)

View File

@@ -3,7 +3,7 @@ using SS14.Shared.GameObjects;
using SS14.Shared.Interfaces.GameObjects; using SS14.Shared.Interfaces.GameObjects;
using System; using System;
namespace Content.Server.Interfaces.GameObjects namespace Content.Server.GameObjects
{ {
public class ItemComponent : Component, IItemComponent public class ItemComponent : Component, IItemComponent
{ {

View File

@@ -8,7 +8,7 @@ namespace Content.Server.Interfaces.GameObjects
/// <summary> /// <summary>
/// The hand index of the currently active hand. /// The hand index of the currently active hand.
/// </summary> /// </summary>
string ActiveIndex { get; } string ActiveIndex { get; set; }
/// <summary> /// <summary>
/// Enumerates over every held item. /// Enumerates over every held item.