Advertisement
evelynshilosky

InventoryItem - Part 34

Mar 7th, 2024
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.77 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.     public bool isSelected;
  35.     public bool isUseable;
  36.  
  37.    
  38.  
  39.     private void Start()
  40.     {
  41.         itemInfoUI = InventorySystem.Instance.ItemInfoUI;
  42.         itemInfoUI_itemName = itemInfoUI.transform.Find("itemName").GetComponent<Text>();
  43.         itemInfoUI_itemDescription = itemInfoUI.transform.Find("itemDescription").GetComponent<Text>();
  44.         itemInfoUI_itemFunctionality = itemInfoUI.transform.Find("itemFunctionality").GetComponent<Text>();
  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.     // Triggered when the mouse enters into the area of the item that has this script.
  59.     public void OnPointerEnter(PointerEventData eventData)
  60.     {
  61.         itemInfoUI.SetActive(true);
  62.         itemInfoUI_itemName.text = thisName;
  63.         itemInfoUI_itemDescription.text = thisDescription;
  64.         itemInfoUI_itemFunctionality.text = thisFunctionality;
  65.     }
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73.     // Triggered when the mouse exits the area of the item that has this script.
  74.     public void OnPointerExit(PointerEventData eventData)
  75.     {
  76.         itemInfoUI.SetActive(false);
  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.                
  102.                 gameObject.SetActive(false);
  103.                 UseItem();
  104.             }
  105.  
  106.         }
  107.  
  108.     }
  109.  
  110.    
  111.     // Triggered when the mouse button is released over the item that has this script.
  112.     public void OnPointerUp(PointerEventData eventData)
  113.     {
  114.         if (eventData.button == PointerEventData.InputButton.Right)
  115.         {
  116.             if (isConsumable && itemPendingConsumption == gameObject)
  117.             {
  118.                 DestroyImmediate(gameObject);
  119.                 InventorySystem.Instance.ReCalculateList();
  120.                 CraftingSystem.Instance.RefreshNeededItems();
  121.             }
  122.  
  123.         }
  124.     }
  125.  
  126.     private void UseItem()
  127.     {
  128.  
  129.         // Closing all opened UI / Menus
  130.  
  131.         itemInfoUI.SetActive(false);
  132.  
  133.  
  134.  
  135.         InventorySystem.Instance.isOpen = false;
  136.         InventorySystem.Instance.inventoryScreenUI.SetActive(false);
  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.itemToBeDestroyed = gameObject;
  154.                 ConstructionManager.Instance.ActivateConstructionPlacement("FoundationModel");
  155.                 break;
  156.                 //break;
  157.             case "Wall(Clone)":
  158.                 ConstructionManager.Instance.itemToBeDestroyed = gameObject;
  159.                 ConstructionManager.Instance.ActivateConstructionPlacement("WallModel");
  160.                 break;
  161.             case "StorageBox(Clone)":
  162.                 PlacementSystem.Instance.inventoryItemToDestory = gameObject;
  163.                 PlacementSystem.Instance.ActivatePlacementMode("StorageBoxModel");
  164.                 break;
  165.             case "StorageBox":
  166.                 PlacementSystem.Instance.inventoryItemToDestory = gameObject;
  167.                 PlacementSystem.Instance.ActivatePlacementMode("StorageBoxModel");
  168.                 break;
  169.             default:
  170.                 // do nothing
  171.                 break;
  172.         }
  173.     }
  174.     private void ConsumingFunction(float healthEffect, float caloriesEffect, float hydrationEffect)
  175.     {
  176.         itemInfoUI.SetActive(false);
  177.  
  178.         healthEffectCalculation(healthEffect);
  179.  
  180.         caloriesEffectCalculation(caloriesEffect);
  181.  
  182.         hydrationEffectCalculation(hydrationEffect);
  183.  
  184.     }
  185.  
  186.  
  187.     private static void healthEffectCalculation(float healthEffect)
  188.     {
  189.         // --- Health --- //
  190.  
  191.         float healthBeforeConsumption = PlayerState.Instance.currentHealth;
  192.         float maxHealth = PlayerState.Instance.maxHealth;
  193.  
  194.         if (healthEffect != 0)
  195.         {
  196.             if ((healthBeforeConsumption + healthEffect) > maxHealth)
  197.             {
  198.                 PlayerState.Instance.setHealth(maxHealth);
  199.             }
  200.             else
  201.             {
  202.                 PlayerState.Instance.setHealth(healthBeforeConsumption + healthEffect);
  203.             }
  204.         }
  205.     }
  206.  
  207.  
  208.     private static void caloriesEffectCalculation(float caloriesEffect)
  209.     {
  210.         // --- Calories --- //
  211.  
  212.         float caloriesBeforeConsumption = PlayerState.Instance.currentCalories;
  213.         float maxCalories = PlayerState.Instance.maxCalories;
  214.  
  215.         if (caloriesEffect != 0)
  216.         {
  217.             if ((caloriesBeforeConsumption + caloriesEffect) > maxCalories)
  218.             {
  219.                 PlayerState.Instance.setCalories(maxCalories);
  220.             }
  221.             else
  222.             {
  223.                 PlayerState.Instance.setCalories(caloriesBeforeConsumption + caloriesEffect);
  224.             }
  225.         }
  226.     }
  227.  
  228.  
  229.     private static void hydrationEffectCalculation(float hydrationEffect)
  230.     {
  231.         // --- Hydration --- //
  232.  
  233.         float hydrationBeforeConsumption = PlayerState.Instance.currentHydrationPercent;
  234.         float maxHydration = PlayerState.Instance.maxHydrationPercent;
  235.  
  236.         if (hydrationEffect != 0)
  237.         {
  238.             if ((hydrationBeforeConsumption + hydrationEffect) > maxHydration)
  239.             {
  240.                 PlayerState.Instance.setHydration(maxHydration);
  241.             }
  242.             else
  243.             {
  244.                 PlayerState.Instance.setHydration(hydrationBeforeConsumption + hydrationEffect);
  245.             }
  246.         }
  247.     }
  248.  
  249.  
  250. }
  251.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement