/// <summary> /// 从源数据列表中随机取出一个奖品 /// </summary> /// <param name="coins">金币数</param> /// <param name="sourceData">源数据</param> /// <returns></returns> private List <AwardData> FindPropAwards(int coins, List <AwardData> sourceData, ref int leftCoins) { if (coins <= 0) { throw new System.Exception("<><AwardDataHelper.FindPropAwards>Paramater 'cost' must not be less than zero"); } else if (sourceData == null || sourceData.Count == 0) { throw new System.Exception("<><AwardDataHelper.FindPropAwards>Paramater 'sourceData' is null or its length is zero"); } List <AwardData> result = new List <AwardData>(); sourceData = sourceData.FindAll(t => t.Price <= coins);//过滤出价格小于金币数的奖品 if (sourceData.Count == 0) { return(result); //如果一个都没有,则返回空的集合 } int randomIndex = Random.Range(0, sourceData.Count * 10) % sourceData.Count; AwardData awardData = sourceData[randomIndex]; //随机取出一个奖品 int count = coins / awardData.Price; //计算金币数一共购买几个奖品 if (coins % awardData.Price != 0 && (awardData.Price * (count + 1)) / (float)coins <= 1.1f) //如果金币还有剩余,尝试再多买一个,但不能超出所有金币数的10% { count += 1; } leftCoins = coins - awardData.Price * count; awardData.Count = count; result.Add(awardData); return(result); }
/// <summary> /// 增加奖品 /// </summary> /// <param name="awardData">奖品数据</param> public void AddItem(AwardData awardData) { if (awardData != null) { if (awardData.AwardType.ToLower() == AwardTypes.COIN.ToLower() || awardData.AwardType.ToLower() == AwardTypes.FOOD.ToLower()) { this.FundDataManager.AddProperty(awardData.ID, awardData.Count); } else { this.ItemDataManager.AddItem(awardData.ID, awardData.Count); } } else { Debug.LogError("<><AwardDataHelper.AddItem>Parameter 'awardData' is null"); } }