Files
Continentis/Assets/Scripts/MainGame/Base/Interpreters/CardTextInterpreter.cs

84 lines
3.6 KiB
C#
Raw Normal View History

2025-10-23 00:49:44 -04:00
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();
2025-10-27 07:04:34 -04:00
DynamicTextInterpreter.InitializeInterpreter(ref TextInterpreter);
2025-10-23 00:49:44 -04:00
foreach (KeyValuePair<string, InterpretedKeyword> keyword in MainGameManager.Instance.keywordData.interpretedKeywords)
{
TextInterpreter.SetVariable(keyword.Key, keyword.Key);
}
}
2025-11-30 21:22:39 -05:00
public static string InterpretText(CardInstance card, string textToInterpret)
2025-10-23 00:49:44 -04:00
{
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)));
2025-11-30 21:22:39 -05:00
string result = DynamicTextInterpreter.Parse(TextInterpreter, textToInterpret);
Debug.Log($"Interpreted Text: {result}");
return result;
}
2025-10-23 00:49:44 -04:00
2025-11-30 21:22:39 -05:00
public static void InterpretText(CardInstance card)
{
2025-10-23 00:49:44 -04:00
string descriptionToParse = card.contentSubmodule.originalFunctionText;
2025-11-30 21:22:39 -05:00
string result = InterpretText(card, descriptionToParse);
2025-10-23 00:49:44 -04:00
card.contentSubmodule.interpretedFunctionText = result;
}
}
public static partial class CardTextInterpreter
{
2025-11-15 12:17:34 -05:00
private static string GetAttribute(CardInstance card, string attributeName, bool higherIsBetter, bool inPercent)
2025-10-23 00:49:44 -04:00
{
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);
}
}
2025-11-15 12:17:34 -05:00
private static string GetAttribute(CardInstance card, string attributeName, bool inPercent)
2025-10-23 00:49:44 -04:00
{
string displayName = "Display" + attributeName;
int displayValue = card.GetAttribute(displayName);
return DynamicTextInterpreter.GetValue(displayValue, inPercent);
}
2025-11-15 12:17:34 -05:00
private static string RemoveWhenSelecting(CardInstance card, string toRemove)
2025-10-23 00:49:44 -04:00
{
return card.handCardView != null && card.handCardView.isSelecting ? "" : toRemove;
}
}
}