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

532 lines
21 KiB
C#
Raw Normal View History

2025-10-03 00:02:43 -04:00
using System.Collections.Generic;
2025-11-15 09:08:36 -05:00
using System.Linq;
2025-10-03 00:02:43 -04:00
using Continentis.MainGame.Card;
2025-10-23 00:49:44 -04:00
using Continentis.MainGame.Equipment;
using SLSFramework.General;
using SLSFramework.UModAssistance;
2025-10-03 00:02:43 -04:00
using UnityEngine;
namespace Continentis.MainGame.Character
{
public partial class CharacterBase
{
public virtual void InitializeCards()
{
string initialPile = this is PlayerHero ? "Draw" : "Pool";
2025-10-23 00:49:44 -04:00
foreach (string cardDataID in data.initialDeckRef)
2025-10-03 00:02:43 -04:00
{
2025-10-24 09:11:22 -04:00
CardInstance.GenerateCardInstance(ModManager.GetData<CardData>(cardDataID), this, initialPile);
2025-10-23 00:49:44 -04:00
}
foreach (EquipmentBase equipment in equipmentSubmodule.currentEquipments)
{
foreach (string cardDataID in equipment.equipmentData.belongingCardDataRefs)
2025-10-03 00:02:43 -04:00
{
2025-10-24 09:11:22 -04:00
CardInstance.GenerateCardInstance(ModManager.GetData<CardData>(cardDataID), this, initialPile);
2025-10-03 00:02:43 -04:00
}
}
}
}
2025-10-23 00:49:44 -04:00
public partial class CharacterBase
{
/// <summary>
/// 检查是否有足够的体力
/// </summary>
public bool CheckEnoughStamina(int staminaCost)
{
return GetAttribute("Stamina") >= staminaCost;
}
/// <summary>
/// 消耗体力
/// </summary>
public void ModifyStamina(int staminaValue)
{
ModifyAttribute("Stamina", staminaValue);
ClampAttribute("Stamina", 0, GetAttribute("MaximumStamina"));
2025-12-11 17:25:49 -05:00
if (this is PlayerHero)
{
CombatUIManager.Instance.combatMainPage.combatResourcesDisplayer.UpdateIcons();
}
2025-10-23 00:49:44 -04:00
}
/// <summary>
/// 检查是否有足够的魔法
/// </summary>
public bool CheckEnoughMana(int manaCost)
{
return GetAttribute("Mana") >= manaCost;
}
/// <summary>
/// 消耗魔法
/// </summary>
public void ModifyMana(int manaValue)
{
ModifyAttribute("Mana", manaValue);
2026-03-20 11:56:50 -04:00
ClampAttribute("Mana", 0, GetAttribute("MaximumMana"));
2025-12-11 17:25:49 -05:00
if (this is PlayerHero)
{
CombatUIManager.Instance.combatMainPage.combatResourcesDisplayer.UpdateIcons();
}
2025-10-23 00:49:44 -04:00
}
}
2025-10-03 00:02:43 -04:00
public partial class CharacterBase
{
2025-10-23 00:49:44 -04:00
/// <summary>
2026-04-01 12:23:27 -04:00
/// 攻击目标(新版本,使用 AttackContext 携带标签与来源信息)。
2025-10-23 00:49:44 -04:00
/// </summary>
2026-04-01 12:23:27 -04:00
/// <param name="target">攻击目标</param>
2025-10-23 00:49:44 -04:00
/// <param name="startDamage">初始伤害</param>
2026-04-01 12:23:27 -04:00
/// <param name="context">攻击上下文(包含来源卡牌、标签等);传 null 等价于默认上下文</param>
/// <returns>攻击结果</returns>
public AttackResult Attack(CharacterBase target, int startDamage, AttackContext context)
2025-10-03 00:02:43 -04:00
{
2026-04-01 12:23:27 -04:00
context ??= new AttackContext();
bool isSilent = context.HasTag(AttackTags.Silent);
bool isHpRemoval = context.HasTag(AttackTags.HpRemoval);
bool isReactive = context.HasTag(AttackTags.Reactive);
bool ignoreDodge = context.HasAnyTag(AttackTags.GuaranteedHit, AttackTags.HpRemoval);
bool ignoreBlock = context.HasAnyTag(AttackTags.Penetrating, AttackTags.HpRemoval);
bool ignoreShield = context.HasAnyTag(AttackTags.HpRemoval);
// 静默和生命移除均不触发 onStartAttack
if (!isSilent && !isHpRemoval)
2025-11-08 22:22:43 -05:00
{
eventSubmodule.onStartAttack.Invoke(target);
}
2026-04-01 12:23:27 -04:00
// 生命移除:直接扣血,跳过所有防御和事件
if (isHpRemoval)
{
target.HealthRemoval(startDamage, context);
target.characterView.hudContainer.enablingHUDs["MainAttributesBar"].UpdateHud();
return new AttackResult(this, target, startDamage, context, false, 0, 0, startDamage);
}
// 闪避检测
2025-10-23 00:49:44 -04:00
int modifiedStartDamageForDodge = Mathf.RoundToInt(startDamage * GetRawAttribute("DodgeCheckStartDamageMultiplier", 1));
bool dodged = !ignoreDodge && target.CheckDodge(modifiedStartDamageForDodge);
2025-10-03 00:02:43 -04:00
int hurt = 0;
int blocked = 0;
int shielded = 0;
if (!dodged)
{
2025-10-23 00:49:44 -04:00
int remainingDamageAfterBlock = ignoreBlock ? startDamage : target.CheckBlock(startDamage);
2025-10-03 00:02:43 -04:00
if (remainingDamageAfterBlock > 0)
{
blocked = startDamage - remainingDamageAfterBlock;
2025-10-23 00:49:44 -04:00
int remainingDamageAfterShield = ignoreShield ? remainingDamageAfterBlock : target.CheckShield(remainingDamageAfterBlock);
2025-10-03 00:02:43 -04:00
if (remainingDamageAfterShield > 0)
{
shielded = remainingDamageAfterBlock - remainingDamageAfterShield;
hurt = remainingDamageAfterShield;
2026-04-01 12:23:27 -04:00
target.HealthRemoval(remainingDamageAfterShield, context);
2025-10-03 00:02:43 -04:00
}
}
}
target.characterView.hudContainer.enablingHUDs["MainAttributesBar"].UpdateHud();
2026-04-01 12:23:27 -04:00
AttackResult attackResult = new AttackResult(this, target, startDamage, context, dodged, blocked, shielded, hurt);
if (!isSilent)
2025-10-31 10:02:30 -04:00
{
2026-04-01 12:23:27 -04:00
// 角色 EventSubmodule 级别事件(始终触发,用于日志等)
2025-11-08 22:22:43 -05:00
eventSubmodule.onFinishAttack.Invoke(target, attackResult);
2026-04-01 12:23:27 -04:00
target.eventSubmodule.onGetAttacked.Invoke(this, attackResult);
// Buff 层事件:响应式攻击不触发,防止无限递归
if (!isReactive)
2025-11-08 22:22:43 -05:00
{
2026-04-01 12:23:27 -04:00
combatBuffSubmodule.buffList.For(buff =>
{
buff.eventSubmodule?.onDealAttack.Invoke(attackResult);
});
target.combatBuffSubmodule.buffList.For(buff =>
{
buff.eventSubmodule?.onGetAttacked.Invoke(attackResult);
});
}
2025-10-31 10:02:30 -04:00
}
return attackResult;
2025-10-03 00:02:43 -04:00
}
2026-04-01 12:23:27 -04:00
/// <summary>
/// 攻击目标(兼容旧版 API内部转换为 AttackContext 调用)。
/// 新代码请优先使用 Attack(target, damage, AttackContext) 重载。
/// </summary>
public AttackResult Attack(CharacterBase target, int startDamage, CardInstance attackCard = null, bool triggerAttackEvent = true, bool ignoreDodge = false, bool ignoreBlock = false, bool ignoreShield = false)
{
var context = new AttackContext(attackCard);
if (!triggerAttackEvent) context.WithTag(AttackTags.Silent);
if (ignoreDodge) context.WithTag(AttackTags.GuaranteedHit);
if (ignoreBlock) context.WithTag(AttackTags.Penetrating);
return Attack(target, startDamage, context);
}
2025-10-23 00:49:44 -04:00
/// <summary>
/// 检查闪避(闪避失败后会清空闪避值)
/// </summary>
/// <param name="damage">即将受到的伤害</param>
/// <returns>是否闪避成功</returns>
2025-10-03 00:02:43 -04:00
public bool CheckDodge(int damage)
{
2025-11-08 09:50:55 -05:00
int dodge = attributeSubmodule.GetGeneralAttribute("Dodge");
2025-10-03 00:02:43 -04:00
if (dodge > 0)
{
bool success = damage <= dodge;
2026-04-01 12:23:27 -04:00
if (success)
{
MainGameManager.Instance.basePrefabs.GenerateInfoText("Dodged!", characterView);
return true;
}
else
2025-10-03 00:02:43 -04:00
{
attributeSubmodule.generalAttributeGroup.current["Dodge"] = 0;
2026-04-01 12:23:27 -04:00
return false;
2025-10-03 00:02:43 -04:00
}
}
return false;
}
2025-10-23 00:49:44 -04:00
/// <summary>
/// 检查格挡(并且扣除格挡值)
/// </summary>
/// <param name="damage">即将受到的伤害</param>
/// <returns>格挡之后的剩余伤害</returns>
2025-10-03 00:02:43 -04:00
public int CheckBlock(int damage)
{
2025-11-08 09:50:55 -05:00
int block = attributeSubmodule.GetGeneralAttribute("Block");
2025-10-03 00:02:43 -04:00
if (block > 0)
{
bool success = damage <= block;
int remainingDamage = 0;
int blockedDamage = block;
if (!success)
{
attributeSubmodule.generalAttributeGroup.current["Block"] = 0;
remainingDamage = damage - block;
}
else
{
attributeSubmodule.generalAttributeGroup.current["Block"] = block - damage;
blockedDamage = damage;
}
2025-10-23 00:49:44 -04:00
MainGameManager.Instance.basePrefabs.GenerateBlockedText(blockedDamage, characterView);
2025-10-03 00:02:43 -04:00
return remainingDamage;
}
return damage;
}
2025-10-23 00:49:44 -04:00
/// <summary>
2026-04-01 12:23:27 -04:00
/// 检查临时生命(并且扣除临时生命值)
2025-10-23 00:49:44 -04:00
/// </summary>
/// <param name="damage">即将受到的伤害</param>
2026-04-01 12:23:27 -04:00
/// <returns>临时生命吸收后的剩余伤害</returns>
2025-10-03 00:02:43 -04:00
public int CheckShield(int damage)
{
2026-04-01 12:23:27 -04:00
int shield = attributeSubmodule.GetGeneralAttribute("TemporaryHealth");
2025-10-03 00:02:43 -04:00
if (shield > 0)
{
bool success = damage <= shield;
int remainingDamage = 0;
int blockedDamage = shield;
if (!success)
{
2026-04-01 12:23:27 -04:00
attributeSubmodule.generalAttributeGroup.current["TemporaryHealth"] = 0;
2025-10-03 00:02:43 -04:00
remainingDamage = damage - shield;
}
else
{
2026-04-01 12:23:27 -04:00
attributeSubmodule.generalAttributeGroup.current["TemporaryHealth"] = shield - damage;
2025-10-03 00:02:43 -04:00
blockedDamage = damage;
}
2025-10-23 00:49:44 -04:00
MainGameManager.Instance.basePrefabs.GenerateBlockedText(blockedDamage, characterView);
2025-10-03 00:02:43 -04:00
return remainingDamage;
}
return damage;
}
2026-04-01 12:23:27 -04:00
public void HealthRemoval(int damage, AttackContext context = null)
2025-10-03 00:02:43 -04:00
{
2026-04-01 12:23:27 -04:00
int healthBefore = GetAttribute("Health");
2025-10-23 00:49:44 -04:00
ModifyAttribute("Health", -damage);
2026-04-01 12:23:27 -04:00
int healthAfter = GetAttribute("Health");
int maxHealth = GetAttribute("MaximumHealth");
2025-12-12 01:37:30 -05:00
2026-04-01 12:23:27 -04:00
Color dmgTextColor = Color.white;
if (context is { damageKeywords: { Count: > 0 } })
{
foreach (string elementTag in MainGameManager.Instance.elementTags)
{
if (context.damageKeywords.Contains(elementTag))
{
switch (elementTag)
{
case "Fire":
dmgTextColor = Color.red;
break;
case "Ice":
dmgTextColor = Color.cyan;
break;
case "Wind":
dmgTextColor = Color.lightGreen;
break;
case "Earth":
dmgTextColor = Color.darkGoldenRod;
break;
case "Storm":
dmgTextColor = Color.magenta;
break;
case "Light":
dmgTextColor = Color.yellowNice;
break;
case "Darkness":
dmgTextColor = Color.rebeccaPurple;
break;
default:
dmgTextColor = Color.white;
break;
}
}
}
}
MainGameManager.Instance.basePrefabs.GenerateHurtText(damage, characterView, dmgTextColor);
// 血量百分比阈值检查:穿越整十档时通知 LogicBase如 Boss 阶段切换)
if (maxHealth > 0 && logicBase != null)
{
float percentBefore = (float)healthBefore / maxHealth;
float percentAfter = (float)healthAfter / maxHealth;
// 找出所有被穿越的整十档(从高到低依次触发)
for (int threshold = 9; threshold >= 0; threshold--)
{
float t = threshold * 0.1f;
if (percentBefore > t && percentAfter <= t)
{
logicBase.OnHealthThreshold(t);
}
}
}
2025-12-12 01:37:30 -05:00
if (GetAttribute("Health") <= 0)
{
Die();
}
2025-10-03 00:02:43 -04:00
}
public void Heal(int heal)
{
if (heal <= 0) return;
2025-10-23 00:49:44 -04:00
ModifyAttribute("Health", heal);
ClampAttribute("Health", 0, GetAttribute("MaximumHealth"));
MainGameManager.Instance.basePrefabs.GenerateHealText(heal, characterView);
characterView.hudContainer.UpdateAllHUD();
}
}
public partial class CharacterBase
{
/// <summary>
/// 添加格挡(格挡每回合结束后会清空)
/// </summary>
2025-10-24 09:11:22 -04:00
public void AddBlock(int baseBlock, bool applyOffsetAndModifier = true, CharacterBase target = null)
2025-10-23 00:49:44 -04:00
{
target ??= this;
2025-10-24 09:11:22 -04:00
if (!applyOffsetAndModifier)
{
target.ModifyAttribute("Block", baseBlock);
}
else
{
int baseBlockAfterOffset = baseBlock + GetAttribute("BlockGainOffset");
int finalBlock = Mathf.RoundToInt(baseBlockAfterOffset * GetRawAttribute("BlockGainMultiplier", 1));
target.ModifyAttribute("Block", finalBlock);
}
2025-10-23 00:49:44 -04:00
target.characterView.hudContainer.UpdateAllHUD();
}
2025-10-24 09:11:22 -04:00
2025-10-23 00:49:44 -04:00
/// <summary>
/// 添加闪避(闪避在回合结束后或被击中后清空)
/// </summary>
public void AddDodge(int dodge, CharacterBase target = null)
{
int baseDodgeAfterOffset = dodge + GetAttribute("DodgeGainOffset");
int finalDodge = Mathf.RoundToInt(baseDodgeAfterOffset * GetRawAttribute("DodgeGainMultiplier", 1));
target ??= this;
target.ModifyAttribute("Dodge", finalDodge);
target.characterView.hudContainer.UpdateAllHUD();
}
/// <summary>
2026-04-01 12:23:27 -04:00
/// 添加临时生命(不会自动清空)
2025-10-23 00:49:44 -04:00
/// </summary>
public void AddShield(int shield, CharacterBase target = null)
{
2026-04-01 12:23:27 -04:00
int baseShieldAfterOffset = shield + GetAttribute("TemporaryHealthGainOffset");
int finalShield = Mathf.RoundToInt(baseShieldAfterOffset * GetRawAttribute("TemporaryHealthGainMultiplier", 1));
2025-10-23 00:49:44 -04:00
target ??= this;
2026-04-01 12:23:27 -04:00
target.ModifyAttribute("TemporaryHealth", finalShield);
2025-10-23 00:49:44 -04:00
target.characterView.hudContainer.UpdateAllHUD();
2025-10-03 00:02:43 -04:00
}
}
2025-11-15 09:08:36 -05:00
public partial class CharacterBase
{
public virtual void RegisterIntention(params IntentionBase[] intentions)
{
intentionSubmodule.allIntentions.AddRange(intentions);
}
public virtual void IntentionBrain()
{
List<IntentionBase> availableIntentions = intentionSubmodule.allIntentions.Where(intention => intention.Condition()).ToList();
availableIntentions.Sort();
intentionSubmodule.currentIntention = availableIntentions.FirstOrDefault() ?? new IntentionBase(intentionSubmodule);
intentionSubmodule.currentIntention.RefreshCardWeights();
intentionSubmodule.currentIntention.RefreshTargets();
}
public virtual void GetIntendedCards()
{
IntentionBase currentIntention = intentionSubmodule.currentIntention;
List<CardInstance> availableCards = deckSubmodule.PoolPile;
List<IntendedCard> intended = new List<IntendedCard>();
2025-11-16 09:56:20 -05:00
int predictedStamina = Mathf.Min(GetAttribute("MaximumStamina"), GetAttribute("Stamina") + GetAttribute("StaminaRecoverPerAction"));
int remainingStamina = predictedStamina - currentIntention.guaranteedStamina;
int predictedMana = Mathf.Min(GetAttribute("MaximumMana"), GetAttribute("Mana") + GetAttribute("ManaRecoverPerAction"));
int remainingMana = predictedMana - currentIntention.guaranteedMana;
2025-11-15 09:08:36 -05:00
List<CardInstance> forced = new List<CardInstance>();
List<CardInstance> normal = new List<CardInstance>();
foreach (CardInstance card in availableCards)
{
2025-11-15 12:17:34 -05:00
if (card.weightSubmodule.forceUse)
2025-11-15 09:08:36 -05:00
{
forced.Add(card);
}
else
{
normal.Add(card);
}
}
intentionSubmodule.intendedCards.Clear();
//(characterView.hudContainer.enablingHUDs["Intention"] as Intention)?.Clear();
// 1. 优先处理强制选择卡牌
foreach (CardInstance card in forced)
{
if (currentIntention.maxCardCount > 0 && intended.Count >= currentIntention.maxCardCount)
{
break; // 已达数量上限
}
if (CanAfford(card, remainingStamina, remainingMana))
{
if(!CheckAvailabilityAndSetTargets(card, out List<CharacterBase> targets))
{
continue; // 无有效目标或无法使用则跳过
}
intended.Add(new IntendedCard(card, targets));
2026-04-01 12:23:27 -04:00
remainingStamina -= card.GetAttribute(CardAttributes.StaminaCost);
remainingMana -= card.GetAttribute(CardAttributes.ManaCost);
2025-11-15 09:08:36 -05:00
}
// 行动力不足则跳过该卡
}
// 2. 在剩余普通卡牌中基于权重随机选取
while (intended.Count < currentIntention.maxCardCount)
{
// 筛选出当前资源下还能出的牌
List<CardInstance> affordableCards = normal.FindAll(card => CanAfford(card, remainingStamina, remainingMana));
if (affordableCards.Count == 0)
{
break;
}
2025-11-15 12:17:34 -05:00
float totalWeight = affordableCards.Sum(card => card.weightSubmodule.currentWeight);
2025-11-15 09:08:36 -05:00
if (totalWeight <= 0f) break;
float r = Random.value * totalWeight;
float accum = 0f;
CardInstance chosen = null;
foreach (CardInstance card in affordableCards)
{
2025-11-15 12:17:34 -05:00
accum += card.weightSubmodule.currentWeight;
2025-11-15 09:08:36 -05:00
if (r <= accum)
{
chosen = card;
break;
}
}
if (chosen != null)
{
if (!CheckAvailabilityAndSetTargets(chosen, out List<CharacterBase> targets))
{
normal.Remove(chosen);
continue; // 无有效目标或无法使用则跳过
}
intended.Add(new IntendedCard(chosen, targets));
normal.Remove(chosen);
2026-04-01 12:23:27 -04:00
remainingStamina -= chosen.GetAttribute(CardAttributes.StaminaCost);
remainingMana -= chosen.GetAttribute(CardAttributes.ManaCost);
2025-11-15 09:08:36 -05:00
}
}
intentionSubmodule.intendedCards.AddRange(intended);
}
2025-12-13 23:28:23 -05:00
bool CanAfford(CardInstance card, int stamina, int mana)
{
2026-04-01 12:23:27 -04:00
return card.GetAttribute(CardAttributes.StaminaCost) <= stamina &&
card.GetAttribute(CardAttributes.ManaCost) <= mana;
2025-12-13 23:28:23 -05:00
}
public bool CheckAvailabilityAndSetTargets(CardInstance card, out List<CharacterBase> targets)
{
card.DetectTargetsValidity(out List<CharacterBase> valid, out _, out _);
if (valid.Count == 0)
{
targets = null;
return false; // 无有效目标或无法使用则跳过
}
targets = card.SetRandomTargets(valid);
return true;
}
2025-11-15 09:08:36 -05:00
}
2025-10-03 00:02:43 -04:00
}