Advertisement
evelynshilosky

InventoryItem - Part 12

Jun 4th, 2024
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.72 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.EventSystems;
  5. using UnityEngine.UI;
  6.  
  7. public class InventoryItem : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler, IPointerDownHandler, IPointerUpHandler
  8. {
  9.     // --- Is this item trashable --- //
  10.     public bool isTrashable;
  11.  
  12.     // --- Item Info UI --- //
  13.     private GameObject itemInfoUI;
  14.  
  15.     private Text itemInfoUI_itemName;
  16.     private Text itemInfoUI_itemDescription;
  17.     private Text itemInfoUI_itemFunctionality;
  18.  
  19.     public string thisName, thisDescription, thisFunctionality;
  20.  
  21.     // --- Consumption --- //
  22.     private GameObject itemPendingConsumption;
  23.     public bool isConsumable;
  24.  
  25.     public float healthEffect;
  26.     public float caloriesEffect;
  27.     public float hydrationEffect;
  28.  
  29.     // --- Equipping --- //
  30.     public bool isEquippable;
  31.     private GameObject itemPendingEquipping;
  32.     public bool isInsideQuickSlots;
  33.  
  34.     public bool isSelected;
  35.  
  36.  
  37.  
  38.     private void Start()
  39.     {
  40.         itemInfoUI = InventorySystem.Instance.ItemInfoUI;
  41.         itemInfoUI_itemName = itemInfoUI.transform.Find("itemName").GetComponent<Text>();
  42.         itemInfoUI_itemDescription = itemInfoUI.transform.Find("itemDescription").GetComponent<Text>();
  43.         itemInfoUI_itemFunctionality = itemInfoUI.transform.Find("itemFunctionality").GetComponent<Text>();
  44.     }
  45.  
  46.     void Update()
  47.     {
  48.         if (isSelected)
  49.         {
  50.             gameObject.GetComponent<DragDrop>().enabled = false;
  51.         }
  52.         else
  53.         {
  54.             gameObject.GetComponent<DragDrop>().enabled = true;
  55.         }
  56.  
  57.  
  58.     }
  59.  
  60.  
  61.     // Triggered when the mouse enters into the area of the item that has this script.
  62.     public void OnPointerEnter(PointerEventData eventData)
  63.     {
  64.         itemInfoUI.SetActive(true);
  65.         itemInfoUI_itemName.text = thisName;
  66.         itemInfoUI_itemDescription.text = thisDescription;
  67.         itemInfoUI_itemFunctionality.text = thisFunctionality;
  68.     }
  69.  
  70.     // Triggered when the mouse exits the area of the item that has this script.
  71.     public void OnPointerExit(PointerEventData eventData)
  72.     {
  73.         itemInfoUI.SetActive(false);
  74.     }
  75.  
  76.     // Triggered when the mouse is clicked over the item that has this script.
  77.     public void OnPointerDown(PointerEventData eventData)
  78.     {
  79.         //Right Mouse Button Click on
  80.         if (eventData.button == PointerEventData.InputButton.Right)
  81.         {
  82.             if (isConsumable)
  83.             {
  84.                 // Setting this specific gameobject to be the item we want to destroy later
  85.                 itemPendingConsumption = gameObject;
  86.                 consumingFunction(healthEffect, caloriesEffect, hydrationEffect);
  87.             }
  88.  
  89.  
  90.             if (isEquippable && isInsideQuickSlots == false && EquipSystem.Instance.CheckIfFull() == false)
  91.             {
  92.                 EquipSystem.Instance.AddToQuickSlots(gameObject);
  93.                 isInsideQuickSlots = true;
  94.             }
  95.         }
  96.  
  97.        
  98.  
  99.     }
  100.  
  101.  
  102.     // Triggered when the mouse button is released over the item that has this script.
  103.     public void OnPointerUp(PointerEventData eventData)
  104.     {
  105.         if (eventData.button == PointerEventData.InputButton.Right)
  106.         {
  107.             if (isConsumable && itemPendingConsumption == gameObject)
  108.             {
  109.                 DestroyImmediate(gameObject);
  110.                 InventorySystem.Instance.ReCalculateList();
  111.                 CraftingSystem.Instance.RefreshNeededItems();
  112.             }
  113.         }
  114.     }
  115.  
  116.     private void consumingFunction(float healthEffect, float caloriesEffect, float hydrationEffect)
  117.     {
  118.         itemInfoUI.SetActive(false);
  119.  
  120.         healthEffectCalculation(healthEffect);
  121.  
  122.         caloriesEffectCalculation(caloriesEffect);
  123.  
  124.         hydrationEffectCalculation(hydrationEffect);
  125.  
  126.     }
  127.  
  128.  
  129.     private static void healthEffectCalculation(float healthEffect)
  130.     {
  131.         // --- Health --- //
  132.  
  133.         float healthBeforeConsumption = PlayerState.Instance.currentHealth;
  134.         float maxHealth = PlayerState.Instance.maxHealth;
  135.  
  136.         if (healthEffect != 0)
  137.         {
  138.             if ((healthBeforeConsumption + healthEffect) > maxHealth)
  139.             {
  140.                 PlayerState.Instance.setHealth(maxHealth);
  141.             }
  142.             else
  143.             {
  144.                 PlayerState.Instance.setHealth(healthBeforeConsumption + healthEffect);
  145.             }
  146.         }
  147.     }
  148.  
  149.  
  150.     private static void caloriesEffectCalculation(float caloriesEffect)
  151.     {
  152.         // --- Calories --- //
  153.  
  154.         float caloriesBeforeConsumption = PlayerState.Instance.currentCalories;
  155.         float maxCalories = PlayerState.Instance.maxCalories;
  156.  
  157.         if (caloriesEffect != 0)
  158.         {
  159.             if ((caloriesBeforeConsumption + caloriesEffect) > maxCalories)
  160.             {
  161.                 PlayerState.Instance.setCalories(maxCalories);
  162.             }
  163.             else
  164.             {
  165.                 PlayerState.Instance.setCalories(caloriesBeforeConsumption + caloriesEffect);
  166.             }
  167.         }
  168.     }
  169.  
  170.  
  171.     private static void hydrationEffectCalculation(float hydrationEffect)
  172.     {
  173.         // --- Hydration --- //
  174.  
  175.         float hydrationBeforeConsumption = PlayerState.Instance.currentHydrationPercent;
  176.         float maxHydration = PlayerState.Instance.maxHydrationPercent;
  177.  
  178.         if (hydrationEffect != 0)
  179.         {
  180.             if ((hydrationBeforeConsumption + hydrationEffect) > maxHydration)
  181.             {
  182.                 PlayerState.Instance.setHydration(maxHydration);
  183.             }
  184.             else
  185.             {
  186.                 PlayerState.Instance.setHydration(hydrationBeforeConsumption + hydrationEffect);
  187.             }
  188.         }
  189.     }
  190.  
  191.  
  192. }
  193.  
  194.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement