Files
Cielonos/Assets/Scripts/MainGame/Characters/Player/PlayerFunctions.cs

109 lines
3.8 KiB
C#
Raw Normal View History

2026-01-03 18:19:39 -05:00
using UnityEngine;
namespace Cielonos.MainGame.Characters
{
public partial class Player
{
private float deltaTime => selfTimeSm.DeltaTime;
}
public partial class Player
{
private void Regeneration()
{
2026-05-23 08:27:50 -04:00
float healthRegenRate = attributeSm[CharacterAttribute.HealthRegeneration] * deltaTime;
2026-01-03 18:19:39 -05:00
if (healthRegenRate != 0)
{
2026-05-23 08:27:50 -04:00
attributeSm[CharacterAttribute.Health] += healthRegenRate;
attributeSm[CharacterAttribute.Health] = Mathf.Min(attributeSm[CharacterAttribute.Health], attributeSm[CharacterAttribute.MaximumHealth]);
// UI 更新由 AttributeSubmodule 的值变更回调自动触发
2026-01-03 18:19:39 -05:00
}
2026-05-23 08:27:50 -04:00
float energyRegenRate = attributeSm[CharacterAttribute.EnergyRegeneration] * deltaTime;
2026-01-03 18:19:39 -05:00
if (energyRegenRate != 0)
{
2026-04-18 13:57:19 -04:00
AddEnergy(energyRegenRate);
}
}
2026-05-23 08:27:50 -04:00
/// <summary>
/// 增减能量值,正值为增加(溢出部分按 OverloadConversionRate 转换为过载能量),负值为消耗。
/// UI 更新和 onEnergyChanged 事件由属性值变更回调自动触发。
/// </summary>
2026-04-18 13:57:19 -04:00
public void AddEnergy(float amount)
{
if (amount == 0) return;
if (amount > 0)
{
2026-05-23 08:27:50 -04:00
float current = attributeSm[CharacterAttribute.Energy];
float max = attributeSm[CharacterAttribute.MaximumEnergy];
2026-04-18 13:57:19 -04:00
float availableSpace = max - current;
if (amount > availableSpace)
{
2026-05-23 08:27:50 -04:00
attributeSm[CharacterAttribute.Energy] = max;
2026-04-18 13:57:19 -04:00
2026-05-23 08:27:50 -04:00
float conversionRate = attributeSm.Has(CharacterAttribute.OverloadConversionRate) ? attributeSm[CharacterAttribute.OverloadConversionRate] : 1f;
2026-04-18 13:57:19 -04:00
float overflowEnergy = (amount - availableSpace) * conversionRate;
DistributeOverloadEnergy(overflowEnergy);
}
else
{
2026-05-23 08:27:50 -04:00
attributeSm[CharacterAttribute.Energy] += amount;
2026-04-18 13:57:19 -04:00
}
}
else
{
2026-05-23 08:27:50 -04:00
attributeSm[CharacterAttribute.Energy] += amount;
attributeSm[CharacterAttribute.Energy] = Mathf.Max(0, attributeSm[CharacterAttribute.Energy]);
2026-04-18 13:57:19 -04:00
}
}
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);
}
}
2026-05-10 11:47:55 -04:00
foreach (var equip in inventorySc.backpackSm.passiveEquipments)
2026-04-18 13:57:19 -04:00
{
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);
2026-01-03 18:19:39 -05:00
}
}
}
}