Skip to content

Instantly share code, notes, and snippets.

@valkyrienyanko
Last active February 10, 2023 07:34
Show Gist options
  • Save valkyrienyanko/2423495be3d9d22f3df4aa86a3eb01a2 to your computer and use it in GitHub Desktop.
Save valkyrienyanko/2423495be3d9d22f3df4aa86a3eb01a2 to your computer and use it in GitHub Desktop.
// Transfer the item in the inventory slot we are currently hovering over
private void TransferItem()
{
// Do not transfer item if this item is currently being animated
if (CurrentlyAnimating)
return;
// There is no item here thus no item to transfer
if (InventoryItem == null)
return;
// Get the 'other inventory'
var targetInv = GetOtherInventory();
// No 'other inventory' is open, lets use the player inventory so the
// item gets transfered to the same inventory instead of doing nothing
if (targetInv == null)
targetInv = Player.Inventory;
// Try to find a empty slot in the target inventory
var emptySlot = targetInv.TryGetEmptyOrSameTypeSlot(InventoryItem.Item.Type);
// A empty slot was found!
if (emptySlot != -1)
{
// Store temporary reference to the item in this inventory slot
var itemRef = InventoryItem.Item;
// Get a copy of the item sprite that is being transfered
// This is purely for visuals and does not effect the item logic
var graphic = InventoryItem.GenerateGraphic();
graphic.GlobalPosition = Position;
// Get the other slot this item is being transfered to
var otherInvSlot = targetInv.InventorySlots[emptySlot];
//if (otherInvSlot.CurrentlyAnimating)
// return;
// Remove the item before it gets transfered
this.RemoveItem();
var otherInvSlotItem = otherInvSlot.InventoryItem;
var hide = false;
// If the other inventory slot has no item in it, then hide the item that
// gets transfered over. So it does not look like there is a duplicate
// when the graphic sprite is animated over
if (otherInvSlot.InventoryItem == null)
hide = true;
// Set the other inventory slot item to the item that is being transfered over
if (otherInvSlotItem == null)
otherInvSlot.SetItem(itemRef);
else
// If the item transfered has more than one than add that
{
itemRef.Count += otherInvSlotItem.Item.Count;
otherInvSlot.SetItem(itemRef);
}
// Hide the other item
if (hide)
otherInvSlot.InventoryItem.Hide();
// Start animation
CurrentlyAnimating = true;
otherInvSlot.CurrentlyAnimating = true;
// Add graphic to the world
Main.AddToCanvasLayer(graphic);
var tween = Panel.GetTree().CreateTween();
tween.TweenProperty(graphic, "global_position", otherInvSlot.Position, 1)
.SetEase(Tween.EaseType.Out)
.SetTrans(Tween.TransitionType.Cubic);
tween.TweenCallback(Callable.From(() =>
{
// Sprite graphic reached its destination, lets show the other inventory slot item now
otherInvSlot.InventoryItem.Show();
CurrentlyAnimating = false;
otherInvSlot.CurrentlyAnimating = false;
graphic.QueueFree();
}));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment