|
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 |
|
{ |
|
|
|
public bool isTrashable; |
|
|
|
|
|
private GameObject itemInfoUI; |
|
|
|
private Text itemInfoUI_itemName; |
|
private Text itemInfoUI_itemDescription; |
|
private Text itemInfoUI_itemFunctionality; |
|
|
|
public string thisName, thisDescription, thisFunctionality; |
|
|
|
|
|
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; |
|
} |
|
} |
|
|
|
|
|
public void OnPointerEnter(PointerEventData eventData) |
|
{ |
|
itemInfoUI.SetActive(true); |
|
itemInfoUI_itemName.text = thisName; |
|
itemInfoUI_itemDescription.text = thisDescription; |
|
itemInfoUI_itemFunctionality.text = thisFunctionality; |
|
} |
|
|
|
|
|
public void OnPointerExit(PointerEventData eventData) |
|
{ |
|
itemInfoUI.SetActive(false); |
|
} |
|
|
|
|
|
public void OnPointerDown(PointerEventData eventData) |
|
{ |
|
|
|
if (eventData.button == PointerEventData.InputButton.Right) |
|
{ |
|
if (isConsumable) |
|
{ |
|
|
|
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(); |
|
} |
|
} |
|
|
|
|
|
} |
|
|
|
|
|
public void OnPointerUp(PointerEventData eventData) |
|
{ |
|
if (eventData.button == PointerEventData.InputButton.Right) |
|
{ |
|
if (isConsumable && itemPendingConsumption == 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) |
|
{ |
|
|
|
|
|
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) |
|
{ |
|
|
|
|
|
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) |
|
{ |
|
|
|
|
|
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": |
|
ConstructionManager.Instance.ActivateConstructionPlacement("FoundationModel"); |
|
break; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
} |
|
|