159 lines
5.9 KiB
C#
159 lines
5.9 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
namespace SimpleInventorySystem
|
|
{
|
|
[RequireComponent(typeof(Collider))]
|
|
public class PickUp : MonoBehaviour
|
|
{
|
|
public GameObject ItemGameObject;
|
|
public Vector3 alterPosition;
|
|
public Vector3 alterRotation;
|
|
public Vector3 alterScale = Vector3.one;
|
|
private Vector3 oldPosition;
|
|
private Vector3 oldRotation;
|
|
private Vector3 oldScale;
|
|
public ItemUI itemUI;
|
|
private Collider pickUpCollider;
|
|
public PickUpRange pickUpRange;
|
|
public bool HadPickedUp { private set; get; }
|
|
[HideInInspector] public TargetAttachment targetAttachment;
|
|
public SlotUI PlaceInContainerOfItemInSlot;
|
|
|
|
// 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;
|
|
}
|
|
|
|
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)
|
|
{
|
|
foreach (TagSlotPairing tsp in InventorySystem.instance.TagSlotPairings)
|
|
{
|
|
if (!tsp.HasSlotOccupied())
|
|
{
|
|
bool dropped = tsp.slot.DropOntoSlot(itemUI.gameObject);
|
|
if (dropped)
|
|
{
|
|
itemUI.transform.localRotation = Quaternion.Euler(new Vector3());
|
|
itemUI.transform.localScale = new Vector3(1, 1, 1);
|
|
HadPickedUp = 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;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// assume there is no tsp, then try to drop in most available backpack slot
|
|
if (PlaceInContainerOfItemInSlot != null)
|
|
{
|
|
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;
|
|
|
|
// 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()
|
|
{
|
|
|
|
}
|
|
}
|
|
|
|
} |