3DSurvivalGame / Assets /Scripts /InventoryItem.cs
doc2txt's picture
1
3497d64
raw
history blame contribute delete
No virus
7.22 kB
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class InventoryItem : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler, IPointerDownHandler, IPointerUpHandler
{
// --- Is this item trashable --- //
public bool isTrashable;
// --- Item Info UI --- //
private GameObject itemInfoUI;
private Text itemInfoUI_itemName;
private Text itemInfoUI_itemDescription;
private Text itemInfoUI_itemFunctionality;
public string thisName, thisDescription, thisFunctionality;
// --- Consumption --- //
private GameObject itemPendingConsumption;
public bool isConsumable;
public float healthEffect;
public float caloriesEffect;
public float hydrationEffect;
public bool isEquitable;
private GameObject itemPendingEquipping;
public bool isInQuickSlot;
internal bool isSelected;
public bool isUsable;
public GameObject itemPendingToBeUsed;
private void Start()
{
itemInfoUI = InventorySystem.Instance.ItemInfoUI;
itemInfoUI_itemName = itemInfoUI.transform.Find("itemName").GetComponent<Text>();
itemInfoUI_itemDescription = itemInfoUI.transform.Find("itemDescription").GetComponent<Text>();
itemInfoUI_itemFunctionality = itemInfoUI.transform.Find("itemFunctionality").GetComponent<Text>();
}
private void Update()
{
if (isSelected)
{
gameObject.GetComponent<DragDrop>().enabled = false;
}
else
{
gameObject.GetComponent<DragDrop>().enabled = true;
}
}
// Triggered when the mouse enters into the area of the item that has this script.
public void OnPointerEnter(PointerEventData eventData)
{
itemInfoUI.SetActive(true);
itemInfoUI_itemName.text = thisName;
itemInfoUI_itemDescription.text = thisDescription;
itemInfoUI_itemFunctionality.text = thisFunctionality;
}
// Triggered when the mouse exits the area of the item that has this script.
public void OnPointerExit(PointerEventData eventData)
{
itemInfoUI.SetActive(false);
}
// Triggered when the mouse is clicked over the item that has this script.
public void OnPointerDown(PointerEventData eventData)
{
//Right Mouse Button Click on
if (eventData.button == PointerEventData.InputButton.Right)
{
if (isConsumable)
{
// Setting this specific gameobject to be the item we want to destroy later
itemPendingConsumption = gameObject;
consumingFunction(healthEffect, caloriesEffect, hydrationEffect);
}
if (isEquitable && isInQuickSlot == false && !EquipSystem.Instance.CheckIfFull())
{
EquipSystem.Instance.AddToQuickSlots(gameObject);
isInQuickSlot = true;
}
if (isUsable)
{
ConstructionManager.Instance.itemToBeDestroyed = gameObject;
UseItem();
}
}
}
// Triggered when the mouse button is released over the item that has this script.
public void OnPointerUp(PointerEventData eventData)
{
if (eventData.button == PointerEventData.InputButton.Right)
{
if (isConsumable && itemPendingConsumption == gameObject)
{
DestroyImmediate(gameObject);
InventorySystem.Instance.ReCalculateList();
CraftingSystem.Instance.RefreshNeededItems();
}
// if (isUsable && itemPendingToBeUsed == gameObject){
// DestroyImmediate(gameObject);
// InventorySystem.Instance.ReCalculateList();
// CraftingSystem.Instance.RefreshNeededItems();
// }
}
}
private void consumingFunction(float healthEffect, float caloriesEffect, float hydrationEffect)
{
itemInfoUI.SetActive(false);
healthEffectCalculation(healthEffect);
caloriesEffectCalculation(caloriesEffect);
hydrationEffectCalculation(hydrationEffect);
}
private static void healthEffectCalculation(float healthEffect)
{
// --- Health --- //
float healthBeforeConsumption = PlayerState.Instance.currentHealth;
float maxHealth = PlayerState.Instance.maxHealth;
if (healthEffect != 0)
{
if ((healthBeforeConsumption + healthEffect) > maxHealth)
{
PlayerState.Instance.setHealth(maxHealth);
}
else
{
PlayerState.Instance.setHealth(healthBeforeConsumption + healthEffect);
}
}
}
private static void caloriesEffectCalculation(float caloriesEffect)
{
// --- Calories --- //
float caloriesBeforeConsumption = PlayerState.Instance.currentCalories;
float maxCalories = PlayerState.Instance.maxCalories;
if (caloriesEffect != 0)
{
if ((caloriesBeforeConsumption + caloriesEffect) > maxCalories)
{
PlayerState.Instance.setCalories(maxCalories);
}
else
{
PlayerState.Instance.setCalories(caloriesBeforeConsumption + caloriesEffect);
}
}
}
private static void hydrationEffectCalculation(float hydrationEffect)
{
// --- Hydration --- //
float hydrationBeforeConsumption = PlayerState.Instance.currentHydration;
float maxHydration = PlayerState.Instance.maxHydration;
if (hydrationEffect != 0)
{
if ((hydrationBeforeConsumption + hydrationEffect) > maxHydration)
{
PlayerState.Instance.setHydration(maxHydration);
}
else
{
PlayerState.Instance.setHydration(hydrationBeforeConsumption + hydrationEffect);
}
}
}
private void UseItem()
{
itemInfoUI.SetActive(false);
InventorySystem.Instance.isOpen = false;
InventorySystem.Instance.inventoryScreenUI.SetActive(false);
CraftingSystem.Instance.isOpen = false;
CraftingSystem.Instance.craftingScreenUI.SetActive(false);
CraftingSystem.Instance.toolsScreenUI.SetActive(false);
CraftingSystem.Instance.survivalScreenUI.SetActive(false);
CraftingSystem.Instance.refineScreenUI.SetActive(false);
CraftingSystem.Instance.constructionScreenUI.SetActive(false);
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible=false;
SelectionManager.Instance.EnableSelection();
SelectionManager.Instance.enabled = true;
switch (gameObject.name) {
case "Foundation(Clone)":
ConstructionManager.Instance.ActivateConstructionPlacement("FoundationModel");
break;
case "Foundation": //Testing
ConstructionManager.Instance.ActivateConstructionPlacement("FoundationModel");
break;
}
}
}