Added a button to add entities to storage items (#709)

This commit is contained in:
micheel665
2020-02-17 01:19:35 +02:00
committed by GitHub
parent b2b8021d9b
commit 987a39c25e
3 changed files with 83 additions and 19 deletions

View File

@@ -1,10 +1,12 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using Content.Shared.GameObjects.Components.Storage; using Content.Shared.GameObjects.Components.Storage;
using Content.Client.Interfaces.GameObjects;
using Robust.Client.Interfaces.GameObjects.Components; using Robust.Client.Interfaces.GameObjects.Components;
using Robust.Client.UserInterface; using Robust.Client.UserInterface;
using Robust.Client.UserInterface.Controls; using Robust.Client.UserInterface.Controls;
using Robust.Client.UserInterface.CustomControls; using Robust.Client.UserInterface.CustomControls;
using Robust.Client.Player;
using Robust.Shared.GameObjects; using Robust.Shared.GameObjects;
using Robust.Shared.Interfaces.GameObjects; using Robust.Shared.Interfaces.GameObjects;
using Robust.Shared.Interfaces.Network; using Robust.Shared.Interfaces.Network;
@@ -99,6 +101,7 @@ namespace Content.Client.GameObjects.Components.Storage
private Control VSplitContainer; private Control VSplitContainer;
private VBoxContainer EntityList; private VBoxContainer EntityList;
private Label Information; private Label Information;
private Button AddItemButton;
public ClientStorageComponent StorageEntity; public ClientStorageComponent StorageEntity;
protected override Vector2? CustomSize => (180, 320); protected override Vector2? CustomSize => (180, 320);
@@ -129,6 +132,16 @@ namespace Content.Client.GameObjects.Components.Storage
}; };
listScrollContainer.AddChild(EntityList); listScrollContainer.AddChild(EntityList);
VSplitContainer.AddChild(listScrollContainer); VSplitContainer.AddChild(listScrollContainer);
AddItemButton = new Button
{
Text = "Add Item",
ToggleMode = false,
SizeFlagsHorizontal = SizeFlags.FillExpand
};
AddItemButton.OnPressed += OnAddItemButtonPressed;
VSplitContainer.AddChild(AddItemButton);
Contents.AddChild(VSplitContainer); Contents.AddChild(VSplitContainer);
} }
@@ -191,6 +204,19 @@ namespace Content.Client.GameObjects.Components.Storage
args.Button.Pressed = false; args.Button.Pressed = false;
StorageEntity.Interact(control.EntityuID); StorageEntity.Interact(control.EntityuID);
} }
/// <summary>
/// Function assigned to button that adds items to the storage entity.
/// </summary>
private void OnAddItemButtonPressed(BaseButton.ButtonEventArgs args)
{
var controlledEntity = IoCManager.Resolve<IPlayerManager>().LocalPlayer.ControlledEntity;
if (controlledEntity.TryGetComponent(out IHandsComponent hands))
{
StorageEntity.SendNetworkMessage(new InsertEntityMessage());
}
}
} }
/// <summary> /// <summary>

View File

@@ -3,6 +3,7 @@ using System.Linq;
using Content.Server.GameObjects.Components; using Content.Server.GameObjects.Components;
using Content.Server.GameObjects.Components.Items.Storage; using Content.Server.GameObjects.Components.Items.Storage;
using Content.Server.GameObjects.EntitySystems; using Content.Server.GameObjects.EntitySystems;
using Content.Server.Interfaces.GameObjects;
using Content.Shared.GameObjects.Components.Storage; using Content.Shared.GameObjects.Components.Storage;
using Content.Shared.Interfaces; using Content.Shared.Interfaces;
using Robust.Server.GameObjects; using Robust.Server.GameObjects;
@@ -146,25 +147,8 @@ namespace Content.Server.GameObjects
return false; return false;
} }
if (!eventArgs.User.TryGetComponent(out HandsComponent hands)) return PlayerInsertEntity(eventArgs.User);
{ }
return false;
}
//Check that we can drop the item from our hands first otherwise we obviously cant put it inside
if (CanInsert(hands.GetActiveHand.Owner) && hands.Drop(hands.ActiveIndex))
{
if (Insert(eventArgs.AttackWith))
{
return true;
}
}
else
{
Owner.PopupMessage(eventArgs.User, "Can't insert.");
}
return false;
}
/// <summary> /// <summary>
/// Sends a message to open the storage UI /// Sends a message to open the storage UI
@@ -309,6 +293,22 @@ namespace Content.Server.GameObjects
} }
break; break;
} }
case InsertEntityMessage _:
{
_ensureInitialCalculated();
var playerEntity = _playerManager.GetSessionByChannel(netChannel).AttachedEntity;
var storageTransform = Owner.GetComponent<ITransformComponent>();
var playerTransform = playerEntity.GetComponent<ITransformComponent>();
// TODO: Replace by proper entity range check once it is implemented.
if (playerTransform.GridPosition.InRange(_mapManager,
storageTransform.GridPosition,
InteractionSystem.InteractionRange))
{
PlayerInsertEntity(playerEntity);
}
break;
}
case CloseStorageUIMessage _: case CloseStorageUIMessage _:
{ {
var session = _playerManager.GetSessionByChannel(netChannel); var session = _playerManager.GetSessionByChannel(netChannel);
@@ -356,5 +356,31 @@ namespace Content.Server.GameObjects
Remove(entity); Remove(entity);
} }
} }
/// <summary>
/// Inserts an entity into the storage component from the players active hand.
/// </summary>
private bool PlayerInsertEntity(IEntity player)
{
if (!player.TryGetComponent(out IHandsComponent hands) || hands.GetActiveHand == null)
return false;
var toInsert = hands.GetActiveHand;
if (hands.Drop(toInsert.Owner))
{
if (Insert(toInsert.Owner))
{
return true;
}
else
{
hands.PutInHand(toInsert);
}
}
Owner.PopupMessage(player, "Can't insert.");
return false;
}
} }
} }

View File

@@ -30,6 +30,18 @@ namespace Content.Shared.GameObjects.Components.Storage
} }
} }
/// <summary>
/// Component message for adding an entity to the storage entity.
/// </summary>
[Serializable, NetSerializable]
public class InsertEntityMessage : ComponentMessage
{
public InsertEntityMessage()
{
Directed = true;
}
}
/// <summary> /// <summary>
/// Component message for removing a contained entity from the storage entity /// Component message for removing a contained entity from the storage entity
/// </summary> /// </summary>