/// <summary> /// Equips slothing to the specified slot. /// </summary> /// <remarks> /// This will fail if there is already an item in the specified slot. /// </remarks> /// <param name="slot">The slot to put the item in.</param> /// <param name="clothing">The item to insert into the slot.</param> /// <returns>True if the item was successfully inserted, false otherwise.</returns> public bool Equip(Slots slot, ClothingComponent clothing) { if (clothing == null) { throw new ArgumentNullException(nameof(clothing), "Clothing must be passed here. To remove some clothing from a slot, use Unequip()"); } if (clothing.SlotFlags == SlotFlags.PREVENTEQUIP || //Flag to prevent equipping at all (clothing.SlotFlags & SlotMasks[slot]) == 0) //Does the clothing flag have any of our requested slot flags { return(false); } var inventorySlot = SlotContainers[slot]; if (!inventorySlot.Insert(clothing.Owner)) { return(false); } clothing.EquippedToSlot(inventorySlot); var UIupdatemessage = new ServerInventoryMessage() { Inventoryslot = slot, EntityUid = clothing.Owner.Uid, Updatetype = ServerInventoryUpdate.Addition }; SendNetworkMessage(UIupdatemessage); return(true); }
/// <summary> /// Drops the item in a slot. /// </summary> /// <param name="slot">The slot to drop the item from.</param> /// <returns>True if an item was dropped, false otherwise.</returns> public bool Unequip(Slots slot) { if (!CanUnequip(slot)) { return(false); } var inventorySlot = SlotContainers[slot]; var item = inventorySlot.ContainedEntity.GetComponent <ItemComponent>(); if (!inventorySlot.Remove(inventorySlot.ContainedEntity)) { return(false); } var UIupdatemessage = new ServerInventoryMessage() { Inventoryslot = slot, EntityUid = item.Owner.Uid, Updatetype = ServerInventoryUpdate.Removal }; SendNetworkMessage(UIupdatemessage); item.RemovedFromSlot(); // TODO: The item should be dropped to the container our owner is in, if any. var itemTransform = item.Owner.GetComponent <TransformComponent>(); itemTransform.LocalPosition = Owner.GetComponent <TransformComponent>().LocalPosition; return(true); }
/// <summary> /// Adds the item we have equipped to the slot texture and prepares the slot button for removal /// </summary> /// <param name="message"></param> public void AddToSlot(ServerInventoryMessage message) { InventoryButton button = InventorySlots[message.Inventoryslot]; var entity = IoCManager.Resolve <IEntityManager>().GetEntity(message.EntityUid); button.EntityUid = message.EntityUid; var container = button.GetChild("CenterContainer"); button.GetChild <Button>("Button").OnPressed += RemoveFromInventory; button.GetChild <Button>("Button").OnPressed -= AddToInventory; //Gets entity sprite and assigns it to button texture if (entity.TryGetComponent(out IconComponent sprite)) { var tex = sprite.Icon.Default; var rect = button.GetChild("CenterContainer").GetChild <TextureRect>("TextureRect"); if (tex != null) { rect.Texture = tex; rect.Scale = new Vector2(Math.Min(CalculateMinimumSize().X, 32) / tex.Height, Math.Min(CalculateMinimumSize().Y, 32) / tex.Height); } else { throw new NotImplementedException(); } } }
/// <summary> /// Remove element from the UI and update its button to blank texture and prepare for insertion again /// </summary> /// <param name="message"></param> public void RemoveFromSlot(ServerInventoryMessage message) { InventoryButton button = InventorySlots[message.Inventoryslot]; button.GetChild("CenterContainer").GetChild <TextureRect>("TextureRect").Texture = null; button.EntityUid = EntityUid.Invalid; button.GetChild <Button>("Button").OnPressed -= RemoveFromInventory; button.GetChild <Button>("Button").OnPressed += AddToInventory; }