2022-11-15 22:58:54 +00:00
|
|
|
|
using System.Collections;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
|
|
namespace SimpleInventorySystem
|
|
|
|
|
{
|
|
|
|
|
[RequireComponent(typeof(Collider))]
|
|
|
|
|
public class PickUp : MonoBehaviour
|
|
|
|
|
{
|
2022-12-23 03:50:16 +00:00
|
|
|
|
[Header("Pickup Attributes")]
|
|
|
|
|
public ItemUI itemUI;
|
|
|
|
|
public PickUpRange pickUpRange;
|
|
|
|
|
[Header("Item Game Object Attributes")]
|
2022-11-15 22:58:54 +00:00
|
|
|
|
public GameObject ItemGameObject;
|
2022-12-23 03:50:16 +00:00
|
|
|
|
public Rigidbody ItemRigidBody;
|
|
|
|
|
[Header("Item Game Object Transform Modifications")]
|
2022-11-15 22:58:54 +00:00
|
|
|
|
public Vector3 alterPosition;
|
|
|
|
|
public Vector3 alterRotation;
|
|
|
|
|
public Vector3 alterScale = Vector3.one;
|
2022-12-23 03:50:16 +00:00
|
|
|
|
//public SlotUI PlaceInContainerOfItemInSlot;
|
|
|
|
|
// private
|
2022-11-15 22:58:54 +00:00
|
|
|
|
private Vector3 oldPosition;
|
|
|
|
|
private Vector3 oldRotation;
|
|
|
|
|
private Vector3 oldScale;
|
|
|
|
|
private Collider pickUpCollider;
|
2022-12-23 03:50:16 +00:00
|
|
|
|
// Hidden In Inspector
|
2022-11-15 22:58:54 +00:00
|
|
|
|
[HideInInspector] public TargetAttachment targetAttachment;
|
2022-12-23 03:50:16 +00:00
|
|
|
|
// public
|
|
|
|
|
public bool HadPickedUp { private set; get; }
|
2022-11-15 22:58:54 +00:00
|
|
|
|
|
|
|
|
|
// Start is called before the first frame update
|
|
|
|
|
void Start()
|
|
|
|
|
{
|
|
|
|
|
pickUpCollider = GetComponent<Collider>();
|
|
|
|
|
if (!pickUpCollider.isTrigger)
|
|
|
|
|
{
|
|
|
|
|
Debug.LogWarning("PickUp: The collider should be a trigger.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(ItemGameObject == null)
|
|
|
|
|
{
|
|
|
|
|
Debug.LogError("PickUp: ItemGameObject should not be null.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
oldPosition = ItemGameObject.transform.localPosition;
|
|
|
|
|
oldRotation = ItemGameObject.transform.localRotation.eulerAngles;
|
|
|
|
|
oldScale = ItemGameObject.transform.localScale;
|
|
|
|
|
|
|
|
|
|
//itemUI = Item.GetComponent<ItemUI>();
|
|
|
|
|
if (itemUI == null)
|
|
|
|
|
{
|
|
|
|
|
Debug.LogError("PickUp: ItemUI must not be null.");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
itemUI.pickUp = this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pickUpRange = GetComponentInChildren<PickUpRange>();
|
|
|
|
|
if(pickUpRange == null)
|
|
|
|
|
{
|
|
|
|
|
Debug.LogError("PickUp: PickUpRange must not be null.");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
pickUpRange.pickUp = this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void SetItemGameObjectTransform(Vector3 newPosition, Vector3 newRotation, Vector3 newScale)
|
|
|
|
|
{
|
|
|
|
|
ItemGameObject.transform.localPosition = newPosition;
|
|
|
|
|
ItemGameObject.transform.localRotation = Quaternion.Euler(newRotation);
|
|
|
|
|
ItemGameObject.transform.localScale = newScale;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void DropDownHandler(bool keepOldPosition = false)
|
|
|
|
|
{
|
|
|
|
|
if(keepOldPosition)
|
|
|
|
|
ItemGameObject.transform.localPosition = oldPosition;
|
|
|
|
|
ItemGameObject.transform.localRotation = Quaternion.Euler(oldRotation);
|
|
|
|
|
ItemGameObject.transform.localScale = oldScale;
|
|
|
|
|
HadPickedUp = false;
|
|
|
|
|
pickUpCollider.enabled = true;
|
|
|
|
|
pickUpRange.GetComponent<Collider>().enabled = true;
|
2022-12-23 03:50:16 +00:00
|
|
|
|
ItemRigidBody.isKinematic = false;
|
|
|
|
|
ItemRigidBody.useGravity = true;
|
2022-11-15 22:58:54 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void AlterItemGameObjectTransform()
|
|
|
|
|
{
|
|
|
|
|
ItemGameObject.transform.localPosition = alterPosition;
|
|
|
|
|
ItemGameObject.transform.localRotation = Quaternion.Euler(alterRotation);
|
2022-11-17 13:19:28 +00:00
|
|
|
|
if(alterScale != Vector3.zero && alterScale.x != 0 && alterScale.y != 0 && alterScale.z != 0)
|
|
|
|
|
ItemGameObject.transform.localScale = alterScale;
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
ItemGameObject.transform.localScale = Vector3.one;
|
|
|
|
|
}
|
2022-11-15 22:58:54 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void PickUpHandler()
|
|
|
|
|
{
|
|
|
|
|
if (!HadPickedUp)
|
|
|
|
|
{
|
2022-12-11 03:03:30 +00:00
|
|
|
|
if (itemUI.Equip())
|
2022-11-15 22:58:54 +00:00
|
|
|
|
{
|
2022-12-11 03:03:30 +00:00
|
|
|
|
itemUI.transform.localRotation = Quaternion.Euler(new Vector3());
|
|
|
|
|
itemUI.transform.localScale = new Vector3(1, 1, 1);
|
|
|
|
|
HadPickedUp = true;
|
2022-12-23 03:50:16 +00:00
|
|
|
|
Debug.Log("Pickup: Equipped");
|
2022-11-15 22:58:54 +00:00
|
|
|
|
|
2022-12-11 03:03:30 +00:00
|
|
|
|
// TODO: Handle changing transform here
|
|
|
|
|
//ItemGameObject.GetComponent<>;
|
2022-11-15 22:58:54 +00:00
|
|
|
|
|
2022-12-11 03:03:30 +00:00
|
|
|
|
if (InventorySystem.instance.player.AttachItemGameObject(ItemGameObject, itemUI.ItemTag, itemUI))
|
|
|
|
|
{
|
2022-12-23 03:50:16 +00:00
|
|
|
|
Debug.Log("Attached");
|
2023-01-15 11:39:47 +00:00
|
|
|
|
ItemGameObject.layer = LayerMask.NameToLayer("RenderTextureTarget");
|
2022-12-23 03:50:16 +00:00
|
|
|
|
ItemRigidBody.useGravity = false;
|
|
|
|
|
ItemRigidBody.isKinematic = true;
|
2022-12-11 03:03:30 +00:00
|
|
|
|
pickUpCollider.enabled = false;
|
|
|
|
|
pickUpRange.GetComponent<Collider>().enabled = false;
|
2022-11-15 22:58:54 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2023-01-15 11:39:47 +00:00
|
|
|
|
else if (itemUI.Store())
|
|
|
|
|
{
|
|
|
|
|
Debug.Log("Pickup: Stored");
|
|
|
|
|
}
|
2022-11-18 01:50:45 +00:00
|
|
|
|
// assume there is no tsp, then try to drop in most available backpack slot
|
2022-12-23 03:50:16 +00:00
|
|
|
|
/*else if (PlaceInContainerOfItemInSlot != null)
|
2022-11-18 01:50:45 +00:00
|
|
|
|
{
|
2022-12-11 03:03:30 +00:00
|
|
|
|
Debug.Log("Pickup: Trying to placing in container of item in slot");
|
2022-11-18 01:50:45 +00:00
|
|
|
|
if (PlaceInContainerOfItemInSlot.GetItemUI() != null && PlaceInContainerOfItemInSlot.GetItemUI().container != null)
|
|
|
|
|
{
|
|
|
|
|
GridUI grid = PlaceInContainerOfItemInSlot.GetItemUI().container.GetComponent<GridUI>();
|
|
|
|
|
if (grid != null)
|
|
|
|
|
{
|
|
|
|
|
if (grid.DropItemOnGrid(itemUI.gameObject))
|
|
|
|
|
{
|
|
|
|
|
Debug.Log("PickUp: Placed in grid.");
|
|
|
|
|
HadPickedUp = true;
|
|
|
|
|
|
2022-12-11 03:03:30 +00:00
|
|
|
|
itemUI.transform.localPosition = new Vector3(0, 0, 0);
|
|
|
|
|
itemUI.transform.localRotation = Quaternion.Euler(new Vector3(0, 0, 0));
|
|
|
|
|
itemUI.transform.localScale = new Vector3(1, 1, 1);
|
2022-12-23 03:50:16 +00:00
|
|
|
|
itemUI.SetOverrideSortingCanvasEnabled(true);
|
2022-12-11 03:03:30 +00:00
|
|
|
|
|
2022-11-18 01:50:45 +00:00
|
|
|
|
// TODO: Handle changing transform here
|
|
|
|
|
//ItemGameObject.GetComponent<>;
|
|
|
|
|
|
|
|
|
|
if (InventorySystem.instance.player.AttachItemGameObject(ItemGameObject, itemUI.ItemTag, itemUI))
|
|
|
|
|
{
|
|
|
|
|
pickUpCollider.enabled = false;
|
|
|
|
|
pickUpRange.GetComponent<Collider>().enabled = false;
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-12-23 03:50:16 +00:00
|
|
|
|
}*/
|
2022-11-18 01:50:45 +00:00
|
|
|
|
|
|
|
|
|
Debug.Log("Could not pick up");
|
2022-11-15 22:58:54 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Update is called once per frame
|
|
|
|
|
void Update()
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|