Advertisement
evelynshilosky

InventoryItem - Part 19

Oct 20th, 2023 (edited)
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.21 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.EventSystems;
  6. using UnityEngine.UI;
  7.  
  8. public class InventoryItem : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler, IPointerDownHandler, IPointerUpHandler
  9. {
  10.     // --- Is this item trashable --- //
  11.     public bool isTrashable;
  12.  
  13.     // --- Item Info UI --- //
  14.     private GameObject itemInfoUI;
  15.  
  16.     private Text itemInfoUI_itemName;
  17.     private Text itemInfoUI_itemDescription;
  18.     private Text itemInfoUI_itemFunctionality;
  19.  
  20.     public string thisName, thisDescription, thisFunctionality;
  21.  
  22.     // --- Consumption --- //
  23.     private GameObject itemPendingConsumption;
  24.     public bool isConsumable;
  25.  
  26.     public float healthEffect;
  27.     public float caloriesEffect;
  28.     public float hydrationEffect;
  29.  
  30.     // --- Equipping --- //
  31.     public bool isEquippable;
  32.     private GameObject itemPendingEquipping;
  33.     public bool isInsideQuickSlots;
  34.    
  35.     public bool isSelected;
  36.  
  37.     public bool isUseable;
  38.  
  39.    
  40.  
  41.     private void Start()
  42.     {
  43.         itemInfoUI = InventorySystem.Instance.ItemInfoUI;
  44.         itemInfoUI_itemName = itemInfoUI.transform.Find("itemName").GetComponent<Text>();
  45.         itemInfoUI_itemDescription = itemInfoUI.transform.Find("itemDescription").GetComponent<Text>();
  46.         itemInfoUI_itemFunctionality = itemInfoUI.transform.Find("itemFunctionality").GetComponent<Text>();
  47.     }
  48.     void Update()
  49.     {
  50.         if (isSelected)
  51.         {
  52.             gameObject.GetComponent<DragDrop>().enabled = false;
  53.         }
  54.         else
  55.         {
  56.             gameObject.GetComponent<DragDrop>().enabled = true;
  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.  
  77.  
  78.  
  79.     // Triggered when the mouse is clicked over the item that has this script.
  80.     public void OnPointerDown(PointerEventData eventData)
  81.     {
  82.         //Right Mouse Button Click on
  83.         if (eventData.button == PointerEventData.InputButton.Right)
  84.         {
  85.             if (isConsumable)
  86.             {
  87.                 // Setting this specific gameobject to be the item we want to destroy later
  88.                 itemPendingConsumption = gameObject;
  89.                 ConsumingFunction(healthEffect, caloriesEffect, hydrationEffect);
  90.             }
  91.  
  92.  
  93.             if (isEquippable && isInsideQuickSlots == false && EquipSystem.Instance.CheckIfFull() == false)
  94.             {
  95.                 EquipSystem.Instance.AddToQuickSlots(gameObject);
  96.                 isInsideQuickSlots = true;
  97.             }
  98.  
  99.             if (isUseable)
  100.             {
  101.                 ConstructionManager.Instance.itemToBeDestroyed = gameObject;
  102.                 gameObject.SetActive(false);
  103.                 UseItem();
  104.             }
  105.  
  106.  
  107.         }
  108.  
  109.     }
  110.  
  111.    
  112.     // Triggered when the mouse button is released over the item that has this script.
  113.     public void OnPointerUp(PointerEventData eventData)
  114.     {
  115.         if (eventData.button == PointerEventData.InputButton.Right)
  116.         {
  117.             if (isConsumable && itemPendingConsumption == gameObject)
  118.             {
  119.                 DestroyImmediate(gameObject);
  120.                 InventorySystem.Instance.ReCalculateList();
  121.                 CraftingSystem.Instance.RefreshNeededItems();
  122.             }
  123.  
  124.            
  125.  
  126.         }
  127.     }
  128.  
  129.     private void UseItem()
  130.     {
  131.  
  132.         itemInfoUI.SetActive(false);
  133.  
  134.         InventorySystem.Instance.isOpen = false;
  135.         InventorySystem.Instance.inventoryScreenUI.SetActive(false);
  136.  
  137.         CraftingSystem.Instance.isOpen = false;
  138.         CraftingSystem.Instance.craftingScreenUI.SetActive(false);
  139.         CraftingSystem.Instance.toolsScreenUI.SetActive(false);
  140.         CraftingSystem.Instance.survivalScreenUI.SetActive(false);
  141.         CraftingSystem.Instance.refineScreenUI.SetActive(false);
  142.         CraftingSystem.Instance.constructionScreenUI.SetActive(false);
  143.  
  144.         Cursor.lockState = CursorLockMode.Locked;
  145.         Cursor.visible = false;
  146.  
  147.         SelectionManager.Instance.EnableSelection();
  148.         SelectionManager.Instance.enabled = true;
  149.  
  150.         switch (gameObject.name)
  151.         {
  152.             case "Foundation(Clone)":
  153.                 ConstructionManager.Instance.ActivateConstructionPlacement("FoundationModel");
  154.                 break;
  155.             case "Foundation":
  156.                 ConstructionManager.Instance.ActivateConstructionPlacement("FoundationModel"); //For testing
  157.                 break;
  158.             default:
  159.                 // do nothing
  160.                 break;
  161.         }
  162.     }
  163.     private void ConsumingFunction(float healthEffect, float caloriesEffect, float hydrationEffect)
  164.     {
  165.         itemInfoUI.SetActive(false);
  166.  
  167.         healthEffectCalculation(healthEffect);
  168.  
  169.         caloriesEffectCalculation(caloriesEffect);
  170.  
  171.         hydrationEffectCalculation(hydrationEffect);
  172.  
  173.     }
  174.  
  175.  
  176.     private static void healthEffectCalculation(float healthEffect)
  177.     {
  178.         // --- Health --- //
  179.  
  180.         float healthBeforeConsumption = PlayerState.Instance.currentHealth;
  181.         float maxHealth = PlayerState.Instance.maxHealth;
  182.  
  183.         if (healthEffect != 0)
  184.         {
  185.             if ((healthBeforeConsumption + healthEffect) > maxHealth)
  186.             {
  187.                 PlayerState.Instance.setHealth(maxHealth);
  188.             }
  189.             else
  190.             {
  191.                 PlayerState.Instance.setHealth(healthBeforeConsumption + healthEffect);
  192.             }
  193.         }
  194.     }
  195.  
  196.  
  197.     private static void caloriesEffectCalculation(float caloriesEffect)
  198.     {
  199.         // --- Calories --- //
  200.  
  201.         float caloriesBeforeConsumption = PlayerState.Instance.currentCalories;
  202.         float maxCalories = PlayerState.Instance.maxCalories;
  203.  
  204.         if (caloriesEffect != 0)
  205.         {
  206.             if ((caloriesBeforeConsumption + caloriesEffect) > maxCalories)
  207.             {
  208.                 PlayerState.Instance.setCalories(maxCalories);
  209.             }
  210.             else
  211.             {
  212.                 PlayerState.Instance.setCalories(caloriesBeforeConsumption + caloriesEffect);
  213.             }
  214.         }
  215.     }
  216.  
  217.  
  218.     private static void hydrationEffectCalculation(float hydrationEffect)
  219.     {
  220.         // --- Hydration --- //
  221.  
  222.         float hydrationBeforeConsumption = PlayerState.Instance.currentHydrationPercent;
  223.         float maxHydration = PlayerState.Instance.maxHydrationPercent;
  224.  
  225.         if (hydrationEffect != 0)
  226.         {
  227.             if ((hydrationBeforeConsumption + hydrationEffect) > maxHydration)
  228.             {
  229.                 PlayerState.Instance.setHydration(maxHydration);
  230.             }
  231.             else
  232.             {
  233.                 PlayerState.Instance.setHydration(hydrationBeforeConsumption + hydrationEffect);
  234.             }
  235.         }
  236.     }
  237.  
  238.  
  239. }
  240.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement