using System; using System.Collections; using System.Collections.Generic; using MLAPI; using UnityEngine; public class PlayerInventory : NetworkBehaviour { //Dictionary of items [SerializeField] private Dictionary playerItemDict = new Dictionary(); public Dictionary PlayerItemDict{ get{ return playerItemDict; } } //List for the network [SerializeField] private List networkItemList = new List(); public List NetworkItemList{ get{ return networkItemList; } } //Scripts public PlayerStats pStats; void Awake(){ pStats = GetComponent(); } //Adds player Item to a dictionary void AddItem(Item item){ playerItemDict.Add(item.name, item); item.Equip(pStats, this.gameObject); } //Add item to player Inventory public void UpdateItemNetwork(string itemName, int add){ //if Removing items if(add == 0) networkItemList.Remove(itemName); //if adding items else if(add == 1 )networkItemList.Add(itemName); //if nothing is being updated else Debug.Log("Nothing updated"); } //Remove Item player inv public void RemoveItem(Item item){ //if item removal was successful if(playerItemDict.Remove(item.name)){ //Modify player stats item.Unequip(pStats, this.gameObject); } } //Updates Inventory to Add item to player list public int UpdateInventory(Item item, bool ableToAdd){ //if player can add the item if(!playerItemDict.ContainsKey(item.name) && ableToAdd){ Debug.Log("Item Added"); AddItem(item); return 1; } else if(!ableToAdd && !playerItemDict.ContainsKey(item.name)){ //Item cannot be added Debug.Log("Item Cannot Be Added"); return -1; } else{ //If player already has the item it removes it Debug.Log("Item Removed"); RemoveItem(item); return 0; } } //Equips All items in list at start of next scene public void UpdateEquips(List items, Dictionary allItems){ if (items.Count <= 0) { return; } foreach(string itemName in items){ if (itemName == "" || itemName == " ") { continue; } if(!playerItemDict.ContainsKey(itemName)){ AddItem(allItems[itemName]); } else{ Debug.Log("Attempted To Equip Item Already in Inventory"); } } } public void ClearInventory(){ foreach(KeyValuePair item in PlayerItemDict){ RemoveItem(item.Value); } } }