117 lines
		
	
	
		
			2.8 KiB
		
	
	
	
		
			C#
		
	
	
			
		
		
	
	
			117 lines
		
	
	
		
			2.8 KiB
		
	
	
	
		
			C#
		
	
	
| 
 | |
| using UnityEngine.Serialization;
 | |
| 
 | |
| namespace Guru
 | |
| {
 | |
|     using System;
 | |
|     using UnityEngine;
 | |
|     using System.Collections.Generic;
 | |
| 
 | |
|     
 | |
|     [Serializable]
 | |
|     public class GuruInventoryModel: GuruModelBase
 | |
|     {
 | |
|         public Dictionary<string, UserItemInfo> data;
 | |
|         public Dictionary<string, UserItemInfo> Data => data ??= new Dictionary<string, UserItemInfo>(20);
 | |
|         
 | |
|         public bool RegisterItem(string itemName, int initCount = 0, string type = "")
 | |
|         {
 | |
|             if (!HasItem(itemName))
 | |
|             {
 | |
|                 Data[itemName] = new UserItemInfo()
 | |
|                 {
 | |
|                     key = itemName,
 | |
|                     free_amount = initCount,
 | |
|                     paid_amount = 0,
 | |
|                     type = type
 | |
|                 };
 | |
|                 return true;
 | |
|             }
 | |
|             return false;
 | |
|         }
 | |
|         
 | |
|         
 | |
|         public bool HasItem(string itemName)
 | |
|         {
 | |
|             return Data.ContainsKey(itemName);
 | |
|         }
 | |
| 
 | |
|         public UserItemInfo GetItemInfo(string itemName)
 | |
|         {
 | |
|             if (Data.TryGetValue(itemName, out var item))
 | |
|             {
 | |
|                 return item;
 | |
|             }
 | |
|             return null;
 | |
|         }
 | |
| 
 | |
|     }
 | |
| 
 | |
| 
 | |
|     [Serializable]
 | |
|     public class UserItemInfo
 | |
|     {
 | |
|         public bool enableZeroProtection = true; // 默认开启 0 值保护
 | |
|         public string key;
 | |
|         public int free_amount;
 | |
|         public int paid_amount;
 | |
|         public string type;
 | |
| 
 | |
|         /// <summary>
 | |
|         /// 道具总量
 | |
|         /// </summary>
 | |
|         public int TotalAmount => free_amount + paid_amount;
 | |
|         
 | |
|         /// <summary>
 | |
|         /// 增减道具量
 | |
|         /// </summary>
 | |
|         /// <param name="amount"></param>
 | |
|         /// <param name="isPaid"></param>
 | |
|         public bool Change(int amount, bool isPaid = false)
 | |
|         {
 | |
|             if (amount == 0) return false;
 | |
|             
 | |
|             if (isPaid)
 | |
|             {
 | |
|                 if (amount < 0 && enableZeroProtection && paid_amount + amount < 0)
 | |
|                     return false;
 | |
|                 
 | |
|                 paid_amount += amount;
 | |
|             }
 | |
|             else
 | |
|             {
 | |
|                 if (amount < 0 && enableZeroProtection && free_amount + amount < 0)
 | |
|                     return false;
 | |
|                 
 | |
|                 free_amount += amount;
 | |
|             }
 | |
| 
 | |
|             return true;
 | |
|         }
 | |
|         
 | |
|         /// <summary>
 | |
|         /// 强制设置数量
 | |
|         /// </summary>
 | |
|         /// <param name="freeAmount"></param>
 | |
|         /// <param name="paidAmount"></param>
 | |
|         public void Set(int freeAmount, int paidAmount = 0)
 | |
|         {
 | |
|             paid_amount = paidAmount;
 | |
|             free_amount = freeAmount;
 | |
|         }
 | |
|         
 | |
|         /// <summary>
 | |
|         /// 数值清零
 | |
|         /// </summary>
 | |
|         public void Clean()
 | |
|         {
 | |
|             paid_amount = 0;
 | |
|             free_amount = 0;
 | |
|         }
 | |
| 
 | |
| 
 | |
|     }
 | |
| 
 | |
| 
 | |
| 
 | |
| } |