Advertisement
evelynshilosky

InventoryItem - Part 20

Oct 31st, 2023
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.48 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.     // 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.         itemInfoUI.SetActive(false);
  130.  
  131.         InventorySystem.Instance.isOpen = false;
  132.         InventorySystem.Instance.inventoryScreenUI.SetActive(false);
  133.  
  134.         CraftingSystem.Instance.isOpen = false;
  135.         CraftingSystem.Instance.craftingScreenUI.SetActive(false);
  136.         CraftingSystem.Instance.toolsScreenUI.SetActive(false);
  137.         CraftingSystem.Instance.survivalScreenUI.SetActive(false);
  138.         CraftingSystem.Instance.refineScreenUI.SetActive(false);
  139.         CraftingSystem.Instance.constructionScreenUI.SetActive(false);
  140.  
  141.         Cursor.lockState = CursorLockMode.Locked;
  142.         Cursor.visible = false;
  143.  
  144.         SelectionManager.Instance.EnableSelection();
  145.         SelectionManager.Instance.enabled = true;
  146.  
  147.         switch (gameObject.name)
  148.         {
  149.             case "Foundation(Clone)":
  150.                 ConstructionManager.Instance.ActivateConstructionPlacement("FoundationModel");
  151.                 break;
  152.             case "Foundation":
  153.                 ConstructionManager.Instance.ActivateConstructionPlacement("FoundationModel"); //For testing
  154.                 break;
  155.             case "Wall(Clone)":
  156.                 ConstructionManager.Instance.ActivateConstructionPlacement("WallModel");
  157.                 break;
  158.             case "Wall":
  159.                 ConstructionManager.Instance.ActivateConstructionPlacement("WallModel"); //For testing
  160.                 break;
  161.             default:
  162.                 // do nothing
  163.                 break;
  164.         }
  165.     }
  166.     private void ConsumingFunction(float healthEffect, float caloriesEffect, float hydrationEffect)
  167.     {
  168.         itemInfoUI.SetActive(false);
  169.  
  170.         healthEffectCalculation(healthEffect);
  171.  
  172.         caloriesEffectCalculation(caloriesEffect);
  173.  
  174.         hydrationEffectCalculation(hydrationEffect);
  175.  
  176.     }
  177.  
  178.  
  179.     private static void healthEffectCalculation(float healthEffect)
  180.     {
  181.         // --- Health --- //
  182.  
  183.         float healthBeforeConsumption = PlayerState.Instance.currentHealth;
  184.         float maxHealth = PlayerState.Instance.maxHealth;
  185.  
  186.         if (healthEffect != 0)
  187.         {
  188.             if ((healthBeforeConsumption + healthEffect) > maxHealth)
  189.             {
  190.                 PlayerState.Instance.setHealth(maxHealth);
  191.             }
  192.             else
  193.             {
  194.                 PlayerState.Instance.setHealth(healthBeforeConsumption + healthEffect);
  195.             }
  196.         }
  197.     }
  198.  
  199.  
  200.     private static void caloriesEffectCalculation(float caloriesEffect)
  201.     {
  202.         // --- Calories --- //
  203.  
  204.         float caloriesBeforeConsumption = PlayerState.Instance.currentCalories;
  205.         float maxCalories = PlayerState.Instance.maxCalories;
  206.  
  207.         if (caloriesEffect != 0)
  208.         {
  209.             if ((caloriesBeforeConsumption + caloriesEffect) > maxCalories)
  210.             {
  211.                 PlayerState.Instance.setCalories(maxCalories);
  212.             }
  213.             else
  214.             {
  215.                 PlayerState.Instance.setCalories(caloriesBeforeConsumption + caloriesEffect);
  216.             }
  217.         }
  218.     }
  219.  
  220.  
  221.     private static void hydrationEffectCalculation(float hydrationEffect)
  222.     {
  223.         // --- Hydration --- //
  224.  
  225.         float hydrationBeforeConsumption = PlayerState.Instance.currentHydrationPercent;
  226.         float maxHydration = PlayerState.Instance.maxHydrationPercent;
  227.  
  228.         if (hydrationEffect != 0)
  229.         {
  230.             if ((hydrationBeforeConsumption + hydrationEffect) > maxHydration)
  231.             {
  232.                 PlayerState.Instance.setHydration(maxHydration);
  233.             }
  234.             else
  235.             {
  236.                 PlayerState.Instance.setHydration(hydrationBeforeConsumption + hydrationEffect);
  237.             }
  238.         }
  239.     }
  240.  
  241.  
  242. }
  243.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement