Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Abstract item animations to every item movement #6

Open
1 of 9 tasks
valkyrienyanko opened this issue Feb 10, 2023 · 0 comments
Open
1 of 9 tasks

Abstract item animations to every item movement #6

valkyrienyanko opened this issue Feb 10, 2023 · 0 comments
Labels
help wanted Extra attention is needed

Comments

@valkyrienyanko
Copy link
Contributor

valkyrienyanko commented Feb 10, 2023

Currently item animations are only implemented for when Shift + Clicking to transfer items from one inventory to another. This should eventually be abstracted to all inventory manipulations.

Item animations done so far

  • Shift + Click
  • Double Click
  • Left Click
  • Right Click
  • Hold + Left Click
  • Hold + Right Click
  • 1 2 3 ... 7 8 9
  • Shift + R
  • Shift + T

For reference here is the code for transferring a item.

// 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);
// No empty slot was found!
if (emptySlot == -1)
return;
// 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
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);
}
OtherInventorySlot = otherInvSlot;
// Hide the other item
if (hide)
otherInvSlot.InventoryItem.Hide();
// Start animation
CurrentlyAnimating = true;
otherInvSlot.CurrentlyAnimating = true;
// Add graphic to the world
Main.AddToCanvasLayer(Graphic);
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
// For the love of god I have no fucking idea why I need a
// null check here. I spent 2 hours trying to figure out
// why InventoryItem becomes null. And with the null check
// earlier it was just hiding items. But now it all magically
// works all of a sudden wtf?
// Please just stay working okay?
// Thank you code
// I love you code
otherInvSlot.InventoryItem?.Show();
CurrentlyAnimating = false;
otherInvSlot.CurrentlyAnimating = false;
Graphic.QueueFree();
}));
}

And here is the code a chest calls when it is closed. (relevant because the chest cancels all the animations on closing the chest -- perhaps this code should be moved else where)

public void Close()
{
IsOpen = false;
// Clean up item animations
foreach (var slot in Inventory.OtherInventory.InventorySlots)
slot.ResetCleanUpAnimations();
foreach (var slot in Player.Inventory.InventorySlots)
slot.ResetCleanUpAnimations();
Inventory.OtherInventory = null;
Inventory.Close();
if (!Player.Inventory.IsHotbar)
Player.Inventory.SwitchToHotbarAnimated();
AnimatedSprite2D.Play("close");
ItemPanelDescription.Clear();
}

@valkyrienyanko valkyrienyanko added the help wanted Extra attention is needed label Feb 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

1 participant