Advertisement
evelynshilosky

InventoryItem - Part 11

Jun 3rd, 2024
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.37 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.     public 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.  
  30.     private void Start()
  31.     {
  32.         itemInfoUI = InventorySystem.Instance.ItemInfoUI;
  33.         itemInfoUI_itemName = itemInfoUI.transform.Find("itemName").GetComponent<Text>();
  34.         itemInfoUI_itemDescription = itemInfoUI.transform.Find("itemDescription").GetComponent<Text>();
  35.         itemInfoUI_itemFunctionality = itemInfoUI.transform.Find("itemFunctionality").GetComponent<Text>();
  36.     }
  37.    
  38.     // Triggered when the mouse enters into the area of the item that has this script.
  39.     public void OnPointerEnter(PointerEventData eventData)
  40.     {
  41.         itemInfoUI.SetActive(true);
  42.         itemInfoUI_itemName.text = thisName;
  43.         itemInfoUI_itemDescription.text = thisDescription;
  44.         itemInfoUI_itemFunctionality.text = thisFunctionality;
  45.     }
  46.  
  47.     // Triggered when the mouse exits the area of the item that has this script.
  48.     public void OnPointerExit(PointerEventData eventData)
  49.     {
  50.         itemInfoUI.SetActive(false);
  51.     }
  52.  
  53.     // Triggered when the mouse is clicked over the item that has this script.
  54.     public void OnPointerDown(PointerEventData eventData)
  55.     {
  56.         //Right Mouse Button Click on
  57.         if (eventData.button == PointerEventData.InputButton.Right)
  58.         {
  59.             if (isConsumable)
  60.             {
  61.                 // Setting this specific gameobject to be the item we want to destroy later
  62.                 itemPendingConsumption = gameObject;
  63.                 ConsumingFunction(healthEffect, caloriesEffect, hydrationEffect);
  64.  
  65.             }
  66.                      }
  67.          }
  68.  
  69.          public void OnPointerUp(PointerEventData eventData)
  70.          {
  71.             if (eventData.button == PointerEventData.InputButton.Right)
  72.             {
  73.                 if (isConsumable && itemPendingConsumption == gameObject)
  74.                 {
  75.                     DestroyImmediate(gameObject);
  76.                     InventorySystem.Instance.ReCalculateList();
  77.                     CraftingSystem.Instance.RefreshNeededItems();
  78.                 }
  79.             }
  80.          }
  81.  
  82.          private void ConsumingFunction(float healthEffect, float caloriesEffect, float hydrationEffect)
  83.          {
  84.               itemInfoUI.SetActive(false);
  85.  
  86.               healthEffectCalculation(healthEffect);
  87.  
  88.               caloriesEffectCalculation(caloriesEffect);
  89.  
  90.               hydrationEffectCalculation(hydrationEffect);
  91.  
  92.          }
  93.  
  94.  
  95.          private static void healthEffectCalculation(float healthEffect)
  96.          {
  97.               //Health\\
  98.  
  99.               float healthBeforeConsumption = PlayerState.Instance.currentHealth;
  100.               float maxHealth = PlayerState.Instance.maxHealth;
  101.  
  102.               if (healthEffect != 0)
  103.               {
  104.                     if ((healthBeforeConsumption + healthEffect) > maxHealth)
  105.                     {
  106.                         PlayerState.Instance.setHealth(maxHealth);
  107.                     }
  108.                     else
  109.                     {
  110.                         PlayerState.Instance.setHealth(healthBeforeConsumption + healthEffect);
  111.                     }
  112.               }
  113.          }
  114.  
  115.  
  116.          private static void caloriesEffectCalculation(float caloriesEffect)
  117.          {
  118.               //Calories\\
  119.  
  120.               float caloriesBeforeConsumption = PlayerState.Instance.currentCalories;
  121.               float maxCalories = PlayerState.Instance.maxCalories;
  122.  
  123.               if (caloriesEffect != 0)
  124.               {
  125.                     if ((caloriesBeforeConsumption + caloriesEffect) > maxCalories)
  126.                     {
  127.                         PlayerState.Instance.setCalories(maxCalories);
  128.                     }
  129.                     else
  130.                     {
  131.                         PlayerState.Instance.setCalories(caloriesBeforeConsumption + caloriesEffect);
  132.                     }
  133.               }
  134.          }
  135.  
  136.  
  137.          private static void hydrationEffectCalculation(float hydrationEffect)
  138.          {
  139.               //Hydration\\
  140.  
  141.               float hydrationBeforeConsumption = PlayerState.Instance.currentHydrationPercent;
  142.               float maxHydration = PlayerState.Instance.maxHydrationPercent;
  143.  
  144.               if (hydrationEffect != 0)
  145.               {
  146.                     if ((hydrationBeforeConsumption + hydrationEffect) > maxHydration)
  147.                     {
  148.                         PlayerState.Instance.setHydration(maxHydration);
  149.                     }
  150.                     else
  151.                     {
  152.                         PlayerState.Instance.setHydration(hydrationBeforeConsumption + hydrationEffect);
  153.                     }
  154.               }
  155.          }
  156.  
  157.  
  158. }
  159.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement