Fixed a pickup bug and a pivot bug

This commit is contained in:
tonyomendoza 2023-02-15 00:17:26 -08:00
parent dbcd1a11e0
commit 45397922df
5 changed files with 80 additions and 64 deletions

View File

@ -99,6 +99,7 @@ namespace SimpleInventorySystem
public GameObject ReferencedGrid; public GameObject ReferencedGrid;
[HideInInspector] public SlotUI slot; [HideInInspector] public SlotUI slot;
[HideInInspector] public GridUI grid; [HideInInspector] public GridUI grid;
public bool lootingGrid = false;
public TagSlotGridPairing() public TagSlotGridPairing()
{ {
@ -167,13 +168,13 @@ namespace SimpleInventorySystem
//List<GridUI.Cell> cells = new List<GridUI.Cell>(); //List<GridUI.Cell> cells = new List<GridUI.Cell>();
//bool canMoveInCells = grid.CanMoveInCells(item, slot, cells); //bool canMoveInCells = grid.CanMoveInCells(item, slot, cells);
bool dropped = CanStore(item) && grid.DropItemOnGrid(item.gameObject); bool dropped = CanStore(item) && grid.DropItemOnGrid(item.gameObject);
Debug.Log("IS:Dropped:" + dropped); //Debug.Log("IS:Dropped:" + dropped);
return dropped; return dropped;
} }
protected bool DropOntoSlot(ItemUI item) protected bool DropOntoSlot(ItemUI item)
{ {
bool dropped = CanStore(item) && slot.DropOntoSlot(item.gameObject); bool dropped = CanStore(item) && slot.DropOntoSlot(item.gameObject);
Debug.Log("IS:Dropped:" + dropped); //Debug.Log("IS:Dropped:" + dropped);
return dropped; return dropped;
} }

View File

