Files
Cielonos/Assets/Scripts/MainGame/Characters/Player/PlayerFunctions.cs
SoulliesOfficial 649b7a5ddc 更新
2026-05-23 08:27:50 -04:00

109 lines
3.8 KiB
C#

using UnityEngine;
namespace Cielonos.MainGame.Characters
{
public partial class Player
{
private float deltaTime => selfTimeSm.DeltaTime;
}
public partial class Player
{
private void Regeneration()
{
float healthRegenRate = attributeSm[CharacterAttribute.HealthRegeneration] * deltaTime;
if (healthRegenRate != 0)
{
attributeSm[CharacterAttribute.Health] += healthRegenRate;
attributeSm[CharacterAttribute.Health] = Mathf.Min(attributeSm[CharacterAttribute.Health], attributeSm[CharacterAttribute.MaximumHealth]);
// UI 更新由 AttributeSubmodule 的值变更回调自动触发
}
float energyRegenRate = attributeSm[CharacterAttribute.EnergyRegeneration] * deltaTime;
if (energyRegenRate != 0)
{
AddEnergy(energyRegenRate);
}
}
/// <summary>
/// 增减能量值,正值为增加(溢出部分按 OverloadConversionRate 转换为过载能量),负值为消耗。
/// UI 更新和 onEnergyChanged 事件由属性值变更回调自动触发。
/// </summary>
public void AddEnergy(float amount)
{
if (amount == 0) return;
if (amount > 0)
{
float current = attributeSm[CharacterAttribute.Energy];
float max = attributeSm[CharacterAttribute.MaximumEnergy];
float availableSpace = max - current;
if (amount > availableSpace)
{
attributeSm[CharacterAttribute.Energy] = max;
float conversionRate = attributeSm.Has(CharacterAttribute.OverloadConversionRate) ? attributeSm[CharacterAttribute.OverloadConversionRate] : 1f;
float overflowEnergy = (amount - availableSpace) * conversionRate;
DistributeOverloadEnergy(overflowEnergy);
}
else
{
attributeSm[CharacterAttribute.Energy] += amount;
}
}
else
{
attributeSm[CharacterAttribute.Energy] += amount;
attributeSm[CharacterAttribute.Energy] = Mathf.Max(0, attributeSm[CharacterAttribute.Energy]);
}
}
private void DistributeOverloadEnergy(float totalOverflowAmount)
{
if (totalOverflowAmount <= 0) return;
var overloadSubmodules = new System.Collections.Generic.List<Inventory.OverloadSubmodule>();
if (inventorySc.equipmentSm.currentMainWeapon?.overloadSm != null)
{
overloadSubmodules.Add(inventorySc.equipmentSm.currentMainWeapon.overloadSm);
}
foreach (var equip in inventorySc.equipmentSm.currentSupportEquipments)
{
if (equip?.overloadSm != null)
{
overloadSubmodules.Add(equip.overloadSm);
}
}
foreach (var equip in inventorySc.backpackSm.passiveEquipments)
{
if (equip?.overloadSm != null)
{
overloadSubmodules.Add(equip.overloadSm);
}
}
if (overloadSubmodules.Count == 0) return;
float totalWeight = 0;
foreach (var sm in overloadSubmodules)
{
totalWeight += sm.currentWeight;
}
if (totalWeight <= 0) return;
foreach (var sm in overloadSubmodules)
{
float assignedAmount = totalOverflowAmount * (sm.currentWeight / totalWeight);
sm.ReceiveEnergy(assignedAmount);
}
}
}
}