131 lines
3.0 KiB
C#
131 lines
3.0 KiB
C#
|
using System;
|
||
|
using System.Collections;
|
||
|
using System.Collections.Generic;
|
||
|
using System.Linq;
|
||
|
using UnityEditor;
|
||
|
using UnityEngine;
|
||
|
|
||
|
public static class DataManager
|
||
|
{
|
||
|
public static List<ItemData> ItemDatas;
|
||
|
|
||
|
public static ItemData item(int id)
|
||
|
{
|
||
|
return ItemDatas.FirstOrDefault(vaItemData => vaItemData.item.id == id);
|
||
|
}
|
||
|
|
||
|
public static void AddItem(Item itemData, int itemNum)
|
||
|
{
|
||
|
if (ItemDatas==null)
|
||
|
{
|
||
|
ItemDatas = new List<ItemData>();
|
||
|
}
|
||
|
var listData = ItemDatas.FirstOrDefault(vaItemData => vaItemData.item.id == itemData.id);
|
||
|
if (listData==null)
|
||
|
{
|
||
|
ItemData saveData = new ItemData(itemData, itemNum);
|
||
|
ItemDatas.Add(saveData);
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
listData.num += itemNum;
|
||
|
}
|
||
|
}
|
||
|
public static void RemoveItem(int id,Action<bool> action)
|
||
|
{
|
||
|
var data = ItemDatas.FirstOrDefault(vaItemData => vaItemData.item.id == id);
|
||
|
if (data!=null)
|
||
|
{
|
||
|
if (data.num>=1)
|
||
|
{
|
||
|
data.num --;
|
||
|
action.Invoke(true);
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
action.Invoke(false);
|
||
|
}
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
action.Invoke(false);
|
||
|
}
|
||
|
ResetData();
|
||
|
}
|
||
|
public static void RemoveItem(ItemData itemData,Action<bool> action)
|
||
|
{
|
||
|
var data = ItemDatas.FirstOrDefault(vaItemData => vaItemData.item.id == itemData.item.id);
|
||
|
if (data!=null)
|
||
|
{
|
||
|
if (data.num>=itemData.num)
|
||
|
{
|
||
|
data.num = data.num - itemData.num;
|
||
|
action.Invoke(true);
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
action.Invoke(false);
|
||
|
}
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
action.Invoke(false);
|
||
|
}
|
||
|
ResetData();
|
||
|
}
|
||
|
|
||
|
public static void RemoveItem(List<ItemData> itemDatas,Action<bool> action)
|
||
|
{
|
||
|
foreach (var data in itemDatas)
|
||
|
{
|
||
|
var itemData = ItemDatas.FirstOrDefault(vaItemData => vaItemData.item.id == data.item.id);
|
||
|
if (itemData==null)
|
||
|
{
|
||
|
action.Invoke(false);
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
if (itemData.num<data.num)
|
||
|
{
|
||
|
action.Invoke(false);
|
||
|
return;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
foreach (var data in itemDatas)
|
||
|
{
|
||
|
ItemDatas.FirstOrDefault(vaItemData => vaItemData.item.id == data.item.id)!.num -= data.num;
|
||
|
|
||
|
}
|
||
|
action.Invoke(true);
|
||
|
ResetData();
|
||
|
}
|
||
|
|
||
|
private static void ResetData()
|
||
|
{
|
||
|
for (int i = ItemDatas.Count-1; i >= 0; i--)
|
||
|
{
|
||
|
if (ItemDatas[i].num<=0)
|
||
|
{
|
||
|
ItemDatas.Remove(ItemDatas[i]);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
[Serializable]
|
||
|
public class ItemData
|
||
|
{
|
||
|
public Item item;
|
||
|
public int num;
|
||
|
|
||
|
public ItemData(Item data,int dataNum)
|
||
|
{
|
||
|
item = data;
|
||
|
num = dataNum;
|
||
|
}
|
||
|
}
|
||
|
[Serializable]
|
||
|
public class Item
|
||
|
{
|
||
|
public int id;
|
||
|
}
|