// ***********************************************************************
// Assembly : Game
// Author : Kimch
// Created : 2021-05-01
// Description :
// Last Modified By :
// Last Modified On :
// ***********************************************************************
//
//
// ***********************************************************************
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace G
{
///
/// 宝箱
///
public class BoxProxy : F.GameProxy
{
public class BoxInfo
{
public readonly ItemBox item;
private int _maxWeight = -1;
///
///
///
private int maxWeight
{
get
{
if (_maxWeight < 0)
{
_maxWeight = 0;
var items = item.items;
if (items != null)
for (int i = 0; i < items.Length; i++)
{
if (GetWeight(items[i]) > 0)
_maxWeight += GetWeight(items[i]);
}
}
return _maxWeight;
}
}
///
///
///
///
public BoxInfo(ItemBox boxItem)
{
this.item = boxItem;
}
///
///
///
///
///
static int GetId(int[] data)
{
return data[0];
}
///
///
///
///
///
static int GetWeight(int[] data)
{
return data[1];
}
///
///
///
///
///
static int GetCount(int[] data)
{
int len = data.Length;
if (len <= 2)
return 1;
if (len == 3)
return data[2];
return Random.Range(data[2], data[3]);
}
///
///
///
///
public Item.ItemInfo GetExtraItem()
{
var extra = item.extra;
if (extra != null)
{
return new Item.ItemInfo
{
id = GetId(extra),
count = GetCount(extra),
};
}
return default;
}
///
/// 随机一个
///
///
public Item.ItemInfo GetRndItem()
{
var curWeight = GlobalUtils.GetWeight(maxWeight);
var items = item.items;
if (items != null)
for (int i = 0, l = items.Length; i < l; i++)
{
var it = items[i];
if (GetWeight(it) <= 0)
{
continue;
}
curWeight -= GetWeight(it);
if (curWeight <= 0)
{
return new Item.ItemInfo
{
id = GetId(it),
count = GetCount(it),
};
}
}
return default;
}
///
///
///
///
///
public Item.ItemInfo GetRndItem(int extraWeight)
{
var items = item.items;
if (items != null)
{
int tmpMaxWeight = this.maxWeight;
for (int i = 0, l = items.Length; i < l; i++)
{
var it = items[i];
if (GetWeight(it) <= 0)
{
continue;
}
tmpMaxWeight += extraWeight;
}
var curWeight = GlobalUtils.GetWeight(tmpMaxWeight);
for (int i = 0, l = items.Length; i < l; i++)
{
var it = items[i];
if (GetWeight(it) <= 0)
{
continue;
}
curWeight -= (GetWeight(it) + extraWeight);
if (curWeight <= 0)
{
return new Item.ItemInfo
{
id = GetId(it),
count = GetCount(it),
};
}
}
}
return default;
}
///
///
///
///
private void GetRndItems(List result)
{
int rndCount = item.typeArgs[0];
//处理必中逻辑
for (int i = 0; i < item.items.Length; i++)
{
var it = item.items[i];
if (GetWeight(it) == 0)
{
if (--rndCount < 0)
break;
result.Add(new Item.ItemInfo
{
id = GetId(it),
count = GetCount(it),
});
}
}
//
while (rndCount > 0)
{
rndCount--;
result.Add(GetRndItem());
}
}
///
///
///
///
///
private void GetRndItems(List result, int extraWeight)
{
int rndCount = item.typeArgs[0];
//处理必中逻辑
for (int i = 0; i < item.items.Length; i++)
{
var it = item.items[i];
if (GetWeight(it) == 0)
{
if (--rndCount < 0)
break;
result.Add(new Item.ItemInfo
{
id = GetId(it),
count = GetCount(it),
});
}
}
//
while (rndCount > 0)
{
rndCount--;
result.Add(GetRndItem(extraWeight));
}
}
///
///
///
static List _TempList = new List(16);
///
/// 线程不安全
///
///
public IList GetRndItems()
{
_TempList.Clear();
GetRndItems(_TempList);
return _TempList;
}
///
///
///
///
///
public IList GetRndItems(int extraWeight)
{
_TempList.Clear();
GetRndItems(_TempList, extraWeight);
return _TempList;
}
///
///
///
///
public IList GetRndAndExtraItems()
{
_TempList.Clear();
var extra = GetExtraItem();
if (extra.id > 0)
_TempList.Add(extra);
GetRndItems(_TempList);
return _TempList;
}
///
///
///
///
public Item.ItemInfo GetRndItemByChapter(int index = 0)
{
var chapterIndex = Mathf.Clamp(LevelProxy.Instance.currentChapterId - 1, 0, item.items.Length - 1);
index = Mathf.Clamp(index, 0, item.items[chapterIndex].Length - 1);
var chapterBox = BoxProxy.Instance.GetBoxInfo(item.items[chapterIndex][index]);
return chapterBox.GetRndItem();
}
///
///
///
///
public IList GetRndItemsByChapter(int index = 0)
{
var chapterIndex = Mathf.Clamp(LevelProxy.Instance.currentChapterId - 1, 0, item.items.Length - 1);
index = Mathf.Clamp(index, 0, item.items[chapterIndex].Length - 1);
var chapterBox = BoxProxy.Instance.GetBoxInfo(item.items[chapterIndex][index]);
return chapterBox.GetRndItems();
}
}
#region Field
Dictionary _boxInfoDict;
#endregion
#region Method
///
/// 获取宝箱信息
///
///
///
public BoxInfo GetBoxInfo(int id)
{
_boxInfoDict.TryGetValue(id, out var result);
return result;
}
///
///
///
void InitBox()
{
var boxes = ItemProxy.Instance.GetStaticItems();
_boxInfoDict = new Dictionary(boxes.Count);
for (int i = 0; i < boxes.Count; i++)
{
var boxInfo = new BoxInfo(boxes[i]);
_boxInfoDict.Add(boxInfo.item.id, boxInfo);
}
}
private const string ARCHIVE_KEY_V1 = "box_v1";
public void LoadBox()
{
}
public void SaveBox()
{
}
#endregion
#region Proxy
public static BoxProxy Instance => GetInstance();
public override void LoadCompleted()
{
InitBox();
}
public override void ReadArchive()
{
LoadBox();
}
#endregion
}
}