diff --git a/Content.Server/EntryPoint.cs b/Content.Server/EntryPoint.cs index 6483e6b6f4..c8c527c982 100644 --- a/Content.Server/EntryPoint.cs +++ b/Content.Server/EntryPoint.cs @@ -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 { @@ -6,7 +10,16 @@ namespace Content.Server { public override void Init() { - // TODO: Anything at all. + var factory = IoCManager.Resolve(); + + factory.Register(); + factory.RegisterReference(); + + factory.Register(); + factory.RegisterReference(); + + factory.Register(); + factory.RegisterReference(); } } } diff --git a/Content.Server/GameObjects/Components/Items/HandsComponent.cs b/Content.Server/GameObjects/Components/Items/HandsComponent.cs index 0dc7a00e86..371cab4818 100644 --- a/Content.Server/GameObjects/Components/Items/HandsComponent.cs +++ b/Content.Server/GameObjects/Components/Items/HandsComponent.cs @@ -1,11 +1,131 @@ +using Content.Server.Interfaces.GameObjects; using SS14.Shared.GameObjects; using SS14.Shared.Interfaces.GameObjects; using System.Collections.Generic; +using System; +using System.Linq; -namespace Content.Server.Interfaces.GameObjects +namespace Content.Server.GameObjects { public class HandsComponent : Component, IHandsComponent { 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 hands = new Dictionary(); + private IInventoryComponent inventory; + + public override void Initialize() + { + inventory = Owner.GetComponent(); + } + + public IEnumerable 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; + } + + /// + /// Enumerates over the hand keys, returning the active hand first. + /// + private IEnumerable 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); + } } } diff --git a/Content.Server/GameObjects/Components/Items/InventoryComponent.cs b/Content.Server/GameObjects/Components/Items/InventoryComponent.cs index db4f994636..6e206d311a 100644 --- a/Content.Server/GameObjects/Components/Items/InventoryComponent.cs +++ b/Content.Server/GameObjects/Components/Items/InventoryComponent.cs @@ -1,12 +1,13 @@ using Content.Server.Interfaces.GameObjects; using SS14.Server.GameObjects; +using SS14.Server.GameObjects.Components.Container; 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 +namespace Content.Server.GameObjects { public class InventoryComponent : Component, IInventoryComponent { @@ -20,12 +21,18 @@ namespace Content.Server.Interfaces.GameObjects public override void Initialize() { transform = Owner.GetComponent(); + container = Container.Create("inventory", Owner); base.Initialize(); } public override void OnRemove() { + foreach (var slot in slots.Keys) + { + RemoveSlot(slot); + } transform = null; + container = null; base.OnRemove(); } @@ -53,7 +60,7 @@ namespace Content.Server.Interfaces.GameObjects } var inventorySlot = _GetSlot(slot); - if (!CanInsert(slot, item)) + if (!CanInsert(slot, item) || !container.Insert(item.Owner)) { return false; } @@ -66,7 +73,7 @@ namespace Content.Server.Interfaces.GameObjects public bool CanInsert(string slot, IItemComponent item) { var inventorySlot = _GetSlot(slot); - return inventorySlot.Item == null; + return inventorySlot.Item == null && container.CanInsert(item.Owner); } public bool Drop(string slot) diff --git a/Content.Server/GameObjects/Components/Items/ItemComponent.cs b/Content.Server/GameObjects/Components/Items/ItemComponent.cs index 0571ab8492..efebe59f4c 100644 --- a/Content.Server/GameObjects/Components/Items/ItemComponent.cs +++ b/Content.Server/GameObjects/Components/Items/ItemComponent.cs @@ -3,7 +3,7 @@ using SS14.Shared.GameObjects; using SS14.Shared.Interfaces.GameObjects; using System; -namespace Content.Server.Interfaces.GameObjects +namespace Content.Server.GameObjects { public class ItemComponent : Component, IItemComponent { diff --git a/Content.Server/Interfaces/GameObjects/Components/Items/IHandsComponent.cs b/Content.Server/Interfaces/GameObjects/Components/Items/IHandsComponent.cs index d5ff2c1bec..52a63baf05 100644 --- a/Content.Server/Interfaces/GameObjects/Components/Items/IHandsComponent.cs +++ b/Content.Server/Interfaces/GameObjects/Components/Items/IHandsComponent.cs @@ -8,7 +8,7 @@ namespace Content.Server.Interfaces.GameObjects /// /// The hand index of the currently active hand. /// - string ActiveIndex { get; } + string ActiveIndex { get; set; } /// /// Enumerates over every held item.