151 lines
3.8 KiB
C#
151 lines
3.8 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using TMPro;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
public class Inventory : MonoBehaviour
|
|
{
|
|
public Transform inventorySlotsParent;
|
|
public List<LootData> lootDatas;
|
|
playerNetwork playerNet;
|
|
void Awake(){
|
|
playerNet = GetComponent<playerNetwork>();
|
|
}
|
|
|
|
void Start(){
|
|
UpdateUI();
|
|
for(int i=0; i < inventorySlotsParent.childCount; i++){
|
|
int index =0;
|
|
index = i;
|
|
inventorySlotsParent.GetChild(i).GetChild(1).GetComponent<Button>().onClick.AddListener(()=>{ DropItem(index); });
|
|
inventorySlotsParent.GetChild(i).GetChild(0).GetComponent<Button>().onClick.AddListener(()=>{ UseItem(index); });
|
|
|
|
}
|
|
}
|
|
|
|
public Dictionary<string, int> getInventoryDictionary (){
|
|
Dictionary<string, int> invDictionary = new Dictionary<string, int>();
|
|
foreach(LootData loot in lootDatas){
|
|
if(invDictionary.ContainsKey(loot.type)){
|
|
invDictionary[loot.type]+= loot.count;
|
|
|
|
}
|
|
else{
|
|
invDictionary.Add(loot.type, loot.count);
|
|
}
|
|
}
|
|
return invDictionary;
|
|
}
|
|
|
|
public void setInventoryData(Dictionary<string, int> invSetData){
|
|
foreach(LootData loot in lootDatas){
|
|
if(invSetData.ContainsKey(loot.type)){
|
|
loot.count += invSetData[loot.type];
|
|
}
|
|
}
|
|
}
|
|
|
|
private void UseItem(int index){
|
|
if(playerNet.health >= 100){
|
|
return;
|
|
}
|
|
RemoveItem(lootDatas[index].type);
|
|
switch(lootDatas[index].type){
|
|
case "apple" :
|
|
playerNet.SetHealth(playerNet.health + 10 );
|
|
break;
|
|
case "potion" :
|
|
playerNet.SetHealth(playerNet.health + 30 );
|
|
break;
|
|
case "potion2" :
|
|
playerNet.SetHealth(playerNet.health + 15 );
|
|
break;
|
|
case "meat" :
|
|
playerNet.SetHealth(playerNet.health + 25 );
|
|
break;
|
|
case "strawberry" :
|
|
playerNet.SetHealth(playerNet.health + 5 );
|
|
break;
|
|
|
|
//
|
|
}
|
|
|
|
|
|
}
|
|
|
|
void DropItem(int index){
|
|
Debug.Log("Dropping item " + index);
|
|
if(RemoveItem(lootDatas[index].type)){
|
|
playerNet.DropPickup(lootDatas[index].type);
|
|
}else{
|
|
Debug.Log("No items to drop in slot " + index);
|
|
}
|
|
}
|
|
|
|
public void AddItem(string type){
|
|
playerNet.SavePlayerData();
|
|
foreach(LootData loot in lootDatas){
|
|
if(loot.type == type){
|
|
loot.count++;
|
|
break;
|
|
}
|
|
}
|
|
|
|
UpdateUI();
|
|
}
|
|
|
|
public bool RemoveItem(string type){
|
|
playerNet.SavePlayerData();
|
|
foreach(LootData loot in lootDatas){
|
|
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(LootData loot in lootDatas){
|
|
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 < lootDatas.Count){
|
|
chosenSprite= (lootDatas[i].count >0) ? lootDatas[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 ? lootDatas[i].count.ToString() : "";
|
|
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
[Serializable]
|
|
public class LootData{
|
|
public string type;
|
|
public Sprite image;
|
|
public GameObject prefab;
|
|
public int count;
|
|
public int spawnProbability = 50;
|
|
} |