//Use item function that calls the itemuse funciton internal void UseItem(TheInventoryItem item) { if (ItemUse != null) { ItemUse(this, new InventoryEventArgs(item)); } }
//Checks tag to either have the item go active to be used as a general pickup item. Instant touch which is made to instantly pickup the flashlight //in order to simulate carrying items over to the next level. If attackWeapon it will place the item in hand using inventory scripts then calls upon //the item script states in order to utilize the other movements //REFERENCED: https://www.youtube.com/watch?v=twjrs4u_5bc&list=PLboXykqtm8dynMisqs4_oKvAIZedixvtf&index=6 private void InventoryItemUse(object sender, InventoryEventArgs e) { TheInventoryItem item = e.Item; GameObject goItem = (item as MonoBehaviour).gameObject; if (goItem.tag == "Pickup" || goItem.tag == "attackWeapon" || goItem.tag == "instantTouch") { if (goItem.activeSelf == false) { goItem.SetActive(true); } else { goItem.SetActive(false); } } if (goItem.tag == "attackWeapon") { goItem.transform.parent = animator.GetBoneTransform(HumanBodyBones.LeftMiddleDistal); } else { goItem.transform.parent = animator.GetBoneTransform(HumanBodyBones.RightHand); } }
//Removes object item and places it in a location active //REFERENCED: https://www.youtube.com/watch?v=Pc8K_DVPgVM&list=PLboXykqtm8dynMisqs4_oKvAIZedixvtf&index=3 private void InventoryItemRemove(object sender, InventoryEventArgs e) { TheInventoryItem item = e.Item; GameObject goItem = (item as MonoBehaviour).gameObject; goItem.SetActive(true); goItem.transform.parent = null; }
//When out of the trigger field the pickuptext UI will be turned off void OnTriggerExit(Collider other) { TheInventoryItem item = other.GetComponent <TheInventoryItem>(); if (item != null) { HUD.PickupTextOff(); pickupItem = null; } }
//Checks and finds the item via image and component to then be used by the player. public void OnItemClicked() { ItemDrag dragHandler = gameObject.transform.Find("Image").GetComponent <ItemDrag>(); TheInventoryItem item = dragHandler.Item; Debug.Log(item.Name); //Calls for the item to be used daInventory.UseItem(item); item.OnUse(); }
//Drops the item public void OnDrop(PointerEventData eventData) { RectTransform invPanel = transform as RectTransform; //Checks if the mouse pointer is within the inventory panel or not. If not then it will indicate that the item has been dropped if (!RectTransformUtility.RectangleContainsScreenPoint(invPanel, Input.mousePosition)) { //Grabs the item along with the drag TheInventoryItem item = eventData.pointerDrag.gameObject.GetComponent <ItemDrag>().Item; if (item != null) { //Drops the item //Debug.Log("It has been dropped " + item); inventory.RemoveItem(item); item.OnDrop(); } } }
//Adding pickup deactivate items and place them into inventory //REFERENCED: https://www.youtube.com/watch?v=Hj7AZkyojdo&list=PLboXykqtm8dynMisqs4_oKvAIZedixvtf //REFERENCED: https://www.youtube.com/watch?v=90OiysC4j5Y&list=PLboXykqtm8dynMisqs4_oKvAIZedixvtf&index=11 void OnTriggerEnter(Collider other) { if (other.gameObject.CompareTag("Pickup") || other.gameObject.CompareTag("itemNotUsed") || other.gameObject.CompareTag("attackWeapon") || other.gameObject.CompareTag("instantTouch")) { //This is to place item into the inventory made. TheInventoryItem item = other.gameObject.GetComponent <TheInventoryItem>(); if (item != null && !other.gameObject.CompareTag("instantTouch")) { pickupItem = item; HUD.PickupTextOn(""); } if (other.gameObject.CompareTag("instantTouch")) { inventory.AddItem(item); } } }
//Removes the item from the inventory list then drops the item. Turns on colliders to make it the same as it was before. Activating the object in the view public void RemoveItem(TheInventoryItem item) { //Debug.Log("This is the remove item"); if (myItems.Contains(item)) { myItems.Remove(item); item.OnDrop(); //Debug.Log("This is the ondrop"); Collider collider = (item as MonoBehaviour).GetComponent <Collider>(); if (collider != null) { collider.enabled = true; } ItemRemove?.Invoke(this, new InventoryEventArgs(item)); } }
//Adds items depending if there are slots available and public void AddItem(TheInventoryItem item) { if (myItems.Count < SLOTS) { Collider collider = (item as MonoBehaviour).GetComponent <Collider>(); if (collider.enabled) { //Checks to see if collider enabled worked properly since we are using triggers. Debug.Log("collider check"); collider.enabled = false; myItems.Add(item); item.OnPickup(); ItemAdd?.Invoke(this, new InventoryEventArgs(item)); } } }
public InventoryEventArgs(TheInventoryItem e) { Item = e; }