Advertisement
evelynshilosky

InventoryItem - Part 35

Mar 19th, 2024
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.96 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.  
  157.             case "Wall(Clone)":
  158.                 ConstructionManager.Instance.itemToBeDestroyed = gameObject;
  159.                 ConstructionManager.Instance.ActivateConstructionPlacement("WallModel");
  160.                 break;
  161.  
  162.             case "StorageBox":
  163.                 PlacementSystem.Instance.inventoryItemToDestory = gameObject;
  164.                 PlacementSystem.Instance.ActivatePlacementMode("StorageBoxModel");
  165.                 break;
  166.             case "Campfire(Clone)":
  167.                 PlacementSystem.Instance.inventoryItemToDestory = gameObject;
  168.                 PlacementSystem.Instance.ActivatePlacementMode("CampfireModel");
  169.                 break;
  170.             case "Campfire":
  171.                 PlacementSystem.Instance.inventoryItemToDestory = gameObject;
  172.                 PlacementSystem.Instance.ActivatePlacementMode("CampfireModel");
  173.                 break;
  174.             default:
  175.                 // do nothing
  176.                 break;
  177.         }
  178.     }
  179.     private void ConsumingFunction(float healthEffect, float caloriesEffect, float hydrationEffect)
  180.     {
  181.         itemInfoUI.SetActive(false);
  182.  
  183.         healthEffectCalculation(healthEffect);
  184.  
  185.         caloriesEffectCalculation(caloriesEffect);
  186.  
  187.         hydrationEffectCalculation(hydrationEffect);
  188.  
  189.     }
  190.  
  191.  
  192.     private static void healthEffectCalculation(float healthEffect)
  193.     {
  194.         // --- Health --- //
  195.  
  196.         float healthBeforeConsumption = PlayerState.Instance.currentHealth;
  197.         float maxHealth = PlayerState.Instance.maxHealth;
  198.  
  199.         if (healthEffect != 0)
  200.         {
  201.             if ((healthBeforeConsumption + healthEffect) > maxHealth)
  202.             {
  203.                 PlayerState.Instance.setHealth(maxHealth);
  204.             }
  205.             else
  206.             {
  207.                 PlayerState.Instance.setHealth(healthBeforeConsumption + healthEffect);
  208.             }
  209.         }
  210.     }
  211.  
  212.  
  213.     private static void caloriesEffectCalculation(float caloriesEffect)
  214.     {
  215.         // --- Calories --- //
  216.  
  217.         float caloriesBeforeConsumption = PlayerState.Instance.currentCalories;
  218.         float maxCalories = PlayerState.Instance.maxCalories;
  219.  
  220.         if (caloriesEffect != 0)
  221.         {
  222.             if ((caloriesBeforeConsumption + caloriesEffect) > maxCalories)
  223.             {
  224.                 PlayerState.Instance.setCalories(maxCalories);
  225.             }
  226.             else
  227.             {
  228.                 PlayerState.Instance.setCalories(caloriesBeforeConsumption + caloriesEffect);
  229.             }
  230.         }
  231.     }
  232.  
  233.  
  234.     private static void hydrationEffectCalculation(float hydrationEffect)
  235.     {
  236.         // --- Hydration --- //
  237.  
  238.         float hydrationBeforeConsumption = PlayerState.Instance.currentHydrationPercent;
  239.         float maxHydration = PlayerState.Instance.maxHydrationPercent;
  240.  
  241.         if (hydrationEffect != 0)
  242.         {
  243.             if ((hydrationBeforeConsumption + hydrationEffect) > maxHydration)
  244.             {
  245.                 PlayerState.Instance.setHydration(maxHydration);
  246.             }
  247.             else
  248.             {
  249.                 PlayerState.Instance.setHydration(hydrationBeforeConsumption + hydrationEffect);
  250.             }
  251.         }
  252.     }
  253.  
  254.  
  255. }
  256.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement