Add smart equip shortcuts (#873)

This commit is contained in:
Hugal31
2020-05-05 00:39:15 +02:00
committed by GitHub
parent 8e516038fc
commit fbaafa8366
8 changed files with 86 additions and 3 deletions

View File

@@ -1,9 +1,14 @@
using System;
using System.Linq;
using Content.Server.GameObjects;
using Content.Server.GameObjects.Components;
using Content.Server.GameObjects.Components.Stack;
using Content.Server.Interfaces;
using Content.Server.Interfaces.GameObjects;
using Content.Server.Throw;
using Content.Shared.GameObjects.Components.Inventory;
using Content.Shared.Input;
using Content.Shared.Interfaces;
using Content.Shared.Physics;
using JetBrains.Annotations;
using Robust.Server.GameObjects;
@@ -19,6 +24,7 @@ using Robust.Shared.Interfaces.Map;
using Robust.Shared.Interfaces.Physics;
using Robust.Shared.Interfaces.Timing;
using Robust.Shared.IoC;
using Robust.Shared.Localization;
using Robust.Shared.Log;
using Robust.Shared.Map;
using Robust.Shared.Maths;
@@ -33,6 +39,7 @@ namespace Content.Server.GameObjects.EntitySystems
#pragma warning disable 649
[Dependency] private readonly IMapManager _mapManager;
[Dependency] private readonly IEntitySystemManager _entitySystemManager;
[Dependency] private readonly IServerNotifyManager _notifyManager;
#pragma warning restore 649
private const float ThrowForce = 1.5f; // Throwing force of mobs in Newtons
@@ -50,6 +57,8 @@ namespace Content.Server.GameObjects.EntitySystems
input.BindMap.BindFunction(ContentKeyFunctions.Drop, new PointerInputCmdHandler(HandleDrop));
input.BindMap.BindFunction(ContentKeyFunctions.ActivateItemInHand, InputCmdHandler.FromDelegate(HandleActivateItem));
input.BindMap.BindFunction(ContentKeyFunctions.ThrowItemInHand, new PointerInputCmdHandler(HandleThrowItem));
input.BindMap.BindFunction(ContentKeyFunctions.SmartEquipBackpack, InputCmdHandler.FromDelegate(HandleSmartEquipBackpack));
input.BindMap.BindFunction(ContentKeyFunctions.SmartEquipBelt, InputCmdHandler.FromDelegate(HandleSmartEquipBelt));
}
/// <inheritdoc />
@@ -190,5 +199,53 @@ namespace Content.Server.GameObjects.EntitySystems
return true;
}
private void HandleSmartEquipBackpack(ICommonSession session)
{
HandleSmartEquip(session, EquipmentSlotDefines.Slots.BACKPACK);
}
private void HandleSmartEquipBelt(ICommonSession session)
{
HandleSmartEquip(session, EquipmentSlotDefines.Slots.BELT);
}
private void HandleSmartEquip(ICommonSession session, EquipmentSlotDefines.Slots equipementSlot)
{
var plyEnt = ((IPlayerSession) session).AttachedEntity;
if (plyEnt == null || !plyEnt.IsValid())
return;
if (!plyEnt.TryGetComponent(out HandsComponent handsComp) || !plyEnt.TryGetComponent(out InventoryComponent inventoryComp))
return;
if (!inventoryComp.TryGetSlotItem(equipementSlot, out ItemComponent equipmentItem)
|| !equipmentItem.Owner.TryGetComponent<ServerStorageComponent>(out var storageComponent))
{
_notifyManager.PopupMessage(plyEnt, plyEnt, Loc.GetString("You have no {0} to take something out of!", EquipmentSlotDefines.SlotNames[equipementSlot].ToLower()));
return;
}
var heldItem = handsComp.GetHand(handsComp.ActiveIndex)?.Owner;
if (heldItem != null)
{
storageComponent.PlayerInsertEntity(plyEnt);
}
else
{
if (storageComponent.StoredEntities.Count == 0)
{
_notifyManager.PopupMessage(plyEnt, plyEnt, Loc.GetString("There's nothing in your {0} to take out!", EquipmentSlotDefines.SlotNames[equipementSlot].ToLower()));
}
else
{
var lastStoredEntity = Enumerable.Last(storageComponent.StoredEntities);
if (storageComponent.Remove(lastStoredEntity))
handsComp.PutInHandOrDrop(lastStoredEntity.GetComponent<ItemComponent>());
}
}
}
}
}