87 lines
2.4 KiB
C#
87 lines
2.4 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using UnityEngine;
|
|
|
|
namespace Managers
|
|
{
|
|
public class BagManager
|
|
{
|
|
PlayerInfo Owner;
|
|
public BagManager(PlayerInfo owner)
|
|
{
|
|
this.Owner = owner;
|
|
}
|
|
public void AddItem(int key, int value)
|
|
{
|
|
if (Owner == null) return;
|
|
if (Owner.info.BagInfo.ContainsKey(key))
|
|
{
|
|
Owner.info.BagInfo[key] = Owner.info.BagInfo[key] + value;
|
|
}
|
|
else
|
|
{
|
|
Owner.info.BagInfo.Add(key, value);
|
|
}
|
|
|
|
Owner.OnUpdateBagInfo?.Invoke();
|
|
Owner.OnUpdateMainUI?.Invoke();
|
|
}
|
|
public void UserItem(int id, int num)
|
|
{
|
|
if (Owner == null) return;
|
|
if (Owner.info.BagInfo.ContainsKey(id))
|
|
{
|
|
foreach (var item in Date.Instance.ItemDate)//测试使用道具
|
|
{
|
|
if (item.Id == id && item.type == ItemClass.User)
|
|
{
|
|
if(Remove(item,num))
|
|
{
|
|
Debug.Log("使用道具");
|
|
item.UseItem(item);
|
|
}
|
|
}
|
|
if(item.Id == id && item.type == ItemClass.Up)
|
|
{
|
|
Remove(item, num);
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
Debug.LogError("道具缺失");
|
|
return;
|
|
}
|
|
}
|
|
private bool Remove(Item item,int num)
|
|
{
|
|
if (Owner.info.BagInfo[item.Id] < num)
|
|
{
|
|
Debug.LogFormat("UserItem [{0}] count Insufficient", item.ItemName);
|
|
return false;
|
|
}
|
|
Owner.info.BagInfo[item.Id] = Owner.info.BagInfo[item.Id] - num;//背包减少
|
|
|
|
if (Owner.info.BagInfo[item.Id] <= 0)
|
|
{
|
|
Owner.info.BagInfo.Remove(item.Id);
|
|
}
|
|
|
|
Owner.OnUpdateBagInfo?.Invoke();
|
|
Owner.OnUpdateMainUI?.Invoke();
|
|
|
|
Owner.WriteBagInfo();
|
|
|
|
//保存数据
|
|
//Debug.Log(Owner.info.BagValue[item.Id]);
|
|
|
|
SaveSystem.SaveDate(Owner.info.DatePath, Owner.info);
|
|
|
|
return true;
|
|
}
|
|
}
|
|
}
|