UGUI--背包系统之一-------InventoryManager

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using LitJson;
using System.IO;
using UnityEngine.UI;


public class InventoryManager : MonoBehaviour{

    #region 单例模式
    private static InventoryManager _instance;
    public static InventoryManager Instance
    {
        get
        {
            if (_instance == null)
            {
                _instance = GameObject.Find("Manager").GetComponent<InventoryManager>();
            }
            return _instance;
        }
    }
    #endregion

    private List<ItemBase> itemList;

    #region 提示信息相关的属性
    private ToolTip toolTip;
    private bool toolTipIsShow = false;
    private Canvas canvas;
    private Vector2 offsetPos = new Vector2(8, -10);
    #endregion

    #region 鼠标拾取物品属性
    private Item pickItem;
    public Item PickItem
    {
        get
        {
            return pickItem;
        }
    }
    private bool isPickedItem = false;
    public bool IsPickedItem
    {
        get
        {
            return isPickedItem;
        }
    }
    #endregion

    void Awake()
    {      
        ParseItemJson();
        toolTip = GameObject.FindObjectOfType<ToolTip>();
        canvas = GameObject.Find("Canvas").GetComponent<Canvas>();
        pickItem = GameObject.Find("MoveItem").GetComponent<Item>();
        pickItem.Hide();
 
    }
    void Update()
    {
        if (isPickedItem)
        {
            Vector2 pos;
            RectTransformUtility.ScreenPointToLocalPointInRectangle(canvas.transform as RectTransform, Input.mousePosition, null, out pos);
            pickItem.SetLocalPostion(pos);
        }
        else if (toolTipIsShow)
        {
            Vector2 pos;
            RectTransformUtility.ScreenPointToLocalPointInRectangle(canvas.transform as RectTransform, Input.mousePosition, null, out pos);
            toolTip.SetLocalPosition(pos+offsetPos);
        }
        if (isPickedItem && Input.GetMouseButtonDown(0) && UnityEngine.EventSystems.EventSystem.current.IsPointerOverGameObject(-1) == false)
        {
            RemoveAll();
        }
    }

