Files
Continentis/Assets/Scripts/MainGame/Base/Interpreters/CardTextInterpreter.cs
SoulliesOfficial dd2657573a 卡牌更新
2026-04-08 04:48:35 -04:00

87 lines
3.8 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System;
using System.Collections.Generic;
using Continentis.MainGame.Card;
using DynamicExpresso;
using UnityEngine;
namespace Continentis.MainGame
{
public static partial class CardTextInterpreter
{
private static readonly Interpreter TextInterpreter;
static CardTextInterpreter()
{
TextInterpreter = new Interpreter();
DynamicTextInterpreter.InitializeInterpreter(ref TextInterpreter);
foreach (KeyValuePair<string, InterpretedKeyword> keyword in MainGameManager.Instance.keywordData.interpretedKeywords)
{
TextInterpreter.SetVariable(keyword.Key, keyword.Key);
}
}
public static string InterpretText(CardInstance card, string textToInterpret)
{
foreach (KeyValuePair<string, float> attribute in card.attributeSubmodule.attributeGroup.current)
{
TextInterpreter.SetVariable(attribute.Key, attribute.Value);
}
TextInterpreter.SetFunction("RemoveWhenSelecting", new Func<string, string>((toRemove) => RemoveWhenSelecting(card, toRemove)));
TextInterpreter.SetFunction("Attribute", new Func<string, string>((name) => GetAttribute(card, name, true, false)));
TextInterpreter.SetFunction("Attribute", new Func<string, bool, string>((name, high) => GetAttribute(card, name, high, false)));
TextInterpreter.SetFunction("Attribute", new Func<string, bool, bool, string>((name, high, percent) => GetAttribute(card, name, high, percent)));
// 第一阶段:根据卡牌上下文(持有者、目标等)裁剪条件标签块
string resolved = CardTextTagResolver.Resolve(card, textToInterpret);
// 第二阶段Dynamic Expresso 求值 $Attribute / $Keyword 等表达式
string result = DynamicTextInterpreter.Parse(TextInterpreter, resolved);
return result;
}
public static void InterpretText(CardInstance card)
{
string descriptionToParse = card.contentSubmodule.originalFunctionText;
string result = InterpretText(card, descriptionToParse);
card.contentSubmodule.interpretedFunctionText = result;
}
}
public static partial class CardTextInterpreter
{
private static string GetAttribute(CardInstance card, string attributeName, bool higherIsBetter, bool inPercent)
{
string displayName = "Display_" + attributeName;
string baseName = "Base_" + attributeName;
string baseOffsetName = "Base_" + attributeName + "_Offset";
if (!inPercent)
{
int displayValue = card.GetAttribute(displayName);
int baseValue = card.GetAttribute(baseName) + card.GetAttribute(baseOffsetName);
return DynamicTextInterpreter.GetValue(displayValue, baseValue, higherIsBetter, false);
}
else
{
float rawDisplayValue = card.GetRawAttribute(displayName);
float rawBaseValue = card.GetRawAttribute(baseName) + card.GetRawAttribute(baseOffsetName);
return DynamicTextInterpreter.GetValue(rawDisplayValue, rawBaseValue, higherIsBetter, true);
}
}
private static string GetAttribute(CardInstance card, string attributeName, bool inPercent)
{
string displayName = "Display_" + attributeName;
int displayValue = card.GetAttribute(displayName);
return DynamicTextInterpreter.GetValue(displayValue, inPercent);
}
private static string RemoveWhenSelecting(CardInstance card, string toRemove)
{
return card.handCardView != null && card.handCardView.isSelecting ? "" : toRemove;
}
}
}