From c7de99418304bd14af310bab826680a7508ca22a Mon Sep 17 00:00:00 2001 From: PJB3005 Date: Sat, 30 Sep 2017 12:17:45 +0200 Subject: [PATCH] Item pickup seems to work, crash due to netcode. --- .../Components/Items/HandsComponent.cs | 48 ++++++++++++++++++- Resources/Prototypes/Entities/Items.yml | 7 +++ 2 files changed, 53 insertions(+), 2 deletions(-) create mode 100644 Resources/Prototypes/Entities/Items.yml diff --git a/Content.Server/GameObjects/Components/Items/HandsComponent.cs b/Content.Server/GameObjects/Components/Items/HandsComponent.cs index f384741ae1..f5f20970b9 100644 --- a/Content.Server/GameObjects/Components/Items/HandsComponent.cs +++ b/Content.Server/GameObjects/Components/Items/HandsComponent.cs @@ -1,6 +1,7 @@ -using Content.Server.Interfaces.GameObjects; +using Content.Server.Interfaces.GameObjects; using Content.Shared.GameObjects; using SS14.Server.GameObjects.Events; +using SS14.Server.Interfaces.GameObjects; using SS14.Shared; using SS14.Shared.GameObjects; using SS14.Shared.Interfaces.GameObjects; @@ -32,11 +33,16 @@ namespace Content.Server.GameObjects private Dictionary hands = new Dictionary(); private List orderedHands = new List(); private IInventoryComponent inventory; + private IServerTransformComponent transform; private YamlMappingNode tempParametersMapping; + // Mostly arbitrary. + public const float PICKUP_RANGE = 2; + public override void Initialize() { inventory = Owner.GetComponent(); + transform = Owner.GetComponent(); if (tempParametersMapping != null) { foreach (var node in tempParametersMapping.GetNode("hands")) @@ -46,12 +52,15 @@ namespace Content.Server.GameObjects } Owner.SubscribeEvent(OnKeyChange, this); + Owner.SubscribeEvent(OnClick, this); base.Initialize(); } public override void OnRemove() { inventory = null; + Owner.UnsubscribeEvent(this); + Owner.UnsubscribeEvent(this); base.OnRemove(); } @@ -165,6 +174,10 @@ namespace Content.Server.GameObjects var slot = inventory.AddSlot(HandSlotName(index)); hands[index] = slot; orderedHands.Add(index); + if (ActiveIndex == null) + { + ActiveIndex = index; + } } public void RemoveHand(string index) @@ -218,11 +231,24 @@ namespace Content.Server.GameObjects public void OnKeyChange(object sender, EntityEventArgs uncast) { var cast = (BoundKeyChangeEventArgs)uncast; - if (cast.Actor != Owner || cast.KeyFunction != BoundKeyFunctions.SwitchHands || cast.KeyState != BoundKeyState.Down) + if (cast.Actor != Owner || cast.KeyState != BoundKeyState.Down) { return; } + switch (cast.KeyFunction) + { + case BoundKeyFunctions.SwitchHands: + SwapHands(); + break; + case BoundKeyFunctions.Drop: + Drop(ActiveIndex); + break; + } + } + + private void SwapHands() + { var index = orderedHands.FindIndex(x => x == ActiveIndex); index++; if (index >= orderedHands.Count) @@ -232,5 +258,23 @@ namespace Content.Server.GameObjects ActiveIndex = orderedHands[index]; } + + public void OnClick(object sender, EntityEventArgs uncast) + { + var cast = (ClickedOnEntityEventArgs)uncast; + if (cast.MouseButton != MouseClickType.Left || Owner.EntityManager.GetEntity(cast.Clicker) != Owner) + { + return; + } + + var target = Owner.EntityManager.GetEntity(cast.Clicked); + var targetTransform = target.GetComponent(); + if (!target.TryGetComponent(out var item) || (targetTransform.WorldPosition - transform.WorldPosition).Length > PICKUP_RANGE) + { + return; + } + + PutInHand(item, ActiveIndex, fallback: false); + } } } diff --git a/Resources/Prototypes/Entities/Items.yml b/Resources/Prototypes/Entities/Items.yml new file mode 100644 index 0000000000..c531503e36 --- /dev/null +++ b/Resources/Prototypes/Entities/Items.yml @@ -0,0 +1,7 @@ +- type: entity + name: "Toolbox 2: Handle edition" + parent: Toolbox + id: ToolboxItem + components: + - type: Item +