projectEli/Assets/SimpleInventorySystem/Scripts/Inventory/PickUp.cs

124 lines
4.2 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;
// 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);
ItemGameObject.transform.localScale = alterScale;
}
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;
}
}
}
}
}
}
// Update is called once per frame
void Update()
{
}
}
}