    #region 解析json,把一条条物品信息存在list类里面,提供外界访问点,从list取出信息
    void ParseItemJson()
    {
        itemList = new List<ItemBase>();
        TextAsset t = Resources.Load<TextAsset>("Comsumable");
        //StreamReader sr = new StreamReader(Application.dataPath + "/Resources/Comsumable.json");
        //if (sr == null) return;
        //string json = sr.ReadToEnd();
        JsonData jsonDate = JsonMapper.ToObject(t.text);
        for (int i = 0; i < jsonDate.Count; i++)
        {
            JsonData idValue = jsonDate[i]["id"];
            JsonData nameValue = jsonDate[i]["name"];
            JsonData iconValue = jsonDate[i]["icon"];
            JsonData typeValue = jsonDate[i]["type"];
            JsonData qualityValue = jsonDate[i]["quality"];
            JsonData maxStackValue = jsonDate[i]["maxStack"];
            JsonData descriptionValue = jsonDate[i]["description"];
            JsonData buyPriceValue = jsonDate[i]["buyPrice"];
            JsonData sellPriceValue = jsonDate[i]["sellPrice"];

            int id = int.Parse(idValue.ToString());
            string name = nameValue.ToString();
            string icon = iconValue.ToString();
            ItemBase.Itemtype type = (ItemBase.Itemtype)System.Enum.Parse(typeof(ItemBase.Itemtype), typeValue.ToString());
            ItemBase.ItemQuiality quality = (ItemBase.ItemQuiality)System.Enum.Parse(typeof(ItemBase.ItemQuiality), qualityValue.ToString());
            int maxStack = int.Parse(maxStackValue.ToString());
            string description = descriptionValue.ToString();
            int buyPrice = int.Parse(buyPriceValue.ToString());
            int sellPrice = int.Parse(sellPriceValue.ToString());

            ItemBase item = null;
            switch (type)
            {
                case ItemBase.Itemtype.Consumable:
                    JsonData hpValue = jsonDate[i]["hp"];
                    JsonData mpValue = jsonDate[i]["mp"];
                    int hp = int.Parse(hpValue.ToString());
                    int mp = int.Parse(mpValue.ToString());
                    item = new Consumable(id, name, icon, type, quality, description, maxStack, buyPrice, sellPrice, hp, mp);
                    break;
                case ItemBase.Itemtype.Equipment:
                    JsonData strengthValue = jsonDate[i]["strength"];
                    JsonData intellectValue = jsonDate[i]["intellect"];
                    JsonData agilityValue = jsonDate[i]["agility"];
                    JsonData staminaValue = jsonDate[i]["stamina"];
                    JsonData equipmentTypeValue = jsonDate[i]["equipmentType"];
                    int strength = int.Parse(strengthValue.ToString());
                    int intellect = int.Parse(intellectValue.ToString());
                    int agility = int.Parse(agilityValue.ToString());
                    int stamina = int.Parse(staminaValue.ToString());
                    Equipment.EquipmentType equipmentType = (Equipment.EquipmentType)System.Enum.Parse(typeof(Equipment.EquipmentType), equipmentTypeValue.ToString());
                    item = new Equipment(id, name, icon, type, quality, description, maxStack, buyPrice, sellPrice, strength,intellect,agility,stamina,equipmentType);
                    break;
                case ItemBase.Itemtype.Weapon:
                    JsonData damageValue = jsonDate[i]["damage"];
                    JsonData weapTypeValue = jsonDate[i]["weapType"];
                    int damage = int.Parse(damageValue.ToString());
                    Weapon.WeapType weapType = (Weapon.WeapType)System.Enum.Parse(typeof(Weapon.WeapType), weapTypeValue.ToString());
                    item = new Weapon(id, name, icon, type, quality, description, maxStack, buyPrice, sellPrice,damage,weapType);
                    break;
                case ItemBase.Itemtype.Material:
                    item = new Material(id, name, icon, type, quality, description, maxStack, buyPrice, sellPrice);
                    break;
                default:
                    break;
            }
            itemList.Add(item);                      
        }
    }
    public ItemBase GetItemById(int id)
    {
        foreach (ItemBase item in itemList)
        {
            if (item.ID == id)
            {
                return item;
            }
        }
        return null;
    }
    #endregion 

    #region 显示和隐藏提示框
    public void ShowTooltip(string text)
    {
        if (isPickedItem) return;
        toolTipIsShow = true;
        toolTip.ShowTooltip(text);
    }
    public void HideTooltip()
    {
        toolTipIsShow = false;
        toolTip.HideTooltip();
    }
    #endregion

    #region 拾取和释放鼠标小的物体
    public void PickUpItem(Item item,int num)
    {
        isPickedItem = true;
        pickItem.Show();
        pickItem.SetIcon(item.itemBase,num);
        toolTip.HideTooltip();
    }
    public void RemoveAmount(int num=1)
    {
        pickItem.ReduceAmount(num);
        if (pickItem.Amount <= 0)
        {
            RemoveAll();
        }
    }
    public void RemoveAll()
    {
        isPickedItem = false;
        pickItem.Hide();
    }
    #endregion

    public void Save()
    {
        Knapsack.Instance.SaveInventory();
        Chest.Instance.SaveInventory();
        CharacterPanel.Instance.SaveInventory();
        ForgePanel.Instance.SaveInventory();
        PlayerPrefs.SetInt("CoinAmount", GameObject.FindGameObjectWithTag("Player").GetComponent<Player>().CoinAmount);
    }
    public void Load()
    {
        Knapsack.Instance.LoadInventory();
        Chest.Instance.LoadInventory();
        CharacterPanel.Instance.LoadInventory();
        ForgePanel.Instance.LoadInventory();
        if (PlayerPrefs.HasKey("CoinAmount"))
        {
          int amount=  PlayerPrefs.GetInt("CoinAmount");
          GameObject.FindGameObjectWithTag("Player").GetComponent<Player>().CoinAmount = amount;
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值