265 lines
8.6 KiB
C#
265 lines
8.6 KiB
C#
|
||
/****************************************************
|
||
文件:Assets/Code/Scripts/Gameplay/UI/Energy/EnergyManager.cs
|
||
作者:yuntao-Hu
|
||
时间:2024/5
|
||
描述:管理体力相关信息
|
||
接口:
|
||
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)
|
||
public bool Item2Energy(int itemID, int itemAmount, out int energyAmount)
|
||
public bool ItemActually2Energy(int itemID, int itemAmount, out int energyAmount)
|
||
public bool Energy2Item(int itemID, int energyAmount, out int itemAount)
|
||
public bool Energy2ItemActually(int itemID, int energyAmount, out int itemAount)
|
||
*****************************************************/
|
||
|
||
using Framework;
|
||
using Gameplay.Common;
|
||
using Gameplay.Net;
|
||
using Gameplay;
|
||
using NLDPB;
|
||
using PhxhSDK;
|
||
using System;
|
||
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using UnityEngine;
|
||
using static UI_BuyEnergyController;
|
||
|
||
public class EnergyManager : Singlenton<EnergyManager>
|
||
{
|
||
|
||
ConvertRatesWrap convertRatesWrap = new ConvertRatesWrap();
|
||
|
||
#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>();
|
||
|
||
//TODO 根据后续需求,考虑用配置读入转化率
|
||
//AddRate(CONVERSION_RATE_ITEM_TOENERGY, CONVERSION_RATE_ENERGY_FROMITEM);
|
||
AddRate(GLConfig.Inst.data.DiamondItemId, GLConfig.Inst.data.DiamondExchangeEnergy);
|
||
}
|
||
void AddRate(int itemID, int consume, int recharge)
|
||
{
|
||
convertRates.Add(itemID, new ConversionRate(consume, recharge));
|
||
}
|
||
void AddRate(int itemID, List<int> exchangeRate)
|
||
{
|
||
if (exchangeRate.Count >= 2)
|
||
convertRates.Add(itemID, new ConversionRate(exchangeRate[0], exchangeRate[1]));
|
||
else
|
||
DebugUtil.LogError("转化率list传入参数不足!");
|
||
}
|
||
|
||
//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
|
||
|
||
/// <summary>
|
||
/// 获取最大体力
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
public int GetMaxEnergyPoint()
|
||
{
|
||
var level = Actor.Instance.Base.GetProp(ActorProp.Level);
|
||
var actorCfg = TableManager.Instance.Tables.ActorLevel.DataList[(int)level - 1];
|
||
return actorCfg.MaxEnergy;
|
||
}
|
||
/// <summary>
|
||
/// 获取当前体力
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
public uint GetNowEnergy()
|
||
{
|
||
return Actor.Instance.Item.GetItemCount(GLConfig.Inst.data.EnergyPointID);
|
||
}
|
||
/// <summary>
|
||
/// 获取下一次体力恢复时间
|
||
/// </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);
|
||
}
|
||
|
||
|
||
|
||
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)
|
||
{
|
||
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
|
||
return 0;
|
||
}
|
||
/// <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
|
||
return 0;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 该物品(份数)可以兑换成多少体力
|
||
/// </summary>
|
||
/// <param name="itemID">物品ID</param>
|
||
/// <param name="itemAmount">消耗物品数量(用于传给服务器的份数)</param>
|
||
/// <param name="energyAmount">可兑换体力数目</param>
|
||
/// <returns>能否兑换</returns>
|
||
public bool Item2Energy(int itemID, int itemAmount, out int energyAmount)
|
||
{
|
||
energyAmount = 0;
|
||
if (isItemCanBuyEnergy(itemID))
|
||
{
|
||
energyAmount = itemAmount * convertRatesWrap.GetRecharge(itemID);
|
||
return true;
|
||
}
|
||
else
|
||
return false;
|
||
}
|
||
/// <summary>
|
||
/// 该物品(实际值)可以兑换成多少体力
|
||
/// </summary>
|
||
/// <param name="itemID">物品ID</param>
|
||
/// <param name="itemAmount">消耗物品数量(显示实际值)</param>
|
||
/// <param name="energyAmount">可兑换体力数目</param>
|
||
/// <returns>能否兑换</returns>
|
||
public bool ItemActually2Energy(int itemID, int itemAmount, out int energyAmount)
|
||
{
|
||
energyAmount = 0;
|
||
if (isItemCanBuyEnergy(itemID))
|
||
{
|
||
energyAmount = itemAmount / convertRatesWrap.GetConsume(itemID) * convertRatesWrap.GetRecharge(itemID);
|
||
return true;
|
||
}
|
||
else
|
||
return false;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 兑换相应体力需要多少物品(份数)
|
||
/// </summary>
|
||
/// <param name="itemID">物品ID</param>
|
||
/// <param name="energyAmount">兑换体力数目</param>
|
||
/// <param name="itemAount">需消耗物品数量(用于传给服务器的份数)</param>
|
||
/// <returns>能否兑换</returns>
|
||
public bool Energy2Item(int itemID, int energyAmount, out int itemAount)
|
||
{
|
||
itemAount = 0;
|
||
if (isItemCanBuyEnergy(itemID))
|
||
{
|
||
itemAount = (int)Math.Ceiling((double)energyAmount / convertRatesWrap.GetRecharge(itemID)) * convertRatesWrap.GetConsume(itemID);
|
||
return true;
|
||
}
|
||
else
|
||
return false;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 兑换相应体力需要多少物品(实际值)
|
||
/// </summary>
|
||
/// <param name="itemID">物品ID</param>
|
||
/// <param name="energyAmount">兑换体力数目</param>
|
||
/// <param name="itemAount">需消耗物品数量(显示实际值)</param>
|
||
/// <returns>能否兑换</returns>
|
||
public bool Energy2ItemActually(int itemID, int energyAmount, out int itemAount)
|
||
{
|
||
itemAount = 0;
|
||
if (isItemCanBuyEnergy(itemID))
|
||
{
|
||
itemAount = (int)Math.Ceiling((double)energyAmount / convertRatesWrap.GetRecharge(itemID)) * convertRatesWrap.GetConsume(itemID);
|
||
return true;
|
||
}
|
||
else
|
||
return false;
|
||
}
|
||
|
||
#endregion
|
||
}
|