@ -113,10 +113,10 @@ namespace SimpleInventorySystem
{ {
if (itemUI.Equip()) if (itemUI.Equip())
{ {
itemUI.transform.localRotation = Quaternion.Euler(new Vector3()); Debug.Log("Pickup: Equipped");
itemUI.transform.localScale = new Vector3(1, 1, 1); itemUI.FixLocalRotation();
itemUI.FixLocalScale();
HadPickedUp = true; HadPickedUp = true;
//Debug.Log("Pickup: Equipped");
// TODO: Handle changing transform here // TODO: Handle changing transform here
//ItemGameObject.GetComponent<>; //ItemGameObject.GetComponent<>;
@ -133,14 +133,14 @@ namespace SimpleInventorySystem
} }
else if (itemUI.Store()) else if (itemUI.Store())
{ {
//Debug.Log("Pickup: Stored"); Debug.Log("Pickup: Stored");
itemUI.transform.localRotation = Quaternion.Euler(new Vector3()); itemUI.FixLocalRotation();
itemUI.transform.localScale = new Vector3(1, 1, 1); itemUI.FixLocalScale();
HadPickedUp = true; HadPickedUp = true;
if (InventorySystem.instance.player.AttachItemGameObject(ItemGameObject, itemUI.ItemTag, itemUI)) if (InventorySystem.instance.player.AttachItemGameObject(ItemGameObject, itemUI.ItemTag, itemUI))
{ {
//Debug.Log("Attached"); Debug.Log("Attached");
ItemRigidBody.useGravity = false; ItemRigidBody.useGravity = false;
ItemRigidBody.isKinematic = true; ItemRigidBody.isKinematic = true;
pickUpCollider.enabled = false; pickUpCollider.enabled = false;
@ -149,7 +149,7 @@ namespace SimpleInventorySystem
} }
else else
{ {
//Debug.Log("Could not pick up"); Debug.Log("Could not pick up");
} }
// assume there is no tsp, then try to drop in most available backpack slot // assume there is no tsp, then try to drop in most available backpack slot
/*else if (PlaceInContainerOfItemInSlot != null) /*else if (PlaceInContainerOfItemInSlot != null)

View File

@ -43,23 +43,16 @@ namespace SimpleInventorySystem
[HideInInspector] public StorageWindowUI StorageWindow; [HideInInspector] public StorageWindowUI StorageWindow;
[HideInInspector] public LimitStackSlotCapacity limitStackSlotCapacity; [HideInInspector] public LimitStackSlotCapacity limitStackSlotCapacity;
internal void ResetTransform()
{
RectTransform rect = GetComponent<RectTransform>();
rect.anchorMin = rect.anchorMax = rect.pivot = new Vector2(0, 1);
rect.anchoredPosition = new Vector2();
imageSizeOnGrid = new Size(width * InventorySystem.instance.DefaultSizeOnGrid.Width, height * InventorySystem.instance.DefaultSizeOnGrid.Height);
rect.sizeDelta = new Vector2(imageSizeOnGrid.Width, imageSizeOnGrid.Height);
}
[HideInInspector] public PickUp pickUp; [HideInInspector] public PickUp pickUp;
[HideInInspector] public TargetAttachment targetAttachment; [HideInInspector] public TargetAttachment targetAttachment;
// private variables // private variables
private float eulerAngle; private float eulerAngle;
public float EulerAngle { get { return eulerAngle; } }
private Vector3 pivot; private Vector3 pivot;
private Vector2 oldImageSizeDelta; private Vector2 oldImageSizeDelta;
private Vector2 oldSizeDelta; private Vector2 oldSizeDelta;
private RectTransform rectTransform; private RectTransform rectTransform;
public RectTransform RectTransform { get { return rectTransform; } }
private CanvasGroup canvasGroup; private CanvasGroup canvasGroup;
private float scale = 1f; private float scale = 1f;
private SlotUI slot; private SlotUI slot;
@ -113,9 +106,57 @@ namespace SimpleInventorySystem
} }
} }
public void FixLocalScale()
{
rectTransform.localScale = new Vector3(1, 1, 1);
}
public void FixLocalRotation()
{
rectTransform.localRotation = Quaternion.Euler(new Vector3(0, 0, EulerAngle));
}
public bool PointerIsHoveredOver { get { return InventorySystem.IsMouseOverItemUITop(this); } } public bool PointerIsHoveredOver { get { return InventorySystem.IsMouseOverItemUITop(this); } }
//public Item item; //public Item item;
private void InitializeRectTransform()
{
rectTransform.rotation = Quaternion.Euler(new Vector3(0,0,0));
eulerAngle = Mathf.Abs(Mathf.CeilToInt(transform.rotation.eulerAngles.z));
switch (eulerAngle)
{
case -90:
imageOrientation = (height > width) ? Orientation.Portrait : Orientation.Landscape;
rotationIndex = 1;
break;
case 180:
imageOrientation = (width > height) ? Orientation.Landscape : Orientation.Portrait;
rotationIndex = 2;
break;
case 90:
imageOrientation = (height > width) ? Orientation.Portrait : Orientation.Landscape;
rotationIndex = 3;
break;
default:
imageOrientation = (width > height) ? Orientation.Landscape : Orientation.Portrait;
rotationIndex = 0;
break;
}
rectTransform.anchorMin = rectTransform.anchorMax = new Vector2(0, 1);
pivot = rectTransform.pivot = new Vector2(0, 1);
}
internal void ResetTransform()
{
rectTransform = GetComponent<RectTransform>();
rectTransform.anchorMin = rectTransform.anchorMax = new Vector2(0, 1);
// rect.pivot = new Vector2(0, 1);
rectTransform.anchoredPosition = new Vector2();
imageSizeOnGrid = new Size(width * InventorySystem.instance.DefaultSizeOnGrid.Width, height * InventorySystem.instance.DefaultSizeOnGrid.Height);
rectTransform.sizeDelta = new Vector2(imageSizeOnGrid.Width, imageSizeOnGrid.Height);
}
private void Awake() private void Awake()
{ {
@ -148,29 +189,7 @@ namespace SimpleInventorySystem
Debug.LogError("Inventory ItemUI must contain a positive integer value for Width and Height: " + name); Debug.LogError("Inventory ItemUI must contain a positive integer value for Width and Height: " + name);
} }
/*eulerAngle = Mathf.Abs(Mathf.CeilToInt(transform.rotation.eulerAngles.z)); InitializeRectTransform();
switch (eulerAngle)
{
case -90:
imageOrientation = (height > width) ? Orientation.Portrait : Orientation.Landscape;
break;
case 180:
imageOrientation = (width > height) ? Orientation.Landscape : Orientation.Portrait;
break;
case 90:
imageOrientation = (height > width) ? Orientation.Portrait : Orientation.Landscape;
break;
default:
imageOrientation = (width > height) ? Orientation.Landscape : Orientation.Portrait;
break;
}*/
imageOrientation = (width > height) ? Orientation.Landscape : Orientation.Portrait;
rotationIndex = 0;
//rectTransform.pivot = pivot;
//InstantiateItem();
} }
public Rarity GetRarity() public Rarity GetRarity()
@ -226,24 +245,11 @@ namespace SimpleInventorySystem
} }
} }
public void InstantiateItem()
{
//if (item == null && itemPrefab != null)
//{
// item = Instantiate(itemPrefab);
//}
}
public SlotUI GetSlotUI() public SlotUI GetSlotUI()
{ {
return slot; return slot;
} }
public void OnEndDrag(PointerEventData eventData)
{
//rectTransform.localPosition = new Vector3(0, 0);
}
public bool QuickMove() public bool QuickMove()
{ {
bool moved = false; bool moved = false;
@ -265,10 +271,10 @@ namespace SimpleInventorySystem
bool equipped = false; bool equipped = false;
foreach (TagSlotPairing tsp in InventorySystem.instance.TagSlotPairings) foreach (TagSlotPairing tsp in InventorySystem.instance.TagSlotPairings)
{ {
//Debug.Log($"Equip: {tsp.itemTag}, {tsp.slot.gameObject.name}"); Debug.Log($"Equip: {tsp.itemTag}, {tsp.slot.gameObject.name}");
if (tsp.slot == slot) if (tsp.slot == slot)
{ {
//Debug.Log("Could not equip, storing"); Debug.Log("Could not equip to slot since this item is already occupying the slot.");
continue; continue;
} }
@ -299,7 +305,7 @@ namespace SimpleInventorySystem
bool stored = false; bool stored = false;
foreach (TagSlotGridPairing tsgp in InventorySystem.instance.TagSlotGridPairings) foreach (TagSlotGridPairing tsgp in InventorySystem.instance.TagSlotGridPairings)
{ {
//Debug.Log($"Store: {tsgp.itemTag}, {tsgp.ReferencedGrid}"); Debug.Log($"Store: {tsgp.itemTag}, {tsgp.ReferencedGrid}");
if (tsgp.CanStore(this)) if (tsgp.CanStore(this))
{ {
GrabAndDragItem(); // TODO: See if this conflicts with anything GrabAndDragItem(); // TODO: See if this conflicts with anything
@ -382,8 +388,8 @@ namespace SimpleInventorySystem
{ {
ApplyRectTransformResize(); // TODO: Fix this so it maintains rotation later ApplyRectTransformResize(); // TODO: Fix this so it maintains rotation later
transform.localRotation = Quaternion.Euler(new Vector3(0, 0, eulerAngle)); transform.localRotation = Quaternion.Euler(new Vector3(0, 0, eulerAngle));
rectTransform.pivot = pivot;
} }
rectTransform.pivot = pivot;
} }
public void ApplyRectTransformResize() public void ApplyRectTransformResize()
@ -569,10 +575,6 @@ namespace SimpleInventorySystem
overrideSortingCanvas.sortingLayerID = 5; overrideSortingCanvas.sortingLayerID = 5;
//overrideSortingCanvas.sortingLayerName = "ItemUI"; //overrideSortingCanvas.sortingLayerName = "ItemUI";
}*/ }*/
eulerAngle = 0;
transform.localRotation = Quaternion.Euler(0, 0, eulerAngle);
pivot = rectTransform.pivot = new Vector2(0, 1);
} }
public void InitializeImageSize() public void InitializeImageSize()

View File

@ -210,6 +210,9 @@ namespace SimpleInventorySystem
{ {
HideImage(); HideImage();
} }
else
{
}
foreach (GroupSlotUI groupSlot in groupSlots) foreach (GroupSlotUI groupSlot in groupSlots)
{ {

View File

@ -56829,6 +56829,11 @@ PrefabInstance:
propertyPath: m_LocalEulerAnglesHint.z propertyPath: m_LocalEulerAnglesHint.z
value: 0 value: 0
objectReference: {fileID: 0} objectReference: {fileID: 0}
- target: {fileID: 2770326605790841687, guid: 9f005685dc17f4643b4a1156970fe2f6,
type: 3}
propertyPath: TagSlotGridPairings.Array.data[6].lootingGrid
value: 1
objectReference: {fileID: 0}
- target: {fileID: 2770326605790841688, guid: 9f005685dc17f4643b4a1156970fe2f6, - target: {fileID: 2770326605790841688, guid: 9f005685dc17f4643b4a1156970fe2f6,
type: 3} type: 3}
propertyPath: pauseMenu propertyPath: pauseMenu
@ -56997,7 +57002,7 @@ PrefabInstance:
- target: {fileID: 2770326606141421692, guid: 9f005685dc17f4643b4a1156970fe2f6, - target: {fileID: 2770326606141421692, guid: 9f005685dc17f4643b4a1156970fe2f6,
type: 3} type: 3}
propertyPath: m_AnchoredPosition.y propertyPath: m_AnchoredPosition.y
value: -0.00092929165 value: -0.00087603735
objectReference: {fileID: 0} objectReference: {fileID: 0}
- target: {fileID: 2770326606150301029, guid: 9f005685dc17f4643b4a1156970fe2f6, - target: {fileID: 2770326606150301029, guid: 9f005685dc17f4643b4a1156970fe2f6,
type: 3} type: 3}
@ -57444,6 +57449,11 @@ PrefabInstance:
propertyPath: m_SizeDelta.y propertyPath: m_SizeDelta.y
value: 0 value: 0
objectReference: {fileID: 0} objectReference: {fileID: 0}
- target: {fileID: 8600850533622123873, guid: 9f005685dc17f4643b4a1156970fe2f6,
type: 3}
propertyPath: FitItemImageInSlotImage
value: 1
objectReference: {fileID: 0}
m_RemovedComponents: [] m_RemovedComponents: []
m_SourcePrefab: {fileID: 100100000, guid: 9f005685dc17f4643b4a1156970fe2f6, type: 3} m_SourcePrefab: {fileID: 100100000, guid: 9f005685dc17f4643b4a1156970fe2f6, type: 3}
--- !u!64 &3968453558880047856 --- !u!64 &3968453558880047856