2024-05-15 18:37:08 +08:00
|
|
|
|
|
|
|
|
|
|
/****************************************************
|
|
|
|
|
|
文件:Assets/Code/Scripts/Gameplay/UI/Energy/EnergyManager.cs
|
|
|
|
|
|
作者:yuntao-Hu
|
|
|
|
|
|
时间:2024/5
|
|
|
|
|
|
描述:管理体力相关信息
|
|
|
|
|
|
接口:
|
2024-05-20 12:51:49 +08:00
|
|
|
|
public int ItemIDBuyEnergyCount()
|
|
|
|
|
|
public int GetItemIDBuyEnergy(int index)
|
2024-05-15 18:37:08 +08:00
|
|
|
|
public int GetMaxEnergyPoint()
|
|
|
|
|
|
public uint GetNowEnergy()
|
|
|
|
|
|
public DateTime GetNextEnergyRecoverTime()
|
|
|
|
|
|
|
|
|
|
|
|
public void BuyEnergy(int itemID, uint usageAmount)
|
|
|
|
|
|
public bool isItemCanBuyEnergy(int itemID)
|
|
|
|
|
|
public int GetConvertConsumes(int itemID, int usageAmount = 1)
|
|
|
|
|
|
public int GetConvertRecharges(int itemID, int usageAmount = 1)
|
2024-05-17 19:53:15 +08:00
|
|
|
|
|
2024-05-15 18:37:08 +08:00
|
|
|
|
*****************************************************/
|
|
|
|
|
|
|
2024-05-17 19:53:15 +08:00
|
|
|
|
using cfg.ActorCfg;
|
2024-05-15 14:24:25 +08:00
|
|
|
|
using Framework;
|
2024-05-17 19:53:15 +08:00
|
|
|
|
using Gameplay;
|
2024-05-15 14:24:25 +08:00
|
|
|
|
using Gameplay.Common;
|
|
|
|
|
|
using Gameplay.Net;
|
|
|
|
|
|
using NLDPB;
|
|
|
|
|
|
using PhxhSDK;
|
|
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
|
|
|
|
|
|
public class EnergyManager : Singlenton<EnergyManager>
|
|
|
|
|
|
{
|
2024-05-17 19:53:15 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 体力药物品ID
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
private const int ENERGY_ITEM_ID = 8;//TODO 体力药物品ID,考虑加入globalconfig
|
|
|
|
|
|
private readonly int[] itemIdBuyEnergy = { ENERGY_ITEM_ID, GLConfig.Inst.data.DiamondItemId };//按优先级顺序,将钻石补充方式放在最后
|
|
|
|
|
|
|
2024-05-20 12:51:49 +08:00
|
|
|
|
ConvertRatesWrap convertRatesWrap;
|
|
|
|
|
|
|
2024-05-15 18:37:08 +08:00
|
|
|
|
#region class
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 存储体力转换率
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
class ConvertRatesWrap
|
|
|
|
|
|
{
|
|
|
|
|
|
private const int CONVERSION_RATE_ITEM_TOENERGY = 1;
|
|
|
|
|
|
private const int CONVERSION_RATE_ENERGY_FROMITEM = 30;
|
|
|
|
|
|
private Dictionary<int, ConversionRate> convertRates;
|
|
|
|
|
|
|
|
|
|
|
|
public ConvertRatesWrap()
|
|
|
|
|
|
{
|
|
|
|
|
|
convertRates = new Dictionary<int, ConversionRate>();
|
|
|
|
|
|
|
2024-05-20 12:51:49 +08:00
|
|
|
|
for(int i = 0; i < EnergyManager.Instance.itemIdBuyEnergy.Length; i++)
|
|
|
|
|
|
{
|
|
|
|
|
|
int itemID = EnergyManager.Instance.itemIdBuyEnergy[i];
|
|
|
|
|
|
AddRate(itemID, RateConfig(itemID));
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
List<int> RateConfig(int itemID)
|
|
|
|
|
|
{
|
|
|
|
|
|
//TODO 更新后续读取配置的方式
|
|
|
|
|
|
if (itemID == GLConfig.Inst.data.DiamondItemId)
|
|
|
|
|
|
{
|
|
|
|
|
|
return GLConfig.Inst.data.DiamondExchangeEnergy;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
DebugUtil.LogError("配置中不包含的体力补充方式!请更新配置\n错误物品id: " + itemID);
|
|
|
|
|
|
return default;
|
|
|
|
|
|
}
|
2024-05-15 18:37:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
void AddRate(int itemID, int consume, int recharge)
|
|
|
|
|
|
{
|
|
|
|
|
|
convertRates.Add(itemID, new ConversionRate(consume, recharge));
|
|
|
|
|
|
}
|
|
|
|
|
|
void AddRate(int itemID, List<int> exchangeRate)
|
|
|
|
|
|
{
|
2024-05-20 12:51:49 +08:00
|
|
|
|
if (exchangeRate == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
DebugUtil.LogError("传入转化率list为null,该转化率在配置中不存在\n错误物品id: " + itemID);
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
2024-05-15 18:37:08 +08:00
|
|
|
|
if (exchangeRate.Count >= 2)
|
|
|
|
|
|
convertRates.Add(itemID, new ConversionRate(exchangeRate[0], exchangeRate[1]));
|
|
|
|
|
|
else
|
2024-05-20 12:51:49 +08:00
|
|
|
|
DebugUtil.LogError("转化率list传入参数不足!\n错误物品id: " + itemID);
|
2024-05-15 18:37:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//API
|
|
|
|
|
|
public bool isRateContained(int itemID)
|
|
|
|
|
|
{
|
|
|
|
|
|
return convertRates.ContainsKey(itemID);
|
|
|
|
|
|
}
|
|
|
|
|
|
public int GetConsume(int itemID)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (convertRates.ContainsKey(itemID))
|
|
|
|
|
|
{
|
|
|
|
|
|
return convertRates[itemID].consume;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
DebugUtil.LogError("不包含的体力补充方式!请检查!");
|
|
|
|
|
|
return default;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
public int GetRecharge(int itemID)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (convertRates.ContainsKey(itemID))
|
|
|
|
|
|
{
|
|
|
|
|
|
return convertRates[itemID].recharge;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
DebugUtil.LogError("不包含的体力补充方式!请检查!");
|
|
|
|
|
|
return default;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//class
|
|
|
|
|
|
public class ConversionRate
|
|
|
|
|
|
{
|
|
|
|
|
|
public int consume;
|
|
|
|
|
|
public int recharge;
|
|
|
|
|
|
|
|
|
|
|
|
public ConversionRate(int consume, int recharge)
|
|
|
|
|
|
{
|
|
|
|
|
|
this.consume = consume;
|
|
|
|
|
|
this.recharge = recharge;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
#region API
|
2024-05-20 12:51:49 +08:00
|
|
|
|
public int ItemIDBuyEnergyCount()
|
|
|
|
|
|
{
|
|
|
|
|
|
return itemIdBuyEnergy.Length;
|
|
|
|
|
|
}
|
2024-05-17 19:53:15 +08:00
|
|
|
|
public int GetItemIDBuyEnergy(int index)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (index < itemIdBuyEnergy.Length)
|
|
|
|
|
|
return itemIdBuyEnergy[index];
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
DebugUtil.LogError("购买体力物品ID超出范围,请检查!");
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-05-15 14:24:25 +08:00
|
|
|
|
/// <summary>
|
2024-05-15 18:37:08 +08:00
|
|
|
|
/// 获取最大体力
|
2024-05-15 14:24:25 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public int GetMaxEnergyPoint()
|
|
|
|
|
|
{
|
2024-05-20 12:51:49 +08:00
|
|
|
|
var level = Actor.Instance.Base.GetProp(ActorProp.Level);
|
|
|
|
|
|
var actorCfg = ActorLevelTableHelper.Instance.Get((int)level);
|
2024-05-15 19:49:25 +08:00
|
|
|
|
return actorCfg.MaxEnergy;
|
2024-05-15 14:24:25 +08:00
|
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
2024-05-15 18:37:08 +08:00
|
|
|
|
/// 获取当前体力
|
2024-05-15 14:24:25 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public uint GetNowEnergy()
|
|
|
|
|
|
{
|
|
|
|
|
|
return Actor.Instance.Item.GetItemCount(GLConfig.Inst.data.EnergyPointID);
|
|
|
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
2024-05-15 18:37:08 +08:00
|
|
|
|
/// 获取下一次体力恢复时间
|
2024-05-15 14:24:25 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public DateTime GetNextEnergyRecoverTime()
|
|
|
|
|
|
{
|
|
|
|
|
|
DateTime lastDateTime = CommonUtils.GetTimeByTimestamp(Actor.Instance.Logic.GetCount((uint)LogicID.EnergyRecoverTime));
|
|
|
|
|
|
|
|
|
|
|
|
return lastDateTime.AddSeconds(TableManager.Instance.Tables.GlobalConfig.EnergyPointRecoverTime);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2024-05-15 18:37:08 +08:00
|
|
|
|
|
|
|
|
|
|
public void BuyEnergy(int itemID, uint usageAmount)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (isItemCanBuyEnergy(itemID))
|
|
|
|
|
|
NetHelper.ConvertResources((uint)itemID, usageAmount, (uint)GLConfig.Inst.data.EnergyPointID);
|
|
|
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 该物品是否有补充体力功能
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="itemID"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public bool isItemCanBuyEnergy(int itemID)
|
|
|
|
|
|
{
|
2024-05-20 12:51:49 +08:00
|
|
|
|
convertRatesWrap ??= new ConvertRatesWrap();
|
2024-05-15 18:37:08 +08:00
|
|
|
|
return convertRatesWrap.isRateContained(itemID);
|
|
|
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 每份物品补充体力时的物品实际消耗量,参数可设定份数
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="itemID"></param>
|
|
|
|
|
|
/// <param name="usageAmount">物品份数</param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public int GetConvertConsumes(int itemID, int usageAmount = 1)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (isItemCanBuyEnergy(itemID))
|
|
|
|
|
|
return usageAmount * convertRatesWrap.GetConsume(itemID);
|
|
|
|
|
|
else
|
2024-05-17 19:53:15 +08:00
|
|
|
|
{
|
|
|
|
|
|
DebugUtil.LogError("补充体力列表内不包含该物品,不可补充体力!请检查");
|
2024-05-15 18:37:08 +08:00
|
|
|
|
return 0;
|
2024-05-17 19:53:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-05-15 18:37:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 每份物品补充体力时的体力补充量,参数可设定份数
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="itemID"></param>
|
|
|
|
|
|
/// <param name="usageAmount">物品份数</param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public int GetConvertRecharges(int itemID, int usageAmount = 1)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (isItemCanBuyEnergy(itemID))
|
|
|
|
|
|
return usageAmount * convertRatesWrap.GetRecharge(itemID);
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2024-05-17 19:53:15 +08:00
|
|
|
|
DebugUtil.LogError("补充体力列表内不包含该物品,不可补充体力!请检查");
|
|
|
|
|
|
return 0;
|
2024-05-15 18:37:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2024-05-17 19:53:15 +08:00
|
|
|
|
/// 获取当前选中状态下的最大消耗数目(实际数据需要乘比例转化)
|
2024-05-15 18:37:08 +08:00
|
|
|
|
/// </summary>
|
2024-05-17 19:53:15 +08:00
|
|
|
|
/// <param name="itemID"></param>
|
|
|
|
|
|
/// <param name="itemLimited"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public int MaxConsumeAmountWithoutConvert(int itemID, out bool itemLimited)
|
2024-05-15 18:37:08 +08:00
|
|
|
|
{
|
2024-05-17 19:53:15 +08:00
|
|
|
|
int maxConsumeRate = 0;
|
|
|
|
|
|
int maxConsumeRateByEnergy = 0;
|
2024-05-15 18:37:08 +08:00
|
|
|
|
|
2024-05-17 19:53:15 +08:00
|
|
|
|
if (itemID <= 0)
|
2024-05-15 18:37:08 +08:00
|
|
|
|
{
|
2024-05-17 19:53:15 +08:00
|
|
|
|
DebugUtil.LogError("补充体力方式对应的物品ID不存在,请检查配置!");
|
2024-05-15 18:37:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-05-17 19:53:15 +08:00
|
|
|
|
//道具数目限制
|
|
|
|
|
|
uint AmountInStorage = Actor.Instance.Item.GetItemCount(itemID);
|
|
|
|
|
|
if (GetConvertConsumes(itemID) != 0)
|
|
|
|
|
|
maxConsumeRate = (int)(AmountInStorage / GetConvertConsumes(itemID));
|
|
|
|
|
|
|
|
|
|
|
|
//体力上限限制
|
|
|
|
|
|
DataItem energyData = TableManager.Instance.Tables.Item.Get(GLConfig.Inst.data.EnergyPointID);
|
|
|
|
|
|
uint energyToMax = (uint)energyData.MaxCount - Actor.Instance.Item.GetItemCount(GLConfig.Inst.data.EnergyPointID);
|
|
|
|
|
|
if (GetConvertRecharges(itemID) != 0)
|
|
|
|
|
|
maxConsumeRateByEnergy = (int)(energyToMax / GetConvertRecharges(itemID));
|
|
|
|
|
|
|
|
|
|
|
|
itemLimited = maxConsumeRate <= maxConsumeRateByEnergy;
|
|
|
|
|
|
|
|
|
|
|
|
return Math.Min(maxConsumeRate, maxConsumeRateByEnergy);
|
|
|
|
|
|
}
|
2024-05-15 18:37:08 +08:00
|
|
|
|
#endregion
|
2024-05-15 14:24:25 +08:00
|
|
|
|
}
|