Inventory.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using TMPro;
  5. using UnityEngine;
  6. using UnityEngine.UI;
  7. public class Inventory : MonoBehaviour
  8. {
  9. public Transform inventorySlotsParent;
  10. public CraftManager craftManager;
  11. public InventoryItemsCollection lootData;
  12. public InventoryManager inventoryManager;
  13. playerNetwork playerNet;
  14. void Awake(){
  15. playerNet = GetComponent<playerNetwork>();
  16. CraftManager.instance = craftManager;
  17. }
  18. private void UseItem(int index){
  19. if(playerNet.health >= 100){
  20. return;
  21. }
  22. RemoveItem(lootData.items[index].type);
  23. switch(lootData.items[index].type){
  24. case "apple" :
  25. playerNet.SetHealth(playerNet.health + 10 );
  26. break;
  27. case "potion" :
  28. playerNet.SetHealth(playerNet.health + 35 );
  29. break;
  30. case "potion2" :
  31. playerNet.SetHealth(playerNet.health + 25 );
  32. break;
  33. case "meat" :
  34. playerNet.SetHealth(playerNet.health + 40 );
  35. break;
  36. case "strawberry" :
  37. playerNet.SetHealth(playerNet.health + 5 );
  38. break;
  39. //
  40. }
  41. }
  42. void DropItem(int index){
  43. Debug.Log("Dropping item " + index);
  44. if(RemoveItem(lootData.items[index].type)){
  45. playerNet.DropPickup(lootData.items[index].type);
  46. }else{
  47. Debug.Log("No items to drop in slot " + index);
  48. }
  49. }
  50. public void AddItem(string type){
  51. inventoryManager.AddInvItem(type);
  52. return;
  53. foreach(item loot in lootData.items){
  54. if(loot.type == type){
  55. loot.count++;
  56. break;
  57. }
  58. }
  59. UpdateUI();
  60. playerNet.SavePlayerData();
  61. }
  62. public bool RemoveItem(string type){
  63. playerNet.SavePlayerData();
  64. foreach(item loot in lootData.items){
  65. if(loot.type == type){
  66. if(loot.count <=0){return false;}
  67. loot.count--;
  68. break;
  69. }
  70. }
  71. UpdateUI();
  72. return true;
  73. }
  74. public int GetStock(string type){
  75. int count =0;
  76. foreach(item loot in lootData.items){
  77. if(loot.type == type){
  78. count = loot.count;
  79. break;
  80. }
  81. }
  82. UpdateUI();
  83. return count;
  84. }
  85. public void UpdateUI(){
  86. // for(int i =0; i < inventorySlotsParent.childCount; i++){
  87. // Sprite chosenSprite = null;
  88. // if(i < lootData.items.Length){
  89. // chosenSprite= (lootData.items[i].count >0) ? lootData.items[i].image : null;
  90. // }
  91. // inventorySlotsParent.GetChild(i).GetChild(0).GetComponent<Image>().sprite = chosenSprite;
  92. // inventorySlotsParent.GetChild(i).GetChild(0).gameObject.SetActive(chosenSprite != null);
  93. // inventorySlotsParent.GetChild(i).GetChild(2).GetComponent<TMP_Text>().text = chosenSprite != null ? lootData.items[i].count.ToString() : "";
  94. // }
  95. }
  96. }
  97. [Serializable]
  98. public class LootData{
  99. public string type;
  100. public Sprite image;
  101. public GameObject prefab;
  102. public int count;
  103. public int spawnProbability = 50;
  104. }