Files
Continentis/Assets/Scripts/MainGame/Character/CharacterAssistanceFunctions.cs

147 lines
4.7 KiB
C#
Raw Normal View History

2025-10-03 00:02:43 -04:00
using UnityEngine;
namespace Continentis.MainGame.Character
{
2025-10-23 00:49:44 -04:00
#region Fraction
public partial class CharacterBase
{
public bool IsOpponent(CharacterBase other)
{
if (this.fraction is Fraction.Player or Fraction.Ally)
{
return other.fraction is Fraction.Enemy or Fraction.Neutral;
}
else if (this.fraction is Fraction.Enemy)
{
return other.fraction is Fraction.Player or Fraction.Ally or Fraction.Neutral;
}
else // Neutral
{
return other.fraction is Fraction.Player or Fraction.Ally or Fraction.Enemy;
}
}
public bool IsAlly(CharacterBase other)
{
if (this.fraction is Fraction.Player or Fraction.Ally)
{
return other.fraction is Fraction.Player or Fraction.Ally;
}
else if (this.fraction is Fraction.Enemy)
{
return other.fraction is Fraction.Enemy;
}
else // Neutral
{
return other.fraction is Fraction.Neutral;
}
}
}
#endregion
2025-10-03 00:02:43 -04:00
#region Attributes
public partial class CharacterBase
{
/// <summary>
2026-04-08 04:48:35 -04:00
/// 获取角色的属性值
2025-10-03 00:02:43 -04:00
/// </summary>
2025-10-23 00:49:44 -04:00
public int GetAttribute(string attributeName, int defaultValue = 0)
2025-10-03 00:02:43 -04:00
{
if (attributeSubmodule.generalAttributeGroup.current.ContainsKey(attributeName))
{
2025-11-08 09:50:55 -05:00
return attributeSubmodule.GetGeneralAttribute(attributeName);
2025-10-03 00:02:43 -04:00
}
2025-10-23 00:49:44 -04:00
2026-04-08 04:48:35 -04:00
Debug.LogWarning($"Attribute {attributeName} not found in attribute group.");
2025-10-23 00:49:44 -04:00
return defaultValue;
2025-10-03 00:02:43 -04:00
}
2025-10-23 00:49:44 -04:00
/// <summary>
2026-04-08 04:48:35 -04:00
/// 获取角色的原始属性值
2025-10-23 00:49:44 -04:00
/// 如果属性名包含“Multiplier”默认值改为1
/// </summary>
public float GetRawAttribute(string attributeName, float defaultValue = 0)
2025-10-03 00:02:43 -04:00
{
if (attributeSubmodule.generalAttributeGroup.current.TryGetValue(attributeName, out float attribute))
{
return attribute;
}
2026-04-08 04:48:35 -04:00
Debug.LogWarning($"Attribute {attributeName} not found in attribute group.");
2025-10-23 00:49:44 -04:00
if(attributeName.Contains("Multiplier"))
{
defaultValue = 1f;
}
return defaultValue;
2025-10-03 00:02:43 -04:00
}
/// <summary>
2026-04-08 04:48:35 -04:00
/// 设置角色的属性值
2025-10-03 00:02:43 -04:00
/// </summary>
public void SetAttribute(string attributeName, int value)
{
if (attributeSubmodule.generalAttributeGroup.current.ContainsKey(attributeName))
{
attributeSubmodule.generalAttributeGroup.current[attributeName] = value;
return;
}
2026-04-08 04:48:35 -04:00
Debug.LogWarning($"Attribute {attributeName} not found in attribute group.");
2025-10-03 00:02:43 -04:00
}
/// <summary>
2026-04-08 04:48:35 -04:00
/// 修改角色的属性值
2025-10-03 00:02:43 -04:00
/// </summary>
public void ModifyAttribute(string attributeName, int delta)
{
2026-04-08 04:48:35 -04:00
if (attributeSubmodule.generalAttributeGroup.current.ContainsKey(attributeName))
2025-10-03 00:02:43 -04:00
{
2026-04-08 04:48:35 -04:00
attributeSubmodule.generalAttributeGroup.current[attributeName] += delta;
2025-10-03 00:02:43 -04:00
return;
}
2026-04-08 04:48:35 -04:00
Debug.LogWarning($"Attribute {attributeName} not found in attribute group.");
}
/// <summary>
/// 修改角色的属性值并进行范围限制
/// </summary>
/// <returns>实际的添加或减少的数值(可能因为范围限制而小于 delta</returns>
public int ModifyAndClampAttribute(string attributeName, int delta, int min = 0, int max = int.MaxValue)
{
int currentAttribute = GetAttribute(attributeName);
int actualModifiedValue = delta;
if (delta < 0)
2025-10-03 00:02:43 -04:00
{
2026-04-08 04:48:35 -04:00
actualModifiedValue = Mathf.Min(-delta, currentAttribute - min);
}
else if (delta > 0)
{
actualModifiedValue = Mathf.Min(delta, max - currentAttribute);
2025-10-03 00:02:43 -04:00
}
2026-04-08 04:48:35 -04:00
ModifyAttribute(attributeName, delta);
ClampAttribute(attributeName, min, max);
return actualModifiedValue;
2025-10-03 00:02:43 -04:00
}
2025-10-23 00:49:44 -04:00
public void ClampAttribute(string attributeName, int min, int max)
{
if (GetAttribute(attributeName) < min)
{
SetAttribute(attributeName, min);
}
else if (GetAttribute(attributeName) > max)
{
SetAttribute(attributeName, max);
}
}
2025-10-03 00:02:43 -04:00
}
#endregion
}