projectEli/Assets/SimpleInventorySystem/Scripts/Inventory/PickUp.cs
2022-12-22 19:50:16 -08:00

171 lines
6.5 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace SimpleInventorySystem
{
[RequireComponent(typeof(Collider))]
public class PickUp : MonoBehaviour
{
[Header("Pickup Attributes")]
public ItemUI itemUI;
public PickUpRange pickUpRange;
[Header("Item Game Object Attributes")]
public GameObject ItemGameObject;
public Rigidbody ItemRigidBody;
[Header("Item Game Object Transform Modifications")]
public Vector3 alterPosition;
public Vector3 alterRotation;
public Vector3 alterScale = Vector3.one;
//public SlotUI PlaceInContainerOfItemInSlot;
// private
private Vector3 oldPosition;
private Vector3 oldRotation;
private Vector3 oldScale;
private Collider pickUpCollider;
// Hidden In Inspector
[HideInInspector] public TargetAttachment targetAttachment;
// public
public bool HadPickedUp { private set; get; }
// 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;
ItemRigidBody.isKinematic = false;
ItemRigidBody.useGravity = true;
}
public void AlterItemGameObjectTransform()
{
ItemGameObject.transform.localPosition = alterPosition;
ItemGameObject.transform.localRotation = Quaternion.Euler(alterRotation);
if(alterScale != Vector3.zero && alterScale.x != 0 && alterScale.y != 0 && alterScale.z != 0)
ItemGameObject.transform.localScale = alterScale;
else
{
ItemGameObject.transform.localScale = Vector3.one;
}
}
public void PickUpHandler()
{
if (!HadPickedUp)
{
if (itemUI.Equip())
{
itemUI.transform.localRotation = Quaternion.Euler(new Vector3());
itemUI.transform.localScale = new Vector3(1, 1, 1);
HadPickedUp = true;
Debug.Log("Pickup: Equipped");
// TODO: Handle changing transform here
//ItemGameObject.GetComponent<>;
if (InventorySystem.instance.player.AttachItemGameObject(ItemGameObject, itemUI.ItemTag, itemUI))
{
Debug.Log("Attached");
ItemRigidBody.useGravity = false;
ItemRigidBody.isKinematic = true;
pickUpCollider.enabled = false;
pickUpRange.GetComponent<Collider>().enabled = false;
}
}
// assume there is no tsp, then try to drop in most available backpack slot
/*else if (PlaceInContainerOfItemInSlot != null)
{
Debug.Log("Pickup: Trying to placing in container of item in slot");
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;
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);
itemUI.SetOverrideSortingCanvasEnabled(true);
// 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;
}
}
}
}*/
Debug.Log("Could not pick up");
}
}
// Update is called once per frame
void Update()
{
}
}
}