Inventory.cs 3.1 KB

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