Files
Continentis/Assets/Scripts/MainGame/Base/Interpreters/CardTextInterpreter.cs
SoulliesOfficial 61a397dd4c MOD!
2025-10-23 00:49:44 -04:00

86 lines
4.1 KiB
C#

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();
TextInterpreter.SetFunction("Value", new Func<float, string>((cv) => DynamicTextInterpreter.GetValue(cv, false)));
TextInterpreter.SetFunction("Value", new Func<float, float, string>((cv, bv) => DynamicTextInterpreter.GetValue(cv, bv, true, false)));
TextInterpreter.SetFunction("Value", new Func<float, float, bool, string>((cv, bv, high) => DynamicTextInterpreter.GetValue(cv, bv, high, false)));
TextInterpreter.SetFunction("Value", new Func<float, float, bool, bool, string>((cv, bv, high, percent) => DynamicTextInterpreter.GetValue(cv, bv, high, percent)));
foreach (KeyValuePair<string, InterpretedKeyword> keyword in MainGameManager.Instance.keywordData.interpretedKeywords)
{
TextInterpreter.SetVariable(keyword.Key, keyword.Key);
}
}
public static void InterpretText(CardLogicBase card, bool overrideDescription = false)
{
card.contentSubmodule.keywords.Clear();
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 descriptionToParse = card.contentSubmodule.originalFunctionText;
string result = DynamicTextInterpreter.Parse(TextInterpreter, descriptionToParse, card.contentSubmodule.keywords, card.contentSubmodule.hintKeywords);
card.contentSubmodule.interpretedFunctionText = result;
Debug.Log($"Interpreted Description: {result}");
}
}
public static partial class CardTextInterpreter
{
private static string GetAttribute(CardLogicBase 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(CardLogicBase card, string attributeName, bool inPercent)
{
string displayName = "Display" + attributeName;
int displayValue = card.GetAttribute(displayName);
return DynamicTextInterpreter.GetValue(displayValue, inPercent);
}
private static string RemoveWhenSelecting(CardLogicBase card, string toRemove)
{
return card.handCardView != null && card.handCardView.isSelecting ? "" : toRemove;
}
}
}