123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using TMPro;
- using UnityEngine;
- using UnityEngine.UI;
- public class Inventory : MonoBehaviour
- {
- public Transform inventorySlotsParent;
- public InventoryItemsCollection lootData;
- public InventoryManager inventoryManager;
- playerNetwork playerNet;
- void Awake(){
- playerNet = GetComponent<playerNetwork>();
- }
- private void UseItem(int index){
- if(playerNet.health >= 100){
- return;
- }
- RemoveItem(lootData.items[index].type);
- switch(lootData.items[index].type){
- case "apple" :
- playerNet.SetHealth(playerNet.health + 10 );
- break;
- case "potion" :
- playerNet.SetHealth(playerNet.health + 35 );
- break;
- case "potion2" :
- playerNet.SetHealth(playerNet.health + 25 );
- break;
- case "meat" :
- playerNet.SetHealth(playerNet.health + 40 );
- break;
- case "strawberry" :
- playerNet.SetHealth(playerNet.health + 5 );
- break;
- //
- }
-
- }
- void DropItem(int index){
- Debug.Log("Dropping item " + index);
- if(RemoveItem(lootData.items[index].type)){
- playerNet.DropPickup(lootData.items[index].type);
- }else{
- Debug.Log("No items to drop in slot " + index);
- }
- }
- public void AddItem(string type){
- inventoryManager.AddInvItem(type);
- return;
- foreach(item loot in lootData.items){
- if(loot.type == type){
- loot.count++;
- break;
- }
- }
-
- UpdateUI();
- playerNet.SavePlayerData();
- }
- public bool RemoveItem(string type){
- playerNet.SavePlayerData();
- foreach(item loot in lootData.items){
- if(loot.type == type){
- if(loot.count <=0){return false;}
- loot.count--;
-
- break;
- }
- }
- UpdateUI();
- return true;
- }
- public int GetStock(string type){
- int count =0;
- foreach(item loot in lootData.items){
- if(loot.type == type){
- count = loot.count;
- break;
- }
- }
- UpdateUI();
- return count;
- }
- public void UpdateUI(){
- // for(int i =0; i < inventorySlotsParent.childCount; i++){
- // Sprite chosenSprite = null;
- // if(i < lootData.items.Length){
- // chosenSprite= (lootData.items[i].count >0) ? lootData.items[i].image : null;
- // }
- // inventorySlotsParent.GetChild(i).GetChild(0).GetComponent<Image>().sprite = chosenSprite;
- // inventorySlotsParent.GetChild(i).GetChild(0).gameObject.SetActive(chosenSprite != null);
-
- // inventorySlotsParent.GetChild(i).GetChild(2).GetComponent<TMP_Text>().text = chosenSprite != null ? lootData.items[i].count.ToString() : "";
-
- // }
- }
- }
- [Serializable]
- public class LootData{
- public string type;
- public Sprite image;
- public GameObject prefab;
- public int count;
- public int spawnProbability = 50;
- }
|