mmorpg/Assets/Script/Inventory/Inventory.cs

125 lines
3.1 KiB
C#
Executable File

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;
}